Course Home

Shor's 9-Qubit Code

An interactive guide to the first quantum error-correcting code — from classical bits to quantum fault tolerance.

The central challenge: Quantum information is fragile. A single qubit can suffer bit-flip errors (X), phase-flip errors (Z), or any combination. Unlike classical bits, we cannot simply copy a qubit (the no-cloning theorem forbids it). Shor's 1995 code showed that quantum error correction is possible by encoding 1 logical qubit into 9 physical qubits.

Roadmap: We'll build understanding layer by layer:

1. Classical 3-bit repetition code (corrects bit-flips)

2. Quantum 3-qubit bit-flip code (quantum analogue)

3. The Hadamard transform and its magic: turning phase-flips into bit-flips

4. Quantum 3-qubit phase-flip code

5. Shor's 9-qubit code: combining both

1. Classical 3-Bit Repetition Code

The simplest error-correcting code: to protect a single bit, repeat it three times. If noise flips one bit, majority voting recovers the original.

b b b b COPY Time →
Interactive: Classical Repetition Code
Choose your message bit:
Original Encoded (3 copies) After noise Decoded
0
0
0
0
0
0
0
0
Syndrome:
Encode a bit and then flip one of the three copies. Majority vote will recover the original.

How majority voting works

ReceivedSyndromeCorrectionDecoded
000No errorNone0
001Bit 3 flippedFlip bit 30
010Bit 2 flippedFlip bit 20
100Bit 1 flippedFlip bit 10
111No errorNone1
110Bit 3 flippedFlip bit 31
101Bit 2 flippedFlip bit 21
011Bit 1 flippedFlip bit 11

Key insight: We detect errors by comparing bits (computing the syndrome), not by looking at the value of any individual bit. This same principle extends to quantum codes.

Self-test: Why can the 3-bit code correct 1 error but not 2?
With 2 flipped bits, the majority vote agrees with the flipped bits, not the original. For example, if we encode 0 as 000 and two bits flip to give 110, majority vote returns 1 (wrong). The code's distanceThe minimum number of single-qubit errors needed to turn one valid codeword into another. A code with distance d can correct ⌊(d−1)/2⌋ errors. is 3 — it can detect up to $d-1=2$ errors but only correct $\lfloor(d-1)/2\rfloor = 1$.
Self-test: What does the syndrome (1,0) tell us?
The syndrome is computed from parity checks: bit 1 XOR bit 2, and bit 2 XOR bit 3. Syndrome (1,0) means bits 1 and 2 disagree, but bits 2 and 3 agree. Since bit 2 and bit 3 agree with each other, bit 1 must be the one that flipped. The syndrome localizes the error without revealing the actual bit value.

2. Quantum 3-Qubit Bit-Flip Code

We can't clone a qubit, but we can entangle it with ancillaAn extra "helper" qubit used to extract error information without disturbing the encoded data. Measured and discarded after syndrome extraction. qubits. The encoding maps:

The No-Cloning Theorem: It is mathematically impossible to create an exact copy of an unknown quantum state. If cloning were possible, we could violate the uncertainty principle and enable faster-than-light communication. This means quantum error correction must use entanglement rather than copying. The encoding $|\psi\rangle \to \alpha|000\rangle + \beta|111\rangle$ does not create three copies of $|\psi\rangle$ -- it creates an entangled state where no single qubit carries the full information about $\alpha$ and $\beta$.

Classical Repetition

  • Encode: Copy the bit 3 times
  • Detect: Compare bits directly
  • Correct: Majority vote
  • Handles: Bit-flip errors only

Quantum Bit-Flip

  • Encode: Entangle with ancilla qubits
  • Detect: Syndrome (parity) checks
  • Correct: Apply correction gate
  • Handles: X errors only
$$|0\rangle \;\rightarrow\; |000\rangle$$ $$|1\rangle \;\rightarrow\; |111\rangle$$ $$\alpha|0\rangle + \beta|1\rangle \;\rightarrow\; \alpha|000\rangle + \beta|111\rangle$$
|ψ⟩ |0⟩ |0⟩ CNOT: Flips target qubit q2 if control q1 is |1⟩ CNOT: Flips target qubit q3 if control q1 is |1⟩ α|000⟩ + β|111⟩ CNOT CNOT Time →

This is achieved with two CNOTControlled-NOT gate: flips the target qubit if and only if the control qubit is |1⟩. The fundamental two-qubit gate for entangling and encoding. gates. If a bit-flip (X) hits one qubit, we detect it using syndromeA set of parity-check outcomes that identifies which error occurred, without revealing the encoded quantum information. Like an error fingerprint. measurements — parity checks that reveal which qubit flipped without revealing α or β.

