A formal verification library for quantum computing in Lean 4, loosely inspired by QuantumLib.
Quantumlib.lean is a formalization of quantum computing concepts in Lean 4, built on top of Mathlib. It provides:
- Quantum Gates: Hadamard, Pauli gates (X, Y, Z), rotation gates, phase shifts, controlled gates (CNOT), and more
- Pauli Operators: Efficient representation of n-qubit Pauli operators with phase tracking and custom notation
- Pauli Maps: Linear combinations of Pauli operators for quantum error correction
- Stabilizer Codes: Formalization of quantum error correcting codes (bit flip code, Shor's 9-qubit code)
- Quantum States: Bra/Ket notation and quantum basis states
- Matrix Operations: Kronecker products, tensor powers, unitary verification
- Proof Automation: Custom tactics for quantum gate equality proofs
This library aims to provide a rigorous foundation for reasoning about quantum circuits, quantum error correction, and quantum algorithms.
The library implements standard quantum gates with full formalization:
-
Single-qubit gates:
hadamard: Hadamard gate (H)sqrtx: Square root of X gate (βX or V gate)Οx,Οy,Οz: Pauli matricesphaseShift Ο: Phase shift gate S(Ο)xRotate ΞΈ,yRotate ΞΈ: Rotation gates Rx(ΞΈ), Ry(ΞΈ)rotate ΞΈ Ο Ξ΄: General single-qubit rotation U3(ΞΈ, Ο, Ξ΄)
-
Multi-qubit gates:
cnot: Controlled-NOT gateswap: SWAP gatecontrolM M: Controlled version of any gate MhadamardK k: k-fold tensor product of Hadamard gates
Efficient representation of multi-qubit Pauli operators with decidable equality:
@[ext]
structure Pauli (n : β) where
m : ZMod 4 -- Phase: (-i)^m
z : BitVec n -- Z components
x : BitVec n -- X components
deriving DecidableEq, BEq, FintypeCustom notation using [P| ... ] syntax:
-- Single Pauli operators
[P| XXI ] -- X β X β I (3-qubit operator)
[P| ZZI ] -- Z β Z β I
[P| iXYZ ] -- iΒ·(X β Y β Z) with phase
[P| -XYZ ] -- -(X β Y β Z) negative phase
-- Pauli multiplication (automatic phase tracking)
[P| XXI ] * [P| IYZ ] -- Computes product with correct phaseThe notation automatically:
- Computes tensor products from string notation
- Tracks phases (including Β±1, Β±i)
- Handles Y = iXZ decomposition
- Pretty-prints back to readable form
Features:
- Decidable equality for fast comparison
- Efficient multiplication using phase tracking via
phaseFlipsWith - Commutator checking:
P.commutesWith Q - Weight calculation: number of non-identity Paulis
- Kronecker product (tensor):
P β Q - Conversion to complex matrices:
P.toCMatrix
Linear combinations of Pauli operators using [PA| ... ] notation:
-- Single Pauli as a map
[PA| XXI ]
-- Linear combination with coefficients
[PA| #(2) β’ XXI + #(3) β’ ZZI ]
-- Multiplication (automatically normalized)
[PA| (XXI + ZZI) * IYZ ]PauliMap n is defined as MonoidAlgebra β (Pauli n), representing expressions like:
βα΅’ cα΅’ Pα΅’
where cα΅’ β β and Pα΅’ are Pauli operators.
Operations:
- Normalized form: Automatically extracts phases into coefficients
- Addition and multiplication: Full ring structure
- Conversion to matrices:
pm.toCMatrix
Stabilizer codes (Quantumlib/Data/Error/Operator.lean):
structure StabilizerCode (n k : β) where
generators : List (Pauli n) -- Stabilizer generators
logicalX : Fin k β Pauli n -- Logical X operators
logicalZ : Fin k β Pauli n -- Logical Z operators
stabilizers_commute : ... -- All stabilizers commute
logical_commute : ... -- Logical ops have correct commutation
logical_independent : ... -- Logical ops are independent of stabilizersImplemented codes:
- 3-qubit Bit Flip Code:
def BitFlip.code : StabilizerCode 3 1
-- Generators: [P| ZZI ], [P| IZZ ]
-- Logical X: [P| XXX ]
-- Logical Z: [P| ZZZ ]Proven properties:
- Corrects single-qubit X errors:
{[P| IIX ], [P| IXI ], [P| XII ]} - Syndrome calculation is injective on error set
- Stabilizer group membership is decidable
- Shor's 9-qubit Code:
def Shor9.code : StabilizerCode 9 1
-- 8 generators (6 Z-type, 2 X-type)
-- Corrects arbitrary single-qubit errorsDirac notation for quantum states:
-- Computational basis states
β£0β© -- |0β© state
β£1β© -- |1β© state
-- Bra-ket products
β£0β©β¨0β£ -- Projector onto |0β©
β£0β©β¨1β£ -- Outer product
β¨0β£1β© -- Inner product (equals 0)Custom tactics for quantum computing proofs:
-
solve_matrix: Automatically prove matrix equalities by case analysis on indiceslemma hadamard_mul_hadamard : hadamard * hadamard = 1 := by solve_matrix [hadamard]
-
Future:
decide_complex(planned): Decision procedure for complex number equality in quantum gates, supporting β[i], β[β2, i], cyclotomic extensions, and trigonometric values
Quantumlib/
βββ Data/
β βββ Basis/ # Quantum basis states and ket-bra notation
β β βββ Basic.lean # Ket/bra definitions
β β βββ Notation.lean # Dirac notation (β£0β©, β£1β©, etc.)
β β βββ Equivs.lean # Basis equivalences and identities
β βββ Gate/ # Quantum gates
β β βββ Basic.lean # Core gates (Hadamard, CNOT, SWAP, βX)
β β βββ Pauli/ # Pauli operators
β β β βββ Defs.lean # Pauli structure and operations
β β β βββ Lemmas.lean # Pauli algebra lemmas
β β β βββ Notation.lean # [P| ... ] and [PA| ... ] notation
β β βββ Rotate.lean # Rotation gates (Rx, Ry, Rz, U3)
β β βββ PhaseShift.lean # Phase shift gates (S, T)
β β βββ Equivs.lean # Gate equivalences and identities
β β βββ Hermitian.lean # Hermiticity proofs
β β βββ Unitary.lean # Unitarity proofs
β β βββ ConjTranspose.lean
β β βββ Lemmas.lean
β βββ Error/
β βββ Operator.lean # Stabilizer codes and error correction
βββ ForMathlib/ # Extensions to Mathlib (may be upstreamed)
β βββ Data/
β βββ Complex/ # Complex number utilities
β βββ Matrix/ # Matrix operations
β β βββ Basic.lean # Matrix utilities
β β βββ Kron.lean # Kronecker products
β β βββ Unitary.lean # Unitary matrices
β β βββ PowBitVec.lean # Tensor powers (M^βk)
β βββ BitVec/ # Bit vector utilities
β β βββ Basic.lean # BitVec operations
β β βββ Lemmas.lean # BitVec lemmas
β βββ Fin.lean # Finite type utilities
βββ Tactic/ # Custom tactics
β βββ SolveMatrix.lean # Matrix equality automation
β βββ Basic.lean
βββ Computation.lean # Quantum circuit computation examples
βββ Basic.lean # Main imports
# Clone the repository
git clone https://github.com/inQWIRE/LeanQuantum
cd LeanQuantum
# Build the library
lake buildAdd to your lakefile.lean:
require quantumlib from git
"https://github.com/inQWIRE/LeanQuantum"Then import in your Lean files:
import Quantumlibimport Quantumlib
-- Hadamard is its own inverse
example : hadamard * hadamard = 1 := by
solve_matrix [hadamard]
-- Pauli X is a NOT gate
example : Οx * β£0β© = β£1β© := by
solve_matrix [Οx]
-- Pauli gates square to identity
example : Οx * Οx = 1 := by
solve_matrix [Οx]-- βX exists and satisfies βX Β· βX = X
lemma sqrtx_mul_sqrtx : sqrtx * sqrtx = Οx := by
simp [sqrtx, Οx]
ring_nf
norm_num [Complex.I_sq]import Quantumlib.Data.Gate.Pauli
-- Create Pauli operators using notation
#check [P| XXI ] -- Pauli 3
#check [P| ZZZ ] -- Pauli 3
#check [P| iXYZ ] -- Pauli 3 (with phase i)
-- Multiplication automatically tracks phases
example : [P| X ] * [P| Y ] = [P| iZ ] := by
decide
-- Commutation checking
example : [P| XX ].commutesWith [P| ZZ ] = true := by
decide
example : [P| XZ ].commutesWith [P| ZX ] = false := by
decideimport Quantumlib.Data.Gate.Pauli
-- Single Pauli operator as a map
#check [PA| XXI ]
-- Linear combinations
def myObservable : PauliMap 3 := [PA| #(2) β’ XXI + #(3) β’ ZZI ]
-- Products (automatically normalized)
example : [PA| X ] * [PA| Y ] = [PA| #(Complex.I) β’ Z ] := by
rflimport Quantumlib.Data.Error.Operator
-- The 3-qubit bit flip code
example : BitFlip.code.generators = [[P| ZZI ], [P| IZZ ]] := by
rfl
-- Syndrome of an error
example : BitFlip.code.syndrome [P| IIX ] = [false, true] := by
rfl
-- The code corrects single X errors
example : BitFlip.code.corrects {[P| IIX ], [P| IXI ], [P| XII ]} :=
BitFlip.corrects_single_qubit_errors-- CNOT acts as controlled-X
lemma cnot_decompose : cnot = β£1β©β¨1β£ β Οx + β£0β©β¨0β£ β 1 := by
solve_matrix [cnot, Οx]The library includes formal proofs of key quantum computing properties:
- Gate identities: HΒ² = I, XΒ² = I, YΒ² = I, ZΒ² = I
- Hermiticity: Pauli gates, Hadamard, rotation gates are Hermitian
- Unitarity: All gates preserve inner products
- Pauli commutation: [X,Y] = 2iZ, etc.
- Gate decompositions: CNOT, controlled gates, βX
- Stabilizer properties: Generators commute, logical operators anticommute
- Error correction: Bit flip code corrects single-qubit X errors
- Basic quantum gates (H, X, Y, Z, CNOT, SWAP, βX)
- Pauli operator algebra with decidable equality
- Custom Pauli notation
[P| ... ]and[PA| ... ] - PauliMap for linear combinations
- Stabilizer code framework
- 3-qubit bit flip code (fully verified)
- Shor's 9-qubit code (partial verification)
- Ket-bra notation
- Rotation and phase shift gates
-
solve_matrixtactic for gate equality proofs - Hermiticity verification
- Robust
solve_matrixvia decidable complex equality: Building a decision procedure for complex number equality in the algebraic fragment used by quantum gates (β[i], β[β2, i], cyclotomic extensions, trigonometric values)
- Complete unitarity proofs for rotation gates
- More quantum error correction codes
- 5-qubit perfect code
- 7-qubit Steane code
- Surface codes
- CSS codes
- Quantum circuits
- Circuit composition
- Circuit optimization
- Circuit equivalence
- Quantum algorithms
- Deutsch-Jozsa
- Grover's algorithm
- Quantum Fourier Transform
- Shor's algorithm (circuit)
- Measurement and observables
- Density matrices and mixed states
- Quantum channels and noise
Contributions are welcome! Areas where help is particularly appreciated:
- Completing unitarity proofs for rotation gates
- More stabilizer codes (Steane, surface codes, etc.)
- Quantum algorithms formalization
- Documentation and examples
- Performance improvements to tactics
Please open an issue or pull request on GitHub.
- Lean-QuantumInfo: Another (more complete) quantum computing library in Lean 4
- QuantumLib: Quantum computing library in Coq (inspiration for this project)
- Mathlib: Lean 4's mathematics library
- SQIR: Small Quantum Intermediate Representation in Coq
- Qiskit: IBM's quantum computing framework (Python)
If you use Quantumlib.lean in your research, please cite:
@software{quantumlib_lean,
title = {Quantumlib.lean: Formal Verification of Quantum Computing in Lean 4},
author = {Fady Adal},
year = {2025},
url = {https://github.com/inQWIRE/LeanQuantum}
}This project is loosely inspired by QuantumLib from the inQWIRE group.
Special thanks to:
- The Lean community for Lean 4 and Mathlib
- Professor Robert Rand and the inQWIRE group
This project is licensed under the MIT License - see the LICENSE file for details.
Author: Fady Adal Status: Active development Lean Version: v4.26.0-rc2 Repository: https://github.com/inQWIRE/LeanQuantum