Skip to content
1 change: 1 addition & 0 deletions Physlib.lean
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ public import Physlib.QuantumMechanics.PlanckConstant
public import Physlib.QuantumMechanics.PoschlTeller.Basic
public import Physlib.QuantumMechanics.PoschlTeller.Scattering
public import Physlib.QuantumMechanics.QuantumSystem.Basic
public import Physlib.QuantumMechanics.Qubit.PauliGroups
public import Physlib.QuantumMechanics.RectangularBarrier.Basic
public import Physlib.QuantumMechanics.SpaceDQuantumSystem
public import Physlib.Relativity.Bispinors.Basic
Expand Down
318 changes: 318 additions & 0 deletions Physlib/QuantumMechanics/Qubit/PauliGroups.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,318 @@
/-
Copyright (c) 2026 Samyak Rai. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Samyak Rai
-/

module

public import Mathlib.Data.ZMod.Basic
public import Physlib.Relativity.PauliMatrices.Basic

/-!

# Pauli groups

The single- and n-qubit Pauli groups in their binary symplectic representation.

## i. Overview

The single-qubit Pauli group is a 16-element matrix group, consisting of the four Pauli
matrices, including the identity, each with four possible phase factors. A single-qubit Pauli
operator is recorded by a phase `p : ZMod 4`, contributing the factor `i^p`, and two bits
`x` and `z`: the operator carries an `X` when `x = 1` and a `Z` when `z = 1`, so it carries a
`Y` when both are `1`. This is the definition `PauliGroup.single`.

Two single-qubit Pauli operators commute exactly when their symplectic product, which does not
depend on the phases, is zero. This is recorded by `PauliGroup.symplecticSingle` and proved in
`PauliGroup.single_commute_iff`.

The n-qubit Pauli group consists of tensor products of single-qubit Pauli operators with a
global phase. An n-qubit operator is recorded by a phase `p : ZMod 4` and two bitstrings
`x z : Fin n → ZMod 2`. The pair `(x, z)` is a row of a Pauli tableau: `x k` records an `X` on
qubit `k`, `z k` records a `Z`, and both together give `Y` up to phase. The operator itself is
`PauliGroup.multi`, a matrix on the computational basis `Fin n → Fin 2`. The symplectic product
of two such rows is `PauliGroup.symplectic`. Two n-qubit Pauli operators commute exactly when
their symplectic product is zero, which is `PauliGroup.multi_commute_iff`.

## ii. Key results

- `PauliGroup.single` : the single-qubit Pauli operator `i^p X^x Z^z` for phase `p` and bits
`x` and `z`.
- `PauliGroup.symplecticSingle` : the symplectic product of two single-qubit Pauli operators.
- `PauliGroup.single_commute_iff` : two single-qubit Pauli operators commute if and only if
their symplectic product is zero.
- `PauliGroup.multi` : the n-qubit Pauli operator `i^p ⊗ₖ (X^{xₖ} Z^{zₖ})`.
- `PauliGroup.symplectic` : the symplectic product of two n-qubit Pauli labels.
- `PauliGroup.multi_commute_iff` : two n-qubit Pauli operators commute if and only if their
symplectic product is zero.

## iii. Table of contents

- A. The single-qubit Pauli operators
- B. The symplectic product
- C. The n-qubit Pauli operators

## iv. References