Interactive: Quantum Bit-Flip Code
Logical qubit state: α|0⟩ + β|1⟩
|ψ⟩ Encode Error Syndrome Correct
Step 1 / 5
|ψ⟩
|0⟩
|0⟩
Syndrome:
Step 1 — Initial state: We have our logical qubit |ψ⟩ = α|0⟩ + β|1⟩ and two ancilla qubits initialized to |0⟩. The full state is (α|0⟩ + β|1⟩) ⊗ |00⟩.
When you reach the error step, choose which qubit to hit:

Syndrome table for bit-flip code

Syndrome ($Z_1Z_2,\; Z_2Z_3$)ErrorCorrection
0, 0NoneNone
1, 0X on qubit 1Apply X to qubit 1
1, 1X on qubit 2Apply X to qubit 2
0, 1X on qubit 3Apply X to qubit 3

The syndrome measurements check parity between adjacent qubits without collapsing the encoded state. This is the quantum magic: we learn where the error is, but nothing about α or β.

How do we measure syndromes without disturbing the data? We use extra qubits called ancillas (from Latin ancilla, "helper"). The procedure works in three steps:

  1. Prepare an ancilla qubit in state $|0\rangle$.
  2. Entangle it with the data qubits using CNOT gates. For example, to measure $Z_1Z_2$ parity, apply CNOT from qubit 1 to the ancilla, then CNOT from qubit 2 to the ancilla. The ancilla now holds the parity $q_1 \oplus q_2$.
  3. Measure only the ancilla. If it reads $|0\rangle$, parity is even (qubits agree); if $|1\rangle$, parity is odd (qubits disagree).

The data qubits are never measured directly — the ancilla absorbs only the parity information, leaving the superposition $\alpha$ and $\beta$ intact.

Try it: syndrome decoder

Toggle the two syndrome bits to see which qubit the decoder identifies as errored.

Syndrome:
q1
q2
q3

Syndrome (0,1): Error on qubit 3 — apply X to qubit 3.

Limitation: This code only corrects bit-flip (X) errors. But qubits also suffer phase-flip (Z) errors, where |1⟩ picks up a minus sign: |1⟩ → −|1⟩. The bit-flip code is completely blind to these!

Self-test: Why can't we just measure the qubits to check for errors?
Measuring a qubit in the computational basis would collapse the superposition $\alpha|0\rangle + \beta|1\rangle$ to either $|0\rangle$ or $|1\rangle$, destroying the quantum information we're trying to protect. Syndrome measurements are cleverly designed to extract only parity information (which qubit differs from its neighbors) without learning anything about $\alpha$ or $\beta$. This is the key difference from classical error correction.

3. The Hadamard Transform: Changing the Nature of Errors

The Hadamard gate H is the key that connects bit-flips and phase-flips. It maps between the computational basis $\{|0\rangle, |1\rangle\}$ and the Hadamard basis $\{|+\rangle, |-\rangle\}$:

$$H|0\rangle = |+\rangle = \frac{|0\rangle + |1\rangle}{\sqrt{2}}$$ $$H|1\rangle = |-\rangle = \frac{|0\rangle - |1\rangle}{\sqrt{2}}$$

The crucial insight

In the Hadamard basis, the roles of X and Z are swapped:

Bit-flip X:

$X|0\rangle = |1\rangle$
$X|1\rangle = |0\rangle$

In the $\{|+\rangle,|-\rangle\}$ basis:

$X|+\rangle = |+\rangle$
$X|-\rangle = -|-\rangle$

X acts like a phase-flip in the Hadamard basis!

Phase-flip Z:

$Z|0\rangle = |0\rangle$
$Z|1\rangle = -|1\rangle$

In the $\{|+\rangle,|-\rangle\}$ basis:

$Z|+\rangle = |-\rangle$
$Z|-\rangle = |+\rangle$

Z acts like a bit-flip in the Hadamard basis!

Mathematically: $HXH = Z$ and $HZH = X$

The Hadamard Matrix

$$H = \frac{1}{\sqrt{2}} \begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix}$$

Key properties:

$H^2 = I$ — applying H twice gives back the original state (H is its own inverse)

$H = H^\dagger$ — H is both unitary and Hermitian

H is a Fourier transform on a single qubit — it rotates between conjugate bases

Interactive: Hadamard Conjugation

Apply H, then an error, then H again. Watch how the error type transforms.

Start
|0⟩
After H
|+⟩
After error
After final H
Choose an error to apply to |+⟩ and see what happens when you transform back with H.

