|q⟩ Bad Qubits

advanced · Programming · Implementing Error-Correcting Codes

Correcting Arbitrary Single-Qubit Errors

The three-qubit bit-flip code corrects XX errors. To correct an arbitrary single-qubit error — any XX, YY, or ZZ — we need the structure of the Shor code. This lesson first shows the mechanics of a coherent correction on the small bit-flip code, then explains how the same idea scales to full Pauli protection.

Why correcting X, Z, and Y is enough

Recall the digitization principle: an arbitrary single-qubit error is a linear combination E=c0I+c1X+c2Y+c3ZE = c_0 I + c_1 X + c_2 Y + c_3 Z. Syndrome measurement projects the state onto one Pauli branch, so a code that handles XX, ZZ, and YY on every qubit handles all small errors. And since Y=iXZY = iXZ, a code that independently catches XX and ZZ catches YY for free — the YY error simply lights up both the bit-flip and phase-flip syndromes at once.

This is the design logic of the Shor code: its inner blocks correct XX on any qubit, its outer phase structure corrects ZZ on any block, and the combination corrects YY.

Coherent correction without measurement

We can recover a logical qubit using only unitary gates — no mid-circuit measurement, no classical feedback. The trick is to let the redundancy qubits control the correction directly:

  1. Decode. Apply the inverse encoder. For the bit-flip code the encoder is cx(0,1); cx(0,2), and since CNOT is its own inverse, repeating it decodes. The logical amplitude gathers back onto the data qubit while qubits 1 and 2 are left holding the error syndrome.
  2. Vote. A single Toffoli ccx(2,1,0) flips the data qubit exactly when both partners read 11. For a single bit flip this is precisely the majority correction.

Walking the example

We encode +L=12(000+111)|+_L\rangle = \tfrac{1}{\sqrt{2}}(|000\rangle+|111\rangle), inject XX on qubit 1 (giving 12(010+101)\tfrac{1}{\sqrt{2}}(|010\rangle+|101\rangle)), then decode and vote. The data qubit returns to +|+\rangle; qubits 1 and 2 are left in the syndrome state 10|10\rangle, which records "the middle qubit was hit." The grader checks the full three-qubit statevector, which is fully determined: two amplitudes of 1/21/\sqrt{2} that differ only in the qubit-0 bit.

From bit-flip to arbitrary errors

To upgrade this to full single-qubit protection you wrap the bit-flip correction (run inside each of the three blocks) with an analogous phase correction across the blocks — exactly the nine-qubit Shor code. The block-level circuits detect XX; transforming each block into the Hadamard basis turns a ZZ on that block into a detectable bit flip; and YY triggers both. The recovery is the corresponding product of XX and ZZ corrections.

Try it

Correct the injected bit-flip coherently: decode, then vote with a Toffoli. The grader checks the full statevector — qubit 0 must come back to +|+\rangle.

Run your code to see the quantum state.

After running, the two populated amplitudes are at indices 2 (010) and 6 (110), each 0.707\approx 0.707. They differ only in the qubit-0 bit, confirming the logical qubit is restored to +|+\rangle with the syndrome parked on qubits 1 and 2.

Sign in on the full site to ask questions and join the discussion.