Skip to content

fix: resolve subroutine qubit array arguments through the scope - #446

Open
AJ-comp wants to merge 3 commits into
amazon-braket:mainfrom
AJ-comp:fix/subroutine-qubit-array-arguments
Open

AJ-comp wants to merge 3 commits into
amazon-braket:mainfrom
AJ-comp:fix/subroutine-qubit-array-arguments

Conversation

@AJ-comp

@AJ-comp AJ-comp commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Issue #, if available:

Fixes #445.

Description of changes:

This branch is stacked on #425: it is #425's head (a90bed8) plus one commit, 92ceb7b, which is the change for this issue. Until #425 is merged, the diff against main also contains #425. Both change the gate visitor, and on main this fix would also need the scope fixes for branched programs that #425 makes, so the branch builds on #425 instead of conflicting with it.

Inside a subroutine, qubit operands were looked up by name in qubit_mapping, which holds only the global registers. This change resolves them through the current scope instead.

  • Resolve the qubit operands of measure, reset, barrier, subroutine calls and let through the scope to the qubits they refer to, and compose their indices with the alias target. An argument bound to part of a register (q[1:2], q[{3, 1}]) can be indexed like a register, and an index error names the argument.
  • Resolve gate operands once. Broadcasting and builtin dispatch visited operands that were already resolved, so a gate or subroutine parameter with the name of the operand's register captured it. Control qubits that ctrl @ inserts into a gate body were captured the same way, and now get names that no program can use. The flag for the advanced-features warning, which the second visit set for a set index, is now set where the operand is resolved.
  • Keep a let alias declared in a subroutine local to it. An alias of a register or of another alias refers to the qubits directly. A concatenation gets a register under a name that no program can use, one per scope depth, so nested subroutines do not share it.
  • Index the tuple of qubits instead of a NumPy array when a range or set is measured, so measuredQubits holds ints and GateModelTaskResult.json() works.

Public APIs, the result schema and supported syntax do not change.

Testing done:

  • Linux x86-64, Python 3.13.18. SDK and schemas are installed from their main branches as in tox.ini (1.128.3.dev0 and 1.33.1.dev0).
  • Unit tests: 2277 passed and 71 xfailed, both with the numba JIT enabled and with NUMBA_DISABLE_JIT=1. No xfail markers changed.
  • Integration tests: 307 passed.
  • 22 new tests (57 cases) in test_interpreter.py and test_mcm.py, plus one new case in test_advanced_language_features. On fix: preserve measurement-dependent subroutine return values #425's head, 43 of these 58 cases fail. The other 15 pass there too:
    • five check that undeclared names and classical variables still raise KeyError when used as qubits;
    • one checks that measuring an element through a global alias gives an int;
    • one checks that a set index on a gate operand still triggers the advanced-features warning;
    • five cover qubit arguments named like a register or a classical variable in programs that branch inside the subroutine; fix: preserve measurement-dependent subroutine return values #425 already makes them pass, and four of them fail on main;
    • three check that a qubit argument or register that a block shadows with a classical variable after a mid-circuit measurement refers to its qubits again after the block.
  • Every reproduction in the issue fails on main (bb30184) and on 1.43.2, and gives the expected result on this branch. The same programs pass the OpenQASM analyzer of the Microsoft QDK, and those that measure into a global register give the same results on its simulator.
  • The new and changed lines in interpreter.py and program_context.py are fully covered by unit tests, branches included.
  • ruff check src, ruff format --check on the changed files, and git diff --check pass.

Merge Checklist

General

Tests

  • I have added tests that prove my fix is effective or that my feature works (if appropriate)
  • I have checked that my tests are not configured for a specific region or account (if appropriate)

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

🤖 Generated with Claude Code

https://claude.ai/code/session_01Ard2P3eLBmoqeX6wiw3SYi

AJ-comp and others added 3 commits November 11, 2026 04:09
Inside a subroutine, a qubit argument is a scoped alias of the caller's
qubits, but qubit operands were resolved by looking their name up in
qubit_mapping, which only holds the global registers. Indexing a qubit
array argument therefore failed or acted on the wrong qubits:

- measure, reset and barrier on an element, slice or set of the
  argument raised AttributeError ('Identifier' object has no attribute
  'values'), or acted on a global register with the argument's name;
- passing an element or a slice of the argument to another subroutine,
  or aliasing it with let, raised KeyError.

Resolve such operands through the scope to the qubits they refer to,
and compose their indices with the alias target, so that an argument
bound to part of a register, such as q[1:2] or q[{3, 1}], can be
indexed like a register, and index errors name the alias.

Gate operands are now resolved once. A broadcast gate call was visited
again, and the operands of a builtin gate were visited again before
dispatch, so an operand that was already resolved was looked up again
where a gate or subroutine parameter with the name of its register
shadowed it. Control qubits inserted into a gate body were captured
the same way; they now get names that no program can use.

A let alias declared in a subroutine was added to qubit_mapping under
its own name. qubit_mapping is not scoped, so the alias replaced a
register or alias of the same name from the caller: after the
subroutine returned, that name still referred to the alias's qubits,
and an argument that referred to the caller's alias of the same name
changed inside the subroutine. An alias declared in a subroutine or
a for-loop body now refers to the qubits directly, or, for a
concatenation, to a register under a name that no program can use, one
for each scope depth.

Indexing qubits with a range or a set returned numpy integers, so the
measured qubits of a result could not be serialized
(GateModelTaskResult.json() raised TypeError). Index the tuple of
qubits directly so that they stay ints.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ard2P3eLBmoqeX6wiw3SYi
@AJ-comp
AJ-comp requested a review from a team as a code owner December 4, 2026 10:04

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.

Qubit array subroutine arguments fail to index, and parameters named like registers act on the wrong qubits

2 participants