toJson_Gate = (gate, context=new CustomGateSet()) => {
    let found = Gates.findKnownGateById(gate.serializedId, context);
    if (found === gate) {
        return gate.serializedId;
    }
    if (found !== undefined && found.param !== undefined) {
        return {id: gate.serializedId, arg: gate.param};
    }

    if (gate.name === "Parse Error") {
        return gate.tag;
    }

    let result = {};
    if (gate.serializedId !== "") {
        result.id = gate.serializedId;
    }
    if (gate.serializedId.startsWith("~") ? gate.symbol !== '' : gate.symbol !== gate.serializedId) {
        result.name = gate.symbol;
    }

    if (gate.stableDuration() === Infinity && gate.knownMatrixAt(0) !== undefined) {
        result.matrix = toJson_Matrix(gate.knownMatrixAt(0.25));
    } else if (gate.knownCircuit !== undefined) {
        result.circuit = toJson_CircuitDefinition(gate.knownCircuit, context);
    } else {
        throw new DetailedError("Don't known how to serialize gate's function.", {gate});
    }

    return result;
}
@param {!Gate} gate
@param {!CustomGateSet=} context
@returns {!object}