Simple Algorithm
Quantum Computing · Excited State · 26 min

Stabilizer Codes & the Surface Code

The leading blueprint for fault-tolerant quantum computers.

Writing down the codewords of a code on hundreds of qubits is hopeless. Writing down the handful of Pauli operators that leave those codewords unchanged is easy. That change of viewpoint, the stabilizer formalism, turns error correction into bookkeeping and leads straight to the surface code, today's leading blueprint for a fault-tolerant quantum computer.

What you’ll learn

  • Define a stabilizer group and its codespace, and prove that rr independent generators on nn qubits leave k=n−rk=n-r logical qubits.
  • Compute syndromes from commutation relations, and identify logical operators as Paulis that commute with every stabilizer without being one.
  • Read [[n,k,d]][[n,k,d]] parameters and relate the distance dd to the number of correctable errors.
  • Describe the rotated surface code: its checks, logical operators, syndrome-extraction circuits and qubit count.
  • Explain the threshold and the scaling pL∼(p/pth)(d+1)/2p_L\sim(p/p_{\mathrm{th}})^{(d+1)/2} of the logical error rate.

Describe a code by what fixes it

In the three-qubit bit-flip code the codewords are α∣000⟩+β∣111⟩\alpha\ket{000}+\beta\ket{111}. Here is a different description of the same space: it is the set of states that the two operators Z1Z2Z_1Z_2 and Z2Z3Z_2Z_3 both leave unchanged. Each operator compares the parity of two neighbors, and both ∣000⟩\ket{000} and ∣111⟩\ket{111} pass both tests with eigenvalue +1+1.

Two operators instead of a list of states may look like a small saving. For a code on 100 qubits it is the difference between 21002^{100} amplitudes and a few dozen Pauli strings. This is the stabilizer formalism, developed by Gottesman, and independently by Calderbank, Rains, Shor and Sloane, in 1996–97.

Think first Why must a stabilizer group be abelian, and why must it exclude −I-I?

Otherwise the codespace is empty. If −I∈S-I\in\mathcal{S}, a codeword obeys ∣ψ⟩=−∣ψ⟩\ket{\psi}=-\ket{\psi}, so ∣ψ⟩=0\ket{\psi}=0. If g,h∈Sg,h\in\mathcal{S} anticommute, then ∣ψ⟩=gh∣ψ⟩=−hg∣ψ⟩=−∣ψ⟩\ket{\psi}=gh\ket{\psi}=-hg\ket{\psi}=-\ket{\psi}: the same contradiction.

DerivationCounting logical qubits
  1. P=∏i=1rI+gi2P = \prod_{i=1}^{r}\frac{I+g_i}{2}

    Elements of S\mathcal{S} square to +I+I (since −I∉S-I\notin\mathcal{S}), so they are Hermitian with eigenvalues ±1\pm1, and (I+gi)/2(I+g_i)/2 projects onto the +1+1 eigenspace of gig_i. The factors commute, so PP projects onto C(S)\mathcal{C}(\mathcal{S}).

  2. P=12r∑g∈SgP = \frac{1}{2^r}\sum_{g\in\mathcal{S}} g

    Expanding the product gives one term per subset of generators, which is one term per group element because the generators are independent.

  3. Tr⁡g=0for every g∈S, g≠I\Tr g = 0\quad\text{for every } g\in\mathcal{S},\ g\neq I

    A tensor product of Paulis with at least one non-identity factor is traceless, and −I-I, ±iI\pm iI cannot lie in S\mathcal{S}.

  4. dim⁡C(S)=Tr⁡P=2n2r=2 n−r\dim\mathcal{C}(\mathcal{S}) = \Tr P = \frac{2^n}{2^r} = 2^{\,n-r}

    Only the identity term survives. So rr independent stabilizers on nn qubits leave k=n−rk=n-r logical qubits. Bit-flip code: n=3n=3, r=2r=2, k=1k=1.

Check your understanding

Which of these generating sets does not define a stabilizer code with a non-trivial codespace?

  1. ⟨X⊗X, Z⊗Z⟩\langle X\otimes X,\ Z\otimes Z\rangle
  2. ⟨X⊗Z, Z⊗X⟩\langle X\otimes Z,\ Z\otimes X\rangle
  3. ⟨Z1Z2, Z2Z3⟩\langle Z_1Z_2,\ Z_2Z_3\rangle
  4. ⟨X⊗X, Z⊗I⟩\langle X\otimes X,\ Z\otimes I\rangle
Show the answer

D. ⟨X⊗X, Z⊗I⟩\langle X\otimes X,\ Z\otimes I\rangle

X⊗XX\otimes X and Z⊗IZ\otimes I clash on one qubit (XX against ZZ), so they anticommute and no state is a +1+1 eigenvector of both. Two Paulis commute exactly when they anticommute on an even number of positions: X⊗ZX\otimes Z and Z⊗XZ\otimes X clash on both qubits and therefore commute.

Syndromes, logical operators and distance

Suppose a Pauli error EE hits a codeword ∣ψ⟩\ket{\psi}. For each generator, either giE=Egig_iE=Eg_i or giE=−Egig_iE=-Eg_i. In the second case

gi (E∣ψ⟩)=−E gi∣ψ⟩=− E∣ψ⟩,g_i\,(E\ket{\psi}) = -E\,g_i\ket{\psi} = -\,E\ket{\psi},

so measuring gig_i now gives −1-1. Collect these signs into the syndrome s(E)∈{0,1}rs(E)\in\{0,1\}^r:

gi E=(−1)si(E) E gi,i=1,…,rg_i\,E = (-1)^{s_i(E)}\,E\,g_i,\qquad i=1,\dots,r
The syndrome depends only on the error, never on the encoded state. Every codeword gives the same outcomes, so measuring the generators reveals nothing about α\alpha and β\beta.

Every Pauli error falls into exactly one of three classes:

  • s(E)≠0s(E)\neq0: detected. A decoder can try to undo it.
  • E∈SE\in\mathcal{S} (up to a phase): harmless. It acts as the identity on every codeword.
  • s(E)=0s(E)=0 but E∉SE\notin\mathcal{S}: undetectable and harmful. EE maps the codespace to itself but changes the encoded state: it is a logical operator.
Code[[n,k,d]][[n,k,d]]GeneratorsNote
Bit-flip[[3,1,1]][[3,1,1]]Z1Z2Z_1Z_2, Z2Z3Z_2Z_3Corrects one XX error, but Z1Z_1 alone is a logical operator, so d=1d=1.
Four-qubit code[[4,2,2]][[4,2,2]]XXXXXXXX, ZZZZZZZZDetects (but cannot correct) any single-qubit error.
Five-qubit code[[5,1,3]][[5,1,3]]XZZXIXZZXI and its cyclic shiftsSmallest code that corrects an arbitrary single-qubit error.
Steane[[7,1,3]][[7,1,3]]3 XX-type and 3 ZZ-type, from the Hamming codeEvery Clifford gate is transversal.
Shor[[9,1,3]][[9,1,3]]6 weight-2 ZZ-type, 2 weight-6 XX-typeThe first quantum code (1995).
Rotated surface code[[d2,1,d]][[d^2,1,d]]local weight-4 and weight-2 checksOnly nearest-neighbor interactions on a 2D grid.
Think first The five-qubit code has generators XZZXIXZZXI, IXZZXIXZZX, XIXZZXIXZZ, ZXIXZZXIXZ. What syndrome does a ZZ error on qubit 1 produce? (Order the bits like the generators.)

ZZ anticommutes with XX and YY and commutes with II and ZZ. On qubit 1 the generators read X,I,X,ZX, I, X, Z, so the syndrome is 10101010. The 15 single-qubit errors give 15 different non-zero syndromes, and 15=24−115=2^4-1 uses all of them: the code is perfect.

Measuring a stabilizer with an ancilla

To read a syndrome bit you must measure the product gig_i without measuring the individual qubits, which would collapse the encoded superposition. The trick is to copy the parity onto a fresh ancilla qubit and measure only that.

