Exercises: Quantum Error Correction in Code

These need qiskit and qiskit-aer. Solutions to starred exercises are in Answers to Selected Exercises.

Before you start: every exercise that injects noise must verify the noise survived transpilation. Transpile at optimization_level=0 and assert the id-gate count. Case Study 1 is what happens otherwise.


The three-qubit code

25.1 ★ Build the bit-flip encoder and confirm that the encoded state of $\alpha|0\rangle + \beta|1\rangle$ is $\alpha|000\rangle + \beta|111\rangle$. Then compute the reduced density matrix of a single qubit and show it carries no information about $\alpha$ — no-cloning is not violated.

25.2 ★ Implement syndrome extraction and verify the lookup table: inject $X$ on each data qubit in turn and confirm the syndrome is $(1,0)$, $(1,1)$, $(0,1)$ respectively.

25.3 ★ Measure the logical error rate under $X$ noise at $p = 0.05, 0.1, 0.2$ and compare against $3p^2 - 2p^3$.

25.4 ★ Show that the transpiler deletes id gates: print count_ops() after transpiling at optimization levels 0 through 3. Which levels preserve the noise slots?

25.5 ★★ Solve $3p^2 - 2p^3 = p$ by hand, then confirm the crossover numerically by finding the smallest $p$ at which the encoded rate exceeds the unencoded rate.

25.6 ★★ Show that the syndrome carries no information about the encoded state: measure the syndrome distribution for several different $(\alpha, \beta)$ at fixed noise and confirm it does not change.


Blind spots

25.7 ★ Reproduce §25.3's failure: run the phase-flip code under $Z$ noise storing $|+\rangle$ and confirm you get 0.0000 at $p = 0.6$. Explain why that is impossible.

25.8 ★★ Use qiskit.quantum_info.Clifford to conjugate $Z_0$, $Z_1$, $Z_2$ back through the phase-flip encoder and confirm the logical component is an $X$.

25.9 ★★ Build the full 12-cell grid — two codes × two logical states × three error types — at $p = 0.05$, and identify the four blind cells.

25.10 ★★ Explain each of the four blind cells. Two are blind because the stored state is an eigenstate of the physical error; two because the residual logical error stabilizes it. Which are which?

25.11 ★★ Why is there no blind cell in the $Y$ column? Prove it.

25.12 ★★★ Write is_blind(code, state, error) from first principles rather than from a lookup table: conjugate the error through the decoder, identify the logical component, and test whether it stabilizes the stored state. Confirm it reproduces the four cells.


One code is never enough

25.13 ★ Measure the bit-flip code's logical error rate under $Z$ noise storing $|+_L\rangle$. Compare with $1-(1-p)^3$ and explain the factor of three.

25.14 ★★ For each of the two three-qubit codes, compute the worst case over both logical basis states and all three error types at $p = 0.05$. Is either code better than no encoding?

25.15 ★★ Implement the Shor nine-qubit encoder and verify by injecting all 54 single-qubit Paulis that every one is corrected exactly.

25.16 ★★ Measure the Shor code's logical error rate under independent $Z$ noise from $p = 0.005$ to $0.08$. Where is the breakeven, and does it match $1/27$?

25.17 ★★ Do the same for $X$ noise. Why is the breakeven $1/9$ rather than $1/27$? What does the asymmetry tell you about the code's structure?

25.18 ★★★ Derive both breakevens analytically, starting from the per-block error probability, and check them against your measurements.


Stabilizers

25.19 ★ Verify that the generators of each code in STABILIZERS pairwise commute.

25.20 ★ Compute $[[n,k,d]]$ for the Shor and Steane codes from their generator sets. Why does Steane achieve distance 3 with two fewer qubits?

25.21 ★★ Show that $\{XII, ZII\}$ cannot generate a stabilizer code, and explain what goes wrong physically if you try to measure both.

25.22 ★★ For the three-qubit bit-flip code, find the logical operators $X_L$ and $Z_L$: Paulis that commute with every stabilizer but are not themselves in the stabilizer group.

25.23 ★★★ Implement syndrome extraction for the Steane code (six generators, seven qubits) and verify it corrects any single-qubit error. How many ancillas does it need?


