Skip to content

Factor out the per-basis prep/meas error emission in the circuit builders - #105

Merged
ivanbasov merged 1 commit into
mainfrom
refactor/prep-meas-flip-error-helpers
Nov 20, 2026
Merged

ivanbasov merged 1 commit into
mainfrom
refactor/prep-meas-flip-error-helpers

Conversation

@ivanbasov

@ivanbasov ivanbasov commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Follows #104, which is merged. The branch is rebased onto main and contains one commit (+145/−315).

What

Refactoring only, no behaviour change. The surface-code and color-code Circuit classes repeated the block "emit X_ERROR for a Z-basis failure, Z_ERROR for an X-basis failure" 14 times each, and every site spelled out which data.noise_model key belongs to which basis. The DEM precompute repeated the same mapping as ternaries.

Changes

  • qec/noise_model.py: NoiseModel.spam_probability(kind, basis) is now the single place that maps a prep/meas operation in a basis to its key.
  • qec/surface_code/memory_circuit.py, qec/color_code/memory_circuit.py: Circuit._spam_probability and Circuit._flip_error replace the duplicated blocks in add_reset, add_single_error, add_measure_reset, add_measure and the final-round data-readout flip.
  • qec/precompute_dem.py: build_single_p_marginal and build_single_p_marginal_color_code index the prep/meas probabilities by basis code instead of branching.

For a basis or error type other than X/Z, which no caller passes, the result differs: previously an unparsable circuit line or a silent skip, now a ValueError from spam_probability or an empty string.

Verification

  • Circuit text and DEM probability vectors are byte-identical before and after for 15680 configurations: both codes, scalar and NoiseModel noise (including distinct and zero prep/meas values), d=3 and 5, 1/2/4 rounds, both bases, noisy_init / noisy_meas / add_detectors / add_boundary_detectors, four surface-code rotations, color-code gidney_style_noise / flip_triangle.
  • No existing test is modified. New tests for NoiseModel.spam_probability in test_noise_model.py, including rejected arguments.
  • Local: unittest discover 528 tests, 57 skipped, 1 failure (test_required_model_files_present, pretrained weights not present locally); pytest on the color, precompute, noise-model and torch-setup tests 325 passed, 13 skipped; yapf clean.

The builders run once per configuration, so there is no runtime effect.

🤖 Generated with Claude Code

@ivanbasov
ivanbasov requested a review from bmhowe23 November 19, 2026 02:28
@ivanbasov
ivanbasov force-pushed the refactor/prep-meas-flip-error-helpers branch from 4e3d14d to 0042374 Compare November 19, 2026 23:54
…ders

The surface-code and color-code Circuit classes repeated the same
"emit X_ERROR for a Z-basis failure, Z_ERROR for an X-basis failure"
block 14 times each, once per noise mode and basis, and the mapping
from (operation, basis) to a noise-model key was written out at every
site. The DEM precompute repeated the same mapping as ternaries.

- NoiseModel.spam_probability(kind, basis) is now the single place
  that maps a prep/meas operation in a basis to its key.
- Circuit._spam_probability and Circuit._flip_error (both builders)
  replace the duplicated blocks in add_reset, add_single_error,
  add_measure_reset, add_measure and the final-round data-readout flip.
- build_single_p_marginal and build_single_p_marginal_color_code index
  the prep/meas probabilities by basis code instead of branching.

No behaviour change: circuit text and DEM probability vectors are
byte-identical before and after for 15680 builder/precompute
configurations (both codes, scalar and NoiseModel noise, both bases).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Ivan Basov <ibasov@nvidia.com>
@ivanbasov
ivanbasov force-pushed the refactor/prep-meas-flip-error-helpers branch from 0042374 to 9a13fef Compare November 19, 2026 23:55
@ivanbasov
ivanbasov marked this pull request as ready for review November 19, 2026 23:55
@ivanbasov
ivanbasov merged commit a42994e into main Nov 20, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants