Skip to content

Stabilizer codes

The Shor code took nine-qubit state vectors to write down and a paragraph of reasoning for each kind of error. That does not scale. This lesson introduces the stabilizer formalism, introduced by Daniel Gottesman in the 1990s, in which a code is a short list of operators and a syndrome is found by counting letters. Nearly every code in use today is described this way.

A Pauli string on nn qubits is a choice of II, XX, YY or ZZ for each qubit, such as Z⊗Z⊗IZ\otimes Z\otimes I. We drop the ⊗\otimes and write ZZIZZI. The number of letters that are not II is the string’s weight.

Two facts about Pauli strings carry the whole subject.

Every Pauli string squares to the identity, so its eigenvalues are +1+1 and −1-1. Measuring one gives one of those two answers.

Any two Pauli strings either commute or anticommute. For single letters you can check directly that XZ=−ZXXZ = -ZX, and the same holds for any two different letters among XX, YY, ZZ. A letter commutes with itself and with II. For strings, each position where the letters are different and neither is II contributes one factor of −1-1. So:

Count the positions where both strings have a non-identity letter and the letters differ. If the count is even the strings commute. If it is odd they anticommute.

For example, XXXX and ZZZZ clash in two positions, so they commute, even though XX and ZZ on their own do not. ZZIZZI and XIIXII clash in one position, so they anticommute.

Check

Do XZZXIXZZXI and IXZZXIXZZX commute?

An operator SS stabilizes a state ∣ψ⟩\ket{\psi} if it leaves it alone:

S∣ψ⟩=∣ψ⟩.S\ket{\psi} = \ket{\psi}.

In other words ∣ψ⟩\ket{\psi} is an eigenstate of SS with eigenvalue +1+1. Some examples:

  • ZZ stabilizes ∣0⟩\ket{0}, and −Z-Z stabilizes ∣1⟩\ket{1}.
  • XX stabilizes ∣+⟩\ket{+}, and −X-X stabilizes ∣−⟩\ket{-}.
  • XXXX and ZZZZ both stabilize the Bell state 12(∣00⟩+∣11⟩)\tfrac{1}{\sqrt2}(\ket{00} + \ket{11}). The first swaps its two terms. The second gives each term a sign of (+1)(+1)(+1)(+1) or (−1)(−1)(-1)(-1).

The Bell state is the only two-qubit state stabilized by both XXXX and ZZZZ. So instead of writing out its amplitudes, we can specify it by naming those two operators. That is the central idea: describe states by what leaves them unchanged.

PuzzleA state from its stabilizers

Starting from ∣00⟩\ket{00}, prepare the one state that is stabilized by both XXXX and −ZZ-ZZ. No target is shown: work out what the state must be.

This puzzle needs JavaScript.

Hint

−ZZ-ZZ stabilizes a state when ZZZZ gives −1-1 on it, which means the two bits always differ. XXXX swaps ∣01⟩\ket{01} and ∣10⟩\ket{10}, so both must appear with the same amplitude.

Each Pauli string, other than the identity, has half of all states in its +1+1 eigenspace and half in its −1-1 eigenspace. Requiring a state to be stabilized by one string therefore halves the available dimension. Requiring a second, independent string that commutes with the first halves it again.

On nn qubits the full space has dimension 2n2^n. With n−kn - k independent commuting strings, what remains has dimension 2k2^k: room for exactly kk qubits. Two strings on two qubits leave dimension 20=12^0 = 1, a single state, as you just found.

The strings must commute with one another, or nothing would be left at all. If SS and TT anticommute and both stabilize ∣ψ⟩\ket{\psi}, then ∣ψ⟩=ST∣ψ⟩=−TS∣ψ⟩=−∣ψ⟩\ket{\psi} = ST\ket{\psi} = -TS\ket{\psi} = -\ket{\psi}, which forces ∣ψ⟩=0\ket{\psi} = 0.

A stabilizer code is given by a list of commuting Pauli strings, called its generators. The code space is the set of all states stabilized by every one of them.

The bit-flip code is the smallest example. Its generators are

ZZIandIZZ.ZZI \qquad\text{and}\qquad IZZ.

