Unexpected quantum-circuit results are usually easier to diagnose when you separate logical errors from measurement, simulator, and hardware effects. This reusable workflow shows how to isolate the problem, build a minimal example, compare execution modes, and decide what to check before changing the algorithm.
Overview
Debugging quantum programming is different from debugging ordinary application code. A classical program can often expose an incorrect value at the line where it was created. A quantum circuit produces a state, applies measurements that may disturb it, and often reports a distribution of sampled outcomes rather than one deterministic answer.
Start by treating every result as the output of a complete execution pipeline:
- Circuit construction: qubits, gates, registers, parameters, and measurement operations are defined.
- Compilation or transpilation: the circuit may be rewritten for a simulator or a particular hardware backend.
- Execution: the circuit runs either once or for multiple shots.
- Readout: results are returned as state amplitudes, probabilities, counts, expectation values, or another data structure.
An error at any stage can look like a problem in the algorithm. For example, an apparently wrong bit string may be caused by reversed bit ordering, while a noisy distribution may be expected when the circuit is executed on hardware. Debugging becomes more reliable when you change one variable at a time and record which execution mode produced each result.
If quantum computing is new to you, it may help to review the foundations in our quantum computing glossary before working through the checklist. The key terms are qubit, gate, measurement, shot, statevector, probability, and basis.
Checklist by scenario
When the circuit returns an unexpected bit string
- Write down the expected state. Do not begin with a vague expectation such as “the circuit should produce one.” State whether you expect a computational-basis state, a probability distribution, or an expectation value.
- Check the initial state. Most circuits begin in the all-zero state, but preparation steps, reused qubits, or notebook state can change that assumption.
- Inspect gate order. Quantum gates generally do not commute. Swapping two operations can change the result even if the same gates are present.
- Check the target qubit for every gate. A gate applied to qubit 1 instead of qubit 0 may be valid code with an entirely different meaning.
- Check measurement mapping. Confirm which classical bit stores each qubit's result and how the framework displays multi-bit strings.
A useful diagnostic is a one- or two-qubit circuit whose answer can be calculated by hand. For example, apply an X gate to one qubit and measure it. If the result is not consistently the expected basis value in an ideal simulator, investigate the circuit and result mapping before testing the larger algorithm.
When counts do not match the expected probabilities
A shot-based run is a sample, not a direct printout of exact probabilities. If a state has probabilities of 75% and 25%, a finite run may produce counts close to those proportions rather than exactly equal to them. Increase the number of shots only after checking that the circuit is correct.
- Confirm that the result is being interpreted as counts divided by total shots.
- Check whether the framework uses a random seed and whether it is being set where you expect.
- Verify that the circuit is measured in the basis relevant to your question.
- Compare the sampled counts with an ideal probability calculation or statevector result.
- Repeat the run to distinguish sampling variation from a systematic error.
For an ideal simulator, a persistent large difference between calculated probabilities and counts usually points to a circuit, measurement, or interpretation issue. On hardware, noise and readout errors are additional explanations.
When a statevector or probability result looks wrong
Statevector output describes amplitudes before measurement in an idealised simulation. It is not the same thing as a list of measurement results. The probability of a basis state is obtained from the squared magnitude of its amplitude, while the amplitude itself can be negative or complex.
Use this sequence:
- Confirm the number of qubits. A statevector for n qubits has a dimension that grows as 2n.
- Check the framework's basis-state and qubit-order convention.
- Inspect amplitudes before adding measurements, if the simulator supports that operation.
- Convert amplitudes to probabilities and check that the probabilities sum approximately to one, allowing for small numerical error.
- Compare the result with a smaller circuit containing only the suspected gate sequence.
Adding a measurement to a circuit can also change what can be inspected. Keep an analysis copy without measurement operations when you need to examine the pre-measurement state, and use a separate execution copy for sampled counts.
When the simulator raises an error
Simulator errors are often caused by a mismatch between the circuit and the requested output method. Common examples include measuring a circuit when a statevector method expects no measurement, requesting an unsupported gate, using an invalid qubit index, or passing parameters that have not been assigned.
Read the error from the first line that identifies your code, not only the final exception message. Then check:
- the installed SDK and simulator versions;
- the backend's supported operations and measurement model;
- the number of qubits and classical bits;
- that all symbolic parameters have values before execution;
- that the circuit object has not been modified unexpectedly in a previous notebook cell.
Framework APIs change over time, so consult the documentation for the version installed in your environment. Keep the failing circuit small enough to run in a clean process and include the exact error message when asking for help.
When the simulator and hardware disagree
First establish whether the comparison is fair. Run the same logical circuit, with the same number of shots, measurement basis, parameter values, and result interpretation. Then separate three possible causes:
- Compilation differences: hardware execution normally requires gate decomposition, qubit mapping, and scheduling.
- Physical noise: gates, idle periods, and measurements are imperfect on real devices.
- Readout effects: the reported classical bit may not reflect the qubit state perfectly.
Inspect the compiled circuit where the platform makes it available. Compare a very small calibration-style circuit first, then add the algorithm's gates gradually. For a practical explanation of fidelity, error rates, and related measures, see our guide to quantum computing benchmarks.
What to double-check
Measurement order and bit-string conventions
Measurement order is one of the most common sources of apparently invalid results. Suppose qubit 0 is measured into classical bit 0 and qubit 1 into classical bit 1. A framework may display the resulting string with the highest-index classical bit on the left. The physical assignments can be correct even when the displayed string appears reversed.
Use an asymmetric test rather than an all-zero or all-one test. Prepare only qubit 0 in the 1 state, measure both qubits, and inspect which displayed position changes. Repeat with only qubit 1. Record the mapping in a comment or test assertion.
# Pseudocode: confirm displayed bit order with asymmetric tests
prepare_qubit(0, state=1)
measure(qubit=0, classical_bit=0)
measure(qubit=1, classical_bit=1)
run(shots=small_number)
# Document which character in the returned string represents each bit
Do not infer ordering from a single result key. Verify it with two separate tests and the framework's documented convention.
Classical-register and result-key handling
Some frameworks return a single bit string; others preserve registers, return arrays, or expose counts through a result object. Confirm whether spaces, register names, or hexadecimal representations are included. A parser written for one result format can silently misinterpret another.
Parameter binding and data types
Variational and quantum machine learning circuits often contain symbolic parameters. Check that every parameter is bound once, in the intended order, and with the expected numeric type. Print the bound values immediately before execution. A list of values in the wrong order can produce a valid but incorrect circuit.
Compilation and optimisation
Compare the original circuit with its compiled form. Optimisation may cancel gates, reorder operations that commute, or decompose an instruction into native operations. These transformations should preserve the intended computation, but inspecting them can reveal an incorrect coupling map, a wrong target backend, or a gate that was not supported as assumed.
Randomness and reproducibility
Save the circuit definition, parameter values, backend name, shot count, SDK version, compilation settings, and any seed or run identifier. Reproducibility does not mean every hardware run will be identical; it means another person can identify which conditions produced the result.
Build a minimal reproducible example
Reduce the failure to the smallest circuit that still shows it. Remove unrelated gates, data loading, visualisation, and application-specific code. A good example contains:
- the environment and package versions;
- the smallest circuit definition;
- the execution method and backend;
- the exact observed output or error;
- the expected output and how it was derived.
This process often identifies the bug before anyone else needs to inspect the code.
Common mistakes
- Changing several things at once. If you change the gate sequence, shots, backend, and result parser together, you cannot tell which change mattered.
- Expecting deterministic output from a superposition. A valid quantum state can produce multiple outcomes. Compare distributions or expectation values rather than one shot.
- Comparing statevector amplitudes with counts. These are different representations and require different interpretations.
- Testing only symmetric states. All-zero and all-one outputs can hide reversed bit order and register-mapping bugs.
- Ignoring implicit measurement. Some execution workflows add measurement or sample from a state on your behalf. Know what the backend actually returns.
- Assuming hardware should match an ideal simulator exactly. Use the simulator as a logical reference, then account for compilation, noise, and readout when moving to hardware.
- Relying on a notebook's hidden state. Restart the kernel and run the minimal example from top to bottom.
- Copying an old API example without checking its version. Quantum developer tools evolve quickly. Verify constructors, backend names, execution primitives, and result accessors against current documentation.
When to revisit
Use this checklist whenever a new circuit produces an unexpected result, but revisit your project-wide debugging notes at predictable points as well. Review them when you change SDK versions, switch simulators, move from local execution to a cloud backend, add a new hardware provider, or introduce a compilation or optimisation pass.
Before a seasonal planning or research cycle, rerun a small validation suite containing:
- an X-gate measurement test for basic qubit and bit mapping;
- a two-qubit entanglement test for gate order and correlation handling;
- a probability test comparing statevector-derived probabilities with shot counts;
- a parameter-binding test with deliberately distinct parameter values;
- a compiled-versus-uncompiled comparison on the intended backend class.
Record the expected behaviour, not just the latest output. When tools or workflows change, update the examples, error messages, and result-access code in your internal notes. For a broader route from fundamentals to practical development, continue with our quantum computing learning path for beginners and explore relevant open-source quantum projects.
The most effective quantum circuit debugging workflow is deliberately ordinary: isolate the smallest failure, verify assumptions with asymmetric tests, compare like with like, and preserve the execution details. Follow those steps before rewriting the algorithm, and many measurement and simulation errors become straightforward engineering problems.