Skip to content
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

Deprecate Instruction.condition in favor of IfElseOp #9556

Closed
kdk opened this issue Feb 8, 2023 · 6 comments · Fixed by #13223
Closed

Deprecate Instruction.condition in favor of IfElseOp #9556

kdk opened this issue Feb 8, 2023 · 6 comments · Fixed by #13223
Assignees
Labels
Milestone

Comments

@kdk
Copy link
Member

kdk commented Feb 8, 2023

What should we add?

Instruction.condition is Qiskit's way of representing classical execution of instructions based on the values in specified classical registers. However, due to it's location as a property on Instruction (but outside params) has historically lead to it being overlooked, resulting in a handful of bugs where this condition information is either lost or not considered e.g. when evaluating instruction order (Some examples: #6475, #6810, #8553, #8040, #7950, #7247, #7006, #6994, #6583, #6192, #6016, #4727, #4672, #3164, #3215, #2667, #2571 .) Additionally, it is one of the pieces of mutable state that is currently preventing us from moving toward more lightweight Operation instances ( to resolve e.g. #3800, #5895, #7624, and #9372 ).

IfElseOp can currently represent a superset of what can be expressed with Instruction.condition and is more inline with how we expect to handle conditional and control flow operations going forward, so we should plan to deprecate Instruction.condition with equivalent usage of IfElseOp. This issue is to discuss and plan that process.

Deprecating Instruction.conditionwhile maintaining full backwards compatibility seems difficult (due to both a change in Operation type from Instruction to IfElseOp, and due to .condition being a property of the Instruction without context of the QuantumCircuit in which it is included). The following are two use cases to consider:

>>> qc = qk.QuantumCircuit(2,1)
>>> qc.h(0)
>>> qc.measure(0,0)
>>> qc.x(1).c_if(0, 0)
qc = qk.QuantumCircuit(1, 1)
qc.x(0)
qc.data[0][0].condition = (qc.clbits[0], 1)

Additionally, we should:
1)Updating existing visualization to inspect IfElseOp operations and, when possible, draw as an equivalent condition
2)Updating qiskit.assemble, similarly to visualization, to replace applicable IfElseOp s with their equivalent condition representation

@kdk kdk added the type: feature request New feature or request label Feb 8, 2023
@kdk kdk added this to the 0.25.0 milestone Apr 19, 2023
@mtreinish mtreinish added the on hold Can not fix yet label Jun 22, 2023
@mtreinish
Copy link
Member

This depends on #9417 and is blocked until it is implemented.

@jakelishman
Copy link
Member

In practice, the routing performance for conditioned single 1q and 2q blocks is currently very different compared to the .condition path as well, because of the way we do routing. Explicitly: for a .condition gate, we will apply layout transformations and insert swaps as if the 1q/2q gate is a completely standard gate, but in the IfElseOp form, any routing required for the layout will happen conditionally inside the If and then need to be undone within the same block. Given that the chance that a condition is true (by a very naive estimate based on us doing exact numeric equality) is less than 50%, this might be the preferred way, but the eager "stop the world and consider only this gate" approach we take right now is likely compromising parallel routing quality.

@mtreinish
Copy link
Member

TBH, I'm personally less concerned about that for this issue at least. It's an optimization/fix we can work on in parallel and also do post-deprecation. For me, to deprecate c_if/condition we need to be able to functionally do all the same stuff with IfElseOp (which offers a superset of functionality). The only big missing piece is #9417 I think.

@jakelishman
Copy link
Member

Agreed - I'm mostly trying to get these thoughts written down. Kevin and I had a meeting where that routing quality was a concern, so it'll affect some users, but it's likely lower priority than getting the Instruction.condition mutable field gone.

@kdk
Copy link
Member Author

kdk commented Jun 29, 2023

I think this can be taken off 'on hold' since the work for #9417 is in flight, and doesn't strictly block the development of this issue, only its release.

@kdk kdk removed the on hold Can not fix yet label Jun 29, 2023
@mtreinish
Copy link
Member

I looked at this today and I think it's too soon for 0.25 especially with the proposal freeze so close. Besides being blocked on #9417 actually merging (which there are enough moving pieces in progress still that it'll be tight to fit in the release). The actual mechanics of this are more involved than I think is reasonable in the time frame for 0.25, mostly around testing. There are 7685 tests that fail locally because of the deprecation warnings. These will all need to be updated to catch the deprecation warnings. If we want to do this I think the better time frame is to do this early in 0.26/0.45 to ensure we have enough debug time for any issues that will inevitably come up from the deprecation especially for something as core as c_if/condition.

@mtreinish mtreinish modified the milestones: 0.25.0, 0.45.0 Jul 5, 2023
@mtreinish mtreinish modified the milestones: 0.45.0, 1.0.0pre1 Oct 6, 2023
@mtreinish mtreinish modified the milestones: 1.0.0pre1, 1.0.0 Nov 27, 2023
@mtreinish mtreinish modified the milestones: 1.0.0, 1.1.0 Jan 23, 2024
@jakelishman jakelishman modified the milestones: 1.1.0, 1.2.0 Apr 18, 2024
@mtreinish mtreinish modified the milestones: 1.2.0, 1.3.0 Jul 24, 2024
mtreinish added a commit to mtreinish/qiskit-core that referenced this issue Sep 25, 2024
This commit deprecates the Instruction.condition and c_if() method for
removal in 2.0. This functionality has been superseded by the more
sophisiticated `IfElseOp` for some time now. Removing the condition
property will simplify the Qiskit data model as it is implemented in
a kind of ad-hoc manner that adds additional overhead to manually check
it in many places.

For the unittest modifications the deprecation warning for the .condtion
getter is suppressed for the entire suite because this gets triggered
internally by the transpiler and a lot of other places, including from
rust code as until it is removed we need to use it to check that piece
of the data model.

Fixes Qiskit#9556
github-merge-queue bot pushed a commit that referenced this issue Oct 31, 2024
* Deprecate the condition attribute and related functionality

This commit deprecates the Instruction.condition and c_if() method for
removal in 2.0. This functionality has been superseded by the more
sophisiticated `IfElseOp` for some time now. Removing the condition
property will simplify the Qiskit data model as it is implemented in
a kind of ad-hoc manner that adds additional overhead to manually check
it in many places.

For the unittest modifications the deprecation warning for the .condtion
getter is suppressed for the entire suite because this gets triggered
internally by the transpiler and a lot of other places, including from
rust code as until it is removed we need to use it to check that piece
of the data model.

Fixes #9556

* Add missing assertWarns

* Handle deprecation warnings in visual tests too

* Apply suggestions from code review

Co-authored-by: Elena Peña Tapia <57907331+ElePT@users.noreply.github.com>

* Use private attribute in py->rust conversion

* Avoid deprecation warning in non-deprecated code

This commit fixes some places in the code where we were using the
deprecated functionality where it needed a path to persist the behavior
or where the deprecation warning became a performance bottleneck. The
code is updated accordingly and to catch issues like this in the future
the warning filters are adjusted to be more selective.

* Add missing test updates

* Add filter for aer's condition usage and dag drawer

* Fix lint

---------

Co-authored-by: Elena Peña Tapia <57907331+ElePT@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants