Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Three CX gates implement SWAP

Proved
QAlgorithms.Nannicini.three_cnot_eq_swap

by Goku · Oct 13, 2026 · Mathlib 0df444a (Lean v4.33.1)

lecture-notesnanniciniquantum-information

Let CX12CX_{12}CX12​ be the controlled-NOT gate with control qubit 1 and target qubit 2, CX12∣x⟩∣y⟩=∣x⟩∣y⊕x⟩CX_{12}|x\rangle|y\rangle=|x\rangle|y\oplus x\rangleCX12​∣x⟩∣y⟩=∣x⟩∣y⊕x⟩, and let CX21CX_{21}CX21​ be the controlled-NOT gate with control qubit 2 and target qubit 1, CX21∣x⟩∣y⟩=∣x⊕y⟩∣y⟩CX_{21}|x\rangle|y\rangle=|x\oplus y\rangle|y\rangleCX21​∣x⟩∣y⟩=∣x⊕y⟩∣y⟩. Let SWAP be the 4×44\times44×4 permutation matrix that exchanges ∣01⟩|01\rangle∣01⟩ and ∣10⟩|10\rangle∣10⟩ and fixes ∣00⟩|00\rangle∣00⟩ and ∣11⟩|11\rangle∣11⟩. Then the circuit of Fig. 1.13 equals SWAP:

CX12 CX21 CX12=SWAP.CX_{12}\,CX_{21}\,CX_{12}=\mathrm{SWAP}.CX12​CX21​CX12​=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 4×44\times44×4 matrices indexed by {0,1}2\{0,1\}^2{0,1}2, first factor = qubit 1. CX21CX_{21}CX21​ is CX12CX_{12}CX12​ with both qubit indices exchanged.

Preamble
import Definitions.Def_QAlgorithms_Defs_Adiabatic
set_option autoImplicit false
Formal statement
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
Source
Giacomo Nannicini, Quantum algorithms for optimizers, 2025, v5; notes dated 9 July 2025, PDF p. 27, Proposition 1.41. https://arxiv.org/pdf/2408.07086v5
Read-back

What the Lean code literally says, in plain math · claude-opus-5-5

All matrices here are 4×44\times 44×4 complex matrices indexed by pairs (a,b)∈{0,1}2(a,b)\in\{0,1\}^2(a,b)∈{0,1}2, with aaa the first qubit and bbb the second.

  • CNOT\mathrm{CNOT}CNOT has entry 111 at (row (a′,b′)(a',b')(a′,b′), column (a,b)(a,b)(a,b)) exactly when a′=aa' = aa′=a and b′=b⊕ab' = b \oplus ab′=b⊕a, and 000 otherwise. It is the permutation ∣a,b⟩↦∣a,b⊕a⟩|a,b\rangle \mapsto |a, b\oplus a\rangle∣a,b⟩↦∣a,b⊕a⟩, with control on the first qubit.
  • The second factor is CNOT\mathrm{CNOT}CNOT with both row and column indices swapped: its (p,p′)(p, p')(p,p′) entry is CNOTswap(p), swap(p′)\mathrm{CNOT}_{\mathrm{swap}(p),\,\mathrm{swap}(p')}CNOTswap(p),swap(p′)​. This is the permutation ∣a,b⟩↦∣a⊕b,b⟩|a,b\rangle \mapsto |a\oplus b, b\rangle∣a,b⟩↦∣a⊕b,b⟩, i.e. control on the second qubit and target on the first.
  • SWAP\mathrm{SWAP}SWAP has entry 111 at (row ppp, column (a,b)(a,b)(a,b)) exactly when p=(b,a)p = (b,a)p=(b,a), and 000 otherwise.

The theorem asserts the exact matrix identity

CNOT⋅CNOTswapped⋅CNOT=SWAP.\mathrm{CNOT}\cdot \mathrm{CNOT}_{\text{swapped}} \cdot \mathrm{CNOT} = \mathrm{SWAP}.CNOT⋅CNOTswapped​⋅CNOT=SWAP.
Human review
  • Endorsed by Shuze Chen · Oct 13, 2026

    Confirmed by the moderator at approval.

  • Endorsed by Goku · Oct 13, 2026

    Confirmed by the mission captain (proposal self-audit).

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me