DerivationParity onto an ancilla
  1. (∑xax∣x⟩)⊗∣0⟩a\Big(\sum_x a_x\ket{x}\Big)\otimes\ket{0}_a

    Data qubits in some state, written in the computational basis; ancilla in ∣0⟩\ket{0}. We want to measure Z1Z2Z3Z4Z_1Z_2Z_3Z_4.

  2. ∑xax∣x⟩⊗∣x1⊕x2⊕x3⊕x4⟩a\sum_x a_x\ket{x}\otimes\ket{x_1\oplus x_2\oplus x_3\oplus x_4}_a

    Apply four CNOTs, each controlled by a data qubit and targeting the ancilla. The ancilla accumulates the parity of the four bits.

  3. (∑x: parity(x)=πax∣x⟩)⊗∣π⟩a\Big(\sum_{x:\ \mathrm{parity}(x)=\pi} a_x\ket{x}\Big)\otimes\ket{\pi}_a

    If the data is an eigenstate of Z1Z2Z3Z4Z_1Z_2Z_3Z_4, every xx in the superposition has the same parity π\pi, and the ancilla factors out. Measuring it returns π\pi with certainty and leaves the data untouched.

  4. If the data mixes both parities, measuring the ancilla projects it onto one parity sector: exactly the projective measurement of Z1Z2Z3Z4Z_1Z_2Z_3Z_4, with projectors 12(I±Z1Z2Z3Z4)\tfrac12(I\pm Z_1Z_2Z_3Z_4).

Try it. Wires q0–q3 hold one plaquette's worth of data in the state 12(∣0000⟩+∣1111⟩)\tfrac{1}{\sqrt2}(\ket{0000}+\ket{1111}), a +1+1 eigenstate of both Z⊗4Z^{\otimes4} and X⊗4X^{\otimes4}, and an XX error has already hit q2. Wire q4 is the ancilla.

An XX-type check such as X1X2X3X4X_1X_2X_3X_4 uses the same idea in the Hadamard basis: prepare the ancilla in ∣+⟩\ket{+}, let it control four CNOTs onto the data, and measure it in the XX basis. By phase kickback the ancilla ends in ∣±⟩\ket{\pm} according to the eigenvalue ±1\pm1.

qasm
OPENQASM 3.0;
include "stdgates.inc";
qubit[4] d;   // data qubits of one plaquette
qubit a;      // ancilla
bit s;
reset a;
h a;                                            // ancilla to |+>
cx a, d[0]; cx a, d[1]; cx a, d[2]; cx a, d[3]; // controlled X on all four
h a;
s = measure a;                                  // 0: eigenvalue +1, 1: eigenvalue -1

Measuring an XX-type check. The controlled-X⊗4X^{\otimes4} kicks the eigenvalue back onto the ancilla.

The surface code

Place d×dd\times d data qubits on a square grid. Each square face between four qubits carries one check, alternating in a checkerboard between XX-type (X⊗4X^{\otimes4}) and ZZ-type (Z⊗4Z^{\otimes4}). Along the edges, every other boundary face carries a weight-2 check. Every check involves only neighboring qubits, which is why the surface code suits a 2D chip: each check is measured by one ancilla sitting in the middle of its face.

For d=3d=3 there are 9 data qubits and 4 XX-type plus 4 ZZ-type checks. Any XX-type and ZZ-type check overlap on 0 or 2 qubits, so all 8 commute, and they are independent: k=9−8=1k=9-8=1.

What you just saw is the key geometric fact. A chain of errors lights checks only at its endpoints: in the middle, every check touches the chain twice. An XX-chain can end silently on the top or bottom edge, where no blue check lies beyond it. A chain stretching from top to bottom therefore commutes with every check without being a stabilizer: it is a logical operator. The shortest has dd qubits, which is why the code has distance dd.

Xˉ=∏one columnX,Zˉ=∏one rowZ,XˉZˉ=−ZˉXˉ\bar X = \prod_{\text{one column}} X, \qquad \bar Z = \prod_{\text{one row}} Z, \qquad \bar X\bar Z = -\bar Z\bar X
Any column (or row) works; different choices differ by stabilizers. A column and a row cross on exactly one qubit, so Xˉ\bar X and Zˉ\bar Z anticommute, as logical Paulis must.
Predict before you look

A distance-dd rotated surface code uses d2d^2 data qubits plus one measurement ancilla per check. How many physical qubits does a d=5d=5 patch need in total?

