Skip to content

general_decomposer

check_gate_replacement

check_gate_replacement(
    gate: Gate, replacement_gates: Iterable[Gate]
) -> None

Checks that the replacement gate(s) are valid by verifying that they operate on the same qubits and preserve the quantum state up to a global phase.

Parameters:

Name Type Description Default
gate Gate

Gate that is being replaced.

required
replacement_gates Iterable[Gate]

Gate(s) that are replacing the original gate.

required

Raises:

Type Description
ValueError

If the replacement gates do not operate on the same qubits as the original gate.

ValueError

If the replacement gates do not preserve the quantum state up to a global phase.

Source code in opensquirrel/passes/decomposer/general_decomposer.py
def check_gate_replacement(gate: Gate, replacement_gates: Iterable[Gate]) -> None:
    """Checks that the replacement gate(s) are valid by verifying that they operate on the same
    qubits and preserve the quantum state up to a global phase.

    Args:
        gate (Gate): Gate that is being replaced.
        replacement_gates (Iterable[Gate]): Gate(s) that are replacing the original gate.

    Raises:
        ValueError: If the replacement gates do not operate on the same qubits as the original gate.
        ValueError: If the replacement gates do not preserve the quantum state up to a global phase.

    """
    gate_qubit_indices = gate.qubit_indices
    replacement_gates_qubit_indices = set()
    replaced_matrix = get_circuit_matrix(get_reindexed_circuit([gate], gate_qubit_indices))

    if is_identity_matrix_up_to_a_global_phase(replaced_matrix):
        return

    for replacement_gate in replacement_gates:
        replacement_gates_qubit_indices.update(replacement_gate.qubit_indices)

    if set(gate_qubit_indices) != replacement_gates_qubit_indices:
        msg = f"replacement for gate {gate.name!r} does not operate on the correct qubits"
        raise ValueError(msg)

    replacement_matrix = get_circuit_matrix(get_reindexed_circuit(replacement_gates, gate_qubit_indices))

    if not are_matrices_equivalent_up_to_global_phase(replaced_matrix, replacement_matrix):
        msg = f"replacement for gate {gate.name!r} does not preserve the quantum state"
        raise ValueError(msg)

decompose

decompose(ir: IR, decomposer: Decomposer) -> None

Decomposes the statements in the circuit IR using the provided decomposer.

Parameters:

Name Type Description Default
ir IR

The circuit IR to decompose.

required
decomposer Decomposer

The decomposer to use for decomposing the gates.

required
Source code in opensquirrel/passes/decomposer/general_decomposer.py
def decompose(ir: IR, decomposer: Decomposer) -> None:
    """Decomposes the statements in the circuit IR using the provided decomposer.

    Args:
        ir (IR): The circuit IR to decompose.
        decomposer (Decomposer): The decomposer to use for decomposing the gates.

    """
    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

replace(
    ir: IR,
    gate: type[Gate],
    replacement_gates_function: Callable[..., list[Gate]],
) -> None

Replaces all occurrences of a specific gate in the circuit IR with a given sequence of other gates.

Parameters:

Name Type Description Default
ir IR

The circuit IR to modify.

required
gate type[Gate]

Gate to replace.

required
replacement_gates_function Callable[..., list[Gate]]

Function that returns a list of replacement gates.

required
Source code in opensquirrel/passes/decomposer/general_decomposer.py
def replace(ir: IR, gate: type[Gate], replacement_gates_function: Callable[..., list[Gate]]) -> None:
    """Replaces all occurrences of a specific gate in the circuit IR with a given sequence of other
    gates.

    Args:
        ir (IR): The circuit IR to modify.
        gate (type[Gate]): Gate to replace.
        replacement_gates_function (Callable[..., list[Gate]]): Function that returns a list of replacement gates.

    """
    generic_replacer = _GenericReplacer(gate, replacement_gates_function)
    decompose(ir, generic_replacer)