A Quantum Program Debugging Checklist: From State Preparation to Measurement
When a quantum circuit produces unexpected results, use this repeatable debugging workflow to isolate problems in qubit indexing, gate order, state preparation, measurement mapping, simulator settings, noise assumptions, and classical post-processing.
Overview
Debugging a quantum program is different from debugging ordinary deterministic code. A circuit can be logically correct while its measured output looks unfamiliar because of superposition, interference, entanglement, finite shot counts, noise, or a mismatch between quantum and classical bit ordering. The most efficient approach is to reduce the problem to a small, observable test rather than changing several gates at once.
Start with a precise expectation. Write down the state, probability distribution, or measured bitstring you believe the circuit should produce. Then identify which layer is failing:
- Construction: the circuit does not contain the gates or registers you intended.
- State preparation: the initial state or input encoding is wrong.
- Evolution: gate order, controlled operations, or parameters differ from the algorithm.
- Measurement: basis rotations, qubit indexing, or classical-bit mapping changes the result.
- Execution: shots, backend settings, or noise affect the observed distribution.
- Post-processing: counts, bitstrings, signs, or objective values are interpreted incorrectly.
Keep a minimal reproduction for every failure. It should include the smallest circuit that demonstrates the issue, the SDK and version used, the simulator or hardware target, the number of shots, and the expected versus observed result. This record makes debugging faster and helps distinguish a code error from normal sampling variation.
Checklist by scenario
1. The circuit returns the wrong bitstring
- Confirm qubit indexing. Check whether the SDK labels qubits from zero and whether diagrams place the lowest-index qubit at the top or bottom. Never infer the displayed bitstring order from the diagram alone.
- Inspect classical-bit mapping. A measurement instruction may map quantum bit 0 to a classical bit other than position 0. Write the mapping explicitly and test it with a circuit that flips only one qubit.
- Check gate order. Quantum operations generally apply in the order represented by the circuit's execution model, but visual layouts and chained code can make the sequence easy to misread. Print or draw the circuit and compare every operation with the algorithm on paper.
- Test one operation at a time. Begin with the initial state, then add each gate or gate group. Record the state or counts after every meaningful step.
A useful probe is a one-hot test: prepare exactly one selected qubit in the |1⟩ state, measure it, and verify which classical bit changes. Repeat this for each qubit used by the program. This quickly exposes reversed wire assumptions and register-order mistakes.
2. Probabilities look plausible but do not match expectation
- Run an ideal state inspection. Use a statevector or equivalent simulator when the circuit is small enough. Compare amplitudes or probabilities before measurement.
- Separate amplitudes from probabilities. Amplitudes can be negative or complex; measurement probabilities are obtained from their squared magnitudes. A phase difference may not appear in computational-basis counts until later gates cause interference.
- Increase or repeat shots. A shot-based result is a sample, not an exact probability table. Compare empirical frequencies with the ideal distribution and use the same shot count when comparing two circuit versions.
- Check parameter values and units. Rotation gates normally take angles in radians unless the framework explicitly states otherwise. Confirm parameter binding, expression evaluation, and the order of parameters.
For state-level checks, compare the circuit with the examples in Quantum State Debugging with Bloch Spheres, Amplitudes, and Entanglement Checks. Use a statevector to inspect an idealized state, not as evidence that hardware will produce the same distribution.
3. Entanglement or interference appears to be missing
- Verify that the control and target qubits are the intended wires.
- Check whether a controlled gate is being applied before the control qubit has the expected value.
- Remove later gates temporarily and measure the intermediate state.
- Test the circuit with a known two-qubit pattern, such as a superposition followed by a controlled operation, before adding algorithm-specific logic.
Remember that measuring too early changes the state and can remove the interference or correlations your later operations require. In a debugging copy, place measurements only at the end unless an intermediate measurement is part of the algorithm.
4. Simulation and hardware disagree
- Match the execution model. Compare ideal statevector simulation, shot-based ideal simulation, noisy simulation, and hardware in separate tests.
- Use the same circuit representation. Transpilation, decomposition, basis-gate conversion, and qubit mapping can alter the physical implementation even when the logical circuit is unchanged.
- Record noise assumptions. A noisy simulator is meaningful only when its noise model and placement match the question being tested.
- Check readout effects. A circuit may prepare the expected state while measurement errors distort the returned counts.
The Quantum Simulator Selection Guide can help you choose between statevector, stabilizer, tensor-network, and shot-based methods. For controlled comparisons, change one execution setting at a time.
5. The circuit is correct but the application result is wrong
- Save raw counts or expectation values before converting them into labels, scores, or objective functions.
- Check whether the application expects a probability, an amplitude, a signed expectation value, or the most frequent bitstring.
- Validate endianness when converting bitstrings to integers.
- Test post-processing with hand-written fixtures, including all-zero, all-one, and a single-set-bit result.
What to double-check
Use a layered diagnostic order
Debug from the least complicated execution mode to the most realistic one:
- Construct the circuit and inspect its text or diagram.
- Run a deterministic basis-state test where possible.
- Inspect an ideal statevector or exact observable.
- Run the ideal circuit with finite shots.
- Add a documented noise model.
- Only then compare with a hardware execution.
This order prevents you from blaming hardware noise for a classical mapping error. It also makes failures reproducible. The simulator type should match the circuit and diagnostic goal; a statevector is useful for amplitudes, while shot-based simulation is better for testing measurement and sampling behavior.
Instrument the workflow
For each run, log the circuit identifier, number of qubits, parameters, transpiled or compiled form when relevant, simulator or backend, shot count, random seed if supported, and raw result. Keep the expected result beside the observed result rather than relying on visual inspection.
Add assertions for small circuits. For example, assert that a circuit with no gates measures the all-zero state in an ideal basis measurement, and that applying an X operation to one selected wire changes only the corresponding logical bit. More advanced tests can compare expectation values within a tolerance instead of requiring identical sampled counts. See Quantum Circuit Unit Testing in Python for patterns involving assertions, golden results, and continuous integration.
Distinguish algorithm errors from noise
First establish a correct ideal result. Then add realistic effects one at a time: finite shots, gate noise, decoherence, readout error, and mitigation or correction. If you apply error mitigation, preserve the unmitigated output as a baseline. The Quantum Noise Models in Python guide and the error mitigation comparison provide useful follow-up paths.
Common mistakes
- Trusting the diagram's visual order. Treat the SDK's documented indexing and returned-result format as authoritative.
- Changing several gates at once. Make one controlled change, rerun the smallest test, and record the result.
- Expecting one sampled run to equal the theoretical distribution. Compare distributions over enough shots and use tolerances in tests.
- Inspecting only the final measurement. Intermediate state or observable checks often reveal the first point where behavior diverges.
- Measuring during a debugging experiment. Measurement is an operation, not a passive read, and can alter subsequent behavior.
- Ignoring parameter binding. Confirm that symbolic values were assigned and that the intended values reached the executed circuit.
- Mixing logical and physical qubits. Compilation may remap qubits. Compare logical results with the correct remapping rather than assuming wire positions stayed fixed.
- Hiding post-processing in a single expression. Keep bit reversal, integer conversion, normalization, and label selection as visible, testable steps.
For circuits involving optimization, also inspect the classical loop. A faulty cost-function sign, parameter ordering, or optimizer update can look like a quantum circuit error. The QAOA in Python guide is a useful example of separating circuit construction, evaluation, and optimization.
When to revisit
Revisit this checklist whenever a circuit fails a test, produces a new distribution, moves from simulation to hardware, or is refactored into a different SDK workflow. It is especially useful before a project milestone, after changing register layouts, and when upgrading a framework or execution provider. APIs for circuit construction, result objects, transpilation, simulator options, and diagnostic output can change, so verify commands against the version used by your project.
Make the checklist part of the repository rather than keeping it as an informal note. Add a short debugging record with:
- the smallest failing circuit;
- the expected state or distribution;
- the observed raw output;
- qubit and classical-bit mappings;
- execution mode, shots, and noise settings;
- the first layer where the result diverges; and
- the test that prevents the regression.
When you are ready to broaden the workflow, use the Quantum Developer Roadmap to connect debugging practice with project-based quantum programming skills. The immediate next step is simple: reproduce the issue with the smallest circuit, verify indexing with a one-hot test, inspect the ideal state, and only then investigate sampling, noise, or application-level interpretation.