Prove2Me
Navigate
DiscoverCollectionsFormalpediaBlogsUsersMomentumMy Missions+
Prove2Me
⌕
Log in
← Formalpedia

Pauli group, weights, quantum codes, stabilizers, CSS codes, r-filters

Definition
QAlgorithms_Defs_QECC

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

lecture-notesquantum-information

Pauli group, weights, quantum codes, stabilizers, CSS codes, r-filters

QAlgorithms.opsWeight

For n∈Nn\in\mathbb{N}n∈N and a tuple o=(o1,…,on)o=(o_1,\dots,o_n)o=(o1​,…,on​) of Pauli labels from {I,X,Y,Z}\{I,X,Y,Z\}{I,X,Y,Z}, the weight is the number of positions qqq with oq≠Io_q \ne Ioq​=I:

wt(o)=#{q:oq≠I}.\mathrm{wt}(o) = \#\{q : o_q \neq I\}.wt(o)=#{q:oq​=I}.

For n=0n=0n=0 the weight is 000.

QAlgorithms.Stabilizer.PauliString.weight

A Pauli string on nnn qubits is a pair (c,o)(c, o)(c,o), where c∈Z/4c\in\mathbb{Z}/4c∈Z/4 is a phase exponent and o=(o1,…,on)o=(o_1,\dots,o_n)o=(o1​,…,on​) is a tuple of labels in {I,X,Y,Z}\{I,X,Y,Z\}{I,X,Y,Z}. Its weight is wt(o)=#{q:oq≠I}\mathrm{wt}(o)=\#\{q : o_q\ne I\}wt(o)=#{q:oq​=I}. The phase ccc is ignored.

QAlgorithms.pauliOps

For a tuple o=(o1,…,on)o=(o_1,\dots,o_n)o=(o1​,…,on​) of Pauli labels, this is the 2n×2n2^n\times 2^n2n×2n complex matrix of the Pauli string with phase exponent 000. Its entries, for y,z∈{0,1}ny, z \in\{0,1\}^ny,z∈{0,1}n, are

(σo)y,z=∏qσoq(yq,zq),\big(\sigma_{o}\big)_{y,z} = \prod_{q} \sigma_{o_q}(y_q, z_q),(σo​)y,z​=q∏​σoq​​(yq​,zq​),

that is, σo1⊗⋯⊗σon\sigma_{o_1}\otimes\cdots\otimes\sigma_{o_n}σo1​​⊗⋯⊗σon​​. The single-qubit matrices, with row and column index 000 meaning ∣0⟩|0\rangle∣0⟩, are

σI=I,σX=(0110),σY=(0−ii0),σZ=(100−1).\sigma_I = I,\qquad \sigma_X=\begin{pmatrix}0&1\\1&0\end{pmatrix},\qquad \sigma_Y=\begin{pmatrix}0&-i\\ i&0\end{pmatrix},\qquad \sigma_Z=\begin{pmatrix}1&0\\0&-1\end{pmatrix}.σI​=I,σX​=(01​10​),σY​=(0i​−i0​),σZ​=(10​0−1​).

For n=0n=0n=0 the result is the 1×11\times11×1 matrix (1)(1)(1).

QAlgorithms.pauliSpan

For n,t∈Nn,t\in\mathbb{N}n,t∈N, this is the complex linear span, inside the space of 2n×2n2^n\times 2^n2n×2n complex matrices, of all Pauli-string matrices of weight at most ttt:

