Lecture 5 gave an eight-step recipe and ran it on the average of a list. This lecture runs it on a subject you already know on paper: quantum information on a few qubits. The mathematics is finite-dimensional linear algebra over , which Mathlib covers well, and the proofs turn out to be short. The real work sits in two steps of the recipe: step 1 (how to represent a state) and step 4 (which of several reasonable statements of a theorem to prove).

The destination is the no-cloning theorem: no physical process copies an unknown quantum state. On paper the proof is four lines of inner products. In Lean it is about as short, once two questions are settled. What exactly is a state? And what exactly does “no process copies” say? The lecture builds a small library, LeanFV.Quantum, in lean/LeanFV/Quantum/Qubit.lean and lean/LeanFV/Quantum/NoCloning.lean. Every block below imports it, and every name you see (X, ket0, braket, CNOT, no_cloning) is defined and proved there without sorry.

Notebook, homework and cheatsheet

Notebook 7: Qubits in Lean runs the Pauli identities, shows what #eval can and cannot do over , and compares numerical checks with proofs. Homework 3 asks for more Pauli identities, a normalisation proof and a small no-cloning corollary. Cheatsheet: Representing qubits in Lean, Matrices, vectors and inner products, Does the statement say what you mean?.

Step 1 again: what is a qubit?

A qubit state is a unit vector in , and an -level system is a unit vector in . Mathlib gives at least three ways to write such a vector, and they are not interchangeable.

  1. Functions Fin 2 → ℂ. A vector is a function from indices to entries; ![a, b] builds one. A matrix acts on it with *ᵥ (Matrix.mulVec). This is the most direct choice for calculation.
  2. EuclideanSpace ℂ (Fin 2). The same functions behind a wrapper (WithLp) that tells Mathlib to use the Euclidean norm and the inner product . All of Mathlib’s theory of inner product spaces (orthonormal bases, adjoints, isometries) then applies.
  3. Column matrices Matrix (Fin 2) (Fin 1) ℂ. Kets and bras become matrices, so is a matrix product. This matches bra–ket notation, at the cost of matrices everywhere.

The first two look alike but carry different norms, and the difference is a trap. Mathlib treats a function type as a product of copies of , and the norm it gives a product is the largest coordinate norm (the sup norm). So the vector has norm as a function, and the expected only inside EuclideanSpace:

import Mathlib
import LeanFV.Quantum.NoCloning
 
open Matrix Complex LeanFV.Quantum
 
-- the sup norm: max(|1|, |1|) = 1
example : ‖(fun _ => 1 : Fin 2 → ℂ)‖ = 1 := by simp
 
-- the Euclidean norm: √(|1|² + |1|²) = √2
example :
    ‖(WithLp.toLp 2 (fun _ => 1) : EuclideanSpace ℂ (Fin 2))‖ = √2 := by
  rw [EuclideanSpace.norm_eq]
  simp
 
-- a ket as a column matrix
#check (!![1; 0] : Matrix (Fin 2) (Fin 1) ℂ)

Suppose you defined “normalised state” as ‖ψ‖ = 1 with ψ : Fin 2 → ℂ. Lean would accept the definition, and every theorem about normalised states would quietly be about a different set, one that contains . This is Lecture 5’s warning in a new form: the kernel checks that a definition is well typed, not that it means what you meant, and only a sanity check like the one above shows the difference.

The library sidesteps the norm. A state is a plain function, and the inner product is written out by hand (Definition 7.1).

Definition 7.1 (Kets and brackets).

In LeanFV.Quantum, Ket n is n → ℂ for a finite index type n. The bracket is braket ψ φ = star ψ ⬝ᵥ φ, that is . A ket is normalised when braket ψ ψ = 1. The basis states are ket0 = ![1, 0] and ket1 = ![0, 1].

example : braket (fun _ => 1 : Ket (Fin 2)) (fun _ => 1) = 2 := by
  simp [braket, dotProduct]

Why not EuclideanSpace? Because gates are matrices, and *ᵥ acts on plain functions. With EuclideanSpace, every matrix application unwraps and rewraps the vector (WithLp.toLp, .ofLp), or first converts the matrix into a linear map. Plain functions keep the matrix side simple. The price is that we must prove the few inner-product facts we need ourselves.

