Repository navigation
Conversation
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
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 againstmainalso contains #425. Both change the gate visitor, and onmainthis 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.measure,reset,barrier, subroutine calls andletthrough 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.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.letalias 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.measuredQubitsholds ints andGateModelTaskResult.json()works.Public APIs, the result schema and supported syntax do not change.
Testing done:
mainbranches as intox.ini(1.128.3.dev0 and 1.33.1.dev0).NUMBA_DISABLE_JIT=1. No xfail markers changed.test_interpreter.pyandtest_mcm.py, plus one new case intest_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:KeyErrorwhen used as qubits;main;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.interpreter.pyandprogram_context.pyare fully covered by unit tests, branches included.ruff check src,ruff format --checkon the changed files, andgit diff --checkpass.Merge Checklist
General
Tests
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