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

Fix Instruction.repeat with conditionals #11940

Merged
merged 4 commits into from
Mar 6, 2024

Conversation

jakelishman
Copy link
Member

Summary

We can't put register conditionals within an Instruction.definition field; the data model of QuantumCircuit doesn't permit closing over registers from within definitions. This commit moves a condition to the outer Instruction that's returned.

Details and comments

Fix #11935
Fix #11936 (though note that #11761 would then be triggered for OQ3).

Important

As implemented, this commit isn't necessarily a completely valid transformation, but I'm putting it up in this form for a bit of discussion.

The point is that an instruction can in theory affect its own condition; consider a Measure().c_if(cr, value), where the measure instruction within the circuit is applied to a clbit that's in cr. To go further, consider the custom instruction whose definition is h q[0]; measure q[0] -> c[0]; - this is clearly not idempotent under a c-based condition (one could argue that the bare measure was, in the context of a noiseless abstract circuit, since the first measure would collapse the state).

Given that Instruction.repeat is only applied without a circuit context, and we're moving to remove Instruction.condition for these same sorts of reasons (in favour of IfElseOp), I think this is a transformation we can accept, though perhaps with additional documentation.

We can't put register conditionals within an `Instruction.definition`
field; the data model of `QuantumCircuit` doesn't permit closing over
registers from within definitions.  This commit moves a condition to the
outer `Instruction` that's returned.
@jakelishman jakelishman added Changelog: Bugfix Include in the "Fixed" section of the changelog mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library labels Mar 4, 2024
@jakelishman jakelishman added this to the 1.1.0 milestone Mar 4, 2024
@jakelishman jakelishman requested a review from a team as a code owner March 4, 2024 13:06
@qiskit-bot
Copy link
Collaborator

One or more of the the following people are requested to review this:

  • @Qiskit/terra-core

@coveralls
Copy link

coveralls commented Mar 4, 2024

Pull Request Test Coverage Report for Build 8171222509

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 12 of 12 (100.0%) changed or added relevant lines in 1 file are covered.
  • 44 unchanged lines in 3 files lost coverage.
  • Overall coverage increased (+0.04%) to 89.31%

Files with Coverage Reduction New Missed Lines %
qiskit/circuit/instruction.py 2 95.91%
crates/qasm2/src/lex.rs 4 92.19%
qiskit/qpy/binary_io/circuits.py 38 91.55%
Totals Coverage Status
Change from base Build 8117135103: 0.04%
Covered Lines: 59123
Relevant Lines: 66200

💛 - Coveralls

1ucian0
1ucian0 previously approved these changes Mar 6, 2024
Copy link
Member

@1ucian0 1ucian0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix looks good to me. I just have a small non-blocking comment on the release note.

@jakelishman
Copy link
Member Author

I've also updated the documentation of Instruction.repeat to include the warning against the potential non-idempotency of conditional instructions in a357a4d.

@1ucian0 1ucian0 enabled auto-merge March 6, 2024 11:25
@1ucian0 1ucian0 added this pull request to the merge queue Mar 6, 2024
Merged via the queue into Qiskit:main with commit 6ebb7aa Mar 6, 2024
12 checks passed
@jakelishman jakelishman deleted the fix-conditional-instruction-repeat branch March 6, 2024 12:53
mtreinish pushed a commit to mtreinish/qiskit-core that referenced this pull request Mar 11, 2024
* Fix `Instruction.repeat` with conditionals

We can't put register conditionals within an `Instruction.definition`
field; the data model of `QuantumCircuit` doesn't permit closing over
registers from within definitions.  This commit moves a condition to the
outer `Instruction` that's returned.

* Avoid 'to_mutable' if not needed

* Add proviso on repeated conditionals in documentation

* Update wording in release note

Co-authored-by: Luciano Bello <bel@zurich.ibm.com>

---------

Co-authored-by: Luciano Bello <bel@zurich.ibm.com>
IsmaelCesar pushed a commit to comp-quantica/qiskit-terra that referenced this pull request Mar 13, 2024
* Fix `Instruction.repeat` with conditionals

We can't put register conditionals within an `Instruction.definition`
field; the data model of `QuantumCircuit` doesn't permit closing over
registers from within definitions.  This commit moves a condition to the
outer `Instruction` that's returned.

* Avoid 'to_mutable' if not needed

* Add proviso on repeated conditionals in documentation

* Update wording in release note

Co-authored-by: Luciano Bello <bel@zurich.ibm.com>

---------

Co-authored-by: Luciano Bello <bel@zurich.ibm.com>
@1ucian0 1ucian0 added the affects extended support This issue (also) affects extended support label May 30, 2024
@1ucian0
Copy link
Member

1ucian0 commented May 30, 2024

This seems (based on the presence of affected code) to affect stable/0.46. Shall we port it?

@1ucian0
Copy link
Member

1ucian0 commented Jun 4, 2024

@Mergifyio backport stable/0.46

Copy link
Contributor

mergify bot commented Jun 4, 2024

backport stable/0.46

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Jun 4, 2024
* Fix `Instruction.repeat` with conditionals

We can't put register conditionals within an `Instruction.definition`
field; the data model of `QuantumCircuit` doesn't permit closing over
registers from within definitions.  This commit moves a condition to the
outer `Instruction` that's returned.

* Avoid 'to_mutable' if not needed

* Add proviso on repeated conditionals in documentation

* Update wording in release note

Co-authored-by: Luciano Bello <bel@zurich.ibm.com>

---------

Co-authored-by: Luciano Bello <bel@zurich.ibm.com>
(cherry picked from commit 6ebb7aa)
github-merge-queue bot pushed a commit that referenced this pull request Jun 4, 2024
* Fix `Instruction.repeat` with conditionals

We can't put register conditionals within an `Instruction.definition`
field; the data model of `QuantumCircuit` doesn't permit closing over
registers from within definitions.  This commit moves a condition to the
outer `Instruction` that's returned.

* Avoid 'to_mutable' if not needed

* Add proviso on repeated conditionals in documentation

* Update wording in release note

Co-authored-by: Luciano Bello <bel@zurich.ibm.com>

---------

Co-authored-by: Luciano Bello <bel@zurich.ibm.com>
(cherry picked from commit 6ebb7aa)

Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects extended support This issue (also) affects extended support Changelog: Bugfix Include in the "Fixed" section of the changelog mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library
Projects
None yet
5 participants