Quiz: Debugging Quantum Programs
Answers with explanations at the end.
1. Give the four reasons the classical debugging loop — run, print, look, narrow — is unavailable for quantum programs.
2. State the one-sentence discipline that replaces it.
3. What does Statevector.from_instruction give you that hardware never can, and what does it
cost?
4. Write prefix(qc, k) and say what it is for.
5. At roughly what qubit count does statevector debugging stop working, and what are the three options past it?
6. Operator(p) == Operator(q) is False but Statevector.equiv is True for $XZXZ$ versus $I$.
Which is correct?
7. Under what circumstance does a global phase become observable? Show what it turns into.
8. Give the rule for choosing among the four equality tests.
9. Why does binary search apply to finding the first divergent instruction? Name the property.
10. How many comparisons does bisection need for Chapter 23's 3,368-gate Shor circuit, and for a 20-million-gate one?
11. A state-based bisection from $|000\rangle$ reports no divergence for a circuit whose process fidelity against the reference is 0.9498. Explain.
12. Why is $|{+}{+}{+}\rangle$ also blind, and why is the reason different?
13. Give the three blind-fraction measurements, and state the conclusion they force.
14. Name the three verdicts bisect returns and say why the third exists.
15. A colleague says "I'll just pick a test input that exercises the buggy gate." What is wrong with this plan?
16. Give the diagnostic for each of: bit-order confusion, a dirty ancilla, a wrong QFT convention, and silently deleted gates.
17. On a noiseless simulator, a data register's purity is 0.6250. What does this mean, and what does it definitively not mean?
18. Verifying a transpiled circuit gives process fidelity 0.001406. What is the most likely explanation, and what is the fix?
19. Why can you not verify transpilation for a 127-qubit backend by building operators? What should a tool do instead of returning a number?
20. State tomography costs $3^n$ settings. Given that, what are the four checks that actually constitute hardware debugging?
Answers
1. (a) You cannot print an intermediate state — reading collapses it, so observing the bug destroys it. (b) You cannot step through execution — no breakpoint pauses a superposition. (c) You cannot read the output — it is a sampled distribution, and a subtly wrong circuit differs from a correct one by about shot noise ($0.5/\sqrt N$). (d) You cannot distinguish a bug from noise — Chapter 19's dirty ancilla was indistinguishable from decoherence.
2. Debug on a simulator, where you can see everything; validate on hardware, where you can see nothing. They are separate activities with separate tools.
3. Every amplitude, exactly — no shots, no sampling, no noise. It costs $2^n$ complex amplitudes of memory: 16 GB at 30 qubits, 16 TB at 40.
4.
def prefix(qc, k):
out = QuantumCircuit(qc.num_qubits)
for inst in qc.data[:k]:
out.append(inst)
return out
Combined with Statevector.from_instruction it is a stepping debugger — the state after any
number of operations — and it is what bisection searches over.
5. Around 30–35 qubits on a large machine. Past it: the stabilizer method if the circuit is Clifford (Chapter 11), matrix-product-state if entanglement is low, or debug subcircuits and reason about how they compose.
6. Both. They answer different questions. $XZXZ = -I$ differs from $I$ by a global phase, which
cancels in $|\langle\psi|\psi\rangle|^2$ and is therefore unobservable — so equiv is right for a
circuit run as a whole program, and Operator equality is right if it will be used as a controlled
subroutine.
7. When the gate is controlled. $\text{ctrl}(-I)$ applies $-1$ on the $|1\rangle$ branch and
$+1$ on the $|0\rangle$ branch — which is a $Z$ on the control, entirely observable. Both
Operator == and Operator.equiv return False for the controlled versions, so it is a real
difference and not a phase convention.
8. Choose by USE, not by strictness. Standalone program → equiv/process_fidelity.
Sub-circuit that will be controlled → exact Operator equality. After a specific input →
state_fidelity. Input-independent whole map → process_fidelity or Operator.
9. Divergence is monotone: once two circuits differ, appending more gates cannot make them agree again. A monotone predicate over an ordered index is exactly what binary search requires.
10. $\lceil\log_2 n\rceil + 1$: 13 comparisons for 3,368 gates, and 26 for 20 million.
11. From $|000\rangle$ every control reaching the buggy gate is in $|0\rangle$, so the controlled-phase gate is the identity and the wrong angle never fires. The states really are identical; the measurement is correct and the conclusion is backwards. The test cannot fail, so it is not evidence.
12. Because the Hadamard earlier in the circuit maps $|+\rangle$ to $|0\rangle$ on the relevant wire before the buggy gate arrives. $|000\rangle$ is blind trivially; $|{+}{+}{+}\rangle$ is blind after one gate. Two unrelated mechanisms, same result — which is what makes reasoning about it unreliable.
13. 4/8 computational basis states blind (000, 010, 100, 110), 11/27 (41%) of
states over $\{0,1,+\}$, and 0/100 random states. Conclusion: use the operator when you can
build one, and random inputs otherwise. Structured states have structured blind spots.
14. DIVERGED (found the first differing instruction), AGREE (equal, verified
input-independently), and BLIND (a difference exists that this input cannot see). The third
exists because "I found nothing" and "there is nothing" are different claims, and a debugger that
conflates them certifies broken circuits as correct.
15. Choosing a good input requires already knowing where the bug is. 41% of structured inputs were blind here, and $|1{+}0\rangle$ — which has both a $|+\rangle$ and a $|1\rangle$ and looks obviously adequate — is blind because $q_0$ is in $|0\rangle$. The plan is not slightly unreliable; it is circular.
16. Bit order: Operator(a) == Operator(b.reverse_bits()) — if reversing makes them equal, the
logic is right and the convention is wrong. Dirty ancilla: purity(partial_trace(sv, ancillas)).
Wrong convention: process_fidelity against the library gate — one convention scores 1.0.
Deleted gates: diff count_ops() across transpilation.
17. It means the data register is entangled with something that was not uncomputed — Chapter 19's dirty ancilla. It definitively does not mean noise: there is no noise on a noiseless simulator, and the number is exact rather than statistical. On hardware the same symptom would be indistinguishable from decoherence.
18. The comparison ignored the layout. Transpilation assigns your logical qubits to physical
ones (initial_index_layout) and then routing moves them again (final_index_layout), so you are
comparing circuits on mismatched wires. Fix: Operator.from_circuit(transpiled), which applies
both permutations — giving 1.0000000000.
19. Because the transpiled circuit has 127 qubits and its unitary would be $2^{127}\times 2^{127}$ — about $1.7\times10^{38}$ amplitudes per row. It does not exist. A tool should raise with the alternative in the message (transpile to a small backend with similar coupling, or test the output distribution) rather than silently falling back to a comparison nobody can interpret.
20. $3^n$ settings for state tomography and $\sim12^n$ for process tomography make it a diagnostic for one or two qubits — 3.5 billion settings at 20. So hardware debugging is: (a) does the output distribution match the simulator's; (b) does it degrade the way noise degrades — smoothly with depth, worse on bad qubits, responsive to mitigation; (c) does a scaled-down version you can simulate work; (d) do the classical checks pass? The last is why Chapter 23's Shor and Chapter 24's QAOA verify their answers classically — that Las Vegas structure is the only form of hardware debugging that scales.