- [Pauli group, Wikipedia](https://en.wikipedia.org/wiki/Pauli_group)
- [Nielsen and Chuang, Quantum Computation and Quantum Information][nielsen_chuang_qci]

## A. The single-qubit Pauli operators

-/

@[expose] public section

open PauliMatrix
open KroneckerDelta

namespace PauliGroup

/-- The single-qubit Pauli operator `i^p X^x Z^z`, for phase `p` and bits `x` and `z`.
As `p : ZMod 4`, `x : ZMod 2` and `z : ZMod 2` range over their values, these are the
16 elements of the one-qubit Pauli group. -/
def single (p : ZMod 4) (x z : ZMod 2) : Matrix (Fin 2) (Fin 2) ℂ :=
Complex.I ^ p.val • (σ1 ^ x.val * σ3 ^ z.val)

/-!

## B. The symplectic product

-/

/-- The symplectic product of two elements `(x, z)` and `(x', z')` of `(ℤ/2ℤ)²`:
`(x, z) ⊙ (x', z') = z x' - x z'`.
The bits record the presence of an `X` and a `Z` factor, as in `PauliGroup.single`. -/
def symplecticSingle (x z x' z' : ZMod 2) : ZMod 2 :=
z * x' - x * z'

/-- A generic criterion: if two matrices differ in a single entry after multiplication in the
two possible orders, they do not commute. -/
private lemma not_commute_of_entry_ne {A B : Matrix (Fin 2) (Fin 2) ℂ} {i j : Fin 2}
(h : (A * B) i j ≠ (B * A) i j) : ¬ Commute A B := by
intro hcomm
rw [commute_iff_eq] at hcomm
exact h (congrArg (fun M => M i j) hcomm)

/-- The symplectic product vanishes exactly when the corresponding Pauli matrices commute. -/
private lemma commute_pauli_iff (x z x' z' : ZMod 2) :
Commute (σ1 ^ x.val * σ3 ^ z.val) (σ1 ^ x'.val * σ3 ^ z'.val) ↔
symplecticSingle x z x' z' = 0 := by
have hbits : ∀ a : ZMod 2, a = 0 ∨ a = 1 := by decide
have hval0 : (0 : ZMod 2).val = 0 := rfl
have hval1 : (1 : ZMod 2).val = 1 := rfl
rcases hbits x with rfl | rfl <;>
rcases hbits z with rfl | rfl <;>
rcases hbits x' with rfl | rfl <;>
rcases hbits z' with rfl | rfl <;>
simp only [hval0, hval1, pow_zero, pow_one, one_mul, mul_one, symplecticSingle,
mul_zero, sub_self, sub_zero] <;>
first
| exact iff_of_true (Commute.refl _) (by decide)
| exact iff_of_true (Commute.one_left _) (by decide)
| exact iff_of_true (Commute.one_right _) (by decide)
| exact iff_of_false
(not_commute_of_entry_ne (A := σ1) (B := σ3) (i := 0) (j := 1)
(by norm_num [pauliMatrix])) (by decide)
| exact iff_of_false
(fun h => (not_commute_of_entry_ne (A := σ3) (B := σ1) (i := 0) (j := 1)
(by norm_num [pauliMatrix])) h) (by decide)
| exact iff_of_false
(not_commute_of_entry_ne (A := σ1) (B := σ1 * σ3) (i := 0) (j := 0)
(by norm_num [pauliMatrix])) (by decide)
| exact iff_of_false
(fun h => (not_commute_of_entry_ne (A := σ1 * σ3) (B := σ1) (i := 0) (j := 0)
(by norm_num [pauliMatrix])) h) (by decide)
| exact iff_of_false
(not_commute_of_entry_ne (A := σ3) (B := σ1 * σ3) (i := 0) (j := 1)
(by norm_num [pauliMatrix])) (by decide)
| exact iff_of_false
(fun h => (not_commute_of_entry_ne (A := σ1 * σ3) (B := σ3) (i := 0) (j := 1)
(by norm_num [pauliMatrix])) h) (by decide)


/-- Phases do not affect commutation. -/
private lemma commute_single_iff (p q : ZMod 4) (x z x' z' : ZMod 2) :
Commute (single p x z) (single q x' z') ↔
Commute (σ1 ^ x.val * σ3 ^ z.val) (σ1 ^ x'.val * σ3 ^ z'.val) := by
simp only [single]
rw [Commute.smul_left_iff₀ (pow_ne_zero p.val Complex.I_ne_zero),
Commute.smul_right_iff₀ (pow_ne_zero q.val Complex.I_ne_zero)]

/-- Two single-qubit Pauli operators commute if and only if their symplectic product is
zero. -/
lemma single_commute_iff (p q : ZMod 4) (x z x' z' : ZMod 2) :
Commute (single p x z) (single q x' z') ↔ symplecticSingle x z x' z' = 0 := by
rw [commute_single_iff, commute_pauli_iff]

/-!

## C. The n-qubit Pauli operators

A Pauli label, or a row of a Pauli tableau, is a pair of bitstrings `x z : Fin n → ZMod 2`.
On qubit `k`, `x k = 1` records an `X` factor and `z k = 1` records a `Z` factor; both together
give `Y` up to phase. A global phase `p : ZMod 4` contributes the factor `i^p`.

-/

variable {n : ℕ}

/-- The n-fold Kronecker product of a family of square matrices, indexed by bitstrings.
Entrywise, `(tensorAll A) i j = ∏ k, A k (i k) (j k)`. -/
private def tensorAll (A : Fin n → Matrix (Fin 2) (Fin 2) ℂ) :
Matrix (Fin n → Fin 2) (Fin n → Fin 2) ℂ :=
fun i j => ∏ k, A k (i k) (j k)

/-- The `n`-qubit Pauli operator `i^p ⊗ₖ (X^{xₖ} Z^{zₖ})`, acting on the computational basis
`Fin n → Fin 2`. The bitstrings `x` and `z` are the Pauli labels, i.e. the tableau row:
`x k = 1` puts an `X` on qubit `k`, `z k = 1` puts a `Z`, and both give `Y` up to phase. -/
def multi (p : ZMod 4) (x z : Fin n → ZMod 2) :
Matrix (Fin n → Fin 2) (Fin n → Fin 2) ℂ :=
fun i j => Complex.I ^ p.val * ∏ k, (σ1 ^ (x k).val * σ3 ^ (z k).val) (i k) (j k)

/-- The symplectic product of two `n`-qubit Pauli labels, the sum of the single-qubit
symplectic products. It does not depend on the phases. -/
def symplectic (x z x' z' : Fin n → ZMod 2) : ZMod 2 :=
∑ k, symplecticSingle (x k) (z k) (x' k) (z' k)

-- Private proof infrastructure for the commutation criterion.

/-- Tensor products multiply factorwise: the product of two `tensorAll`s is the `tensorAll`
of the factorwise products. This is the `∏`/`∑` swap `Fintype.prod_sum`. -/
private lemma tensorAll_mul (A B : Fin n → Matrix (Fin 2) (Fin 2) ℂ) :
tensorAll A * tensorAll B = tensorAll (fun k => A k * B k) := by
classical
ext i j
simp only [tensorAll, Matrix.mul_apply, ← Finset.prod_mul_distrib]
exact (Fintype.prod_sum (fun k (l : Fin 2) => A k (i k) l * B k l (j k))).symm

/-- Scalars pull out of a `tensorAll` as a single product. -/
private lemma tensorAll_smul (c : Fin n → ℂ) (A : Fin n → Matrix (Fin 2) (Fin 2) ℂ) :
tensorAll (fun k => c k • A k) = (∏ k, c k) • tensorAll A := by
ext i j
simp [tensorAll, Finset.prod_mul_distrib]

/-- A `tensorAll` of nonzero matrices is nonzero: pick a nonzero entry in each factor. -/
private lemma tensorAll_ne_zero (A : Fin n → Matrix (Fin 2) (Fin 2) ℂ)
(hA : ∀ k, A k ≠ 0) : tensorAll A ≠ 0 := by
classical
have h : ∀ k, ∃ i j, A k i j ≠ 0 := by
intro k
by_contra! hne
exact hA k (Matrix.ext fun i j => hne i j)
choose i j hij using h
intro h0
have h1 : ∏ k, A k (i k) (j k) = 0 := congr_fun (congr_fun h0 i) j
obtain ⟨k, _, hk⟩ := Finset.prod_eq_zero_iff.mp h1
exact hij k hk

/-- The sign `(-1)^e` attached to a symplectic bit `e : ZMod 2`. -/
private def phaseSign (e : ZMod 2) : ℂ := (-1) ^ e.val

/-- `phaseSign` turns addition of bits into multiplication of signs. -/
private lemma phaseSign_add (a b : ZMod 2) : phaseSign (a + b) = phaseSign a * phaseSign b := by
have hbits : ∀ a : ZMod 2, a = 0 ∨ a = 1 := by decide
have hval0 : (0 : ZMod 2).val = 0 := rfl
have hval1 : (1 : ZMod 2).val = 1 := rfl
have h11 : (1 : ZMod 2) + 1 = 0 := rfl
rcases hbits a with rfl | rfl <;> rcases hbits b with rfl | rfl <;>
simp only [phaseSign, hval0, hval1, h11, pow_zero, pow_one, zero_add, add_zero] <;>
norm_num

/-- A product of signs is the sign of the sum. -/
private lemma phaseSign_prod (s : Finset (Fin n)) (e : Fin n → ZMod 2) :
∏ k ∈ s, phaseSign (e k) = phaseSign (∑ k ∈ s, e k) := by
classical
induction s using Finset.induction_on with
| empty => simp [phaseSign]
| insert a s ha ih =>
simp only [Finset.prod_insert ha, Finset.sum_insert ha, ih, phaseSign_add]

/-- The sign is `1` exactly when the bit is `0`. -/
private lemma phaseSign_eq_one_iff (e : ZMod 2) : phaseSign e = 1 ↔ e = 0 := by
have hbits : ∀ a : ZMod 2, a = 0 ∨ a = 1 := by decide
have hval0 : (0 : ZMod 2).val = 0 := rfl
have hval1 : (1 : ZMod 2).val = 1 := rfl
rcases hbits e with rfl | rfl
· simp [phaseSign, hval0]
· simp [phaseSign, hval1]; norm_num

/-- The phase-free `n`-qubit Pauli tensor `⊗ₖ (X^{xₖ} Z^{zₖ})`, with no global phase. -/
private def pauliTensor (x z : Fin n → ZMod 2) :
Matrix (Fin n → Fin 2) (Fin n → Fin 2) ℂ :=
tensorAll (fun k => σ1 ^ (x k).val * σ3 ^ (z k).val)

/-- `multi` is a global phase times the phase-free tensor. -/
private lemma multi_eq_smul (p : ZMod 4) (x z : Fin n → ZMod 2) :
multi p x z = Complex.I ^ p.val • pauliTensor x z := by
ext i j
simp [multi, pauliTensor, tensorAll]

/-- Swapping two single-qubit Pauli factors produces the sign `(-1)` raised to their
symplectic product. This is the only 16-case check. -/
private lemma sigma_mul_sigma (x z x' z' : ZMod 2) :
(σ1 ^ x.val * σ3 ^ z.val) * (σ1 ^ x'.val * σ3 ^ z'.val) =
phaseSign (symplecticSingle x z x' z') •
((σ1 ^ x'.val * σ3 ^ z'.val) * (σ1 ^ x.val * σ3 ^ z.val)) := by
have hbits : ∀ a : ZMod 2, a = 0 ∨ a = 1 := by decide
have hval0 : (0 : ZMod 2).val = 0 := rfl
have hval1 : (1 : ZMod 2).val = 1 := rfl
rcases hbits x with rfl | rfl <;>
rcases hbits z with rfl | rfl <;>
rcases hbits x' with rfl | rfl <;>
rcases hbits z' with rfl | rfl <;>
simp only [hval0, hval1, pow_zero, pow_one, one_mul, mul_one, symplecticSingle,
phaseSign] <;>
ext i j <;> fin_cases i <;> fin_cases j <;>
simp [pauliMatrix, hval0, hval1]

/-- The tensor product picks up the product of the single-qubit signs, which is the sign of
the symplectic sum. -/
private lemma pauliTensor_comm_sign (x z x' z' : Fin n → ZMod 2) :
pauliTensor x z * pauliTensor x' z' =
phaseSign (symplectic x z x' z') • (pauliTensor x' z' * pauliTensor x z) := by
simp only [pauliTensor]
rw [tensorAll_mul]
have hfun : (fun k => (σ1 ^ (x k).val * σ3 ^ (z k).val) *
(σ1 ^ (x' k).val * σ3 ^ (z' k).val)) =
fun k => phaseSign (symplecticSingle (x k) (z k) (x' k) (z' k)) •
((σ1 ^ (x' k).val * σ3 ^ (z' k).val) * (σ1 ^ (x k).val * σ3 ^ (z k).val)) :=
funext fun k => sigma_mul_sigma (x k) (z k) (x' k) (z' k)
rw [hfun, tensorAll_smul]
have hprod : (∏ k, phaseSign (symplecticSingle (x k) (z k) (x' k) (z' k))) =
phaseSign (symplectic x z x' z') := by
simpa [symplectic] using
phaseSign_prod Finset.univ (fun k => symplecticSingle (x k) (z k) (x' k) (z' k))
rw [hprod, ← tensorAll_mul]

/-- A single phase-free Pauli factor `X^x Z^z` is a unit. -/
private lemma pauliFactor_isUnit (x z : ZMod 2) : IsUnit (σ1 ^ x.val * σ3 ^ z.val) :=
(isUnit_of_invertible (σ1 ^ x.val)).mul (isUnit_of_invertible (σ3 ^ z.val))

/-- Two `n`-qubit Pauli operators commute if and only if their symplectic product is zero. -/
lemma multi_commute_iff (p q : ZMod 4) (x z x' z' : Fin n → ZMod 2) :
Commute (multi p x z) (multi q x' z') ↔ symplectic x z x' z' = 0 := by
-- Global phases do not affect commutation.
rw [multi_eq_smul, multi_eq_smul,
Commute.smul_left_iff₀ (pow_ne_zero p.val Complex.I_ne_zero),
Commute.smul_right_iff₀ (pow_ne_zero q.val Complex.I_ne_zero), commute_iff_eq,
pauliTensor_comm_sign]
set M := pauliTensor x' z' * pauliTensor x z with hMdef
-- The phase-free product is nonzero: each factor is a unit.
have hne : M ≠ 0 := by
rw [hMdef]
simp only [pauliTensor]
rw [tensorAll_mul]
exact tensorAll_ne_zero _ (fun k =>
((pauliFactor_isUnit (x' k) (z' k)).mul (pauliFactor_isUnit (x k) (z k))).ne_zero)
-- A nonzero matrix is fixed by a scalar only when the scalar is `1`.
have hcancel : phaseSign (symplectic x z x' z') • M = M ↔
phaseSign (symplectic x z x' z') = 1 := by
have h1 : phaseSign (symplectic x z x' z') • M - M =
(phaseSign (symplectic x z x' z') - 1) • M := by
rw [sub_smul, one_smul]
rw [← sub_eq_zero, h1, smul_eq_zero_iff_left hne, sub_eq_zero]
rw [hcancel, phaseSign_eq_one_iff]

end PauliGroup
Loading