Skip to content

Support bit registers, arrays and index expressions as for-loop iterables (#393) - #406

Draft
TheGupta2012 wants to merge 2 commits into
feature-385-391-bit-repr-negative-indicesfrom
feature-393-for-loop-iterables
Draft

Support bit registers, arrays and index expressions as for-loop iterables (#393)#406
TheGupta2012 wants to merge 2 commits into
feature-385-391-bit-repr-negative-indicesfrom
feature-393-for-loop-iterables

Conversation

@TheGupta2012

@TheGupta2012 TheGupta2012 commented Aug 24, 2026

Copy link
Copy Markdown
Member

Closes #393.

Problem

Qasm3Visitor._visit_forin_loop branched on the concrete type of set_declaration and fell through for anything that was not a DiscreteSet or a RangeDefinition. The spec also allows a bit[n] value, a one-dimensional array[<scalar>, n], and an index expression arriving at either.

Approach

The branch is factored into _resolve_loop_iterable, which returns the plain list of values plus the loop-variable initializer. The unrolling loop below is unchanged.

For an Identifier or IndexExpression, the expression is evaluated once and the result dispatched on type: a BitValue yields its bits in b[0], b[1], ... order (bit 0 is the leftmost bitstring character); a one-dimensional ndarray yields tolist(). tolist() produces native Python scalars, so the loop variable is bound by copy and a write to it cannot reach the source array. Anything else — a scalar, a BitstringLiteral — still raises the original "Unexpected type ... of set_declaration in loop." error. The existing loop-limit check now covers the new forms too.

Tests

tests/qasm3/test_loop.py: a bit[n] register asserting MSB-first order, a bit loop variable over a register, a one-dimensional array, an index-expression iterable (a[1:2], inclusive), a write to the loop variable asserting the source array is unchanged, a multi-dimensional array raising ValidationError, and a scalar index expression raising the unsupported-type error.

Gap

A loop variable of type bit inherits the pre-existing unroll behavior: each iteration emits its own bit[1] x; declaration, exactly as for bit x in {0, 1} does today. That is unchanged by this PR.

Classical let aliases (case 3 of the issue) are not implemented. let c = b; on a classical register still fails at the alias statement itself, before any loop is reached, because _visit_alias_statement only resolves quantum registers. That work is tracked in #392.

Stacked on #404 (feature-385-391-bit-repr-negative-indices); rebases onto main once #404 merges.

🤖 Generated with Claude Code

…bles (#393)

`Qasm3Visitor._visit_forin_loop` branched on the concrete type of
`set_declaration` and rejected anything that was not a `DiscreteSet` or a
`RangeDefinition`. Factor that branch into `_resolve_loop_iterable`, which
returns the plain list of values to iterate and the loop-variable
initializer, leaving the unrolling loop unchanged.

The new path evaluates the iterable expression once and dispatches on the
result: a `BitValue` yields its bits in `b[0]`, `b[1]`, ... order, and a
one-dimensional `ndarray` yields `tolist()`. That covers a `bit[n]`
register, a one-dimensional `array[<scalar>, n]`, and any index expression
arriving at either. A multi-dimensional array raises `ValidationError`, per
the spec's one-dimension restriction, and `tolist()` copies into native
Python scalars so a write to the loop variable cannot reach the source.

Classical `let` aliases are still unsupported; they fail at the alias
statement itself and are tracked in #392.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@argus-eye

argus-eye Bot commented Aug 24, 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: 3
  • Diff lines (±): 221
  • Historical avg: ~243.6k tokens · ~$0.95 · across last 10 review(s)

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

@coderabbitai

coderabbitai Bot commented Aug 24, 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: 1c168ea6-f8f2-4b7c-bdc0-41cf0812069a

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.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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