Show the answer

49 qubits

2525 data qubits and d2−1=24d^2-1=24 checks: 25+24=4925+24=49. In general 2d2−12d^2-1 physical qubits: 17 for d=3d=3, 97 for d=7d=7. Each logical qubit costs O(d2)\order{d^2} physical ones.

Decoding, the threshold and scaling

Measuring all checks yields a syndrome: a set of lit faces. A decoder must guess a correction CC with the same syndrome. Success means CE∈SCE\in\mathcal{S}; failure means CECE is a logical operator. For independent errors of small probability pp, the most likely explanation is the shortest set of chains that pairs up the lit checks (or links them to a suitable boundary). Finding it is a classic graph problem, minimum-weight perfect matching, solvable in polynomial time.

Real checks are measured by noisy circuits, so a syndrome bit can itself be wrong. The remedy is to repeat the measurement round about dd times and decode in space and time. When every gate, preparation and measurement fails with probability pp (circuit-level noise), the surface code has a threshold of roughly pth≈1%p_{\mathrm{th}}\approx1\% (about 0.5%0.5\% to 1%1\%, depending on the noise model and decoder). Below it, the logical error per round falls as

pL≈A(ppth)(d+1)/2p_L \approx A\left(\frac{p}{p_{\mathrm{th}}}\right)^{(d+1)/2}
The decoder fails only when roughly half a logical chain, ⌈d/2⌉=(d+1)/2\lceil d/2\rceil=(d+1)/2 faults for odd dd, conspires to fool it. AA is a model-dependent constant, of order 0.010.01–0.10.1 in simulations.

Increasing dd by 2 divides pLp_L by the error-suppression factor Λ≈pth/p\Lambda\approx p_{\mathrm{th}}/p. At p=pth/10p=p_{\mathrm{th}}/10, each step d→d+2d\to d+2 buys a factor of 10, and reaching pL∼10−12p_L\sim10^{-12} takes d≈21d\approx21: roughly 900 physical qubits per logical qubit. Above threshold (Λ<1\Lambda<1), bigger codes are worse.

Check your understanding

A lab measures logical error rates of 3.0×10−33.0\times10^{-3} per round at d=3d=3 and 1.5×10−31.5\times10^{-3} at d=5d=5. If the scaling law holds, what should it expect at d=7d=7?

  1. 1.5×10−31.5\times10^{-3}: the code has saturated
  2. 7.5×10−47.5\times10^{-4}
  3. 3.75×10−43.75\times10^{-4}
  4. 3.0×10−33.0\times10^{-3}: larger codes add more qubits that can fail
Show the answer

B. 7.5×10−47.5\times10^{-4}

Λ=3.0/1.5=2\Lambda=3.0/1.5=2, and each step d→d+2d\to d+2 divides pLp_L by Λ\Lambda: 1.5×10−3/2=7.5×10−41.5\times10^{-3}/2=7.5\times10^{-4}. Λ>1\Lambda>1 means the device is below threshold, so every extra ring of qubits helps.

Key takeaways

  • A stabilizer code is the joint +1+1 eigenspace of an abelian Pauli group without −I-I; rr independent generators on nn qubits leave k=n−rk=n-r logical qubits.
  • The syndrome records which generators anticommute with the error. Logical operators commute with all generators without being stabilizers; the lightest one sets the distance dd.
  • Checks are measured by coupling an ancilla with CNOTs (Hadamard-sandwiched for XX-type checks) and measuring only the ancilla.
  • The rotated surface code [[d2,1,d]][[d^2,1,d]] uses 2d2−12d^2-1 qubits with local checks; its logical operators are chains across the patch.
  • Below a circuit-level threshold near 1%1\%, pL≈A(p/pth)(d+1)/2p_L\approx A(p/p_{\mathrm{th}})^{(d+1)/2}: each step d→d+2d\to d+2 divides the logical error by Λ≈pth/p\Lambda\approx p_{\mathrm{th}}/p, as experiments now confirm.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

