Showing posts with label Error Mitigation. Show all posts
Showing posts with label Error Mitigation. Show all posts

Wednesday, November 18, 2026

Quantum Software Part 2: Testing and Debugging Quantum Software

This is Part 2 of a 3-part series on quantum software. You write an automated check, called a unit test, for a quantum program. The test runs a two-qubit circuit 1,000 times (called shots) and expects both qubits to read 0 in exactly 500 of those runs. It fails on almost every run, because the chance of landing on exactly 500 is about 2.5 percent. The program is working correctly and the test is written for the wrong kind of output.

Part 1 explained why the answers vary. This post covers how you test and debug software that behaves this way, and how it adapts to different machines.

A small example uses two qubits. The program links them so they always give matching answers when you measure them, either both 0 or both 1. The circuit takes four lines of Python in Qiskit, an open-source toolkit for writing quantum programs.

First, run it on a perfect simulator, which is software that mimics a quantum computer with no errors. Use 1,000 shots. The first run gave 491 results of both 0 and 509 of both 1. The second run gave 517 and 483. The counts change each run, and the pattern holds: about half and half, with no mismatches. A mismatch would mean one qubit reads 0 and the other reads 1.

Real machines make errors. The next numbers come from a software simulator, not a physical machine, set to assumed error rates of 1 to 3 percent on each operation. Across three runs, mismatches appeared in 6.9, 4.4, and 6.0 percent of shots. In the first run, 69 of 1,000 shots were mismatches. These random errors are called noise, and you can measure them.

Testing works on the pattern. A simple test checks that the qubits match in at least 90 percent of shots. The noisy run passed with 93.1 percent. A test that demands both qubits read 0 in exactly 500 runs fails on nearly every run.

Knowing the answer is right depends on the problem. Some answers are easy to check. If a program proposes two numbers that multiply to make a large number, you multiply them on any computer to confirm. Other circuits give the correct answer the highest odds, as Part 1 showed, so you run many shots and take the most common result.

More shots make that result more reliable. Quadrupling the shots roughly halves the statistical error, which is the random wobble in the counts. On a 50/50 result, 1,000 shots gives a margin of error of about 3 percentage points, and 4,000 shots gives about 1.5. You can also run the program on small versions of the problem that a classical computer can solve, then compare. Passing small cases builds trust and does not prove the large case. A fourth case, a problem with no known answer, comes up in Part 3.

Software can reduce errors. Techniques called error mitigation often run extra versions of your circuit and combine the results into a cleaner estimate. Each extra version requires its own shots, so better accuracy costs more runs. Chapter 12 of the book covers a related effort: AI tools that tune quantum hardware and fix its errors.

Debugging gets harder. In regular code, you add a print statement to see a value. Looking at a qubit means measuring it, which gives you a single value, 0 or 1, and ends that run of the calculation. The no-cloning theorem forbids making a perfect copy to look at instead. Researchers use indirect methods, and those add effort.

Machines also differ. Each one supports only certain operations, and only certain pairs of qubits can interact. A tool called a transpiler rewrites your circuit to fit. IBM's transpiler guide shows a small two-qubit circuit rewritten into a different set of operations for IBM hardware. You write a few operations, and the machine may run a longer sequence of simpler steps to get the same result. In October, I wrote about a faster version of the Qiskit compiler, which IBM reports transpiles two to three times faster on its benchmarks.

Return to the test that demanded both qubits read 0 in exactly 500 runs. A check that the qubits agree in at least 90 percent of shots passes on the same program, and it keeps passing when the counts change.

This is Part 2 of 3. Part 1 covered how quantum computers calculate. Part 3 will cover which problems justify the effort. 

My free book, Quantum from the Ground Up, is in this shared folder.