Skip to content

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

Draft
ivanbasov wants to merge 2 commits into
mainfrom
refactor/prep-meas-flip-error-helpers
Draft

ivanbasov wants to merge 2 commits into
mainfrom
refactor/prep-meas-flip-error-helpers

Conversation

@ivanbasov

Copy link
Copy Markdown
Member

Stacked on #104: this branch contains its commit. Only the last commit, 4e3d14d (+145/−315), belongs to this PR. Draft until #104 merges; the branch is then rebased onto main.

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 and others added 2 commits November 18, 2026 17:11
The color-code Stim builder and the color-code DEM precompute read
p_prep_X, p_prep_Z, p_meas_X and p_meas_Z by the applied Pauli
(Z-basis reset/measure used the *_X keys), while the surface-code
builder, the config comments and the README name a key by the basis
that fails (Z-basis reset/measure uses the *_Z keys and is applied as
X_ERROR). With a non-uniform noise model the color-code path applied
the X-basis rates to the Z basis and vice versa.

Move the color-code readers to the surface-code convention:
- qec/color_code/memory_circuit.py: add_reset, add_single_error,
  add_measure_reset, add_measure and the data-readout flip injected in
  the final round.
- qec/precompute_dem.py: build_single_p_marginal_color_code (ancilla
  prep/meas, data prep and final data-readout entries).

Models with p_prep_X == p_prep_Z and p_meas_X == p_meas_Z, which
includes every shipped config, produce the same circuits and
probability vectors as before.

Tests: add test_color_code_prep_meas_key_convention.py and update the
two expectations in test_color_augmented_dem_precompute.py that pinned
the previous convention.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Ivan Basov <ibasov@nvidia.com>
…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 requested a review from bmhowe23 November 19, 2026 02:28
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.

1 participant