-
Notifications
You must be signed in to change notification settings - Fork 28
fix: walk nested box and if bodies in module transformation passes #345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -628,11 +628,13 @@ def _visit_measurement( # pylint: disable=too-many-locals,too-many-branches,too | |||||||||||||||||||
| ) | ||||||||||||||||||||
| ) | ||||||||||||||||||||
| # if measurement gate is not in branching statement | ||||||||||||||||||||
| src_name, src_idx = QasmVisitor._get_qubit_name_and_id(src_id) | ||||||||||||||||||||
| if not self._in_branching_statement: | ||||||||||||||||||||
| src_name, src_id = src_id.name.name, src_id.indices[0][0].value # type: ignore | ||||||||||||||||||||
| qubit_node = self._module._qubit_depths[(src_name, src_id)] | ||||||||||||||||||||
| qubit_node = self._module._qubit_depths[(src_name, src_idx)] | ||||||||||||||||||||
| qubit_node.depth += 1 | ||||||||||||||||||||
| qubit_node.num_measurements += 1 | ||||||||||||||||||||
| else: | ||||||||||||||||||||
| self._mark_branch_qubit(src_name, src_idx) | ||||||||||||||||||||
| else: | ||||||||||||||||||||
| target_name: str = ( | ||||||||||||||||||||
| target.name if isinstance(target, qasm3_ast.Identifier) else target.name.name | ||||||||||||||||||||
|
|
@@ -661,12 +663,12 @@ def _visit_measurement( # pylint: disable=too-many-locals,too-many-branches,too | |||||||||||||||||||
| target=tgt_id if target else None, | ||||||||||||||||||||
| ) | ||||||||||||||||||||
| # if measurement gate is not in branching statement | ||||||||||||||||||||
| src_name, src_idx = QasmVisitor._get_qubit_name_and_id(src_id) | ||||||||||||||||||||
| if not self._in_branching_statement: | ||||||||||||||||||||
| src_name, src_id = src_id.name.name, src_id.indices[0][0].value # type: ignore | ||||||||||||||||||||
| tgt_name, tgt_id = tgt_id.name.name, tgt_id.indices[0][0].value # type: ignore | ||||||||||||||||||||
|
|
||||||||||||||||||||
| qubit_node, clbit_node = ( | ||||||||||||||||||||
| self._module._qubit_depths[(src_name, src_id)], | ||||||||||||||||||||
| self._module._qubit_depths[(src_name, src_idx)], | ||||||||||||||||||||
| self._module._clbit_depths[(tgt_name, tgt_id)], | ||||||||||||||||||||
| ) | ||||||||||||||||||||
| qubit_node.depth += 1 | ||||||||||||||||||||
|
|
@@ -682,6 +684,8 @@ def _visit_measurement( # pylint: disable=too-many-locals,too-many-branches,too | |||||||||||||||||||
| self._measurement_set.add(target.name) | ||||||||||||||||||||
| elif isinstance(target, qasm3_ast.IndexedIdentifier): | ||||||||||||||||||||
| self._measurement_set.add(target.name.name) | ||||||||||||||||||||
| else: | ||||||||||||||||||||
| self._mark_branch_qubit(src_name, src_idx) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| unrolled_measurements.append(unrolled_measure) | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
@@ -769,7 +773,7 @@ def _visit_reset(self, statement: qasm3_ast.QuantumReset) -> list[qasm3_ast.Quan | |||||||||||||||||||
| qubit_node.depth += 1 | ||||||||||||||||||||
| qubit_node.num_resets += 1 | ||||||||||||||||||||
| else: | ||||||||||||||||||||
| self._is_branch_qubits.add((qubit_name, qubit_id)) | ||||||||||||||||||||
| self._mark_branch_qubit(qubit_name, qubit_id) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| unrolled_resets.append(unrolled_reset) | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
@@ -1042,6 +1046,19 @@ def _register_physical_qubit(self, name: str) -> int: | |||||||||||||||||||
| self._module.num_qubits = max(self._module.num_qubits, phys_idx + 1) | ||||||||||||||||||||
| return phys_idx | ||||||||||||||||||||
|
|
||||||||||||||||||||
| def _mark_branch_qubit(self, qubit_name: str, qubit_idx: int) -> None: | ||||||||||||||||||||
| """Record a qubit operated on inside an if/else block. | ||||||||||||||||||||
|
|
||||||||||||||||||||
| The depth of such a qubit is settled once the branch closes, but it counts as | ||||||||||||||||||||
| used right away so that it is not mistaken for an idle qubit. | ||||||||||||||||||||
|
|
||||||||||||||||||||
| Args: | ||||||||||||||||||||
| qubit_name: The register name (or physical qubit identifier). | ||||||||||||||||||||
| qubit_idx: The index of the qubit within the register. | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| self._is_branch_qubits.add((qubit_name, qubit_idx)) | ||||||||||||||||||||
| self._module._qubit_depths[(qubit_name, qubit_idx)].num_branch_ops += 1 | ||||||||||||||||||||
|
|
||||||||||||||||||||
| @staticmethod | ||||||||||||||||||||
| def _get_qubit_name_and_id( | ||||||||||||||||||||
| qubit: qasm3_ast.IndexedIdentifier | qasm3_ast.Identifier, | ||||||||||||||||||||
|
|
@@ -1182,7 +1199,7 @@ def _visit_basic_gate_operation( | |||||||||||||||||||
| for qubit_subset in unrolled_targets + [ctrls]: | ||||||||||||||||||||
| for qubit in qubit_subset: | ||||||||||||||||||||
| qubit_name, qubit_idx = QasmVisitor._get_qubit_name_and_id(qubit) | ||||||||||||||||||||
| self._is_branch_qubits.add((qubit_name, qubit_idx)) | ||||||||||||||||||||
| self._mark_branch_qubit(qubit_name, qubit_idx) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| # check for duplicate bits | ||||||||||||||||||||
| for final_gate in result: | ||||||||||||||||||||
|
|
@@ -1299,7 +1316,7 @@ def _visit_custom_gate_operation( | |||||||||||||||||||
| for qubit_subset in [op_qubits] + [ctrls]: | ||||||||||||||||||||
| for qubit in qubit_subset: | ||||||||||||||||||||
| qubit_name, qubit_idx = QasmVisitor._get_qubit_name_and_id(qubit) | ||||||||||||||||||||
| self._is_branch_qubits.add((qubit_name, qubit_idx)) | ||||||||||||||||||||
| self._mark_branch_qubit(qubit_name, qubit_idx) | ||||||||||||||||||||
|
Comment on lines
1316
to
+1319
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 **P1 (7/10) · Bug:** Custom gates double-count branch operations after expansion (medium confidence)Operation metadata becomes inaccurate for every custom gate used in a branch, affecting consumers of
Suggested change
React 👎 to dismiss · Argus learns from feedback |
||||||||||||||||||||
|
|
||||||||||||||||||||
| self._scope_manager.pop_scope() | ||||||||||||||||||||
| self._scope_manager.restore_context() | ||||||||||||||||||||
|
|
||||||||||||||||||||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[L3] Non-unrolled path still misses loop and switch bodies — Implementation · Low
Rationale: the walk closes the gap on the unrolled path, but
has_measurements/remove_measurements/has_barriers/remove_barriersfall back toself._statementswhen the module has not been unrolled, and that list can still containfor/while/switchbodies this walker does not descend into. Verified on this branch:Without
unroll(),has_measurements()andhas_barriers()both returnFalseandremove_measurements()is a no-op; afterunroll()both returnTrueand removal works.This is pre-existing —
mainbehaves identically — and the PR strictly improves the situation, so it is not worth holding the merge on. Flagged because this walker is the natural home for the remainder, and it is easy to read the PR as having closed the class of bug entirely.Change requested: either extend
iter_quantum_statementsto loop and switch bodies for the non-unrolled path, or document on those four methods that they are only meaningful afterunroll(). A follow-up issue is fine.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Follow-up filed as #354 and referenced in the description, per your note that an issue is fine here.
Reproduced your case on this branch:
for int i in [0:1] { c[i] = measure q[i]; barrier q; }giveshas_measurements() == has_barriers() == Falsewithoutunroll(),Trueafter. #354 records both remedies you offered (extenditer_quantum_statements, or document the four methods as post-unroll()only).The description now has a "Not closed here" section naming the gap, and carries your finding that
for/while/switchnever reach_unrolled_astwith bodies intact — so the unrolled path really is complete withBoxandBranchingStatement, and the remainder is non-unrolled only.