Feedforward and the threshold

25.24 ★ Replace the unitary correction with mid-circuit measurement and if_test feedforward, and confirm the logical error rates agree.

25.25 ★★ Build the distance $d = 1,3,5,7,9$ fan and confirm the curves cross at $p = 0.5$. Use Aer's stabilizer method and time it against statevector.

25.26 ★★ Compute $\Lambda = P_L(d)/P_L(d+2)$ at several $p$ and confirm it is roughly constant in $d$. At what $p$ does $\Lambda$ fall below 2?

25.27 ★★ Plot $\log P_L$ against $d$ below and above threshold. What is qualitatively different about the two regimes?

25.28 ★★ (Project Checkpoint) Build vqelab/errorcorrection.py with logical_error_rate returning a CodeResult carrying a blind flag, worst_case_logical_error, code_helps, shor_single_error_sweep, suppression_factor, noisy_syndrome_logical_error, and syndrome_breakeven. Write tests asserting:

  1. The bit-flip code follows $3p^2-2p^3$.
  2. The crossover is at $p = 1/2$: correcting helps below and adds error above.
  3. A single-basis-state test is flagged BLIND, and reports 0.0000.
  4. The blind cells are mirrored between the two codes.
  5. The analytic blindness rule agrees with the simulator in all twelve cells.
  6. No blind cell exists for $Y$ noise.
  7. worst_case_logical_error never returns a blind measurement.
  8. The three-qubit code is WORSE THAN NOTHING for arbitrary states.
  9. code_helps has no state parameter — verify with inspect.signature.
  10. The Shor code corrects all 54 single-qubit injections.
  11. Shor's breakeven (1/27) is worse than the three-qubit code's (1/2) despite three times the qubits.
  12. Shor's protection is asymmetric: $X$ breakeven is exactly 3× the $Z$ breakeven.
  13. Every stabilizer generator set commutes; non-commuting sets are rejected.
  14. Below threshold the fan points down; above threshold it reverses.
  15. $\Lambda$ is constant in $d$, and collapses near threshold.
  16. At Chapter 12's measured gate error the code makes things worse.
  17. The noise slots are verified, not assumed.

Tests 3, 8, 11 and 16 are the ones this chapter exists to encode.

25.29 ★★★ Extend the module with blind_cells(code) computed rather than tabulated, and confirm it reproduces _BLIND_CELLS for both codes. Then apply it to the Shor code — how many of its 6 (state, error) combinations are blind?


The threshold you actually face

25.30 ★★ Add depolarizing noise to the syndrome-extraction gates and reproduce the breakeven sweep at $p_{\text{data}} = 0.01$.

25.31 ★★ Repeat at $p_{\text{data}} = 0.05$ and $0.001$. How does the breakeven gate error depend on the data error rate?

25.32 ★★ Pull the current median two-qubit gate error from a fake backend (Chapter 12) and determine whether the three-qubit code would help on that device today.

25.33 ★★★ The breakeven depends on how many gates the syndrome-extraction circuit uses. Replace the three Toffolis with mid-circuit measurement and feedforward, and re-measure the breakeven. Does removing the Toffolis help, and by how much?

25.34 ★★★ Run the repeated correction cycle: extract and correct the syndrome $r$ times in sequence with noise between each round. At what $r$ does accumulated syndrome-circuit noise overwhelm the correction?


Going further

25.35 ★★ Look up the surface code's stabilizers and confirm they are weight-4 and geometrically local on a 2D grid. Why does that matter more than the threshold value itself?

25.36 ★★★ A distance-$d$ surface code uses roughly $2d^2$ physical qubits. Using $\Lambda = 2$ (the value Google reported) and then $\Lambda = 10$, compute the distance and physical-qubit count needed for a logical error rate of $10^{-12}$. Compare with Chapter 15's estimates.

25.37 ★★★ The surface code supports Clifford gates but not $T$. Read about magic state distillation and explain why this makes Chapter 15's resource estimates $T$-count-dominated, and why Chapter 22's rotationCount and rotationDepth mattered so much.