span⁡C{ ic σo1⊗⋯⊗σon  :  c∈Z/4, #{q:oq≠I}≤t }.\operatorname{span}_{\mathbb{C}}\Big\{\, i^{c}\,\sigma_{o_1}\otimes\cdots\otimes\sigma_{o_n} \;:\; c\in\mathbb{Z}/4,\ \#\{q: o_q\ne I\}\le t \,\Big\}.spanC​{icσo1​​⊗⋯⊗σon​​:c∈Z/4, #{q:oq​=I}≤t}.

Here ici^cic means iii raised to the representative of ccc in {0,1,2,3}\{0,1,2,3\}{0,1,2,3}. The case t=0t=0t=0 gives the scalar multiples of the identity.

QAlgorithms.pauliGroup

For n∈Nn\in\mathbb{N}n∈N, this is the subgroup of the unitary group U(2n)U(2^n)U(2n) generated by every unitary whose matrix equals some Pauli-string matrix

ic σo1⊗⋯⊗σon,c∈Z/4, o∈{I,X,Y,Z}n,i^{c}\,\sigma_{o_1}\otimes\cdots\otimes\sigma_{o_n}, \qquad c\in\mathbb{Z}/4,\ o\in\{I,X,Y,Z\}^n,icσo1​​⊗⋯⊗σon​​,c∈Z/4, o∈{I,X,Y,Z}n,

with ccc taken as its representative in {0,1,2,3}\{0,1,2,3\}{0,1,2,3}. The Pauli matrices are as given under pauliOps.

QAlgorithms.IsStabilizerGroup

For n∈Nn\in\mathbb{N}n∈N and a subgroup SSS of the unitary group U(2n)U(2^n)U(2n), the predicate holds exactly when all three of the following hold:

  1. SSS is contained in the nnn-qubit Pauli group, i.e. the subgroup of U(2n)U(2^n)U(2n) generated by all matrices ic σo1⊗⋯⊗σoni^c\,\sigma_{o_1}\otimes\cdots\otimes\sigma_{o_n}icσo1​​⊗⋯⊗σon​​;
  2. SSS is abelian: ab=baab=baab=ba for all a,b∈Sa,b\in Sa,b∈S;
  3. no element of SSS has matrix equal to −I-I−I.

QAlgorithms.stabilizedSpace

For n∈Nn\in\mathbb{N}n∈N and any subgroup SSS of the unitary group U(2n)U(2^n)U(2n), this is the complex subspace of C2n\mathbb{C}^{2^n}C2n (indexed by {0,1}n\{0,1\}^n{0,1}n) given by

VS=⋂M∈Sker⁡(M−I)={ψ∈C2n:Mψ=ψ for all M∈S}.V_S=\bigcap_{M\in S}\ker(M-I)=\{\psi\in\mathbb{C}^{2^n} : M\psi=\psi \text{ for all } M\in S\}.VS​=M∈S⋂​ker(M−I)={ψ∈C2n:Mψ=ψ for all M∈S}.

No hypothesis on SSS is imposed, for example that it is a stabilizer group. If SSS is the trivial group, VSV_SVS​ is all of C2n\mathbb{C}^{2^n}C2n. If SSS contains −I-I−I, then VS={0}V_S=\{0\}VS​={0}.

QAlgorithms.pauliCentralizer

For n∈Nn\in\mathbb{N}n∈N and any subgroup SSS of U(2n)U(2^n)U(2n), this is the subgroup

Pn∩CU(2n)(S)={U∈Pn:UM=MU for all M∈S}.\mathcal{P}_n\cap C_{U(2^n)}(S)=\{U\in\mathcal{P}_n : UM=MU \text{ for all } M\in S\}.Pn​∩CU(2n)​(S)={U∈Pn​:UM=MU for all M∈S}.

Here Pn\mathcal{P}_nPn​ is the subgroup of U(2n)U(2^n)U(2n) generated by all matrices ic σo1⊗⋯⊗σoni^c\,\sigma_{o_1}\otimes\cdots\otimes\sigma_{o_n}icσo1​​⊗⋯⊗σon​​, with c∈Z/4c\in\mathbb{Z}/4c∈Z/4 and o∈{I,X,Y,Z}no\in\{I,X,Y,Z\}^no∈{I,X,Y,Z}n.

QAlgorithms.phaseMultiples

For n∈Nn\in\mathbb{N}n∈N and any subgroup SSS of U(2n)U(2^n)U(2n), this is the set of unitaries U∈U(2n)U\in U(2^n)U∈U(2n) for which there exist c∈Z/4c\in\mathbb{Z}/4c∈Z/4 and M∈SM\in SM∈S with

U=ic M,U = i^{c}\,M,U=icM,

where ccc is taken as its representative in {0,1,2,3}\{0,1,2,3\}{0,1,2,3}. Equivalently, it is the set of all ±M\pm M±M and ±iM\pm iM±iM for M∈SM\in SM∈S. It is defined only as a set, not as a subgroup.

QAlgorithms.pauliSyndrome

Inputs.

  • n,a∈Nn, a \in \mathbb{N}n,a∈N.
  • A family g=(g0,…,ga−1)g = (g_0, \dots, g_{a-1})g=(g0​,…,ga−1​) of 2n×2n2^n \times 2^n2n×2n unitary matrices. Rows and columns are indexed by nnn-bit strings {0,1}n\{0,1\}^n{0,1}n.
  • A single unitary EEE of the same size.

Output. The function returns a vector s∈(Z/2)as \in (\mathbb{Z}/2)^as∈(Z/2)a whose iii-th entry is

si={0if giE=Egi,1otherwise.s_i = \begin{cases} 0 & \text{if } g_i E = E g_i,\\ 1 & \text{otherwise.}\end{cases}si​={01​if gi​E=Egi​,otherwise.​

Remarks.

  • The test is literal matrix commutation, with no allowance for phases. Entry 111 is assigned whenever gig_igi​ and EEE fail to commute exactly, whether they anticommute or stand in any other relation.
  • Nothing is assumed about the gig_igi​ or EEE beyond unitarity. In particular, they need not be Pauli operators.
  • When a=0a = 0a=0 the result is the empty vector.

QAlgorithms.ActsWithin

Inputs.

  • n∈Nn \in \mathbb{N}n∈N.
  • A finite set F⊆{0,…,n−1}F \subseteq \{0, \dots, n-1\}F⊆{0,…,n−1} of wires.
  • An arbitrary complex 2n×2n2^n \times 2^n2n×2n matrix EEE, indexed by nnn-bit strings.

Definition. The predicate holds iff there exist:

  • some k∈Nk \in \mathbb{N}k∈N,
  • an injective map w:{0,…,k−1}→{0,…,n−1}w : \{0,\dots,k-1\} \to \{0,\dots,n-1\}w:{0,…,k−1}→{0,…,n−1} whose image is contained in FFF, and
  • an arbitrary complex 2k×2k2^k \times 2^k2k×2k matrix MMM (not required to be unitary, Hermitian, or anything else),

such that EEE is the embedding of MMM on the wires www. Entrywise, for y,z∈{0,1}ny, z \in \{0,1\}^ny,z∈{0,1}n,

Ey,z={My∘w,  z∘wif ya=za for every wire a∉im⁡w,0otherwise.E_{y,z} = \begin{cases} M_{y\circ w,\; z\circ w} & \text{if } y_a = z_a \text{ for every wire } a \notin \operatorname{im} w,\\ 0 & \text{otherwise.}\end{cases}Ey,z​={My∘w,z∘w​0​if ya​=za​ for every wire a∈/imw,otherwise.​

Here y∘wy \circ wy∘w is the kkk-bit string (yw(0),…,yw(k−1))(y_{w(0)}, \dots, y_{w(k-1)})(yw(0)​,…,yw(k−1)​). In words, EEE is MMM acting on the wires w(0),…,w(k−1)w(0), \dots, w(k-1)w(0),…,w(k−1), in that order, tensored with the identity on all remaining wires.

Degenerate case. k=0k = 0k=0 is allowed. Then MMM is a 1×11 \times 11×1 scalar ccc and E=c IE = c\, IE=cI. So every scalar multiple of the identity satisfies the predicate for every FFF, including F=∅F = \emptysetF=∅.

QAlgorithms.ActsOnAtMost

Inputs.

  • n,t∈Nn, t \in \mathbb{N}n,t∈N.
  • An arbitrary complex 2n×2n2^n \times 2^n2n×2n matrix EEE.

Definition. The predicate holds iff there is a set FFF of wires with ∣F∣≤t|F| \le t∣F∣≤t such that EEE acts within FFF. Expanding the previous definition, this means:

E=M⊗IrestE = M \otimes I_{\text{rest}}E=M⊗Irest​

for some arbitrary complex matrix MMM placed, via an injective wire map, on wires lying in FFF, with EEE acting as the identity on every other wire.

Remarks.

  • For t=0t = 0t=0 the predicate holds exactly for the scalar multiples of the identity.
  • For t≥nt \ge nt≥n it holds for every EEE. One can take FFF to be all wires and www the identity map, which gives E=ME = ME=M.

QAlgorithms.Corrects

Inputs.

  • A finite index type ι\iotaι with decidable equality.
  • A complex linear subspace C⊆CιC \subseteq \mathbb{C}^\iotaC⊆Cι.
  • An arbitrary set SSS of complex ι×ι\iota \times \iotaι×ι matrices. These need not be unitary.

Definition. The predicate holds iff there exists a map RRR from ι×ι\iota\times\iotaι×ι matrices to ι×ι\iota\times\iotaι×ι matrices satisfying two conditions.

  1. RRR is a channel. There exist m∈Nm \in \mathbb{N}m∈N and matrices K0,…,Km−1K_0, \dots, K_{m-1}K0​,…,Km−1​ with
∑jKj†Kj=IandR(ρ)=∑jKjρKj†  for every matrix ρ.\sum_j K_j^\dagger K_j = I \quad\text{and}\quad R(\rho) = \sum_j K_j \rho K_j^\dagger \ \text{ for every matrix } \rho.j∑​Kj†​Kj​=IandR(ρ)=j∑​Kj​ρKj†​  for every matrix ρ.
  1. RRR corrects every error in SSS. For every E∈SE \in SE∈S and every ψ∈C\psi \in Cψ∈C with ∥ψ∥=1\|\psi\| = 1∥ψ∥=1,
R(∣Eψ⟩⟨Eψ∣)=∥Eψ∥2 ∣ψ⟩⟨ψ∣.R\big(|E\psi\rangle\langle E\psi|\big) = \|E\psi\|^2 \, |\psi\rangle\langle\psi| .R(∣Eψ⟩⟨Eψ∣)=∥Eψ∥2∣ψ⟩⟨ψ∣.

Here ∣ϕ⟩⟨ϕ∣|\phi\rangle\langle\phi|∣ϕ⟩⟨ϕ∣ denotes the matrix with entries ϕaϕb‾\phi_a \overline{\phi_b}ϕa​ϕb​​.

Edge cases.

  • If Eψ=0E\psi = 0Eψ=0, both sides of the condition are 000.
  • If C={0}C = \{0\}C={0} or S=∅S = \emptysetS=∅, the second condition is vacuous. The predicate then only requires that some channel exist.

QAlgorithms.IsDetectable

Inputs.

  • A finite index type ι\iotaι.
  • A complex subspace C⊆CιC \subseteq \mathbb{C}^\iotaC⊆Cι.
  • An arbitrary complex ι×ι\iota \times \iotaι×ι matrix EEE.

Definition. The predicate holds iff there is a single complex number ccc such that every unit vector ψ∈C\psi \in Cψ∈C satisfies

⟨ψ,Eψ⟩=c.\langle \psi, E\psi\rangle = c.⟨ψ,Eψ⟩=c.

The inner product is ⟨u,v⟩=∑iui‾ vi\langle u, v\rangle = \sum_i \overline{u_i}\, v_i⟨u,v⟩=∑i​ui​​vi​, conjugate-linear in the first slot.

Remarks.

  • Only these diagonal expectation values are constrained. No condition on ⟨ϕ,Eψ⟩\langle\phi, E\psi\rangle⟨ϕ,Eψ⟩ for distinct ϕ,ψ\phi, \psiϕ,ψ is stated explicitly.
  • If C={0}C = \{0\}C={0}, the predicate holds vacuously.

QAlgorithms.IsQCode

Inputs.

  • n,k,d∈Nn, k, d \in \mathbb{N}n,k,d∈N.
  • A complex subspace CCC of the nnn-qubit space C{0,1}n\mathbb{C}^{\{0,1\}^n}C{0,1}n.

Definition. CCC satisfies the predicate iff both of the following hold.

  1. Dimension. dim⁡CC=2k\dim_{\mathbb{C}} C = 2^kdimC​C=2k.

  2. Detection. Every complex 2n×2n2^n\times 2^n2n×2n matrix EEE that acts on at most d−1d - 1d−1 qubits is detectable on CCC. Concretely:

    • "Acts on at most d−1d-1d−1 qubits" means E=M⊗IE = M \otimes IE=M⊗I with an arbitrary complex matrix MMM on at most d−1d-1d−1 wires.
    • "Detectable on CCC" means there is a constant cEc_EcE​ with ⟨ψ,Eψ⟩=cE\langle\psi, E\psi\rangle = c_E⟨ψ,Eψ⟩=cE​ for all unit ψ∈C\psi \in Cψ∈C.

Edge cases.

  • d−1d - 1d−1 is truncated subtraction, so d=0d = 0d=0 is treated like d=1d = 1d=1. In both cases only scalar multiples of III are required to be detectable, which always holds.
  • If d−1≥nd - 1 \ge nd−1≥n, every 2n×2n2^n \times 2^n2n×2n matrix must be detectable.
  • Only this one-sided requirement is imposed. Nothing says some matrix acting on ddd qubits fails to be detectable, so the predicate is monotone downward in ddd.
  • k=0k = 0k=0 means CCC is one-dimensional.

QAlgorithms.HasDistance

Inputs.

  • n,d∈Nn, d \in \mathbb{N}n,d∈N.
  • A complex subspace C⊆C{0,1}nC \subseteq \mathbb{C}^{\{0,1\}^n}C⊆C{0,1}n.

Definition. The predicate holds iff both of the following hold.

  1. Detection below ddd. Every arbitrary complex 2n×2n2^n\times 2^n2n×2n matrix EEE acting on at most d−1d-1d−1 qubits (truncated subtraction) is detectable on CCC. That is, ⟨ψ,Eψ⟩\langle\psi, E\psi\rangle⟨ψ,Eψ⟩ takes the same value for all unit ψ∈C\psi\in Cψ∈C.

  2. Failure at ddd. There exists some complex matrix EEE acting on at most ddd qubits (that is, E=M⊗IE = M\otimes IE=M⊗I with MMM on at most ddd wires) that is not detectable on CCC. That is, there are unit vectors ψ,ψ′∈C\psi, \psi' \in Cψ,ψ′∈C with

⟨ψ,Eψ⟩≠⟨ψ′,Eψ′⟩.\langle\psi, E\psi\rangle \ne \langle\psi', E\psi'\rangle.⟨ψ,Eψ⟩=⟨ψ′,Eψ′⟩.

Edge case d=0d = 0d=0.

  • The first clause concerns only scalar multiples of III.
  • The second clause asks for a non-detectable matrix acting on 000 qubits. Such a matrix is a scalar multiple of III, and those are always detectable.
  • So the predicate is false for d=0d = 0d=0.

QAlgorithms.IsNondegenerate

Inputs.

  • n,t∈Nn, t \in \mathbb{N}n,t∈N.
  • A complex subspace C⊆C{0,1}nC \subseteq \mathbb{C}^{\{0,1\}^n}C⊆C{0,1}n.

Pauli operators. Consider all assignments o:{0,…,n−1}→{I,X,Y,Z}o : \{0,\dots,n-1\} \to \{I, X, Y, Z\}o:{0,…,n−1}→{I,X,Y,Z} of weight at most ttt. The weight of ooo is the number of wires qqq with o(q)≠Io(q) \ne Io(q)=I. Each such ooo gives the operator

Po=σo(0)⊗⋯⊗σo(n−1),(Po)y,z=∏q(σo(q))yq,zq,P_o = \sigma_{o(0)} \otimes \cdots \otimes \sigma_{o(n-1)}, \qquad (P_o)_{y,z} = \prod_q \big(\sigma_{o(q)}\big)_{y_q, z_q},Po​=σo(0)​⊗⋯⊗σo(n−1)​,(Po​)y,z​=q∏​(σo(q)​)yq​,zq​​,

with phase factor 111. The single-qubit matrices, in basis order (0,1)(0, 1)(0,1), are

σI=(1001),σX=(0110),σY=(0−ii0),σZ=(100−1).\sigma_I = \begin{pmatrix}1&0\\0&1\end{pmatrix}, \quad \sigma_X = \begin{pmatrix}0&1\\1&0\end{pmatrix}, \quad \sigma_Y = \begin{pmatrix}0&-i\\ i&0\end{pmatrix}, \quad \sigma_Z = \begin{pmatrix}1&0\\0&-1\end{pmatrix}.σI​=(10​01​),σX​=(01​10​),σY​=(0i​−i0​),σZ​=(10​0−1​).

Definition. The predicate holds iff for every ψ∈C\psi \in Cψ∈C with ∥ψ∥=1\|\psi\| = 1∥ψ∥=1, the Gram matrix

Ga,bψ=⟨Paψ,  Pbψ⟩,G^{\psi}_{a,b} = \langle P_a \psi,\; P_b \psi\rangle,Ga,bψ​=⟨Pa​ψ,Pb​ψ⟩,

indexed by pairs of such weight-≤t\le t≤t assignments a,ba, ba,b, has nonzero determinant.

Edge cases.

  • For t=0t = 0t=0 the index set is the single all-III assignment, and Gψ=(∥ψ∥2)=(1)G^\psi = (\|\psi\|^2) = (1)Gψ=(∥ψ∥2)=(1).
  • If C={0}C = \{0\}C={0}, the predicate holds vacuously.

QAlgorithms.binDual

Input. A linear subspace C⊆F2nC \subseteq \mathbb{F}_2^nC⊆F2n​, where F2=Z/2\mathbb{F}_2 = \mathbb{Z}/2F2​=Z/2.

Definition. This is the subspace

C⊥={ x∈F2n  :  c⋅x=0 for all c∈C },c⋅x=∑i=0n−1cixi(mod2),C^\perp = \{\, x \in \mathbb{F}_2^n \;:\; c \cdot x = 0 \text{ for all } c \in C \,\}, \qquad c\cdot x = \sum_{i=0}^{n-1} c_i x_i \pmod 2,C⊥={x∈F2n​:c⋅x=0 for all c∈C},c⋅x=i=0∑n−1​ci​xi​(mod2),

that is, the dual code with respect to the standard dot product over F2\mathbb{F}_2F2​.

The accompanying proof obligations only verify that this set contains 000 and is closed under addition and scalar multiplication.

QAlgorithms.bitsOfZMod

Input. A vector v∈F2nv \in \mathbb{F}_2^nv∈F2n​.

Output. The nnn-bit string whose bit on wire qqq is 111 (true) exactly when vq=1v_q = 1vq​=1, and 000 (false) when vq=0v_q = 0vq​=0.

This is the identification F2∋0↦0\mathbb{F}_2 \ni 0 \mapsto 0F2​∋0↦0, 1↦11 \mapsto 11↦1, applied coordinatewise. Wire qqq of the string corresponds to coordinate qqq of vvv, and wire 000 is the most significant bit.

QAlgorithms.cssCosetState

Inputs.

  • A linear subspace C2⊆F2nC_2 \subseteq \mathbb{F}_2^nC2​⊆F2n​.
  • A vector u∈F2nu \in \mathbb{F}_2^nu∈F2n​.

Output. The nnn-qubit vector

∣u+C2⊥⟩  =  ∑w∈C2⊥∣ u+w ⟩.|u + C_2^\perp\rangle \;=\; \sum_{w \in C_2^\perp} \big|\, u + w \,\big\rangle .∣u+C2⊥​⟩=w∈C2⊥​∑​​u+w⟩.

Here:

  • C2⊥={x:c⋅x=0 ∀c∈C2}C_2^\perp = \{x : c\cdot x = 0\ \forall c \in C_2\}C2⊥​={x:c⋅x=0 ∀c∈C2​} is the dual code over F2\mathbb{F}_2F2​.
  • u+wu + wu+w is computed in F2n\mathbb{F}_2^nF2n​, i.e. bitwise XOR.
  • ∣b⟩|b\rangle∣b⟩ is the computational-basis vector of the bit string bbb.

Remarks.

  • The vector is not normalized. Since the terms u+wu + wu+w are distinct, its squared norm is ∣C2⊥∣|C_2^\perp|∣C2⊥​∣.
  • No hypothesis relates uuu to any other code.

QAlgorithms.cssCode

Inputs. Two arbitrary linear subspaces C1,C2⊆F2nC_1, C_2 \subseteq \mathbb{F}_2^nC1​,C2​⊆F2n​.

Definition. This is the complex subspace of the nnn-qubit space

span⁡C{∑w∈C2⊥∣u+w⟩  :  u∈C1},\operatorname{span}_{\mathbb{C}}\Big\{ \textstyle\sum_{w \in C_2^\perp} |u + w\rangle \;:\; u \in C_1 \Big\},spanC​{∑w∈C2⊥​​∣u+w⟩:u∈C1​},

the linear span of the unnormalized coset states ∣u+C2⊥⟩|u + C_2^\perp\rangle∣u+C2⊥​⟩ as uuu ranges over all of C1C_1C1​. Here C2⊥C_2^\perpC2⊥​ is the F2\mathbb{F}_2F2​ dual of C2C_2C2​, and ∣b⟩|b\rangle∣b⟩ is the computational-basis vector of the bit string bbb.

Remarks.

  • No hypothesis such as C2⊥⊆C1C_2^\perp \subseteq C_1C2⊥​⊆C1​ is imposed.
  • Different uuu may give the same coset state, namely when they differ by an element of C2⊥C_2^\perpC2⊥​.
  • C1={0}C_1 = \{0\}C1​={0} is allowed, giving the span of the single vector ∑w∈C2⊥∣w⟩\sum_{w\in C_2^\perp}|w\rangle∑w∈C2⊥​​∣w⟩.

QAlgorithms.cssZeroState

Setting.

  • nnn is a natural number, the number of qubits.
  • C2C_2C2​ is any F2\mathbb{F}_2F2​-linear subspace of F2n\mathbb{F}_2^nF2n​.
  • The state space is C{0,1}n\mathbb{C}^{\{0,1\}^n}C{0,1}n with the standard inner product. For a bit string yyy, ∣y⟩|y\rangle∣y⟩ is the standard basis vector at yyy.

Definition. The vector is

cssZero(C2)  =  ∑w∈C2⊥∣ wˉ ⟩,\mathrm{cssZero}(C_2) \;=\; \sum_{w \in C_2^{\perp}} |\,\bar w\,\rangle ,cssZero(C2​)=w∈C2⊥​∑​∣wˉ⟩,

where:

  • C2⊥={x∈F2n:c⋅x=∑qcqxq=0 in F2 for all c∈C2}C_2^{\perp} = \{x \in \mathbb{F}_2^n : c\cdot x = \sum_q c_q x_q = 0 \text{ in } \mathbb{F}_2 \text{ for all } c\in C_2\}C2⊥​={x∈F2n​:c⋅x=∑q​cq​xq​=0 in F2​ for all c∈C2​} is the binary dual of C2C_2C2​.
  • wˉ∈{0,1}n\bar w \in \{0,1\}^nwˉ∈{0,1}n is the bit string with wˉq=1\bar w_q = 1wˉq​=1 exactly when wq=1∈F2w_q = 1 \in \mathbb{F}_2wq​=1∈F2​.

This is the coset-state construction evaluated at the offset u=0u = 0u=0.

Edge cases.

  • The sum runs over the dual C2⊥C_2^\perpC2⊥​, not over C2C_2C2​ itself.
  • There is no normalization factor: the vector has norm ∣C2⊥∣\sqrt{|C_2^\perp|}∣C2⊥​∣​, which is not 111 in general. It equals 111 only when C2⊥={0}C_2^\perp=\{0\}C2⊥​={0}, i.e. C2=F2nC_2=\mathbb{F}_2^nC2​=F2n​, in which case the vector is ∣0⋯0⟩|0\cdots0\rangle∣0⋯0⟩.
  • For n=0n=0n=0 the vector is the single basis vector of the one-dimensional space.

QAlgorithms.cssPlusState

Setting.

  • nnn is a natural number.
  • C1C_1C1​ is any F2\mathbb{F}_2F2​-linear subspace of F2n\mathbb{F}_2^nF2n​.

Definition. The vector in C{0,1}n\mathbb{C}^{\{0,1\}^n}C{0,1}n is

cssPlus(C1)  =  ∑u∈C1∣ uˉ ⟩,\mathrm{cssPlus}(C_1) \;=\; \sum_{u \in C_1} |\,\bar u\,\rangle ,cssPlus(C1​)=u∈C1​∑​∣uˉ⟩,

where uˉ∈{0,1}n\bar u\in\{0,1\}^nuˉ∈{0,1}n has uˉq=1\bar u_q = 1uˉq​=1 iff uq=1u_q = 1uq​=1 in F2\mathbb{F}_2F2​.

Edge cases.

  • The sum runs over C1C_1C1​ itself, not over a dual.
  • No Hadamard transform is involved.
  • There is no normalization: the norm is ∣C1∣\sqrt{|C_1|}∣C1​∣​. This is 111 only when C1={0}C_1=\{0\}C1​={0}, giving ∣0⋯0⟩|0\cdots 0\rangle∣0⋯0⟩.

QAlgorithms.errorSpace

Setting.

  • nnn is a natural number.
  • CCC is any complex linear subspace of C{0,1}n\mathbb{C}^{\{0,1\}^n}C{0,1}n.
  • F⊆{0,…,n−1}F \subseteq \{0,\dots,n-1\}F⊆{0,…,n−1} is a finite set of wire indices (wire iii is qubit i+1i+1i+1; wire 000 is the most significant).

Definition. errorSpace(C,F)\mathrm{errorSpace}(C,F)errorSpace(C,F) is the complex linear span of all vectors EcE cEc such that:

  • c∈Cc \in Cc∈C, and
  • EEE is a 2n×2n2^n\times 2^n2n×2n complex matrix that "acts within FFF".

"Acts within FFF". This means there exist:

  • some k≥0k\ge 0k≥0,
  • an injective map w:{0,…,k−1}→{0,…,n−1}w:\{0,\dots,k-1\}\to\{0,\dots,n-1\}w:{0,…,k−1}→{0,…,n−1} with image contained in FFF, and
  • an arbitrary complex 2k×2k2^k\times 2^k2k×2k matrix MMM,

such that EEE is MMM placed on wires www. Explicitly:

Ey,z={M y∘w,  z∘wif ya=za for every wire a∉w({0,…,k−1}),0otherwise.E_{y,z} = \begin{cases} M_{\,y\circ w,\; z\circ w} & \text{if } y_a = z_a \text{ for every wire } a \notin w(\{0,\dots,k-1\}),\\ 0 & \text{otherwise.}\end{cases}Ey,z​={My∘w,z∘w​0​if ya​=za​ for every wire a∈/w({0,…,k−1}),otherwise.​

Here y∘wy\circ wy∘w denotes the kkk-bit string (yw(0),…,yw(k−1))(y_{w(0)},\dots,y_{w(k-1)})(yw(0)​,…,yw(k−1)​).

Edge cases.

  • MMM, and hence EEE, need not be unitary or invertible. Any matrix, including 000, is allowed.
  • Taking k=0k=0k=0 gives E=M∅,∅⋅IE = M_{\emptyset,\emptyset}\cdot IE=M∅,∅​⋅I, a scalar multiple of the identity, so C⊆errorSpace(C,F)C \subseteq \mathrm{errorSpace}(C,F)C⊆errorSpace(C,F) always.
  • For F=∅F=\emptysetF=∅, only k=0k=0k=0 is possible, and the space equals CCC.
  • If C={0}C=\{0\}C={0}, the space is {0}\{0\}{0}.

QAlgorithms.errorSpaceWithRef

Setting.

  • nnn is a natural number.
  • RRR is any finite type, serving as a reference system.
  • CCC is a complex subspace of C{0,1}n\mathbb{C}^{\{0,1\}^n}C{0,1}n.
  • FFF is a finite set of wires.

Definition. The subspace of C{0,1}n×R\mathbb{C}^{\{0,1\}^n\times R}C{0,1}n×R (system register first, reference second) is

span⁡C{ v⊗r  :  v∈errorSpace(C,F), r∈CR },\operatorname{span}_{\mathbb{C}}\{\, v\otimes r \;:\; v \in \mathrm{errorSpace}(C,F),\ r\in\mathbb{C}^R \,\},spanC​{v⊗r:v∈errorSpace(C,F), r∈CR},

where:

  • (v⊗r)(z,ρ)=vz rρ(v\otimes r)(z,\rho) = v_z\, r_\rho(v⊗r)(z,ρ)=vz​rρ​, and
  • errorSpace(C,F)\mathrm{errorSpace}(C,F)errorSpace(C,F) is the span of {Ec:c∈C, E an arbitrary (not necessarily unitary) matrix supported on wires in F}\{Ec : c\in C,\ E \text{ an arbitrary (not necessarily unitary) matrix supported on wires in } F\}{Ec:c∈C, E an arbitrary (not necessarily unitary) matrix supported on wires in F}, exactly as in the previous entry.

In other words, it is errorSpace(C,F)⊗CR\mathrm{errorSpace}(C,F)\otimes \mathbb{C}^RerrorSpace(C,F)⊗CR.

Edge case. If RRR is empty, the ambient space and this subspace are both {0}\{0\}{0}.

QAlgorithms.filterSpace

Setting.

  • nnn is a natural number.
  • CCC is a complex subspace of C{0,1}n\mathbb{C}^{\{0,1\}^n}C{0,1}n.
  • rrr is a natural number.

Definition. filterSpace(C,r)\mathrm{filterSpace}(C,r)filterSpace(C,r) is the complex span of all vectors PcP cPc with c∈Cc\in Cc∈C, where PPP is the matrix of a Pauli string of weight at most rrr.

Pauli strings. A Pauli string consists of:

  • a phase c∈Z/4c\in\mathbb{Z}/4c∈Z/4, and
  • single-qubit operators σ0,…,σn−1∈{I,X,Y,Z}\sigma_0,\dots,\sigma_{n-1}\in\{I,X,Y,Z\}σ0​,…,σn−1​∈{I,X,Y,Z}.

Its matrix has entries

Py,z=ic∏q(σq)yq,zq,P_{y,z} = i^{c}\prod_{q} (\sigma_q)_{y_q,z_q},Py,z​=icq∏​(σq​)yq​,zq​​,

i.e. P=ic σ0⊗⋯⊗σn−1P = i^c\,\sigma_0\otimes\cdots\otimes\sigma_{n-1}P=icσ0​⊗⋯⊗σn−1​, using the usual matrices:

X=(0110),Y=(0−ii0),Z=diag⁡(1,−1),X=\begin{pmatrix}0&1\\1&0\end{pmatrix},\qquad Y=\begin{pmatrix}0&-i\\ i&0\end{pmatrix},\qquad Z=\operatorname{diag}(1,-1),X=(01​10​),Y=(0i​−i0​),Z=diag(1,−1),

with index 000 first. The weight of the string is the number of qqq with σq≠I\sigma_q\neq Iσq​=I.

Edge cases.

  • The phase does not affect the span.
  • The weight-000 string is III, so C⊆filterSpace(C,r)C\subseteq\mathrm{filterSpace}(C,r)C⊆filterSpace(C,r) for every rrr, including r=0r=0r=0, where the space equals CCC.
  • For r≥nr\ge nr≥n, all Pauli strings are allowed.

QAlgorithms.filterProj

Setting.

  • nnn is a natural number.
  • CCC is a complex subspace of C{0,1}n\mathbb{C}^{\{0,1\}^n}C{0,1}n.
  • rrr is a natural number.

Definition. filterProj(C,r)\mathrm{filterProj}(C,r)filterProj(C,r) is the 2n×2n2^n\times 2^n2n×2n complex matrix, in the standard basis, of the orthogonal projection (with respect to the standard Hermitian inner product) onto filterSpace(C,r)\mathrm{filterSpace}(C,r)filterSpace(C,r).

filterSpace(C,r)\mathrm{filterSpace}(C,r)filterSpace(C,r) is the span of {Pc:c∈C, P\{P c : c\in C,\ P{Pc:c∈C, P the matrix ic′σ0⊗⋯⊗σn−1i^{c'}\sigma_0\otimes\cdots\otimes\sigma_{n-1}ic′σ0​⊗⋯⊗σn−1​ of a Pauli string with at most rrr non-identity factors}\}}, as in the previous entry.

Edge cases.

  • If C={0}C=\{0\}C={0}, the matrix is 000.
  • Since C⊆filterSpace(C,r)C\subseteq \mathrm{filterSpace}(C,r)C⊆filterSpace(C,r), the projection fixes every vector of CCC.
Definition code
import Definitions.Def_QAlgorithms_Defs_Channel
/-!
# Pauli group, weights, quantum codes, stabilizers, CSS codes, r-filters
(shared definition layer)

D. Gottesman, *An introduction to quantum error correction and fault-tolerant quantum
computation* (arXiv:0904.2557), cited "Gottesman intro p.N" (PDF pages), §§2–4.

Conventions. The `n`-qubit Pauli operators are the frozen `Stabilizer.PauliString n` (overall
phase `i^c`, factors `I, X, Y, Z` with `Y = [[0, −i], [i, 0]] = iXZ`, intro Table 1; factor `q`
acts on wire `q`). The Pauli group `P_n` is a subgroup of Mathlib's group
`Matrix.unitaryGroup (Qubits n) ℂ`. A quantum code is a `Submodule` of
`EuclideanSpace ℂ (Qubits n)` (intro p.3: "the coding space"); codewords in conditions on
states are normalized (`‖ψ‖ = 1`). Binary vectors are `Fin n → ZMod 2`.
-/

namespace QAlgorithms

open scoped ComplexConjugate Matrix

open Stabilizer

/-- Gottesman intro p.3 (Table 1): the four single-qubit Pauli labels `I, X, Y, Z` form a finite
type (needed to index the Pauli operators of weight at most `t` by `Fin n → Pauli`). -/
instance Stabilizer.instFintypePauli : Fintype Stabilizer.Pauli where
  elems := {.I, .X, .Y, .Z}
  complete := fun p => by cases p <;> simp

/-- Gottesman intro p.4 (§2.1): the weight of the tensor product with factors `o`, "the number of
tensor factors which are not `I`". -/
def opsWeight {n : ℕ} (o : Fin n → Pauli) : ℕ :=
  (Finset.univ.filter fun q => o q ≠ Pauli.I).card

/-- Gottesman intro p.4 (§2.1): the weight `wt(Q)` of a Pauli operator `Q ∈ P_n` (independent of
its overall phase). -/
def Stabilizer.PauliString.weight {n : ℕ} (P : PauliString n) : ℕ :=
  opsWeight P.ops

/-- Gottesman intro p.3 (Table 1): the phase-`+1` Pauli matrix `P_1 ⊗ ⋯ ⊗ P_n` with factors `o`
(factor `q` on wire `q`). -/
noncomputable def pauliOps {n : ℕ} (o : Fin n → Pauli) : Matrix (Qubits n) (Qubits n) ℂ :=
  (⟨0, o⟩ : PauliString n).toMatrix

/-- Gottesman intro pp.4–5 (§2.1) and p.19 (Def. 1): the linear span of the Pauli errors of
weight at most `t` ("the n-qubit Pauli group spans the space of `2^n × 2^n` matrices"; the span is
the space of all operators acting on at most `t` qubits). -/
noncomputable def pauliSpan (n t : ℕ) : Submodule ℂ (Matrix (Qubits n) (Qubits n) ℂ) :=
  Submodule.span ℂ {M | ∃ P : PauliString n, P.weight ≤ t ∧ M = P.toMatrix}

/-- Gottesman intro p.3 (§2): the `n`-qubit Pauli group `P_n`, generated by the tensor products of
`I, X, Y, Z` with overall phases `±1, ±i`, as a subgroup of the unitary group. (The set of the
`4^{n+1}` matrices `i^c P_1 ⊗ ⋯ ⊗ P_n` is already a group; taking the closure keeps the definition
free of proof obligations.) -/
noncomputable def pauliGroup (n : ℕ) : Subgroup (Matrix.unitaryGroup (Qubits n) ℂ) :=
  Subgroup.closure {U | ∃ P : PauliString n, (U : Matrix (Qubits n) (Qubits n) ℂ) = P.toMatrix}

/-- Gottesman intro p.9 (§2.3) and p.12: `S` is a stabilizer: an Abelian subgroup of `P_n` not
containing `−1` ("S must be Abelian and cannot contain −1, or the code is trivial"). -/
def IsStabilizerGroup {n : ℕ} (S : Subgroup (Matrix.unitaryGroup (Qubits n) ℂ)) : Prop :=
  S ≤ pauliGroup n ∧ (∀ a ∈ S, ∀ b ∈ S, a * b = b * a) ∧
    ∀ M ∈ S, (M : Matrix (Qubits n) (Qubits n) ℂ) ≠ -1

/-- Gottesman intro p.9 (§2.3): the code `T(S)`, "the set of states `|ψ⟩` for which `M|ψ⟩ = |ψ⟩`
for all `M ∈ S`". -/
noncomputable def stabilizedSpace {n : ℕ} (S : Subgroup (Matrix.unitaryGroup (Qubits n) ℂ)) :
    Submodule ℂ (EuclideanSpace ℂ (Qubits n)) :=
  ⨅ M ∈ S, LinearMap.ker
    (Matrix.toEuclideanLin ((M : Matrix (Qubits n) (Qubits n) ℂ) - 1))

/-- Gottesman intro p.10 (§2.3): `N(S)`, the normalizer of `S` in the Pauli group, "which is in
this case equal to the centralizer, composed of Pauli operators which commute with `S`". -/
noncomputable def pauliCentralizer {n : ℕ} (S : Subgroup (Matrix.unitaryGroup (Qubits n) ℂ)) :
    Subgroup (Matrix.unitaryGroup (Qubits n) ℂ) :=
  pauliGroup n ⊓ Subgroup.centralizer (S : Set (Matrix.unitaryGroup (Qubits n) ℂ))

/-- Gottesman intro p.3 ("the phase of the operators used is generally not very important") and
p.10 (Thm 5): the elements of `S` up to an overall phase `±1, ±i`. Thm 5's `N(S) \ S` is read as
`N(S)` minus this set: taken literally, `−I ∈ N(S) \ S` has weight `0` and would force `d = 0`. -/
def phaseMultiples {n : ℕ} (S : Subgroup (Matrix.unitaryGroup (Qubits n) ℂ)) :
    Set (Matrix.unitaryGroup (Qubits n) ℂ) :=
  {U | ∃ c : ZMod 4, ∃ M ∈ S,
    (U : Matrix (Qubits n) (Qubits n) ℂ) = Complex.I ^ c.val • (M : Matrix (Qubits n) (Qubits n) ℂ)}

/-- Gottesman intro p.10 (§2.3) and p.12 (proof of Lemma 1): the error syndrome of `E` with
respect to the generators `g_0, …, g_{a−1}`: bit `i` is `0` if the `i`th generator commutes with
`E` and `1` otherwise (for Pauli operators: if it anticommutes). -/
noncomputable def pauliSyndrome {n a : ℕ} (g : Fin a → Matrix.unitaryGroup (Qubits n) ℂ)
    (E : Matrix.unitaryGroup (Qubits n) ℂ) : Fin a → ZMod 2 :=
  open Classical in
  fun i => if g i * E = E * g i then 0 else 1

/-- Gottesman intro p.5 (§2.1: "all t-qubit errors really does apply to all t-qubit errors, not
just unitary ones") and p.28 (Claim 1: errors only on the qubits of a set): the operator `E` acts
only on the qubits in `F`: it is `M ⊗ I` for some operator `M` (not necessarily unitary) on qubits
inside `F` (frozen `embedOp`: the identity on the other qubits). -/
def ActsWithin {n : ℕ} (F : Finset (Fin n)) (E : Matrix (Qubits n) (Qubits n) ℂ) : Prop :=
  ∃ (k : ℕ) (w : Fin k ↪ Fin n) (M : Matrix (Qubits k) (Qubits k) ℂ),
    Set.range w ⊆ (F : Set (Fin n)) ∧ E = embedOp M w

/-- Gottesman intro p.5 (Thm 2) and p.7 (§2.2): `E` is a `t`-qubit error, an operator acting on
at most `t` qubits. -/
def ActsOnAtMost {n : ℕ} (t : ℕ) (E : Matrix (Qubits n) (Qubits n) ℂ) : Prop :=
  ∃ F : Finset (Fin n), F.card ≤ t ∧ ActsWithin F E

/-- Gottesman intro p.4 (§2.1: "determine which error occurred and invert it") and p.6 (§2.2):
the code `C` corrects the errors in `S`: one recovery operation `R` (a quantum operation) maps
`E|ψ⟩⟨ψ|E†` back to `|ψ⟩⟨ψ|` for every error `E ∈ S` and every normalized codeword `ψ`, up to the
weight `‖Eψ‖²` forced by trace preservation (an `E` annihilating `ψ` never arises). -/
def Corrects {ι : Type*} [Fintype ι] [DecidableEq ι] (C : Submodule ℂ (EuclideanSpace ℂ ι))
    (S : Set (Matrix ι ι ℂ)) : Prop :=
  ∃ R : Superop ι ι, IsChannel R ∧ ∀ E ∈ S, ∀ ψ ∈ C, ‖ψ‖ = 1 →
    R (pureDensity (act E ψ)) = ((‖act E ψ‖ ^ 2 : ℝ) : ℂ) • pureDensity ψ

/-- Gottesman intro p.7 (§2.2, eq. (34)): the operator `E` satisfies `⟨ψ|E|ψ⟩ = C′(E)` on the code:
the value is the same for all normalized codewords (`E` is detectable). -/
def IsDetectable {ι : Type*} [Fintype ι] (C : Submodule ℂ (EuclideanSpace ℂ ι))
    (E : Matrix ι ι ℂ) : Prop :=
  ∃ c : ℂ, ∀ ψ ∈ C, ‖ψ‖ = 1 → inner ℂ ψ (act E ψ) = c

/-- Gottesman intro p.7 (§2.2): `C` is an `[[n, k, d]]` code: a subspace of `n` qubits of dimension
`2^k` (it encodes `k` qubits) of distance at least `d`, the distance being "the minimum weight of an
operator `E` that violates equation (34)": every operator on at most `d − 1` qubits is detectable.
(At `d = 0` the condition concerns `0`-qubit operators, i.e. scalars, which are always
detectable.) -/
def IsQCode (n k d : ℕ) (C : Submodule ℂ (EuclideanSpace ℂ (Qubits n))) : Prop :=
  Module.finrank ℂ C = 2 ^ k ∧ ∀ E : Matrix (Qubits n) (Qubits n) ℂ, ActsOnAtMost (d - 1) E →
    IsDetectable C E

/-- Gottesman intro p.7 (§2.2) and p.10 (Thm 5): the code `C` has distance exactly `d`: every
operator on at most `d − 1` qubits is detectable, and some operator on `d` qubits is not. -/
def HasDistance {n : ℕ} (C : Submodule ℂ (EuclideanSpace ℂ (Qubits n))) (d : ℕ) : Prop :=
  (∀ E : Matrix (Qubits n) (Qubits n) ℂ, ActsOnAtMost (d - 1) E → IsDetectable C E) ∧
    ∃ E : Matrix (Qubits n) (Qubits n) ℂ, ActsOnAtMost d E ∧ ¬ IsDetectable C E

/-- Gottesman intro p.7 (§2.2, eq. (30)): the code is nondegenerate for `t` errors: "the matrix
`C_ab` in (30) has maximum rank", where `C_ab = ⟨ψ|E_a†E_b|ψ⟩` with `E_a, E_b` ranging over the basis
of the `t`-qubit errors formed by the tensor products of `I, X, Y, Z` with at most `t` non-identity
factors (phases dropped); full rank is `det ≠ 0`, required at every normalized codeword. -/
def IsNondegenerate {n : ℕ} (C : Submodule ℂ (EuclideanSpace ℂ (Qubits n))) (t : ℕ) : Prop :=
  ∀ ψ ∈ C, ‖ψ‖ = 1 →
    (Matrix.of fun a b : {o : Fin n → Pauli // opsWeight o ≤ t} =>
      inner ℂ (act (pauliOps a.1) ψ) (act (pauliOps b.1) ψ)).det ≠ 0

/-- Gottesman intro p.11 (§2.4, CSS codes): the dual `C⊥` of a binary linear code `C`, "those words
which are orthogonal to the codewords of `C`" under the dot product over `F₂`. -/
def binDual {n : ℕ} (C : Submodule (ZMod 2) (Fin n → ZMod 2)) :
    Submodule (ZMod 2) (Fin n → ZMod 2) where
  carrier := {x | ∀ c ∈ C, c ⬝ᵥ x = 0}
  add_mem' := by
    intro x y hx hy c hc
    rw [dotProduct_add, hx c hc, hy c hc, add_zero]
  zero_mem' := by
    intro c _
    exact dotProduct_zero c
  smul_mem' := by
    intro a x hx c hc
    rw [dotProduct_smul, hx c hc, smul_zero]

/-- Gottesman intro p.11 (§2.4): the computational-basis label `|v⟩` of a binary vector
`v ∈ F₂ⁿ` (bit `q` is `1` iff `v_q = 1`). -/
def bitsOfZMod {n : ℕ} (v : Fin n → ZMod 2) : Qubits n :=
  fun q => decide (v q = 1)

/-- Gottesman intro p.11 (§2.4, eq. (51)): the CSS codeword `Σ_{w ∈ C2⊥} |u + w⟩`, unnormalized
("I omit normalization, which is generally unimportant", p.2). -/
noncomputable def cssCosetState {n : ℕ} (C2 : Submodule (ZMod 2) (Fin n → ZMod 2))
    (u : Fin n → ZMod 2) : EuclideanSpace ℂ (Qubits n) :=
  open Classical in
  ∑ w : binDual C2, ket (bitsOfZMod (u + (w : Fin n → ZMod 2)))

/-- Gottesman intro p.11 (§2.4): the CSS code of the binary codes `C1`, `C2`: the span of the
codewords (51) for `u ∈ C1`. (The construction assumes `C2⊥ ⊆ C1`, a hypothesis of the
statements.) -/
noncomputable def cssCode {n : ℕ} (C1 C2 : Submodule (ZMod 2) (Fin n → ZMod 2)) :
    Submodule ℂ (EuclideanSpace ℂ (Qubits n)) :=
  Submodule.span ℂ (Set.range fun u : C1 => cssCosetState C2 (u : Fin n → ZMod 2))

/-- Gottesman intro p.27 (§4.4, Fig. 3, the phase ancilla of Steane EC): the encoded `|0⟩` of the
CSS code, `Σ_{w ∈ C2⊥} |w⟩` (unnormalized). -/
noncomputable def cssZeroState {n : ℕ} (C2 : Submodule (ZMod 2) (Fin n → ZMod 2)) :
    EuclideanSpace ℂ (Qubits n) :=
  cssCosetState C2 0

/-- Gottesman intro p.27 (§4.4, Fig. 3, the bit-flip ancilla of Steane EC): the encoded
`|0⟩ + |1⟩` of the CSS code, the sum `Σ_{u ∈ C1} |u⟩` of all its coset states (unnormalized). -/
noncomputable def cssPlusState {n : ℕ} (C1 : Submodule (ZMod 2) (Fin n → ZMod 2)) :
    EuclideanSpace ℂ (Qubits n) :=
  open Classical in
  ∑ u : C1, ket (bitsOfZMod (u : Fin n → ZMod 2))

/-- Gottesman intro p.28 (§4.4, Claim 1: "errors only on qubits from `F`"): the span of the states
`E|c⟩` with `c` a codeword and `E` any operator acting only on the qubits of `F`. -/
noncomputable def errorSpace {n : ℕ} (C : Submodule ℂ (EuclideanSpace ℂ (Qubits n)))
    (F : Finset (Fin n)) : Submodule ℂ (EuclideanSpace ℂ (Qubits n)) :=
  Submodule.span ℂ {v | ∃ (E : Matrix (Qubits n) (Qubits n) ℂ) (c : EuclideanSpace ℂ (Qubits n)),
    ActsWithin F E ∧ c ∈ C ∧ v = act E c}

/-- Gottesman intro p.28 (§4.4): `errorSpace C F` tensored with a reference register `R` (the data
block may be entangled with the rest of the computer); the block is the first factor. -/
noncomputable def errorSpaceWithRef {n : ℕ} (R : Type*) [Fintype R]
    (C : Submodule ℂ (EuclideanSpace ℂ (Qubits n))) (F : Finset (Fin n)) :
    Submodule ℂ (EuclideanSpace ℂ (Qubits n × R)) :=
  Submodule.span ℂ {x | ∃ v ∈ errorSpace C F, ∃ r : EuclideanSpace ℂ R, x = tensorVec v r}

/-- Gottesman intro p.19 (Def. 1): the subspace "spanned by all states of the form `Q|ψ⟩`, where
`|ψ⟩` is an arbitrary codeword and `Q` is a Pauli error of weight at most `r`". -/
noncomputable def filterSpace {n : ℕ} (C : Submodule ℂ (EuclideanSpace ℂ (Qubits n))) (r : ℕ) :
    Submodule ℂ (EuclideanSpace ℂ (Qubits n)) :=
  Submodule.span ℂ {v | ∃ (P : PauliString n) (c : EuclideanSpace ℂ (Qubits n)),
    P.weight ≤ r ∧ c ∈ C ∧ v = act P.toMatrix c}

/-- Gottesman intro p.19 (Def. 1): the `r`-filter, the (orthogonal) projector onto
`filterSpace C r`, as a matrix; it acts on a block state `ρ` as `conjSuperop (filterProj C r)`. -/
noncomputable def filterProj {n : ℕ} (C : Submodule ℂ (EuclideanSpace ℂ (Qubits n))) (r : ℕ) :
    Matrix (Qubits n) (Qubits n) ℂ :=
  Matrix.toEuclideanLin.symm
    ((filterSpace C r).starProjection : EuclideanSpace ℂ (Qubits n) →ₗ[ℂ] EuclideanSpace ℂ (Qubits n))

/-! ### Sanity tests -/

example : Fintype.card Stabilizer.Pauli = 4 := rfl

example : opsWeight (![Pauli.X, Pauli.Y, Pauli.I] : Fin 3 → Pauli) = 2 := by decide

example {n : ℕ} (c : ZMod 4) : (⟨c, fun _ => Pauli.I⟩ : PauliString n).weight = 0 := by
  simp [Stabilizer.PauliString.weight, opsWeight]

example : bitsOfZMod (![1, 0] : Fin 2 → ZMod 2) = ![true, false] := by decide

example {n : ℕ} (x : Fin n → ZMod 2) : x ∈ binDual (⊥ : Submodule (ZMod 2) (Fin n → ZMod 2)) := by
  intro c hc
  rw [(Submodule.mem_bot _).1 hc, zero_dotProduct]

example {n : ℕ} (C : Submodule ℂ (EuclideanSpace ℂ (Qubits n))) (c : ℂ) :
    IsDetectable C (c • (1 : Matrix (Qubits n) (Qubits n) ℂ)) := by
  refine ⟨c, fun ψ _ hψ => ?_⟩
  have h : act (c • (1 : Matrix (Qubits n) (Qubits n) ℂ)) ψ = c • ψ := by
    ext z; simp [act, Matrix.smul_mulVec]
  rw [h, inner_smul_right, inner_self_eq_norm_sq_to_K, hψ]
  simp

example {n : ℕ} (E : Matrix (Qubits n) (Qubits n) ℂ) (F : Finset (Fin n)) (hE : ActsWithin F E) :
    ActsOnAtMost F.card E :=
  ⟨F, le_rfl, hE⟩

theorem embedOp_one_mem_actsWithin {n : ℕ} (F : Finset (Fin n)) :
    ActsWithin F (1 : Matrix (Qubits n) (Qubits n) ℂ) :=
  ⟨0, ⟨Fin.elim0, fun a => Fin.elim0 a⟩, 1, by
    rintro _ ⟨a, _⟩; exact Fin.elim0 a, (embedOp_one _).symm⟩

end QAlgorithms
Source
https://github.com/shiy1022/lean-quantum-complexity/blob/main/algorithms/QAlgorithms/Defs/QECC.lean . Exact primary-source page locators are preserved in the declaration docstrings.

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 paper
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me