resolve_swaps#
- resolve_swaps(qc: QuantumCircuit) QuantumCircuit[source]#
Remove SWAP gates by folding them into qubit index remapping.
Every SWAP encountered updates an internal permutation table. All subsequent instructions have their qubit operands remapped through this table, so the logical effect of the SWAP is preserved without any physical gates.
This pass should be applied before routing. Instead of decomposing each SWAP into three CX gates, it tracks a running qubit permutation and remaps all subsequent gate operands accordingly. After processing, the circuit contains no SWAP gates and produces the same unitary up to a final qubit permutation. This especially means that the circuit statistics remain unchanged as the measurement instructions get permuted as well.
- Parameters:
- qcQuantumCircuit
Input circuit, potentially containing
swapinstructions.
- Returns:
- QuantumCircuit
A new circuit with all SWAP gates removed and operands remapped.
Examples
Resolve a SWAP by remapping subsequent gate operands:
>>> from qrisp import QuantumCircuit, PassManager >>> from qrisp import resolve_swaps >>> qc = QuantumCircuit(2) >>> qc.h(0) >>> qc.swap(0, 1) >>> qc.cx(0, 1) >>> print(qc) ┌───┐ qb_126: ┤ H ├─X───■── └───┘ │ ┌─┴─┐ qb_127: ──────X─┤ X ├ └───┘ >>> pm = PassManager() >>> pm += resolve_swaps >>> routable_qc = pm.run(qc) >>> print(routable_qc) ┌───┐┌───┐ qb_126: ┤ H ├┤ X ├ └───┘└─┬─┘ qb_127: ───────■──