fix(transpiler): RemoveFinalMeasurements incorrectly removes measurements feeding control-flow conditions#15959
Open
adcorcol wants to merge 1 commit intoQiskit:mainfrom
Conversation
… control-flow conditions calc_final_ops only checked quantum wire successors to decide whether a measurement was final, completely ignoring classical wire successors. This caused measurements whose classical output feeds a while_loop, if_else, or switch_case condition to be incorrectly removed, leaving the condition register at its default value 0 and causing while_loop circuits to run indefinitely. Fix: before marking a measurement as final, check whether any classical successor is a DAGOpNode that is not itself a measure. A measure successor merely overwrites the clbit and is safe to ignore; any other op (control-flow condition, etc.) means the clbit value is being read and the measurement must be preserved. For all other classical successors we err on the side of preservation (conservative but correct). Fixes Qiskit#14319. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Collaborator
|
One or more of the following people are relevant to this code:
|
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.
Summary
RemoveFinalMeasurementswas incorrectly removing measurements whose classical output feeds a control-flow condition (while_loop,if_else,switch_case). The pass only checked quantum wire successors to determine if a measurement was "final", completely ignoring classical wire successors. Removing such measurements left the condition register at its default value0, causingwhile_loopcircuits to run indefinitely.Fixes #14319.
Root cause
In
calc_final_ops, the reverse DAG traversal walks backwards through quantum wire predecessors. Ameasurenode's classical wire successors were never inspected, so a measurement whose clbit feeds awhile_loopcondition was indistinguishable from a truly final one.Fix
Before marking a node as final, check whether any classical successor is a
DAGOpNodethat is not itself ameasure. Ameasuresuccessor merely overwrites the clbit (safe to remove); any other op means the clbit value is being read and the measurement must be preserved. For all other classical successors we err on the side of preservation (conservative but correct).Test plan
test_measure_feeding_while_loop_condition_not_removedtest_measure_feeding_if_else_condition_not_removedtest_measure_feeding_switch_case_condition_not_removedtest_chained_measures_first_is_removed_second_feeds_condition— exercises the!= "measure"branch: first measure is overwritten and correctly removed, second feeds the condition and is preservedLightConepass (which also usescalc_final_ops) tested and unaffected🤖 Generated with Claude Code