Import Stim detector error models for Pauli-frame-style sampling#749
Closed
Oishi1029 wants to merge 1 commit into
Closed
Import Stim detector error models for Pauli-frame-style sampling#749Oishi1029 wants to merge 1 commit into
Oishi1029 wants to merge 1 commit into
Conversation
Add read_detector_error_model and parse_detector_error_model, which lower the useful subset of Stim's .dem format (error, detector, logical_observable, shift_detectors, nested repeat blocks, tags, comments) to a circuit of new DetectorError and DemDeclaration operations sampled by the existing pftrajectories machinery. Includes detectorview/observableview accessors, documentation with the expected output shapes, and statistical tests with 5-sigma tolerances and an in-suite falsification power guard.
Contributor
Benchmark Results (Julia v1)Time benchmarks
Memory benchmarks
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #749 +/- ##
==========================================
+ Coverage 74.01% 74.67% +0.66%
==========================================
Files 111 112 +1
Lines 7778 7989 +211
==========================================
+ Hits 5757 5966 +209
- Misses 2021 2023 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Member
|
We do not accept LLM-generated PRs by unknown developers for our bounties. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #728.
What this adds
read_detector_error_model(path)(andparse_detector_error_model(string)/an
IOmethod) import a Stim detector error model and return aDetectorErrorModelCircuit— anAbstractVector{AbstractOperation}accepteddirectly by the existing
pftrajectoriesmethods, per the issue's corerequirement (no separate sampling backend):
Two new operations carry the
.demsemantics, exactly as suggested in the issue:DetectorError(p, detector_bits, logical_bits)— one independent Bernoullimechanism per
error(p)line; when it fires it XORs its measurement bits intothe frame (mechanisms with identical targets stay independent, as in Stim's sampler);
DemDeclaration(n_detectors, n_logicals)— the anticipated no-op declarationwhose
affectedbitsmakespftrajectoriesallocate output columns even fordeclared-but-never-flipped detectors/observables.
Both implement
affectedbits/affectedqubits/apply!forPauliFrameandRegister, so the same imported circuit also runs undermctrajectories(usedin the tests as an independent cross-check of the sampling statistics). Both are
plain concrete structs picked up automatically by the gate-compactification sum
type — no changes to
sumtypes.jl, and no compactification warning.Parser coverage
error(with^separators),detector,logical_observable,shift_detectors, and (nested)repeatblocks; plus instruction tags,coordinate arguments, comments, blank lines, and case-insensitive names.
Malformed/unsupported syntax raises an
ArgumentErrornaming the offending line(23 cases tested). Semantics were matched against Stim's implementation
(
detector_error_model.cc), not just the format doc — in particular:num_detectors= largest mentioned/declared absolute index + 1 (a trailingshift_detectorsdoes not pad),repeat 0is legal, and targets repeatedwithin one instruction cancel by parity while still counting toward model size.
Coordinates and tags are parsed and discarded (the issue marks them optional).
repeatblocks are unrolled at import time; the docs' foldedrepetition-code model (d=4, r=1000) imports to the expected 3003 detectors /
13 000 mechanisms and is part of the test suite.
Tests (
test/test_stim_dem_import.jl)tolerances of 5 standard errors (SE = √(p(1−p)/n), n = 20 000; false-failure
≈ 6e-7 per assertion for any seed — the math is in the test comments).
(per-trajectory
D1 == L0;P(D0 ⊻ D1)), pinning correct mechanism wiring.(0.2 → 0.3) must land outside the acceptance bands (≈26 SE away), proving on
every CI run that the tests can detect a miscalibrated import.
declared-but-unused detectors, implicit declarations, trailing shifts,
repeat 0, nested repeats, parity cancellation, and the empty model.mctrajectories/Registercross-backend statistical check.IOreading, plus error-message tests.Docs
New page "Stim Detector Error Models" (
docs/src/stim_dem.md) with adeterministic imported example and the expected output shape, linked docstrings
for all new names, and a CHANGELOG entry.
Test evidence
Julia 1.12.6, macOS arm64 (M1 Pro). The 9 broken are pre-existing markers; the
new testitem alone passes 119/119. I also verified the statistical tests detect
injected bugs — I broke the implementation three ways (sampling probability
scaled by 0.9, logical-observable columns shifted by one, XOR replaced by
overwrite) and the suite caught each one, with deviations matching the
theoretically predicted magnitudes. (Local note: the PyTesseractDecoder
extension failed to precompile on my machine due to a missing local Python
wheel — unrelated to this change; the suite passed regardless.)
Disclosure: I used Claude (Anthropic) to help draft the implementation and
tests, which I then reviewed, manually verified, and tested locally. Per the
maintainer's note on AI use: I have contributed to open source before, including
during this unitaryHACK (harmoniqs/Legato.jl#27, harmoniqs/Piccolo.jl#238).
unitaryHACK 2026 submission.