|q⟩ Bad Qubits

advanced · Programming · Implementing Error-Correcting Codes

Checkpoint: Run a QEC Cycle

This checkpoint puts the whole module together: take a genuine quantum superposition, protect it, damage it, and bring it back — all without ever learning what the logical state was. If your circuit works, you have implemented a complete quantum-error-correction cycle.

The cycle, end to end

A QEC cycle has four stages, and you have built each one separately in this module:

  1. Encode. Map the data qubit onto a logical codeword with the encoder (H0H_0 then cx(0,1); cx(0,2) for +L|+_L\rangle).
  2. Error. Noise strikes — here a single XX on qubit 2, the kind the bit-flip code is built to survive.
  3. Extract / decode. Run the encoder in reverse to gather the logical amplitude back onto the data qubit and expose the error as a syndrome on the partners.
  4. Correct. Use the syndrome to apply the recovery — a Toffoli vote that flips the data qubit only when both partners disagree with it.

Why it has to be coherent

The input is +|+\rangle, an equal superposition. A scheme that measured an individual data qubit would collapse it and destroy the logical information. The decode-and-vote recovery uses only unitary gates, so by linearity it acts the same on α0+β1\alpha|0\rangle+\beta|1\rangle as on the basis states: the logical amplitudes α,β\alpha,\beta pass through untouched while the error is undone. This is the essence of QEC — repairing a state you are not allowed to look at.

What success looks like

After the cycle, qubit 0 is back in +|+\rangle and qubits 1 and 2 hold the leftover syndrome of the corrected error. The full three-qubit statevector is therefore deterministic: two amplitudes of 1/21/\sqrt{2} that differ only in the qubit-0 bit. That is the signature of a perfectly recovered logical +|+\rangle.

Try it

Implement the four-step cycle: encode +L|+_L\rangle, inject XX on qubit 2, decode, and correct. The grader checks the recovered statevector — get the logical +|+\rangle back onto qubit 0.

Run your code to see the quantum state.

When the cycle is correct, the statevector shows exactly two populated amplitudes (indices 1 = 001 and 5 = 101), each 0.707\approx 0.707, differing only in the qubit-0 bit. The logical +|+\rangle has survived a bit-flip error — a full QEC cycle, working.

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