Skip to content

fix: preserve shots across measurement-conditioned loop control - #426

Open
AJ-comp wants to merge 5 commits into
amazon-braket:mainfrom
AJ-comp:codex/fix-measurement-loop-shots
Open

AJ-comp wants to merge 5 commits into
amazon-braket:mainfrom
AJ-comp:codex/fix-measurement-loop-shots

Conversation

@AJ-comp

@AJ-comp AJ-comp commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Issue #, if available:

Fixes #424.

Description of changes:

The measurement-conditioned break in #424 exits the Python traversal of a branch, skipping other simulation paths and silently dropping their shots. A loop-control transfer must instead apply only to the paths that execute it, and only to their nearest enclosing loop.

  • Suspend broken and continued paths in the current loop. Broken paths rejoin after that loop; continued paths rejoin at the next iteration.
  • Stop executing the remaining body on suspended paths while allowing other branches to finish.
  • Unwind branch/loop frames and preserve newly created descendant paths when iteration generators finish or close.
  • Preserve each initial path's evaluated for-range across subsequent measurement branches.
  • Recheck while-condition dependencies at iteration boundaries so a condition that becomes path-dependent is evaluated separately for each path.
  • Include the left-hand side in compound-assignment dependencies, and read scoped classical values from their current path rather than stale shared-table entries.

The exact issue repro is covered for both state-vector and density-matrix simulators, alongside the equivalent direct-condition loop as a control.

Testing done:

  • Windows, Python 3.12.9; SDK 1.127.8 and schemas 1.32.2; based on upstream bfefc3c6264de4171f9ac341dbf08dacba19b4c7.
  • Full unit + integration collections: 2,489 passed, 71 existing xfailed (NUMBA_DISABLE_JIT=1; no xfail markers changed).
  • 28 new tests cover exact SV/DM reproductions, output counts and correlations, nested loop ownership, descendant paths, generator/frame cleanup, static-to-path-dependent while transitions, and condition evaluation.
  • Before the fix, 16 of the first 18 regressions fail on the base revision, while the two direct-condition controls pass. The exact issue repro changes from incomplete output to all requested shots returning r=1.
  • Existing full condition support is retained through promotion: sqrt, sizeof, classical subroutines, and function calls inside index expressions. Those four controls pass on both the base revision and this patch; evaluator calls are also checked per path/per condition check.
  • Re-ran the loop-control/context test selections with JIT enabled: 118 passed. Additional independent black-box loop probes: 8/8 passed; independent SV/DM transition/cleanup/condition probes also pass.
  • Full-source Ruff, formatting of all changed files, and git diff --check pass. Sphinx HTML builds successfully; its existing warning output matches the base revision.

Integration update: depends on #425

This branch now includes #425 through a non-rewriting merge. The base remains main, so until #425 is merged the full diff also contains its return-value work. The independent-branch results above describe the original 0a50d8e revision; the following results describe the updated branch.

Resolving the overlapping interpreter/context changes required preserving both function-call preparation and path-local loop control. In particular, compound assignments are expanded before preparing calls, while conditions retain their prepared-call refresh, and break/continue remain local to each simulation path.

The combination also exposed a scope-cleanup bug: with an outer i = 99, a static for int i and a measurement-conditioned break, suspended paths retained the loop-local i and an extra frame. Scope entry now records the current loop's suspended-path list boundaries. Scope exit unwinds active paths and only paths newly suspended inside that scope; previously suspended paths are not unwound twice.

  • Updated branch: 2,555 passed, 71 existing xfailed, using the full unit and integration collections. No failures were relabeled as xfail or skipped.
  • Added 20 SV/DM cross-regressions to the ordinary CI unit-test collection, covering outer-variable restoration, nested loops, break/continue, function-based while conditions, and subroutine returns after loop control.
  • Source Ruff, changed-file formatting, and git diff --check pass. A full-tree formatting check reports the unchanged upstream blank-line issue in test_state_vector_simulator.py; it is not modified here.

The standard readonly array grammar fix in #428 is independent of this branch and is not included in this PR. With that parser fix also merged locally, full Braket tests pass (2,594 passed, 71 existing xfailed), as do 103 focused tests with JIT explicitly enabled. All 39 Qora compiler-to-LocalSimulator execution cases and all 39 checks through the actual extension JSON runner pass without modifying compiler-generated QASM. Qora's .NET tests also pass (1,034 + 13). These are combined local-integration results, not standalone #426 or remote-QPU results.

The combined Sphinx build exits successfully. Its 100 WARNING and four existing docstring-indentation ERROR diagnostics match the earlier return/loop builds after path normalization; there are no new documentation diagnostics.

Merge Checklist

General

  • I have read the CONTRIBUTING doc
  • I used the commit message format described in CONTRIBUTING
  • I have updated internal control-flow docstrings. No public API or result-schema changes are introduced.

Tests

  • I have added regressions for shot conservation, path-specific results, break/continue, nested loops, and frame cleanup, and strengthened existing output-count assertions.
  • Tests do not require an AWS region, account, or credentials.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@AJ-comp
AJ-comp requested a review from a team as a code owner November 10, 2026 19:11
… verification

Resolve overlapping expression preparation and loop routing changes from PRs 425 and 426. Preserve both sets of regression tests.

Also unwind newly suspended break/continue paths when the combined lexical scope closes. Scope-entry offsets avoid touching paths that exited in earlier iterations. Keep 20 local cross-PR verification cases outside the upstream test suite.

This branch has not been deployed

No deployments
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.

Measurement-conditioned break in while(true) silently drops output shots

2 participants