commute_swaps#
- commute_swaps(qc: QuantumCircuit) QuantumCircuit[source]#
Commute single-qubit instructions past SWAP gates.
This pass walks the circuit and moves any single-qubit instruction (gates and measurements) to the other side of an adjacent SWAP on the same qubit. The commuted instruction is placed on the swapped partner qubit immediately after the SWAP.
This is valid because SWAP simply relabels the two qubit lines, so a single-qubit operation applied before the SWAP on qubit a is equivalent to the same operation applied after the SWAP on qubit b (and vice versa).
- Parameters:
- qcQuantumCircuit
Input quantum circuit.
- Returns:
- QuantumCircuit
Circuit with single-qubit instructions commuted past SWAPs.
Examples
Commute a Hadamard past a SWAP:
>>> from qrisp import QuantumCircuit, PassManager >>> from qrisp import commute_swaps >>> qc = QuantumCircuit(2) >>> qc.h(0) >>> qc.swap(0, 1) >>> print(qc) ┌───┐ qb_65: ┤ H ├─X─ └───┘ │ qb_66: ──────X─ >>> pm = PassManager() >>> pm += commute_swaps >>> optimized_qc = pm.run(qc) >>> print(optimized_qc) qb_65: ─X────── │ ┌───┐ qb_66: ─X─┤ H ├ └───┘