Skip to content

fix: walk loop and switch bodies in module statement passes - #360

Open
ryanhill1 wants to merge 2 commits into
mainfrom
fix-walker-loop-switch
Open

fix: walk loop and switch bodies in module statement passes#360
ryanhill1 wants to merge 2 commits into
mainfrom
fix-walker-loop-switch

Conversation

@ryanhill1

@ryanhill1 ryanhill1 commented Aug 7, 2026

Copy link
Copy Markdown
Member

Fixes #354

has_measurements() / remove_measurements() / has_barriers() / remove_barriers() fall back to self._statements when the module has not been unrolled, and the statement walker did not descend into for / while / switch bodies — so an occurrence inside one was invisible and removal was a no-op.

iter_quantum_statements and drop_statements (the walker #345 introduced for box/if) now descend into loop blocks and switch cases/default. The unrolled path is unchanged: loops and switches are fully expanded by unroll() and never reach _unrolled_ast.

Also in this PR:

  • Adds regression coverage ensuring barrier removal preserves unrelated operations inside loop bodies.
  • Adds while-loop coverage for detecting and removing measurements in nested statements.
  • Updates the changelog with the nested loop and switch statement traversal fix.
Nested Statement Removal Flow
sequenceDiagram
  participant BarrierTests as tests/qasm3/test_barrier.py
  participant MeasurementTests as tests/qasm3/test_measurement.py
  participant Module as src/pyqasm/modules/base.py ⚠️
  participant Walker as iter_quantum_statements() ⚠️
  participant Dropper as drop_statements() ⚠️
  participant Changelog as CHANGELOG.md
  BarrierTests->>Module: remove_barriers()
  MeasurementTests->>Module: remove_measurements()
  Module->>Walker: find nested statements
  Module->>Dropper: remove matching statements
  Walker->>Walker: descend into loops and switch cases
  Dropper->>Dropper: descend into loops and switch cases
  Module-->>BarrierTests: preserve unrelated operations
  Module-->>MeasurementTests: remove nested measurements
  Changelog-->>Module: documents traversal update
Loading

Auto-enriched by Argus

has_measurements(), remove_measurements(), has_barriers() and
remove_barriers() fall back to _statements when the module has not been
unrolled, and that list can still hold for/while/switch bodies the
walker did not descend into — so an occurrence inside a loop or switch
was invisible and removal was a no-op. iter_quantum_statements and
drop_statements now descend into loop blocks and switch cases.

Fixes #354
@ryanhill1
ryanhill1 requested a review from TheGupta2012 as a code owner August 7, 2026 13:14
@argus-eye

argus-eye Bot commented Aug 7, 2026

Copy link
Copy Markdown

Argus review

Auto-review is off for this repo. Tick the box below to run a review on this PR.

  • Trigger Argus review

Estimated cost

  • Files changed: 4
  • Diff lines (±): 93
  • Historical avg: ~318.9k tokens · ~$1.35 · across last 6 review(s)

Tip: you can also comment @argus-eye review at any time.

@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: c2da9456-b108-4af0-b269-1661445294b2

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.71429% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/pyqasm/modules/base.py 85.71% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@ryanhill1

Copy link
Copy Markdown
Member Author

@Argus-Eye review

@argus-eye

This comment has been minimized.

@argus-eye argus-eye Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔎 Argus · 10/10 — Correctly extends statement passes into loop and switch bodies

🔍 PR intent vs diff (LLM analysis)

Argus read the diff against the stated intent. This is not an execution log — reviewer still needs to test behavior.

Goal: Make module statement passes detect and remove measurements and barriers inside for, while, and switch bodies before unrolling.
Not in scope:

  • The unrolled path is unchanged; loops and switches are fully expanded by unroll() and do not reach _unrolled_ast with their bodies intact.
    Stated acceptance criteria (from PR/issue — not independently verified):
  • iter_quantum_statements descends into for and while loop blocks and switch cases/default.
  • drop_statements descends into for and while loop blocks and switch cases/default.
  • has_measurements(), remove_measurements(), has_barriers(), and remove_barriers() handle occurrences inside non-unrolled loop and switch bodies.

✅ Intent delivered

Verdict: This PR adds the intended traversal and removal support for measurements and barriers in non-unrolled loop and switch bodies. It is ready to merge.

💡 2 P2 · 4 files reviewed

Architecture: The recursive traversal keeps module-level statement passes consistent with nested AST structure; broader nested-control-flow coverage would further protect this behavior.

2 findings · 2 inline · 0 folded

🔢 118.9k tokens · $0.2737 total
Stage Tokens Cost
Intent 2.6k $0.0000
Triage 2.8k $0.0000
Lead agent 1.5k $0.0000
Review · bug_hunter 20.9k $0.0674
Review · security 21.0k $0.0665
Review · architecture 20.8k $0.0707
Review · regression 21.1k $0.0692
Review 24.9k $0.0000
Acceptance 1.2k $0.0000
Scoring 1.0k $0.0000
Synthesis 932 $0.0000

Contract: production/full · checked: bug_hunter, security, architecture, regression · review took 1m32s

Dashboard → · React 👎 to dismiss · Reply to any inline comment or use @argus-eye help to chat

Comment thread tests/qasm3/test_measurement.py
Comment thread tests/qasm3/test_barrier.py
- while-loop measurement removal now asserted, not just detection
- barrier removal test asserts the loop's gates survive the pass
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.

has_measurements/has_barriers miss loop and switch bodies before unroll()

2 participants