ZZIZZI stabilizes the basis states whose first two bits agree. IZZIZZ stabilizes those whose last two agree. Both hold only for ∣000⟩\ket{000} and ∣111⟩\ket{111}, so the code space is every state α∣000⟩+β∣111⟩\alpha\ket{000} + \beta\ket{111}. And the counting works: three qubits, two generators, one encoded qubit.

These are exactly the two parities you measured in the first lesson. The generators of a code are its syndrome measurements.

Products of generators stabilize the code space too. Here ZZI⋅IZZ=ZIZZZI \cdot IZZ = ZIZ. The generators together with all their products form the stabilizer group. We only ever need to list, and measure, the generators.

Here is the calculation that makes the formalism useful. Let ∣ψ⟩\ket{\psi} be in the code space, let SS be a generator, and let an error EE, itself a Pauli string, strike. Now measure SS.

If EE commutes with SS:

S(E∣ψ⟩)=E S∣ψ⟩=E∣ψ⟩.S\bigl(E\ket{\psi}\bigr) = E\,S\ket{\psi} = E\ket{\psi}.

The damaged state is still a +1+1 eigenstate. The measurement gives +1+1 and the generator sees nothing.

If EE anticommutes with SS:

S(E∣ψ⟩)=−E S∣ψ⟩=−E∣ψ⟩.S\bigl(E\ket{\psi}\bigr) = -E\,S\ket{\psi} = -E\ket{\psi}.

The damaged state is a −1-1 eigenstate. The measurement gives −1-1. We say the generator fires.

So the syndrome of an error is simply the list of generators it anticommutes with. No amplitudes are involved. For the bit-flip code and the error IXIIXI, a flip of the middle qubit: it clashes with ZZIZZI once and with IZZIZZ once, so both fire. That is the syndrome 11 from the table in the first lesson, obtained by looking at letters.

Check

In the bit-flip code, with generators ZZIZZI and IZZIZZ, what is the syndrome of the error YIIYII?

You have already built circuits that measure ZZZZ and XXXX. The general recipe, for any Pauli string, is the kickback circuit from the last lesson:

  1. put an ancilla in ∣+⟩\ket{+} with an HH,
  2. for each letter of the string, apply that letter to its qubit controlled by the ancilla: a CNOT for XX, a CZ for ZZ,
  3. apply HH to the ancilla and measure it.

The ancilla reads 0 if the data is in the +1+1 eigenspace and 1 if it is in the −1-1 eigenspace.

PuzzleMeasure XZ

Measure the Pauli string XZXZ on the two data qubits, writing the result on the bottom wire. The data shown, ∣+⟩∣1⟩\ket{+}\ket{1}, has X=+1X = +1 and Z=−1Z = -1, so XZ=−1XZ = -1 and the bottom wire must end in ∣1⟩\ket{1}. Your circuit is also tested on five other eigenstates of XZXZ, two of them entangled.

This puzzle needs JavaScript.

Hint

HH on the bottom wire, then a CNOT from the bottom wire to the top one, a CZ between the bottom wire and the middle one, and HH on the bottom wire again.

The simulator holds three codes. Choose an error type, click qubits to apply it, and watch which generators fire. A red cell marks a position where the error clashes with a generator. Below the generators are the code’s logical operators, explained in the next section.

SimulatorErrors and syndromes

This simulator needs JavaScript.

Things to try:

  1. Bit-flip code. Put an XX on each qubit in turn and compare with the syndrome table from the first lesson. Then clear it and put a ZZ on any qubit. Nothing fires.
  2. Five-qubit code. Try all fifteen single-qubit errors, XX, YY and ZZ on each of five qubits. Every one gives a different syndrome. Four generators have 24=162^4 = 16 possible syndromes: one for “no error” and one for each of the fifteen. Nothing is wasted.
  3. Two errors. In the five-qubit code, put an XX on qubit 0 and another on qubit 1. The syndrome looks like that of a single error elsewhere, and the decoder’s guess is wrong.
  4. A generator as an error. In the five-qubit code, apply XX, ZZ, ZZ, XX to qubits 0 to 3. Nothing fires, and nothing needs to.

The five-qubit code is the smallest code that corrects an arbitrary error on one qubit. No code on four or fewer qubits can do it.

Experiment 1 found an error that no generator notices but which is not harmless. Experiment 4 found one that no generator notices and which is harmless. The difference is the last piece of the theory.

A Pauli string that commutes with every generator maps the code space to itself. There are two kinds:

  • Products of generators. These stabilize every encoded state, so they do nothing at all.
  • Everything else. These move encoded states around inside the code space. They are the logical operators: they act on the encoded qubit the way XX, YY and ZZ act on a bare one.

For the bit-flip code, XXXXXX swaps ∣000⟩\ket{000} and ∣111⟩\ket{111}, so it is a logical XX. And ZIIZII puts a minus sign on ∣111⟩\ket{111}, so it is a logical ZZ. A logical operator is not unique: multiplying by a generator gives another string with the same action, so IZIIZI, IIZIIZ and ZZZZZZ are logical ZZ as well.

Logical operators are exactly what we want when computing on encoded data, and exactly what we fear from noise. An error that is a logical operator cannot be detected, because it fires nothing, and it changes the data.

The distance dd of a code is the smallest weight of any logical operator: the smallest number of qubits that noise must hit to change the data unnoticed. A code of distance dd

  • detects any error on up to d−1d - 1 qubits,
  • corrects any error on up to ⌊(d−1)/2⌋\lfloor (d-1)/2 \rfloor qubits.

A code is summarised by three numbers, written [[n,k,d]][[n, k, d]]: nn physical qubits, kk logical qubits, distance dd.

Code Generators [[n,k,d]][[n, k, d]]
Bit-flip code 2 [[3,1,1]][[3, 1, 1]]
Five-qubit code 4 [[5,1,3]][[5, 1, 3]]
Steane code (next lesson) 6 [[7,1,3]][[7, 1, 3]]
Shor code 8 [[9,1,3]][[9, 1, 3]]

The bit-flip code has distance 1 because the single-qubit string ZIIZII is a logical operator. As a classical code against bit flips alone it has distance 3, but as a quantum code it has a hole.

Check

A code has distance 5. Errors on how many qubits is it guaranteed to correct?

In this notation the nine-qubit code of the last lesson is eight strings:

ZZIIIIIIIIZZIIIIIIIIIZZIIIIIIIIZZIIIIIIIIIZZIIIIIIIIZZXXXXXXIIIIIIXXXXXX\begin{array}{c} ZZIIIIIII\\ IZZIIIIII\\ IIIZZIIII\\ IIIIZZIII\\ IIIIIIZZI\\ IIIIIIIZZ\\ XXXXXXIII\\ IIIXXXXXX \end{array}

Everything from that lesson can be read off. A ZZ on qubit 4 clashes with both XX-type strings and nothing else. ZZ on qubit 3 and ZZ on qubit 5 differ by Z3Z5Z_3Z_5, which is the product of the third and fourth generators, so they act identically on the code: that was the degeneracy. And nine qubits minus eight generators leaves one logical qubit.

SimulatorThe Shor code

This simulator needs JavaScript.

Things to try:

  1. Put a ZZ on qubit 4. Generators 7 and 8 fire, and nothing else.
  2. Clear it, then put a ZZ on qubit 3 and another on qubit 5. Nothing fires, and the simulator confirms that the pair is a product of generators.
  3. Put an XX on qubit 0 and a ZZ on qubit 4. The two halves of the syndrome are read separately and both errors are corrected.

One surprise is in the bottom two rows. For this code logical XX is made of ZZ‘s and logical ZZ is made of XX‘s. That follows from how it was built: ∣0L⟩\ket{0_L} and ∣1L⟩\ket{1_L} differ by the signs inside the blocks, so it takes phase flips to swap them.

There is a further payoff. Circuits built only from HH, SS and CNOT turn Pauli strings into Pauli strings, so a computer can follow what such a circuit does to a list of generators without ever storing 2n2^n amplitudes. This result, the Gottesman–Knill theorem, is why error-correcting circuits on thousands of qubits can be simulated on a laptop. It is also a reminder that these gates alone cannot give a quantum speedup. The fault tolerance lesson returns to what is missing.

Next: a systematic way of building stabilizer codes from classical ones.

RecallAnswer from memory, then check
LessonFinish this lesson