Skip to content

Testing ​

Tests are standalone Python scripts, not a pytest tree. Each builds an Exam, runs a unittest suite through it, and writes one markdown report page. The harness is test/MDR.py; the continuous-variable assertion helpers are on Question.

Running ​

sh
./do test
./do testcompiles the Rust core in place, runs every test/*.py except MDR.py, and fails if any exam fails. One report page per Exam
The build it uses./do develop, unoptimised and roughly an order of magnitude slower than the release build ./do bench makes and the wheels carry. A report's duration is a debug-build figure
MDR_OUTread once at import in test/MDR.py; default test/_reports (gitignored)
Reports under docs/tests/gitignored; only this page is checked in. A fresh checkout has no report pages, the sidebar lists whichever are present, and ignoreDeadLinks covers /tests/, so a link to a missing report does not fail the build
./do lint --fix on test/MDR.pydo not: its docstrings are copied verbatim into the report tables, so reformatting ships. ./do lint exempts the file

Publish the reports into this site:

sh
MDR_OUT="$(pwd)/docs/tests" ./do test

Report format ​

A # Name heading, the exam description, a pass count and duration, then one row per test:

TestWhat it doesResult
test_vacuum_is_bona_fideVacuum covariance I/2 is physical and saturates the uncertainty bound.✅ pass

The "What it does" column is the test method's docstring, whitespace-collapsed and escaped so that a cell containing |0> does not read as a column break.

MarkerMeaning
✅ passassertion suite completed
❌ failan assertion failed
⚠️ erroran exception other than an assertion escaped
⏭️ skipskipTest, e.g. a GPU-only test on a machine with no adapter

Failures and errors also get a ## Failures section with the formatted exception for each, so a published report is self-contained.

Writing an exam ​

python
import os, sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from MDR import Exam, Question, load

class Channels(Question):
    def test_thermal_loss_stays_physical(self):
        """
        A thermal-loss channel maps bona fide covariances to bona fide covariances.
        """
        ...

if __name__ == "__main__":
    sys.exit(Exam("Channels", "Gaussian channels", "channels.md").run(load(Channels)))
PieceIs
loadunittest.defaultTestLoader.loadTestsFromTestCase
Exam.runa shell exit code; a file with several exams ORs them together
Each examone report file and one page under /tests/, listed in the sidebar by filename

CV assertion helpers ​

Question extends unittest.TestCase with assertions about phase space.

HelperChecksDefault tolerance
assertClose(got, want)scalar equality, |got−want|≤atol1e-13
assertPhysical(V)V symmetric and V+i2Ω⪰0 — the bona fide condition1e-12
assertSymplectic(S)SΩS⊤=Ω1e-12
assertUncertainty(V)per mode, Δx2Δp2≥141e-12
assertNormalized(W, dx, dp)∬Wdxdp=1 on a discretised grid1e-8
assertHusimi(Q)0≤Q≤1/(2π) pointwise, the dxdp bound rather than the d2α one1e-12
gridClose(got, want)two phase-space grids agree pointwise (max abs error)1e-9
samplesClose(samples, expected)empirical outcome frequencies match a probability map; for PNR and sign-binned homodyne0.03
momentsClose(samples, mean, var)continuous samples (e.g. homodyne quadratures) match first and second moments0.05

omega(n) in MDR.py builds the symplectic form for n modes in xpxp ordering; assertPhysical and assertSymplectic are written against it.

HelperExercised over
assertPhysicalevery Gaussian constructor and channel image
gridClose, assertNormalizedthe Wigner and Husimi grids of both state layers
momentsClose, samplesClosethe homodyne, heterodyne and click samplers

Why assertPhysical is the important one ​

A covariance matrix can be symmetric and positive-definite and describe no physical state (the bona fide condition). A thermal-loss channel that mixes noise in at the wrong plane still returns a symmetric, positive-definite matrix. Every channel and symplectic map runs through assertPhysical, and a negative control keeps the check from being vacuous:

python
def test_unphysical_covariance_is_rejected(self):
    V = np.eye(2) * 0.1  # below vacuum in both quadratures
    with self.assertRaises(AssertionError):
        self.assertPhysical(V, msg="sub-vacuum covariance must be rejected")

Conventions are tested, not documented ​

The phase-space convention is global, so no local test would catch a change to it. The Conventions exam pins the contract.

Current exams ​

One source file per layer, one exam per question about it; each report opens with its exam's description. grep -n 'Exam(' test/*.py maps reports back to source files.

Three entries check the harness or the toolchain rather than the physics.

ExamChecks
test_version in SmokeCargo.toml's [package] version against qkd.__version__, which comes through PyO3 from CARGO_PKG_VERSION; a stale compiled _core fails at once
test_absent_gpu_is_not_an_error in GPU fallbackskips rather than fails on a machine that has an adapter; the skip shows as ⏭️ in the report
ApiScopethe refusals: every configuration this tree cannot compute must raise, naming the restriction