The separate quantum-LEAN4 project, a no-cloning formalization outside this course’s library, made the opposite choice. Its states live in EuclideanSpace, and a gate is a LinearIsometryEquiv (written ≃ₗᵢ[ℂ]): a linear bijection whose type already says it preserves inner products. That design gets “unitaries preserve inner products” for free from Mathlib (LinearIsometryEquiv.inner_map_map). It pays later, when a concrete matrix such as CNOT has to be turned into an isometry. Neither choice is wrong; each one moves the work to a different place.

Gates as matrices: the Pauli algebra

A one-qubit gate is a unitary matrix. Mathlib’s !![a, b; c, d] writes a matrix row by row, and the library defines the three Pauli matrices this way:

#print X

Lean answers def LeanFV.Quantum.X : Matrix (Fin 2) (Fin 2) ℂ := !![0, 1; 1, 0]. Similarly, Y is !![0, -I; I, 0] and Z is !![1, 0; 0, -1], where I is Mathlib’s Complex.I. The familiar identities , and are library theorems (X_mul_X, X_mul_Z, X_mul_Y, …). Their proofs follow one pattern:

  1. Unfold the definitions.
  2. Multiply with mul_fin_two, Mathlib’s formula for the product of two matrices.
  3. Compare the matrices entry by entry.
example : X * Z = -(Z * X) := by
  rw [X, Z, mul_fin_two, mul_fin_two]
  ext i j
  fin_cases i <;> fin_cases j <;> simp

ext i j turns one matrix equation into one equation per entry. fin_cases i splits i : Fin 2 into the cases 0 and 1. Then simp evaluates each of the four entries.

Unitarity is a membership statement. Mathlib’s Matrix.unitaryGroup n ℂ is the set of square matrices with , and the lemma mem_unitaryGroup_iff' unfolds membership to exactly that equation. Here star U is the conjugate transpose, also written Uᴴ. For the proof is four rewrites:

