fix: resolve measurement destination index before branching#391
Open
AJ-comp wants to merge 1 commit into
Open
Conversation
A measurement whose classical destination is indexed (`b[i] = measure q[j]`) crashed with `AttributeError: 'Identifier' object has no attribute 'value'` whenever a later condition promoted the deferred measurement to branched execution. The interpreter already resolves the destination index and passes it to `add_measure` as `classical_targets`, and `_branch_measurement` receives it — but it was not forwarded to `_update_classical_from_measurement`, so `_update_indexed_target` re-derived the index from the raw AST via `_resolve_index`, which assumed an `IntegerLiteral`. Lazy resolution cannot work in general: a `for` loop variable is already out of scope by the time the pending measurement is replayed. Forward `classical_targets` and use it. This matches the non-branched path, which already destructures the destination away and uses the resolved indices, and it makes branched execution agree with non-branched execution for `const`, identifier and range destinations. `_resolve_index` had no other caller and is removed. Iterating the resolved indices alongside the measured qubits also fixes range destinations (`b[0:1] = measure q[0:1]`), which previously raised `'RangeDefinition' object has no attribute 'value'`. Removes the xfail from test_5_2_for_loop_operations_with_branching, which this fixes.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #391 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 49 49
Lines 4927 4924 -3
Branches 569 570 +1
=========================================
- Hits 4927 4924 -3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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 #390.
Description of changes:
b[i] = measure q[j]crashes withAttributeError: 'Identifier' object has no attribute 'value'once a later condition promotes the deferred measurement to branched execution. A literal index works;const, identifier and range destinations do not.The index is already resolved before it is needed. It is then dropped, and recomputed by reading
.valueoff the raw AST node — which only anIntegerLiteralhas.This is not a scoping problem:
const int k = 0is still perfectly in scope when the crash happens — it is simply never looked up._resolve_indexreads the attribute straight off the node.Resolving it lazily at replay time would not be a general fix either, though:
const int k = 0forloop variableiKeyError: 'Undefined key: i', the loop has exitedSo the fix is to forward
classical_targetsand use it. That is what the non-branched path already does:_flush_pending_mcm_targetsdiscards the destination (_mcm_dest) and uses the resolved indices. This makes branched execution agree with non-branched execution.Zipping the resolved indices with the measured qubits also fixes range destinations (
b[0:1] = measure q[0:1]), previously'RangeDefinition' object has no attribute 'value'._resolve_indexhad no other caller and is removed.Why this is safe:
_update_indexed_targetis only reached when the destination is anIndexedIdentifier, andinterpreter.pyalways populatesclassical_targetsin that case. The one caller that passes a destination without targets —_handle_measure_ff— uses a plainIdentifier, which routes to_update_identifier_targetinstead.Testing done:
toxpasses — 1188 unit tests, linters clean.Branched execution now matches non-branched:
r[0]literal'1''1''1'r[k],const int k = 0'1''1'r[i],for int i in [0:1]'10''10'r[0:1] = measure q'10''10'Added
test_branched_measurement_into_indexed_destination(literal /const/ non-constidentifier),test_branched_measurement_into_loop_variable_indexandtest_branched_measurement_into_range_destination. Four of the five fail against the unpatched source; the literal row is the regression guard and passes either way.Feed-forward works end to end rather than merely not crashing: with the branch made deterministic,
r[k] = measure q[0]; if (r[0] == true) { x q[1]; }gives{'11': 10}.Also removes the
xfailfromtest_5_2_for_loop_operations_with_branching("Interpreter gap: branched condition BinaryExpression not fully resolved"), which this fixes.Out of scope:
r[1-1] = measure q[0]fails on the branched and non-branched paths, becauseinterpreter.pynever evaluates a destination index that is aBinaryExpression. That is a separate gap, and the two paths agree there.Merge Checklist
Put an
xin the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your pull request.General
Tests
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.