Skip to content

Add support for PREPARE block in stim#3409

Open
joao-boechat wants to merge 9 commits into
mainfrom
joaoboechat/support-prepare-block
Open

Add support for PREPARE block in stim#3409
joao-boechat wants to merge 9 commits into
mainfrom
joaoboechat/support-prepare-block

Conversation

@joao-boechat

@joao-boechat joao-boechat commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Add support for PREPARE blocks in Stim

Summary

Adds a PREPARE { ... } block to the Stim front end: a repeat-until-success region used to deterministically prepare a state that otherwise only occurs probabilistically. This replaces the previous #!preselect_* comment-annotation mechanism.

How it works

  • A PREPARE block runs its body, then re-runs the entire block until every REQUIRE inside it is satisfied.
  • REQUIRE rec[-1] rec[-2] ... reads measurement records (indexed backwards from the most recent measurement) and combines them by XOR (parity). If the parity is 1 the block restarts; if 0 execution continues.
  • A target can be negated (REQUIRE !rec[-1]), which flips that record's contribution to the parity — i.e. it requires the record to be 1 instead of 0.
  • Each block is lowered to a labeled basic block (prepare_N) plus a conditional branch back to it. REQUIRE may only reference records produced inside its own enclosing block; nested blocks are supported.

Modifications

  • qir.rs — lowering for PREPARE/REQUIRE: prepare/continue labels, measurement-record resolution with scope tracking, XOR parity fold, negation (xor … , true), and new diagnostics (RequireOutsidePrepareBlock, MeasurementRecordOutOfBounds, MeasurementRecordOutOfScope).
  • parser.rs — parse PREPARE blocks and REQUIRE; allow negation on measurement-record targets.
  • lex.rs — drop the old #! preselect-annotation lexing.
  • Tests — new qir/tests/prepare_block.rs and parser target tests; removed the obsolete qir/tests/preselect.rs.
  • Samplesamples/notebooks/stim_prepare.ipynb demonstrating preselection, negated requirements, multiple/parity requirements, and nested blocks.

@joao-boechat joao-boechat marked this pull request as ready for review June 30, 2026 22:56
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