Why this matters for error correction: If we encode in the Hadamard basis (using |+⟩ and |−⟩ instead of |0⟩ and |1⟩), then a phase-flip error looks like a bit-flip error. So we can reuse the same repetition code structure to detect phase-flips! This is the key idea behind Shor's code.

Self-test: If HXH = Z and HZH = X, what is HYH?
Since $Y = iXZ$, we get $HYH = H(iXZ)H = i(HXH)(HZH) = iZX$. Now $ZX = iY$ (verify by matrix multiplication), so $HYH = i \cdot iY = -Y$. The Hadamard conjugation sends $Y \to -Y$. Since a global phase of $-1$ is physically unobservable, Y errors are essentially unchanged under Hadamard conjugation.
Self-test: Why is the Hadamard gate its own inverse?
You can verify directly: $H^2 = \frac{1}{2}\begin{pmatrix}1&1\\1&-1\end{pmatrix}\begin{pmatrix}1&1\\1&-1\end{pmatrix} = \frac{1}{2}\begin{pmatrix}2&0\\0&2\end{pmatrix} = I$. Geometrically, H is a reflection on the Bloch sphere (a 180-degree rotation about the axis halfway between X and Z). Any reflection applied twice is the identity.

4. The 3-Qubit Phase-Flip Code

By encoding in the Hadamard basis, we can correct phase-flip errors using the same repetition structure:

$$|0\rangle \;\rightarrow\; |{+}{+}{+}\rangle = \left(\tfrac{|0\rangle+|1\rangle}{\sqrt{2}}\right)^{\otimes 3}$$ $$|1\rangle \;\rightarrow\; |{-}{-}{-}\rangle = \left(\tfrac{|0\rangle-|1\rangle}{\sqrt{2}}\right)^{\otimes 3}$$ $$\alpha|0\rangle + \beta|1\rangle \;\rightarrow\; \alpha|{+}{+}{+}\rangle + \beta|{-}{-}{-}\rangle$$

The encoding circuit: first apply CNOTs (as in the bit-flip code), then apply H to each qubit:

|ψ⟩ |0⟩ |0⟩ CNOT: Entangles q1 and q2 for phase-flip encoding CNOT: Entangles q1 and q3 for phase-flip encoding Hadamard gates: Switch all 3 qubits to the |+⟩/|−⟩ basis for phase-flip protection H H H Bit-flip encoding Basis change |+/-⟩ |+/-⟩ |+/-⟩ Time →

A phase-flip Z on any single qubit is detected via syndrome measurements in the X-basis (measuring $X_1X_2$ and $X_2X_3$).

Interactive: Phase-Flip Code
|ψ⟩ Encode Error Syndrome Correct
Step 1 / 5
|ψ⟩
|0⟩
|0⟩
Syndrome:
Step 1 — Initial state: We start with |ψ⟩ = α|0⟩ + β|1⟩ and two ancillas |0⟩.
When you reach the error step, choose which qubit to hit:

Bit-flip code

Encodes: $|0\rangle\!\to\!|000\rangle$, $|1\rangle\!\to\!|111\rangle$

Corrects: X errors

Syndrome: Z-basis parities

Blind to: Z errors

Phase-flip code

Encodes: $|0\rangle\!\to\!|{+}{+}{+}\rangle$, $|1\rangle\!\to\!|{-}{-}{-}\rangle$

Corrects: Z errors

Syndrome: X-basis parities

Blind to: X errors

Shor's idea: What if we use both codes together? Encode each qubit of the phase-flip code using the bit-flip code. This gives a 9-qubit code that corrects both X and Z errors — and therefore any single-qubit error!

Self-test: Why does the phase-flip code use X-basis syndrome measurements?
The phase-flip code encodes in the Hadamard basis: $|0\rangle_L = |{+}{+}{+}\rangle$, $|1\rangle_L = |{-}{-}{-}\rangle$. A Z error flips $|+\rangle \leftrightarrow |-\rangle$, which looks like a bit-flip in the Hadamard basis. To detect this, we need parity checks in the X-basis (measuring $X_iX_j$), which check whether adjacent qubits agree in the $\{|+\rangle,|-\rangle\}$ basis. Using Z-basis measurements would tell us about the computational basis, where the encoded state looks like a complicated superposition — we'd learn nothing useful and would collapse the state.

5. Shor's 9-Qubit Code

Shor's code is a concatenationBuilding a larger code by nesting one code inside another. Shor's code nests the 3-qubit bit-flip code inside the 3-qubit phase-flip code, yielding 9 qubits total. of the phase-flip code (outer) and the bit-flip code (inner). The encoding is:

$$|0\rangle_L = \frac{(|000\rangle + |111\rangle)(|000\rangle + |111\rangle)(|000\rangle + |111\rangle)}{2\sqrt{2}}$$ $$|1\rangle_L = \frac{(|000\rangle - |111\rangle)(|000\rangle - |111\rangle)(|000\rangle - |111\rangle)}{2\sqrt{2}}$$

Structure: two layers of protection

The 9 qubits are organized into 3 blocks of 3:

Block A (qubits 1-3)
q1
q2
q3
Block B (qubits 4-6)
q4
q5
q6
Block C (qubits 7-9)
q7
q8
q9
1 Logical Phase-flip code Block A Block B Block C Bit-flip code (per block) q1 q2 q3 q4 q5 q6 q7 q8 q9 9 Physical Qubits

Inner code (within each block): The 3-qubit repetition code protects against X (bit-flip) errors. Each block uses Z-parity checks.

Outer code (across blocks): The 3-block structure (in the Hadamard basis) protects against Z (phase-flip) errors. Cross-block X-parity checks detect which block was affected.

Encoding circuit

The full encoding circuit has two stages: first the outer (phase-flip) code, then the inner (bit-flip) code within each block.

Block A Block B Block C |ψ⟩ |0⟩ |0⟩ |0⟩ |0⟩ |0⟩ |0⟩ |0⟩ |0⟩ Phase-flip CNOTs: Spread |ψ⟩ across blocks A, B, C Hadamard gates: Switch block-lead qubits to |+⟩/|−⟩ basis for phase-flip protection H H H Bit-flip CNOTs: Triplicate each block-lead qubit within its block for bit-flip protection Phase-flip encoding Bit-flip encoding Time →
Interactive: Shor's 9-Qubit Code — Full Walkthrough
|ψ⟩ Phase enc. Bit enc. Error BF syn. PF syn. Correct
Step 1 / 7
Block A
|ψ⟩
|0⟩
|0⟩
Block B
|0⟩
|0⟩
|0⟩
Block C
|0⟩
|0⟩
|0⟩
Bit-flip syndrome:
Phase-flip syndrome:
Step 1 — Initial state: Our logical qubit |ψ⟩ = α|0⟩ + β|1⟩ is in qubit 1. All other 8 qubits are |0⟩.
Choose an error (available at the error step):

Error correction procedure

Step 1: Detect bit-flip errors (within each block). Measure $Z_iZ_j$ parity checks for each block. This identifies which qubit (if any) within a block has flipped. Apply X to correct.

Step 2: Detect phase-flip errors (across blocks). Measure the 6-qubit operators $X_1X_2X_3X_4X_5X_6$ and $X_4X_5X_6X_7X_8X_9$. This identifies which block (if any) has a sign flip. Apply Z to any qubit in that block to correct.

Why this corrects any single-qubit error

Any single-qubit error can be decomposed as a linear combination of I, X, Z, and Y = iXZ. When we measure the syndrome, the state collapses into one of these cases:

ErrorBit-flip syndromePhase-flip syndromeCorrection
I (no error)All 0All 0None
X on qubit kIdentifies k within blockAll 0Apply Xk
Z on qubit kAll 0Identifies blockApply Zk
Y on qubit kIdentifies k within blockIdentifies blockApply Yk

The measurement projects the error into one of these discrete cases, and then we apply the appropriate correction. This is the digitization of errors — continuous errors become discrete through measurement.

Self-test: Why does Shor's code need 9 qubits, not 6?
You might think: 3 qubits for bit-flip protection + 3 for phase-flip = 6. But the two codes can't simply run side by side — they'd each protect against only one error type. Shor's insight is concatenation: each of the 3 qubits in the phase-flip code is itself encoded using the 3-qubit bit-flip code, giving $3 \times 3 = 9$. This nesting ensures that bit-flip errors are caught within each block, and phase-flip errors are caught across blocks. (Later, the 7-qubit Steane code and 5-qubit perfect code showed that fewer qubits suffice.)
Self-test: What happens to both syndromes when a Y error occurs?
A Y error equals $iXZ$: it's simultaneously a bit-flip and a phase-flip. The bit-flip syndrome detects the X component (identifying the specific qubit within its block), and the phase-flip syndrome detects the Z component (identifying which block). Both syndromes are nonzero, and together they uniquely identify the affected qubit. The correction is to apply $Y = iXZ$ to that qubit (or equivalently, apply X then Z). Try it in the interactive demo above!