example : X ∈ unitaryGroup (Fin 2) ℂ := by
  rw [mem_unitaryGroup_iff', star_eq_conjTranspose, X_conjTranspose, X_mul_X]

Why not let Lean compute? decide settled questions about Booleans and natural numbers in earlier lectures, but over it fails:

example : X * X = 1 := by decide

Lean reports that decide “failed to reduce” the question to true or false, and that “reduction got stuck at Classical.choice ⋯”. The reason is in the definitions. Equality of complex numbers comes down to equality of real numbers, and a real number is a class of Cauchy sequences. No program can decide in general whether two such sequences have the same limit. So Mathlib’s instance deciding equality on is classical: it exists by the law of excluded middle and contains no algorithm to run. The simp proofs above never need to decide anything; they rewrite with lemmas such as . Notebook 7 meets the same wall through #eval, and gets around it by working over the Gaussian integers, where equality is decidable.

One more fact about unitaries is all that no-cloning will need: they preserve brackets.

#check @braket_unitary

The printed type reads: for U ∈ unitaryGroup n ℂ and any kets ψ, φ, braket (U *ᵥ ψ) (U *ᵥ φ) = braket ψ φ. On paper this is . In the library it is seven rewrites. They move star inside the product (star_mulVec), move the matrix across the dot product (dotProduct_mulVec, vecMul_vecMul), and then use .

Two qubits: Kronecker products

Two qubits live in , and a two-qubit gate is a unitary. Mathlib’s Matrix.kronecker takes A : Matrix l m ℂ and B : Matrix n p ℂ to a matrix whose rows are indexed by pairs, Matrix (l × n) (m × p) ℂ. So a two-qubit matrix is indexed by Fin 2 × Fin 2, not by Fin 4. This representation choice is made for you, and it is a good one: the entry of in row and column is , with no index arithmetic such as .

The infix symbol ⊗ₖ is scoped notation: it exists only after open scoped Kronecker. Before that, Lean cannot even parse it:

#check X ⊗ₖ Z

Lean reads #check X, prints the type of X, and then stops at the symbol with expected token. The message does not mention Kronecker at all, which is what makes this error confusing the first time. After opening the scope, the same line works:

open scoped Kronecker
 
#check X ⊗ₖ Z

The output is kroneckerMap (fun x1 x2 ↦ x1 * x2) X Z : Matrix (Fin 2 × Fin 2) (Fin 2 × Fin 2) ℂ. Lean prints the definition behind the notation, kroneckerMap applied to multiplication, rather than the symbol you typed. When #check shows you something unfamiliar, it is often a notation you wrote, unfolded.

For states, the library defines tensor ψ φ as the ket fun p => ψ p.1 * φ p.2, indexed by pairs. Two lemmas connect the pieces. The first, kronecker_mulVec_tensor, says . The second, braket_tensor, says that brackets multiply:

#check @braket_tensor

The printed type ends in braket (tensor ψ φ) (tensor ψ' φ') = braket ψ ψ' * braket φ φ', which is . The library uses ⊗ₖ to build the controlled-NOT gate, which flips the second qubit when the first is :

In Lean this is !![1, 0; 0, 0] ⊗ₖ 1 + !![0, 0; 0, 1] ⊗ₖ X. The library proves that it is unitary (CNOT_mem_unitaryGroup) and how it acts on product states. With a blank second qubit , CNOT copies a basis state:

example : CNOT *ᵥ tensor ket1 ket0 = tensor ket1 ket1 := by
  rw [CNOT_mulVec_ket1, X_mulVec_ket0]

So some states can be copied. The question is which ones.

No-cloning: two statements, one argument

In the library, Clones U s ψ means U *ᵥ tensor ψ s = tensor ψ ψ: the machine , fed and a blank state , outputs two copies of . The textbook theorem says that no unitary does this for every state. There are two natural ways to say that in Lean.

The existential form is the slogan, word for word. A statement is just a term of type Prop, so you can write it down and name it before anyone proves it:

def NoUniversalCloner : Prop :=
  ¬ ∃ (U : Matrix (Fin 2 × Fin 2) (Fin 2 × Fin 2) ℂ) (s : Ket (Fin 2)),
    U ∈ unitaryGroup (Fin 2 × Fin 2) ℂ ∧ braket s s = 1 ∧
    ∀ ψ : Ket (Fin 2), braket ψ ψ = 1 → Clones U s ψ

The conditional form (Theorem 7.1) says what happens when a single machine clones two given states.

Theorem 7.1 (No-cloning, conditional form).

Let be a unitary on and a normalised state. If and , then or . In Lean: LeanFV.Quantum.no_cloning.

The argument is the same for both forms. Write . The overlap of the two inputs is . The overlap of the two outputs is . A unitary preserves overlaps, so , which forces or . The figure traces the same chain.

inputs outputs z = ⟨ψ|φ⟩ ψ ⊗ s φ ⊗ s ψ ⊗ ψ φ ⊗ φ U U overlap z · 1 = z overlap z · z U preserves overlaps z = z² z = 0 or z = 1

In Lean the chain becomes a calc block, read from the bottom up. Each line is one library lemma or one hypothesis. The final step, from z * z = z to the disjunction, is plain algebra in (eq_zero_or_one_of_mul_self).

example {n : Type} [Fintype n] [DecidableEq n]
    {U : Matrix (n × n) (n × n) ℂ} (hU : U ∈ unitaryGroup (n × n) ℂ)
    {s ψ φ : Ket n} (hs : braket s s = 1)
    (hψ : Clones U s ψ) (hφ : Clones U s φ) :
    braket ψ φ = 0 ∨ braket ψ φ = 1 := by
  apply eq_zero_or_one_of_mul_self
  calc braket ψ φ * braket ψ φ
      = braket (tensor ψ ψ) (tensor φ φ) := (braket_tensor ψ φ ψ φ).symm
    _ = braket (U *ᵥ tensor ψ s) (U *ᵥ tensor φ s) := by rw [hψ, hφ]
    _ = braket (tensor ψ s) (tensor φ s) := braket_unitary hU _ _
    _ = braket ψ φ * braket s s := braket_tensor ψ φ s s
    _ = braket ψ φ := by rw [hs, mul_one]
 
#print axioms no_cloning

The audit reports 'LeanFV.Quantum.no_cloning' depends on axioms: [propext, Classical.choice, Quot.sound], the three standard axioms and nothing else.

Why the choice of form matters. The two forms are not interchangeable, and each has a cost.

  • The conditional form does not say “no cloner exists”. To get the slogan from it you still need two normalised states whose overlap is neither nor . quantum-LEAN4 uses and rather than , so that the overlap is the rational and norm_num handles every calculation.
  • In return, the conditional form works in every dimension . It needs no witnesses, and it says exactly which pairs a machine might clone. It is also sharp. CNOT clones both and onto the blank (CNOT_clones_ket0, CNOT_clones_ket1), and their overlap is . So “a machine never clones two different states” would be false.
  • The existential form fixes qubits and reads like the textbook, so a reader can check it against the slogan at a glance. Proving it in NoCloning.lean is left to you: the file ends with a TODO(you) block holding the skeleton and these trade-offs.

Each form also has hypotheses that are easy to drop by accident, and dropping them gives statements that are still well typed. Two examples show how.

Example 7.1 (Two misstatements).

Drop “normalised” from the ∀ ψ. The theorem then becomes true for a reason that has nothing to do with quantum mechanics. Cloning and with the same linear map would need to equal both (by linearity) and (by cloning). The Lean version below drops the other two conditions as well, unitarity and braket s s = 1, and is still provable: no matrix at all clones every vector. A statement that holds for every matrix, unitary or not, has lost its quantum content.

Swap ∃ s and ∀ ψ. If the blank may depend on the input, as in ∃ U, ∀ ψ, ∃ s, …, the identity machine clones everything: just take . That version of “no cloner exists” is false.

The first misstatement of Example 7.1, proved without any physics:

example :
    ¬ ∃ (U : Matrix (Fin 2 × Fin 2) (Fin 2 × Fin 2) ℂ) (s : Ket (Fin 2)),
      ∀ ψ : Ket (Fin 2), Clones U s ψ := by
  rintro ⟨U, s, h⟩
  have h1 := h ket0
  have h2 := h (2 • ket0)
  have e : tensor (2 • ket0) s = 2 • tensor ket0 s := by
    ext p
    simp [tensor, mul_assoc]
  rw [Clones, e, mulVec_smul, h1] at h2
  have h00 := congrFun h2 (0, 0)
  simp [tensor, ket0] at h00

And the second, where the identity matrix 1 clones any ψ onto the blank ψ:

example (ψ : Ket (Fin 2)) : Clones 1 ψ ψ := by
  simp [Clones]

Physlib, the community library for physics in Lean, proves a third version. Its no_cloning works with density matrices pure ψ instead of vectors, so cloning is only required up to a global phase. Its overlap is the real number , and its conclusion has the shape “if the overlap is less than 1, it is 0”. That gives three Lean statements of one theorem on paper:

stategateformoverlap
this courseKet n (n → ℂ)matrix in unitaryGroupconditional, ”= 0 or = 1”
quantum-LEAN4EuclideanSpace ℂ (Fin 2)≃ₗᵢ[ℂ]existential
Physlibdensity matrix pure ψ𝐔[d × d]conditional, ”< 1 → = 0”

All three are faithful to the theorem on paper, and none of them is the theorem on paper. Reading a formal statement means reading these choices.

Where the field is

Quantum information is one of the newer areas of formalization, and most results are recent. The tags follow the course convention: SELF-REPORTED means the authors announced it and no independent check has been published yet. Lecture 13 puts these in a wider survey.

  • Physlib, formerly HepLean and then PhysLean, was started by Joseph Tooby-Smith (arXiv 2405.08863). It merged with the Lean-QuantumInfo project around March 2026.
  • Generalized quantum Stein’s lemma: SELF-REPORTED. Meiburg, Lessa and Soldati formalized the Hayashi–Yamasaki proof (arXiv 2510.08672). The completion date of April 2026 comes from a single README.
  • Lean-QEC: SELF-REPORTED. Stabilizer, CSS and bivariate-bicycle codes, with code distances checked by machine through SAT solving and bv_decide (arXiv 2605.16523).
  • CHSH rigidity: SELF-REPORTED. Formalizing it exposed a gap in the original argument (arXiv 2604.03884).
  • Shor’s algorithm: SELF-REPORTED. An agent-driven formalization of order finding and of the arithmetic circuits, with resource estimates (arXiv 2607.14082).

The CHSH example shows why this matters. Writing the argument at the level of detail a kernel accepts is a way of reading it more carefully than a referee can. As in this lecture, most of the effort goes into choosing representations and statements, not into the final proof.

Try this

  • In Notebook 7 or in VS Code, prove Z * X = I • Y with the same pattern as X_mul_Z.
  • Prove that ![3/5, 4/5 * I] is normalised (unfold braket and dotProduct, then Fin.sum_univ_two), and compute its bracket with ket0.
  • Fill in the TODO(you) block at the end of lean/LeanFV/Quantum/NoCloning.lean: prove the existential form from no_cloning, choosing witnesses that keep the arithmetic rational.
  • Delete hs from the statement of no_cloning and find the line of the proof that breaks. Then decide whether the weakened statement is still true.

Readings