Verifying and benchmarking cuqml
How a change is verified, and how a speed is measured: when to run the checks, who runs them,
how the result is judged and what is recorded. The verification commands are in A4; the
benchmark commands are in benchmarks/README.md. What a change owes before it gets here is in
development.md.
Part A is written to be handed, alone, to whoever verifies a change.
Part A. Verification
A1. When to run
After any change to code:
cuqml/,verify/, the Dockerfile or the pinned versions.Before a handoff that calls the change verified.
Always the whole set, not only the script written for the change (
CLAUDE.md/AGENTS.md§7). The set is the 14 scripts of A4.verify/qkan/cuqml_solver.pyneeds an image of its own, so it is reported separately, as run or not run.
A change to markdown alone needs no run. Check instead that its links resolve and that it retypes no number.
A2. Who runs it
Someone who did not write the change: a freshly started agent, or another person.
If you are the agent that wrote the change, do not run Part A yourself: spawn another agent to run it. Give that agent this file and the commit to verify, and nothing else: not the conversation that produced the change, and not the author’s reasoning. The code is then written in one context and verified in another, so the verifier cannot inherit the author’s assumptions. If you are the agent that was spawned, you are the verifier: carry on from A3.
The verifier changes nothing. It runs, judges and reports.
A3. Setup
A CUDA GPU, and the versions
README.md› Install lists. Results on other versions are reported as such.A clean checkout of the commit under test:
git statusshows nothing modified.pip install -e ., thenpython -c "import cuqml". The first import builds the four plugins and installs them, with the two target files, into the CUDA-Q installation. Where that tree is not writable, as in the stock image, it uses password-free sudo,sudo -n. Where CUDA-Q came from a wheel that ships no NVQIR headers, as from 0.16 on, it fetches them once into~/.cache/cuqml, which needs network access.In anything you write yourself,
import cuqmlbeforeimport cudaq.
A4. The scripts
Every script that checks a result checks it against CUDA-Q’s own simulator — parity.py
against an independent reference — never against a hand-written expectation, so a convention
that changes underneath fails a script instead of quietly producing wrong numbers.
build_check.py builds, and measure.py times. The Reference and Note columns of the table below
say where a script departs from that.
python verify/build_check.py # the kernels build and run here; the targets rebuild when stale
python verify/gate_support.py # every gate, amplitude-for-amplitude vs qpp-cpu
python verify/verify_api.py # sample / observe / get_state / run
python verify/sampling.py # sample_batch vs cudaq.sample, exactly and statistically
python verify/verify_backend_selection.py # the fp32 state-vector kernels ran, with zero fallback
python verify/equivalence.py # observe and get_state vs CUDA-Q, row by row
python verify/gradients.py # both diff_methods vs cudaq.gradients
python verify/parity.py # each kernel vs an independent reference
python verify/measure.py # forward-pass timings, medians and spread
python verify/large_batch.py # a batch past the launch limit, as a small one
python verify/arguments.py # arguments and captures, as cudaq.observe takes them
python verify/angles.py # computed angles vs the ones CUDA-Q computes
python verify/fp64.py # the fp64 target, every surface vs qpp-cpu and nvidia
python verify/tensornet.py # cuqml-tensornet's expectations, gradients, shots vs CUDA-Q
Run build_check.py first: it fails fast on a broken toolchain. The rest run in any order.
Script |
What it checks |
Reference |
Bar |
Note |
|---|---|---|---|---|
|
The kernels build, and one runs. The targets, each import in a process of its own: current, |
A fixed value: |
1e-6, in the script |
A build check, not a check of results. The verdict says how many of the eight target checks were judged. The last is not judged where this user can write the tree, as root can; any other not judged fails the run. Each target check leaves the targets current |
|
Gate constructs on |
|
|
A construct the target refuses is printed as unsupported and does not fail the run, except on the control-order rows and the eleven custom-operation name kernels, which are not rows of the table; five of those run in a process of their own. Two measurement rows are only run, not compared |
|
|
|
|
Runs on |
|
|
|
|
|
|
That the cuqml kernels ran, and that no dispatcher reached no kernel |
A count of calls, no reference |
none |
Covers the fp32 state-vector build only, one of the four |
|
|
|
|
|
|
Both |
|
|
The loosest bar of the set |
|
The dense, controlled, diagonal, rotation, rotation-gradient and Pauli primitives |
A plain torch contraction that shares no code with the kernel |
|
The one script that takes no reference from CUDA-Q. The fused-layer, sweep and contraction kernels are not in it |
|
Times the forward pass |
none |
none |
A timing, not a check: it judges nothing |
|
A batch past the launch limit, at the seams between slices |
|
|
|
|
Kernel arguments passed as |
|
|
Two of the captured-variable checks read the engine’s program store by count and identity, and one of those replaces the engine’s |
|
Angles a kernel computes, through loops of 50 to 1,500 turns; angles it rounds to single precision; kernels whose circuit follows an argument’s value; a called kernel undone by |
|
|
Seven checks script capture’s probe generator, each with a row that says the scripted draw took place; one scripts the IR’s reading, and three count the engine’s reads of the IR on first calls |
|
The fp64 state-vector target, every surface |
|
|
|
|
|
|
|
Each named bar is a constant at the top of its script; the script is where it is defined, and this table only names it. Some checks carry a literal of their own inside the check.
Two things sit beside the set and do not replace it.
verify/qkan/cuqml_solver.py checks QKAN’s layers on cuqml against CUDA-Q; QKAN is not a
dependency of this repo, so it runs in the image verify/qkan/Dockerfile builds.
docs/src/sample.py is the one run behind the generated page.
A5. What counts as a pass
Every script ends with exit status 0.
None was skipped, and none was stopped early.
No bar was edited, and no check was removed or switched off.
git statusis still clean afterwards.
A pass establishes what the scripts examine and no more. The Note column says where a script examines less than its name suggests.
A6. The report
One row in the Verification runs table of status.md:
the commit, by its full hash;
the date;
the machine: GPU, CUDA-Q version, torch version;
which scripts ran;
the result: pass, or the script that failed with its last lines;
what was not run, and why.
“Not verified” is a valid report. A pass that was not observed is not.
A7. When something fails
Say which of four things it is, because each is handled differently:
The failure is in |
Signs |
What happens |
|---|---|---|
The environment |
The build fails; memory is taken by another process; versions differ from the pinned ones |
Fix the environment and run again. Say so in the report |
The check |
The check asserts something CUDA-Q does not do, or cannot tell right from wrong |
Report it. The check is fixed in place with the reason beside it (development.md §3), as a change of its own |
The implementation |
cuqml disagrees with CUDA-Q on the same input |
Report it to the author. The verifier does not fix it |
The specification |
It is unclear what the right answer is, or CUDA-Q itself looks wrong |
Stop and ask the human ( |
A bar is never loosened and a check never removed to turn a failure into a pass.
A8. Cleanup
A run leaves nothing that git tracks: git status stays clean. In the tree it leaves files
git ignores: cuqml.egg-info/ and __pycache__/. Outside the tree it leaves:
the four plugins, two target files and the build’s stamp in the CUDA-Q installation, which the next build replaces; there is no uninstall step;
the editable install of
cuqml;the kernels’ compiled extension in torch’s extension cache, and under
~/.cache/cuqmlup to two symlinks and, where CUDA-Q came from a wheel, the NVQIR headers fetched for it.
Release the GPU when you are done.
A9. Writing a new check
Compare with CUDA-Q’s own answer for the same input: the call that means the same thing (
CLAUDE.md/AGENTS.md§5), at the same precision or, as the fp32 scripts do, against a more precise reference with the bar at fp32’s floor. Never a value written by hand.Take the reference in the same run, and see first that it can tell right from wrong: a basis state that reads the same backwards cannot catch a reversed bit order.
Use the script’s bar. A new bar needs its reason beside it.
Check the refusal as well as the answer: what cannot be honoured is refused by name.
Cover the edges: each target and precision the feature exists on, a batch of one, and the seam wherever work is done in slices.
Exit non-zero on failure, and print what was compared and by how much.
Say in the commit message which checks fail without the change.
List the script, with its command and its row, in A4.
Part B. Benchmarks
B1. When a benchmark is needed
A claim about speed is being made.
The engine changed in a way that could move published numbers. Until they are measured again, the published tables describe the earlier code: say so in status.md.
B2. The method
benchmarks/_bench.py implements the timing, the bar, the MISMATCH cell and the output;
benchmarks/README.md › Reading the numbers states the rest. Every sweep follows it unless
B3 says otherwise.
Results before timings. A sweep computes both sides’ values and prints
MISMATCHin place of a speedup when they disagree by more than its bar.The baseline is CUDA-Q’s own:
nvidiaortensornet, or cuTensorNet, the library CUDA-Q ships. Both sides run at the same precision, selected the same way.Five trials, each after three warm-up calls. The median is reported, with the ratio of slowest to fastest of cuqml’s trials beside it as
spread. The GPU is synchronised around the timed region.CUDA-Q has no batch axis, so above one row its time is one row’s, multiplied by the batch.
In the two tensor-network sweeps, a call slower than two seconds is timed once, after a first call.
A cell that does not fit is refused before it runs and printed as
>VRAM.A quiet GPU. Whoever runs a sweep starts it once the GPU has held no other process for five minutes, and keeps the run only if none appeared. No script checks this.
One run makes the table, the CSV file and the figure.
Losing cells stay in the table (
CLAUDE.md/AGENTS.md§5).
B3. The sweeps
Sweep |
Measures |
Baseline |
Bar |
Precisions |
Departs from B2 |
|---|---|---|---|---|---|
|
|
|
Its own: 2e-4 at fp32, 1e-10 at fp64 |
fp32, fp64 |
A looser fp32 bar than the shared one |
|
The gradient at one parameter vector |
|
Shared: 1e-4 at fp32, 1e-10 at fp64 |
fp32, fp64 |
|
|
The gradient over a batch |
|
Shared |
fp32, fp64 |
|
|
|
The same layer written by hand on |
Shared |
fp32, fp64 |
PennyLane is optional and not a dependency |
|
Kernels as CUDA-Q writes them, beside a rewrite |
|
Its own: 1e-4, relative to the largest magnitude compared |
fp32 only |
No |
|
|
CUDA-Q’s |
Its own: 1e-5 at fp32, 1e-10 at fp64, scaled by the observable’s coefficients; shots judged by |
fp32, fp64 |
First and second calls, and |
|
The same networks on cuTensorNet itself |
cuTensorNet, checked against |
|
fp32, fp64 |
cuqml’s first call is timed once |
One departure is common to all seven: no sweep writes the commit, the library versions or the
date into its output. _bench.header() prints the GPU, the trial count, the precision and the
two targets.
B4. How numbers reach the READMEs
A published table is the CSV file of one run, and a figure is the one that run drew (
--csv,--png). No number is retyped (CLAUDE.md/AGENTS.md§5).Record the machine and the date with them, in
benchmarks/README.md› Reading the numbers. Record the commit that was measured in status.md, not in a README: the sweeps do not print it, so take it yourself withgit rev-parse HEADbefore the run.Update the tables and the Summary section of
benchmarks/README.mdin the same change.Quote a number with its cell: its width, its batch and its sweep.
B5. Adding a sweep
Build it from
_bench.py: its options, its timing, itsMISMATCHcell, its table and figure output.Compare results as well as timings, with the shared bar unless a reason is written beside another.
Measure generic scenarios, not the one configuration a change was tuned on (
CLAUDE.md/AGENTS.md§5).Keep the scenario’s name in
benchmarks/: no workload name enterscuqml/(CLAUDE.md/AGENTS.md§4).Add its commands and tables to
benchmarks/README.md, and its row to B3.