Its 4×4 matrix in the computational basis {∣00⟩,∣01⟩,∣10⟩,∣11⟩} is
SWAP=1000001001000001.
Many hardware architectures only provide the CNOT (CX) as their native two-qubit gate, so it is
essential to know how to build SWAP from CNOTs.
The three-CNOT decomposition
The recipe is three CNOTs with alternating control and target:
SWAP01=CNOT(0→1)⋅CNOT(1→0)⋅CNOT(0→1).
To see why this works, track an arbitrary computational basis state ∣ab⟩ through the three
steps:
CNOT(0→1): target qubit 1 is XORed with control qubit 0.
∣ab⟩→∣a,b⊕a⟩
CNOT(1→0): target qubit 0 is XORed with qubit 1 (which now holds b⊕a).
∣a,b⊕a⟩→∣a⊕(b⊕a),b⊕a⟩=∣b,b⊕a⟩
CNOT(0→1): target qubit 1 is XORed with the new qubit 0 (which is b).
∣b,b⊕a⟩→∣b,(b⊕a)⊕b⟩=∣b,a⟩
The final state is ∣ba⟩ — exactly the inputs swapped. Since the reasoning holds for all four
basis states, and a unitary is completely determined by its action on a basis, the product of the
three CNOTs is SWAP.
Try it
Build the SWAP gate by applying three CNOTs. The grader compares the full unitary matrix of your
circuit against the SWAP matrix, so an empty or wrong circuit will fail.
Run your code to see the quantum state.
Sign in on the full site to ask questions and join the discussion.