Skip to content

general_decomposer

decompose(ir, decomposer)

Applies decomposer to every gate in the circuit, replacing each gate by the output of decomposer. When decomposer decides to not decomposer a gate, it needs to return a list with the intact gate as single element.

Source code in opensquirrel\decomposer\general_decomposer.py
def decompose(ir: IR, decomposer: Decomposer) -> None:
    """Applies `decomposer` to every gate in the circuit, replacing each gate by the output of `decomposer`.
    When `decomposer` decides to not decomposer a gate, it needs to return a list with the intact gate as single
    element.
    """
    statement_index = 0
    while statement_index < len(ir.statements):
        statement = ir.statements[statement_index]

        if not isinstance(statement, Gate):
            statement_index += 1
            continue

        gate = statement
        replacement_gates: list[Gate] = decomposer.decompose(statement)
        check_gate_replacement(gate, replacement_gates)

        ir.statements[statement_index : statement_index + 1] = replacement_gates
        statement_index += len(replacement_gates)

replace(ir, gate_generator, f)

Does the same as decomposer, but only applies to a given gate.

Source code in opensquirrel\decomposer\general_decomposer.py
def replace(ir: IR, gate_generator: Callable[..., Gate], f: Callable[..., list[Gate]]) -> None:
    """Does the same as decomposer, but only applies to a given gate."""
    generic_replacer = _GenericReplacer(gate_generator, f)

    decompose(ir, generic_replacer)