[{"data":1,"prerenderedAt":4},["ShallowReactive",2],{"LMuItoLZzZ":3},"# Quantumlib.lean\n\nA formal verification library for quantum computing in Lean 4, loosely inspired by [QuantumLib](https://github.com/inQWIRE/QuantumLib).\n\n## Overview\n\nQuantumlib.lean is a formalization of quantum computing concepts in Lean 4, built on top of Mathlib. It provides:\n\n- **Quantum Gates**: Hadamard, Pauli gates (X, Y, Z), rotation gates, phase shifts, controlled gates (CNOT), and more\n- **Pauli Operators**: Efficient representation of n-qubit Pauli operators with phase tracking and custom notation\n- **Pauli Maps**: Linear combinations of Pauli operators for quantum error correction\n- **Stabilizer Codes**: Formalization of quantum error correcting codes (bit flip code, Shor's 9-qubit code)\n- **Quantum States**: Bra/Ket notation and quantum basis states\n- **Matrix Operations**: Kronecker products, tensor powers, unitary verification\n- **Proof Automation**: Custom tactics for quantum gate equality proofs\n\nThis library aims to provide a rigorous foundation for reasoning about quantum circuits, quantum error correction, and quantum algorithms.\n\n## Features\n\n### Quantum Gates\n\nThe library implements standard quantum gates with full formalization:\n\n- **Single-qubit gates**:\n  - `hadamard`: Hadamard gate (H)\n  - `sqrtx`: Square root of X gate (√X or V gate)\n  - `σx`, `σy`, `σz`: Pauli matrices\n  - `phaseShift φ`: Phase shift gate S(φ)\n  - `xRotate θ`, `yRotate θ`: Rotation gates Rx(θ), Ry(θ)\n  - `rotate θ φ δ`: General single-qubit rotation U3(θ, φ, δ)\n\n- **Multi-qubit gates**:\n  - `cnot`: Controlled-NOT gate\n  - `swap`: SWAP gate\n  - `controlM M`: Controlled version of any gate M\n  - `hadamardK k`: k-fold tensor product of Hadamard gates\n\n### Pauli Operators and Notation\n\nEfficient representation of multi-qubit Pauli operators with decidable equality:\n\n```lean\n@[ext]\nstructure Pauli (n : ℕ) where\n  m : ZMod 4        -- Phase: (-i)^m\n  z : BitVec n      -- Z components\n  x : BitVec n      -- X components\nderiving DecidableEq, BEq, Fintype\n```\n\n**Custom notation** using `[P| ... ]` syntax:\n\n```lean\n-- Single Pauli operators\n[P| XXI ]   -- X ⊗ X ⊗ I (3-qubit operator)\n[P| ZZI ]   -- Z ⊗ Z ⊗ I\n[P| iXYZ ]  -- i·(X ⊗ Y ⊗ Z) with phase\n[P| -XYZ ]  -- -(X ⊗ Y ⊗ Z) negative phase\n\n-- Pauli multiplication (automatic phase tracking)\n[P| XXI ] * [P| IYZ ]  -- Computes product with correct phase\n```\n\nThe notation automatically:\n- Computes tensor products from string notation\n- Tracks phases (including ±1, ±i)\n- Handles Y = iXZ decomposition\n- Pretty-prints back to readable form\n\nFeatures:\n- **Decidable equality** for fast comparison\n- **Efficient multiplication** using phase tracking via `phaseFlipsWith`\n- **Commutator checking**: `P.commutesWith Q`\n- **Weight calculation**: number of non-identity Paulis\n- **Kronecker product** (tensor): `P ⊗ Q`\n- **Conversion to complex matrices**: `P.toCMatrix`\n\n### Pauli Maps\n\nLinear combinations of Pauli operators using `[PA| ... ]` notation:\n\n```lean\n-- Single Pauli as a map\n[PA| XXI ]\n\n-- Linear combination with coefficients\n[PA| #(2) • XXI + #(3) • ZZI ]\n\n-- Multiplication (automatically normalized)\n[PA| (XXI + ZZI) * IYZ ]\n```\n\n`PauliMap n` is defined as `MonoidAlgebra ℂ (Pauli n)`, representing expressions like:\n```\n∑ᵢ cᵢ Pᵢ\n```\nwhere `cᵢ ∈ ℂ` and `Pᵢ` are Pauli operators.\n\nOperations:\n- **Normalized form**: Automatically extracts phases into coefficients\n- **Addition and multiplication**: Full ring structure\n- **Conversion to matrices**: `pm.toCMatrix`\n\n### Quantum Error Correction\n\n**Stabilizer codes** (`Quantumlib/Data/Error/Operator.lean`):\n\n```lean\nstructure StabilizerCode (n k : ℕ) where\n  generators : List (Pauli n)      -- Stabilizer generators\n  logicalX : Fin k → Pauli n        -- Logical X operators\n  logicalZ : Fin k → Pauli n        -- Logical Z operators\n  stabilizers_commute : ...         -- All stabilizers commute\n  logical_commute : ...             -- Logical ops have correct commutation\n  logical_independent : ...         -- Logical ops are independent of stabilizers\n```\n\n**Implemented codes**:\n\n1. **3-qubit Bit Flip Code**:\n```lean\ndef BitFlip.code : StabilizerCode 3 1\n-- Generators: [P| ZZI ], [P| IZZ ]\n-- Logical X: [P| XXX ]\n-- Logical Z: [P| ZZZ ]\n```\n\nProven properties:\n- Corrects single-qubit X errors: `{[P| IIX ], [P| IXI ], [P| XII ]}`\n- Syndrome calculation is injective on error set\n- Stabilizer group membership is decidable\n\n2. **Shor's 9-qubit Code**:\n```lean\ndef Shor9.code : StabilizerCode 9 1\n-- 8 generators (6 Z-type, 2 X-type)\n-- Corrects arbitrary single-qubit errors\n```\n\n### Quantum States and Notation\n\nDirac notation for quantum states:\n\n```lean\n-- Computational basis states\n∣0⟩  -- |0⟩ state\n∣1⟩  -- |1⟩ state\n\n-- Bra-ket products\n∣0⟩⟨0∣  -- Projector onto |0⟩\n∣0⟩⟨1∣  -- Outer product\n⟨0∣1⟩  -- Inner product (equals 0)\n```\n\n### Proof Automation\n\nCustom tactics for quantum computing proofs:\n\n- **`solve_matrix`**: Automatically prove matrix equalities by case analysis on indices\n  ```lean\n  lemma hadamard_mul_hadamard : hadamard * hadamard = 1 := by\n    solve_matrix [hadamard]\n  ```\n\n- **Future: `decide_complex`** (planned): Decision procedure for complex number equality in quantum gates, supporting ℚ[i], ℚ[√2, i], cyclotomic extensions, and trigonometric values\n\n## Project Structure\n\n```\nQuantumlib/\n├── Data/\n│   ├── Basis/           # Quantum basis states and ket-bra notation\n│   │   ├── Basic.lean       # Ket/bra definitions\n│   │   ├── Notation.lean    # Dirac notation (∣0⟩, ∣1⟩, etc.)\n│   │   └── Equivs.lean      # Basis equivalences and identities\n│   ├── Gate/            # Quantum gates\n│   │   ├── Basic.lean       # Core gates (Hadamard, CNOT, SWAP, √X)\n│   │   ├── Pauli/           # Pauli operators\n│   │   │   ├── Defs.lean       # Pauli structure and operations\n│   │   │   ├── Lemmas.lean     # Pauli algebra lemmas\n│   │   │   └── Notation.lean   # [P| ... ] and [PA| ... ] notation\n│   │   ├── Rotate.lean      # Rotation gates (Rx, Ry, Rz, U3)\n│   │   ├── PhaseShift.lean  # Phase shift gates (S, T)\n│   │   ├── Equivs.lean      # Gate equivalences and identities\n│   │   ├── Hermitian.lean   # Hermiticity proofs\n│   │   ├── Unitary.lean     # Unitarity proofs\n│   │   ├── ConjTranspose.lean\n│   │   └── Lemmas.lean\n│   └── Error/\n│       └── Operator.lean    # Stabilizer codes and error correction\n├── ForMathlib/          # Extensions to Mathlib (may be upstreamed)\n│   └── Data/\n│       ├── Complex/         # Complex number utilities\n│       ├── Matrix/          # Matrix operations\n│       │   ├── Basic.lean      # Matrix utilities\n│       │   ├── Kron.lean       # Kronecker products\n│       │   ├── Unitary.lean    # Unitary matrices\n│       │   └── PowBitVec.lean  # Tensor powers (M^⊗k)\n│       ├── BitVec/          # Bit vector utilities\n│       │   ├── Basic.lean      # BitVec operations\n│       │   └── Lemmas.lean     # BitVec lemmas\n│       └── Fin.lean         # Finite type utilities\n├── Tactic/              # Custom tactics\n│   ├── SolveMatrix.lean # Matrix equality automation\n│   └── Basic.lean\n├── Computation.lean     # Quantum circuit computation examples\n└── Basic.lean           # Main imports\n\n```\n\n## Installation\n\n### Prerequisites\n\n- [Lean 4](https://leanprover.github.io/) (v4.26.0-rc2 or compatible)\n- [Lake](https://github.com/leanprover/lake) (Lean's build system)\n\n### Building the Library\n\n```bash\n# Clone the repository\ngit clone https://github.com/inQWIRE/LeanQuantum\ncd LeanQuantum\n\n# Build the library\nlake build\n```\n\n### Using in Your Project\n\nAdd to your `lakefile.lean`:\n\n```lean\nrequire quantumlib from git\n  \"https://github.com/inQWIRE/LeanQuantum\"\n```\n\nThen import in your Lean files:\n\n```lean\nimport Quantumlib\n```\n\n## Usage Examples\n\n### Basic Gate Operations\n\n```lean\nimport Quantumlib\n\n-- Hadamard is its own inverse\nexample : hadamard * hadamard = 1 := by\n  solve_matrix [hadamard]\n\n-- Pauli X is a NOT gate\nexample : σx * ∣0⟩ = ∣1⟩ := by\n  solve_matrix [σx]\n\n-- Pauli gates square to identity\nexample : σx * σx = 1 := by\n  solve_matrix [σx]\n```\n\n### Square Root of X Gate\n\n```lean\n-- √X exists and satisfies √X · √X = X\nlemma sqrtx_mul_sqrtx : sqrtx * sqrtx = σx := by\n  simp [sqrtx, σx]\n  ring_nf\n  norm_num [Complex.I_sq]\n```\n\n### Pauli Operators with Notation\n\n```lean\nimport Quantumlib.Data.Gate.Pauli\n\n-- Create Pauli operators using notation\n#check [P| XXI ]   -- Pauli 3\n#check [P| ZZZ ]   -- Pauli 3\n#check [P| iXYZ ]  -- Pauli 3 (with phase i)\n\n-- Multiplication automatically tracks phases\nexample : [P| X ] * [P| Y ] = [P| iZ ] := by\n  decide\n\n-- Commutation checking\nexample : [P| XX ].commutesWith [P| ZZ ] = true := by\n  decide\n\nexample : [P| XZ ].commutesWith [P| ZX ] = false := by\n  decide\n```\n\n### Pauli Maps (Linear Combinations)\n\n```lean\nimport Quantumlib.Data.Gate.Pauli\n\n-- Single Pauli operator as a map\n#check [PA| XXI ]\n\n-- Linear combinations\ndef myObservable : PauliMap 3 := [PA| #(2) • XXI + #(3) • ZZI ]\n\n-- Products (automatically normalized)\nexample : [PA| X ] * [PA| Y ] = [PA| #(Complex.I) • Z ] := by\n  rfl\n```\n\n### Quantum Error Correction\n\n```lean\nimport Quantumlib.Data.Error.Operator\n\n-- The 3-qubit bit flip code\nexample : BitFlip.code.generators = [[P| ZZI ], [P| IZZ ]] := by\n  rfl\n\n-- Syndrome of an error\nexample : BitFlip.code.syndrome [P| IIX ] = [false, true] := by\n  rfl\n\n-- The code corrects single X errors\nexample : BitFlip.code.corrects {[P| IIX ], [P| IXI ], [P| XII ]} :=\n  BitFlip.corrects_single_qubit_errors\n```\n\n### Multi-qubit Gates\n\n```lean\n-- CNOT acts as controlled-X\nlemma cnot_decompose : cnot = ∣1⟩⟨1∣ ⊗ σx + ∣0⟩⟨0∣ ⊗ 1 := by\n  solve_matrix [cnot, σx]\n```\n\n## Verified Properties\n\nThe library includes formal proofs of key quantum computing properties:\n\n- **Gate identities**: H² = I, X² = I, Y² = I, Z² = I\n- **Hermiticity**: Pauli gates, Hadamard, rotation gates are Hermitian\n- **Unitarity**: All gates preserve inner products\n- **Pauli commutation**: [X,Y] = 2iZ, etc.\n- **Gate decompositions**: CNOT, controlled gates, √X\n- **Stabilizer properties**: Generators commute, logical operators anticommute\n- **Error correction**: Bit flip code corrects single-qubit X errors\n\n## Development Roadmap\n\n### Current Status ✅\n\n- [x] Basic quantum gates (H, X, Y, Z, CNOT, SWAP, √X)\n- [x] Pauli operator algebra with decidable equality\n- [x] Custom Pauli notation `[P| ... ]` and `[PA| ... ]`\n- [x] PauliMap for linear combinations\n- [x] Stabilizer code framework\n- [x] 3-qubit bit flip code (fully verified)\n- [x] Shor's 9-qubit code (partial verification)\n- [x] Ket-bra notation\n- [x] Rotation and phase shift gates\n- [x] `solve_matrix` tactic for gate equality proofs\n- [x] Hermiticity verification\n\n### In Progress 🚧\n\n- [ ] **Robust `solve_matrix` via 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)\n\n### Planned Features 📋\n\n- [ ] Complete unitarity proofs for rotation gates\n- [ ] More quantum error correction codes\n  - [ ] 5-qubit perfect code\n  - [ ] 7-qubit Steane code\n  - [ ] Surface codes\n  - [ ] CSS codes\n- [ ] Quantum circuits\n  - [ ] Circuit composition\n  - [ ] Circuit optimization\n  - [ ] Circuit equivalence\n- [ ] Quantum algorithms\n  - [ ] Deutsch-Jozsa\n  - [ ] Grover's algorithm\n  - [ ] Quantum Fourier Transform\n  - [ ] Shor's algorithm (circuit)\n- [ ] Measurement and observables\n- [ ] Density matrices and mixed states\n- [ ] Quantum channels and noise\n\n## Contributing\n\nContributions are welcome! Areas where help is particularly appreciated:\n\n1. **Completing unitarity proofs** for rotation gates\n2. **More stabilizer codes** (Steane, surface codes, etc.)\n3. **Quantum algorithms** formalization\n4. **Documentation and examples**\n5. **Performance improvements** to tactics\n\nPlease open an issue or pull request on [GitHub](https://github.com/inQWIRE/LeanQuantum).\n\n## Related Work\n\n- **[Lean-QuantumInfo](https://github.com/Timeroot/Lean-QuantumInfo)**: Another (more complete) quantum computing library in Lean 4\n- **[QuantumLib](https://github.com/inQWIRE/QuantumLib)**: Quantum computing library in Coq (inspiration for this project)\n- **[Mathlib](https://github.com/leanprover-community/mathlib4)**: Lean 4's mathematics library\n- **[SQIR](https://github.com/inQWIRE/SQIR)**: Small Quantum Intermediate Representation in Coq\n- **[Qiskit](https://qiskit.org/)**: IBM's quantum computing framework (Python)\n\n## Citation\n\nIf you use Quantumlib.lean in your research, please cite:\n\n```bibtex\n@software{quantumlib_lean,\n  title = {Quantumlib.lean: Formal Verification of Quantum Computing in Lean 4},\n  author = {Fady Adal},\n  year = {2025},\n  url = {https://github.com/inQWIRE/LeanQuantum}\n}\n```\n\n## Acknowledgments\n\nThis project is loosely inspired by [QuantumLib](https://github.com/inQWIRE/QuantumLib) from the inQWIRE group.\n\nSpecial thanks to:\n- The Lean community for Lean 4 and Mathlib\n- Professor Robert Rand and the inQWIRE group\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n---\n\n**Author**: Fady Adal\n**Status**: Active development\n**Lean Version**: v4.26.0-rc2\n**Repository**: https://github.com/inQWIRE/LeanQuantum\n",1780241993732]