Three CX gates implement SWAP
ProvedQAlgorithms.Nannicini.three_cnot_eq_swapLet be the controlled-NOT gate with control qubit 1 and target qubit 2, , and let be the controlled-NOT gate with control qubit 2 and target qubit 1, . Let SWAP be the permutation matrix that exchanges and and fixes and . Then the circuit of Fig. 1.13 equals SWAP:
So a swap of two qubits can be built from three CX gates. On hardware where two-qubit gates act only on adjacent qubits, this lets any two qubits interact.
Formalization Note The identity is stated as an equality of matrices indexed by , first factor = qubit 1. is with both qubit indices exchanged.
import Definitions.Def_QAlgorithms_Defs_Adiabatic set_option autoImplicit false
namespace QAlgorithms.Nannicini
open QAlgorithms
/-- Nannicini, Proposition 1.41, PDF p. 27. The circuit of Fig. 1.13, made of three CX gates,
`CX₁₂ CX₂₁ CX₁₂`, swaps qubits 1 and 2: it equals the SWAP matrix of p. 27.
`cnot` is `CX₁₂` (control qubit 1, target qubit 2: `|x⟩|y⟩ ↦ |x⟩|y ⊕ x⟩`, p. 26);
`cnot.submatrix Prod.swap Prod.swap` is `CX₂₁` (control qubit 2, target qubit 1); `swapGate`
is the permutation matrix exchanging `|01⟩` and `|10⟩`. -/
theorem three_cnot_eq_swap :
cnot * cnot.submatrix Prod.swap Prod.swap * cnot = swapGate := by
sorry
end QAlgorithms.Nannicini
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
All matrices here are complex matrices indexed by pairs , with the first qubit and the second.
- has entry at (row , column ) exactly when and , and otherwise. It is the permutation , with control on the first qubit.
- The second factor is with both row and column indices swapped: its entry is . This is the permutation , i.e. control on the second qubit and target on the first.
- has entry at (row , column ) exactly when , and otherwise.
The theorem asserts the exact matrix identity
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.