QisDebug 1.0: Findings Instead of Replay

8 min read

QisDebug 1.0 cover

A month and a half ago I wrote about QisDebug, a debugger that records everything that happens to a Qiskit circuit and lets you replay it. Then I used it on real code, and it didn’t help me find a single bug. The record was complete, but nothing in it said which event was wrong. Stepping through a few hundred events looking for the odd one out is not much better than reading the code.

QisDebug 1.0 is a rebuild around that lesson. It keeps the recording and changes what you get from it: findings, short statements of what looks wrong, with the numbers behind them and the line of your own Python code that caused it.

One command

You don’t change your code at all:

pip install "qisdebug[gui]"
qisdebug run script.py

QisDebug runs the script, records its circuits as they are built, transpiled and run, and then checks the recording in a separate process. Here is a script that calls compose() and forgets that it returns a new circuit:

qisdebug: 3 circuits recorded, 12 checks run, 1 problem(s), 0 warning(s)
PROBLEM  S1  buggy.py:16  compose() returned a new circuit that is never used; the circuit it was
                          called on is unchanged

Nothing raised, and the counts looked plausible. The circuit that ran just wasn’t the one the code seemed to build.

If you’d rather not use the CLI, QISDEBUG=1 python script.py does the same with your usual command, and qisdebug.start() / qisdebug.end() record part of a script or a notebook.

What it checks

Every recorded gate carries the source line and call path that added it, so the checks can use facts a linter reading the source can’t see: which qubit an index resolved to, which version of a circuit actually ran, and which counts keys your code read. The checks come in four groups:

  • S, structure. A compose() result never used, measure_all() added next to an existing classical register, a gate after a qubit’s final measurement, a classical bit overwritten before it is read, a reset on a qubit still entangled with others, a circuit whose gates never change its state.
  • P, what ran. Running an older version of the circuit than the one you built, gates added after the circuit last ran, a circuit transpiled for a different backend, a transpiled circuit that is not equivalent to its input, and an ideal simulator returning an outcome your circuit cannot produce.
  • C, reading results. Looking up a counts key with no probability while its bit-reversed twin has it all: the classic Qiskit bit-order mistake.
  • E, your expectations. Assertions in your code, a baseline run to compare against, or expected states written by you or a coding agent.

Exact answers come from a shadow simulation of the recorded circuit, outside your program, so the job your backend receives is never changed and nothing extra is sent to hardware.

Every check ends as ok, problem or skipped with a reason. If nothing could be checked, QisDebug says so instead of reporting “no problems”.

When the circuit is valid but wrong

Some bugs are not mistakes in Qiskit at all. A crz where you meant cp is a perfectly good circuit; it just isn’t the one you wanted, and no rule can know that. For those, you tell QisDebug what the state should be:

qisdebug.assert_state(qc, expected, label="phase kickback")
PROBLEM  E1  bug_1.py:36  assert_state 'phase kickback' failed: fidelity with the expected state is
                          0.2134 (needs at least 0.999999)

An assertion costs one check when nothing is recording, so it can stay in your code. You can also write expected states for a few lines in a file, and qisdebug run --expect narrows the failure down to the first line where the circuit departs from them.

Reading a run

The QisDebug window: findings, source and circuit with the state

qisdebug run --gui script.py opens a window with three panes that follow one cursor: the findings, your source, and the circuit with the state after the cursor. Click a finding and the source and circuit jump to it. The arrow keys step through the circuit one instruction at a time, and the state pane shows probabilities with the bit order labelled, amplitudes, and Bloch spheres.

qisdebug run --tui script.py is the same view in the terminal, for an SSH session or a container. qisdebug show, qisdebug diff and --json cover scripts and CI, and pytest --qisdebug records each test into its own run.

Debugging with an agent

The MCP server is now six tools instead of twenty-six: run, list_runs, get_finding, inspect, check_expectation and diff_runs. An agent runs your script, reads the findings, fixes the line, and confirms with a diff of the two runs. When there are no findings but the behaviour is wrong, it writes the state it expects at a few lines and lets QisDebug find the first one that fails. QisDebug never decides what your program is supposed to do; the agent supplies that, and QisDebug supplies the ground truth.

pip install "qisdebug[mcp]"
claude mcp add qisdebug -- qisdebug-mcp --root .

Agents with a shell can skip MCP and use the bundled skill, which teaches the same workflow through the CLI.

Does it find real bugs?

I ran the checks on code I didn’t write, with no expectations given.

  • Bugs4Q collects real Qiskit bugs from GitHub and Stack Exchange, each with a buggy and a fixed version. Five of them are silent (a wrong result, no error) and in reach of a check that doesn’t know the program’s intent. QisDebug catches all five, with no wrong finding on any fixed version.
  • The LintQ dataset has 7,568 Qiskit programs from GitHub. Nobody picked them for having a bug, so they show how noisy the checks are on ordinary code. I ran 2,730 of them in a sandbox; 653 got far enough to be checked, and QisDebug reported 33 findings, which I labelled by hand. Eleven point at a real bug, thirteen are true but harmless, and nine are wrong.

The real bugs were the kind you’d want to know about. A half adder measures its inputs into the classical bits that held the sum and carry, so “The sum is” prints the inputs. A truth table reuses one circuit for every row, so every row after 00 prints the wrong output. A notebook adds measure_all() after the run and then plots the old counts. A Grover oracle resets its helper qubit instead of uncomputing it.

The first pass on the LintQ programs had many more wrong findings than that. Each one became a fix with a test, released as 1.1.1. The full results, labels and the comparison with LintQ’s own labelled warnings are in the evaluation.

Upgrading from 0.2

1.0 breaks the 0.2 API. The QisDebug object, the timeline classes and the breakpoints are gone; in most scripts you just delete the QisDebug lines and run qisdebug run script.py. It also needs Qiskit 2. The migration guide maps every 0.2 name to its replacement, and 0.2 stays available with pip install "qisdebug<1".

Limitations

  • Intent bugs need an expectation. A valid circuit that does something other than what you meant gets no finding until you say what you meant
  • Simulators only for exact checks. Hardware runs are recorded, but the exact comparisons need an ideal simulation of the circuit
  • Dynamic circuits are skipped. Mid-circuit measurements with classical control are reported as skipped: dynamic, not checked
  • No live pause. You inspect the run after it finishes, not while it runs
  • One process. Child processes started by your script are not recorded

Source code

QisDebug is open source under the MIT license, on GitHub at github.com/kriss-u/QisDebug, with documentation at qisdebug.readthedocs.io. Every release is archived on Zenodo with a citable DOI. Issues and bug reports from real scripts are especially welcome: each new check starts from one.

Try it yourself

pip install "qisdebug[gui]"
qisdebug run --gui your_script.py

Related Posts