toJson_CircuitDefinition = (v, context) => {
    let result = {
        cols: v.trimEmptyColumnsAtEndIgnoringGateWidths().columns.
            map(e => toJson_GateColumn(e, context || v.customGateSet)).
            map(c => seq(c).skipTailWhile(e => e === 1).toArray())
    };
    if (context === undefined && v.customGateSet.gates.length > 0) {
        result.gates = toJson_CustomGateSet(v.customGateSet);
    }
    if (v.customInitialValues.size > 0) {
        result.init = [];
        let maxInit = seq(v.customInitialValues.keys()).max();
        for (let i = 0; i <= maxInit; i++) {
            let s = v.customInitialValues.get(i);
            result.init.push(
                s === undefined ? 0 :
                s === '1' ? 1 :
                s);
        }
    }
    return result;
}
@param {!CircuitDefinition} v
@param {undefined|!CustomGateSet} context
@returns {!object}