Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 39 ยท Noise and Error Correction

Quantum Error Correction: Bit-Flip and Phase-Flip Codes

Learn how quantum error correction works: the 3-qubit bit-flip and phase-flip codes, syndromes, and the Shor 9-qubit code, simulated in NumPy.

Classical computers protect bits by copying them: store 0 as 000, and if one bit flips, a majority vote fixes it. Quantum error correction looks impossible at first for three reasons. You cannot copy an unknown quantum state (the no-cloning theorem), measuring a qubit destroys its superposition, and qubits can suffer continuous errors, not just flips. Yet in 1995 Peter Shor and Andrew Steane showed that all three obstacles can be overcome.

The key ideas: spread one qubit of information over several entangled qubits (without copying it), measure only parities (the syndrome), which reveal where an error happened but not what the data is, and then undo the error. Start with the simplest codes, which each protect against one kind of error.

The three-qubit bit-flip code

A bit-flip error is the X gate. Encode an arbitrary qubit a|0> + b|1> as

Text
a|000> + b|111>

This is not three copies of the qubit (that would be (a|0> + b|1>)^3). It is an entangled state, built with two CNOT gates from the first qubit onto the other two.

Now suppose one of the three qubits gets flipped. The four possibilities (no error, X on qubit 0, 1, 2) are distinguished by measuring two parities, Z0 Z1 and Z1 Z2 (do these pairs agree or differ?):

Error Parity(0,1) Parity(1,2) Fix
none same same nothing
X on qubit 0 different same X on qubit 0
X on qubit 1 different different X on qubit 1
X on qubit 2 same different X on qubit 2

Measuring a parity only tells whether two qubits agree. It does not reveal whether they are 0 or 1, so the superposition a, b survives. In a real circuit, the parities are measured by CNOT gates onto two extra ancilla qubits, which are then read out.

flowchart LR
    A["Encode: 1 qubit -> 3 qubits"] --> B["Noise: maybe one X"]
    B --> C["Measure parities Z0Z1, Z1Z2 (syndrome)"]
    C --> D["Apply the matching X correction"]
    D --> E["Decode"]

Encode, let noise act, read the syndrome, correct.

Python Bit-flip code: fixing every single X error

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The data qubit's amplitudes a = 0.6 and b = 0.8i are never inspected, yet the original state is restored every time.

How much does it help?

The code fails only if two or more of the three qubits flip. If each qubit flips independently with probability p, the logical error rate is 3p^2 (1 - p) + p^3 = 3p^2 - 2p^3. This is smaller than p whenever p < 1/2. For p = 1%, the logical rate is about 0.03%.

Python Logical vs physical error rate

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

There is a threshold idea here: encoding helps only if the physical error rate is low enough. Larger codes and repeated (concatenated) encoding push the logical error down exponentially, as long as the physical rate is below the code's threshold.

The phase-flip code

Quantum noise also includes phase flips (Z errors), which the bit-flip code ignores. The fix uses a symmetry: a Hadamard gate swaps X and Z. Encode in the Hadamard basis: apply the bit-flip encoding, then a Hadamard to each qubit, so

Text
|0> -> |+++>      |1> -> |--->

A Z error flips |+> to |-> in the same way X flips |0> to |1>. The syndrome is measured with X0 X1 and X1 X2, and the correction is a Z gate.

Python Phase-flip code: Z errors become bit flips in the H basis

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last line shows why one code is not enough: a Z error on the bit-flip codeword changes the logical state (it turns a|0> + b|1> into a|0> - b|1>), but both ZZ parities still read +1, so the error is invisible.

Shor's nine-qubit code

Combine both ideas: concatenate. First encode against phase flips (|0> -> |+++>), then encode each of those three qubits against bit flips. The result uses 9 qubits:

Text
|0> -> (|000> + |111>)(|000> + |111>)(|000> + |111>) / 2^(3/2)
|1> -> (|000> - |111>)(|000> - |111>)(|000> - |111>) / 2^(3/2)

Each block of three catches one X error. A Z error inside a block flips the sign of that block, which the outer phase-flip check detects. Since Y = i * X * Z, the code also fixes Y errors, and since every single-qubit error is a combination of I, X, Y, Z, it corrects any error on one qubit, including small continuous rotations. Measuring the syndrome collapses an arbitrary error into one of the discrete cases, which is why a finite set of corrections suffices. This is the digitization of errors.

The nine-qubit code is rarely used in practice, but it proved the principle. The next lesson covers the efficient stabilizer formalism and surface codes used in real roadmaps.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the syndrome measurement in the bit-flip code reveal?

    Parity checks like Z0Z1 compare qubits without learning their values, so the superposition is preserved.
  2. 2. A three-qubit bit-flip code has physical flip probability p = 0.1. What is the logical error probability?

    3p^2 - 2p^3 = 0.03 - 0.002 = 0.028.
  3. 3. Which errors does the three-qubit bit-flip code NOT detect?

    Z errors leave all Z-parity checks at +1 while changing the logical state's phase.
  4. 4. Why does the nine-qubit Shor code correct arbitrary single-qubit errors?

    This is the digitization of errors: the syndrome projects a continuous error onto a discrete Pauli error.

Summary

  • Quantum error correction spreads one logical qubit over several entangled qubits without cloning it.
  • Syndrome (parity) measurements locate errors without revealing the data.
  • The 3-qubit bit-flip code handles X errors, the phase-flip code (in the Hadamard basis) handles Z, and Shor's 9-qubit code handles both and thus any single-qubit error.
  • Encoding helps only below a threshold physical error rate; then logical errors drop fast.

Next: Stabilizer Codes and Surface Codes