Skip to content

qubit_reindexer

get_reindexed_circuit

get_reindexed_circuit(
    replacement_gates: Iterable[Gate],
    qubit_indices: list[int],
    bit_register_size: int = 0,
) -> Circuit

Reindex the qubit indices of the given (replacement) gates in a circuit.

Parameters:

Name Type Description Default
replacement_gates Iterable[Gate]

The gates to be reindexed.

required
qubit_indices list[int]

The new qubit indices.

required
bit_register_size int

The size of the bit register.

0

Returns:

Type Description
Circuit

The reindexed circuit.

Source code in opensquirrel/reindexer/qubit_reindexer.py
def get_reindexed_circuit(
    replacement_gates: Iterable[Gate],
    qubit_indices: list[int],
    bit_register_size: int = 0,
) -> Circuit:
    """Reindex the qubit indices of the given (replacement) gates in a circuit.

    Args:
        replacement_gates (Iterable[Gate]): The gates to be reindexed.
        qubit_indices (list[int]): The new qubit indices.
        bit_register_size (int): The size of the bit register.

    Returns:
        The reindexed circuit.

    """
    from opensquirrel.circuit import Circuit

    qubit_reindexer = _QubitReindexer(qubit_indices)
    register_manager = RegisterManager(
        OrderedDict({DEFAULT_QUBIT_REGISTER_NAME: QubitRegister(len(qubit_indices))}),
        OrderedDict({DEFAULT_BIT_REGISTER_NAME: BitRegister(bit_register_size)}),
    )
    replacement_ir = IR()
    for gate in replacement_gates:
        gate_with_reindexed_qubits = gate.accept(qubit_reindexer)
        replacement_ir.add_gate(gate_with_reindexed_qubits)
    return Circuit(register_manager, replacement_ir)