SWAP built from three CNOT gates
Three alternating CNOTs exchange two qubits’ states. Many chips have no native SWAP, so compilers do this constantly.
Intermediate · 2 qubits · 3 gates · 3 two-qubit · depth 3 · reviewed November 2026
Open in the Circuit BuilderWhat is happening
Start with q0 = 1 and q1 = 0. The three CNOTs, alternating direction, leave q0 = 0 and q1 = 1. The same sequence swaps any two states, including superpositions and entangled ones.
This matters in practice: on a device where only neighbouring qubits can interact, moving information around costs SWAPs, and each SWAP costs three two-qubit gates with their errors.
Step by step
1. CNOT q0 → q1
q1 becomes q0 XOR q1. With input |10⟩ the state becomes |11⟩.
2. CNOT q1 → q0
q0 becomes q0 XOR q1, which is 0. The state is |01⟩.
3. CNOT q0 → q1
q1 becomes q1 XOR q0. The state stays |01⟩, so the values have swapped.
Expected result
Final state |01⟩ with 100% probability.
Qubit 0 is the leftmost bit. In Qiskit, bitstrings are printed in the reverse order.
Qiskit code
from qiskit import QuantumCircuit from math import pi qc = QuantumCircuit(2, 2) qc.x(0) qc.cx(0, 1) qc.barrier() qc.cx(1, 0) qc.barrier() qc.cx(0, 1) qc.barrier() qc.measure(0, 0) qc.measure(1, 1) # Qiskit prints bit strings with qubit 0 on the RIGHT; this site writes qubit 0 on the LEFT.
Try this
- Change the starting values with the toggles and check all four inputs.
- Replace the three CNOTs with the SWAP gate and compare.
- Set q0 into superposition with H first and see that the swap still works.
Gates used
Related reading
Questions
Why not just use a SWAP gate?
Hardware has a small native gate set, often including a CNOT-like gate. A SWAP is compiled into three of them.