6. When the Code Fails: Understanding Limits

Shor's code can correct any single-qubit error, but what happens when two or more qubits are hit? With distance $d = 3$, the decoder's assumption — at most one error — breaks down, and it can misidentify the problem.

Try it: multi-error failure modes

Toggle X errors on the 3 qubits of a bit-flip block. Watch how the syndrome misleads the decoder when multiple errors occur.

|ψ⟩
|ψ⟩
|ψ⟩
Syndrome: 0 0

No errors. Syndrome (0,0) — the decoder sees no problem.

Beyond Shor's code

Shor's 9-qubit code was the first quantum error-correcting code, but it is far from optimal. Later discoveries reduced the qubit overhead and increased error tolerance:

  • Steane code $[[7,1,3]]$ — corrects the same errors with only 7 qubits using CSS construction
  • Perfect 5-qubit code $[[5,1,3]]$ — the smallest possible single-error-correcting code
  • Surface codes — the leading approach for real hardware, achieving high distance with nearest-neighbor qubits on a 2D grid

All share the same core principles we've explored: encode redundantly, measure syndromes without disturbing data, and correct based on the syndrome pattern.

7. Summary & Key Takeaways

The three big ideas

1. Redundancy without cloning: We can't copy quantum states, but we can spread quantum information across entangled qubits. The encoding creates a code space where errors move the state out of the code space in detectable ways.

2. The Hadamard duality: The Hadamard gate swaps X ↔ Z errors. A phase-flip code is just a bit-flip code conjugated by H. This is a deep consequence of the structure of quantum mechanics — the X and Z bases are related by Fourier transform.

3. Code concatenation: By nesting the bit-flip code inside the phase-flip code (or vice versa), Shor's code corrects both types of errors — and therefore any single-qubit error. The syndrome measurement discretizes continuous errors.

Code parameters

PropertyValue
Physical qubits9
Logical qubits1
Distance3
Correctable errorsAny single-qubit error (X, Z, or Y)
Notation[[9, 1, 3]]
Stabilizer generators8 (= 9 - 1)

Understanding code distance. The notation $[[n, k, d]]$ describes a quantum code encoding $k$ logical qubits into $n$ physical qubits with distance $d$. The distance is the minimum number of single-qubit operations needed to convert one valid codeword into another.

For Shor's $[[9,1,3]]$ code, distance $d = 3$ means:

  • Correct any $\lfloor (d{-}1)/2 \rfloor = 1$ error
  • Detect up to $d - 1 = 2$ errors (but cannot correct both)

This is a fundamental trade-off: higher distance costs more physical qubits but protects against more errors. The surface code, for example, achieves distance $d$ using roughly $2d^2$ physical qubits.

What are stabilizers?

A stabilizerAn operator that leaves every valid codeword unchanged. Measuring stabilizers extracts error syndromes without collapsing the encoded information. is an operator that leaves every valid codeword unchanged: if $|\psi\rangle$ is in the code space, then $S|\psi\rangle = |\psi\rangle$. Think of stabilizers as membership checks — they verify that a state belongs to the code space without disturbing it.

When an error $E$ occurs, some stabilizers will anti-commute with it. Measuring those stabilizers returns $-1$ instead of $+1$, forming a pattern called the syndrome. Each correctable error produces a unique syndrome, so the decoder can identify and reverse the error:

$$\text{syndrome} \;\xrightarrow{\text{lookup}}\; \text{error } E \;\xrightarrow{\text{apply } E}\; \text{correction}$$

Shor's code has 8 stabilizer generators (since $n - k = 9 - 1 = 8$), producing an 8-bit syndrome. Six use $Z$ operators for bit-flip detection within blocks; two use $X$ operators for phase-flip detection between blocks.

Stabilizer generators

GeneratorRole
$Z_1 Z_2$Block A, qubits 1-2 parity
$Z_2 Z_3$Block A, qubits 2-3 parity
$Z_4 Z_5$Block B, qubits 4-5 parity
$Z_5 Z_6$Block B, qubits 5-6 parity
$Z_7 Z_8$Block C, qubits 7-8 parity
$Z_8 Z_9$Block C, qubits 8-9 parity
$X_1 X_2 X_3 X_4 X_5 X_6$Block A-B parity
$X_4 X_5 X_6 X_7 X_8 X_9$Block B-C parity

Historical significance: Peter Shor published this code in 1995, disproving the widespread belief that quantum error correction was impossible. It paved the way for the theory of fault-tolerant quantum computation and all modern quantum error-correcting codes (CSS codes, surface codes, etc.).