A stabilizer code on n=17n=17 qubits has 1515 independent generators. What are the number of logical qubits kk and the codespace dimension?

  1. k=15k=15, dimension 2152^{15}
  2. k=2k=2, dimension 22
  3. k=2k=2, dimension 44
  4. k=8k=8, dimension 282^8
Show the answer

C. k=2k=2, dimension 44

dim⁡C=2n−r=217−15=4\dim\mathcal{C}=2^{n-r}=2^{17-15}=4, the state space of k=2k=2 qubits. Each independent generator halves the space.

Question 2

A Pauli operator EE commutes with every stabilizer generator. Which statement is correct?

  1. EE is either a stabilizer (harmless) or a logical operator (undetectable and harmful)
  2. EE is always harmless, because it produces a trivial syndrome
  3. EE is always a logical error
  4. EE is detected by the syndrome measurement
Show the answer

A. EE is either a stabilizer (harmless) or a logical operator (undetectable and harmful)

A zero syndrome means E∈N(S)E\in\mathcal{N}(\mathcal{S}). If E∈SE\in\mathcal{S} it acts trivially on codewords; if E∈N(S)∖SE\in\mathcal{N}(\mathcal{S})\setminus\mathcal{S} it silently changes the encoded state. The distance is the minimum weight of the latter kind.

Question 3

On the d=3d=3 surface code, an XX flip on the top-middle qubit and one on the top-right qubit light the same single blue face, so the decoder cannot tell them apart. Why is that harmless?

  1. The decoder always picks the qubit that actually flipped
  2. XX errors cannot damage a surface code; only ZZ errors can
  3. The two qubits are entangled, so they are effectively one qubit
  4. Their product, X⊗XX\otimes X on those two qubits, is a stabilizer (the red half-moon), so correcting the "wrong" one leaves a harmless stabilizer
Show the answer

D. Their product, X⊗XX\otimes X on those two qubits, is a stabilizer (the red half-moon), so correcting the "wrong" one leaves a harmless stabilizer

If the error was XX on one qubit and the decoder applies XX on the other, the net effect is X⊗XX\otimes X on the pair: exactly the weight-2 XX check on the top edge, which acts trivially on the code. Quantum codes are often degenerate: different errors with the same syndrome need not be distinguished.

Question 4

A surface code runs at half the threshold (p=pth/2p=p_{\mathrm{th}}/2). Going from d=5d=5 to d=9d=9 changes the logical error rate by roughly what factor?

  1. It halves
  2. It drops by about 4×4\times
  3. It drops by about 16×16\times
  4. It increases, because more qubits can fail
Show the answer

B. It drops by about 4×4\times

Λ≈pth/p=2\Lambda\approx p_{\mathrm{th}}/p=2 per step of +2+2 in distance, and 5→95\to9 is two steps: 22=42^2=4. Equivalently, the exponent (d+1)/2(d+1)/2 goes from 3 to 5, and (1/2)5−3=1/4(1/2)^{5-3}=1/4.

Question 5

Why are surface-code checks measured through an ancilla rather than by measuring the four data qubits directly?

  1. Ancilla measurements are faster
  2. Data qubits cannot be measured in the ZZ basis
  3. Measuring the data would reveal (and collapse) the encoded information; the ancilla records only the parity
  4. The ancilla corrects the error automatically
Show the answer

C. Measuring the data would reveal (and collapse) the encoded information; the ancilla records only the parity

Measuring the data qubits individually measures far more than the product Z⊗4Z^{\otimes4}: it collapses the logical superposition. Coupling to an ancilla and measuring only the ancilla implements the projective measurement of the product, leaving the code state intact.

Go deeper

Further reading

  • D. Gottesman, Stabilizer Codes and Quantum Error Correction, PhD thesis, Caltech (1997), arXiv:quant-ph/9705052.
  • M. A. Nielsen & I. L. Chuang, Quantum Computation and Quantum Information, §10.5 (Cambridge, 10th anniv. ed., 2010).
  • A. G. Fowler, M. Mariantoni, J. M. Martinis & A. N. Cleland, "Surface codes: Towards practical large-scale quantum computation", Phys. Rev. A 86, 032324 (2012).
  • Google Quantum AI and collaborators, "Quantum error correction below the surface code threshold", Nature (2025), arXiv:2408.13687.