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 InverseCancellation to run in classical blocks #13454

Merged
merged 8 commits into from
Nov 26, 2024

Conversation

haimeng-zhang
Copy link
Contributor

Fix issue #13437.

Summary

The InverseCancellation pass cancels pairs of gates that are inverses of each other; it now behaves as expected when running inside classical blocks of a control flow.

Details and comments

This PR fixed the bug reported in #13437 by adding the decorator @control_flow.trivial_recurse so that the pass can be iterated over all control-flow nodes.

@haimeng-zhang haimeng-zhang requested a review from a team as a code owner November 18, 2024 23:59
@qiskit-bot qiskit-bot added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label Nov 18, 2024
@qiskit-bot
Copy link
Collaborator

Thank you for opening a new pull request.

Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient.

While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone.

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core

@CLAassistant
Copy link

CLAassistant commented Nov 18, 2024

CLA assistant check
All committers have signed the CLA.

@mtreinish mtreinish added the stable backport potential The bug might be minimal and/or import enough to be port to stable label Nov 19, 2024
@mtreinish mtreinish added this to the 1.3.0 milestone Nov 19, 2024
@coveralls
Copy link

coveralls commented Nov 19, 2024

Pull Request Test Coverage Report for Build 12038891561

Details

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • 18 unchanged lines in 3 files lost coverage.
  • Overall coverage decreased (-0.02%) to 88.934%

Files with Coverage Reduction New Missed Lines %
crates/accelerate/src/unitary_synthesis.rs 1 92.2%
crates/qasm2/src/lex.rs 5 91.73%
crates/qasm2/src/parse.rs 12 97.15%
Totals Coverage Status
Change from base Build 12038603859: -0.02%
Covered Lines: 79405
Relevant Lines: 89285

💛 - Coveralls

Copy link
Contributor

@kevinhartman kevinhartman left a comment

Choose a reason for hiding this comment

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

Thanks for doing this!

Can you also add some tests? You can probably just more or less copy the tests we had for CXCancellation's control flow handling into test_inverse_cancellation.py.

def test_if_else(self):
"""Test that the pass recurses in a simple if-else."""
with self.assertWarns(DeprecationWarning):
pass_ = CXCancellation()
inner_test = QuantumCircuit(4, 1)
inner_test.cx(0, 1)
inner_test.cx(0, 1)
inner_test.cx(2, 3)
inner_expected = QuantumCircuit(4, 1)
inner_expected.cx(2, 3)
test = QuantumCircuit(4, 1)
test.h(0)
test.measure(0, 0)
test.if_else((0, True), inner_test.copy(), inner_test.copy(), range(4), [0])
expected = QuantumCircuit(4, 1)
expected.h(0)
expected.measure(0, 0)
expected.if_else((0, True), inner_expected, inner_expected, range(4), [0])
self.assertEqual(pass_(test), expected)
def test_nested_control_flow(self):
"""Test that collection recurses into nested control flow."""
with self.assertWarns(DeprecationWarning):
pass_ = CXCancellation()
qubits = [Qubit() for _ in [None] * 4]
clbit = Clbit()
inner_test = QuantumCircuit(qubits, [clbit])
inner_test.cx(0, 1)
inner_test.cx(0, 1)
inner_test.cx(2, 3)
inner_expected = QuantumCircuit(qubits, [clbit])
inner_expected.cx(2, 3)
true_body = QuantumCircuit(qubits, [clbit])
true_body.while_loop((clbit, True), inner_test.copy(), [0, 1, 2, 3], [0])
test = QuantumCircuit(qubits, [clbit])
test.for_loop(range(2), None, inner_test.copy(), [0, 1, 2, 3], [0])
test.if_else((clbit, True), true_body, None, [0, 1, 2, 3], [0])
expected_if_body = QuantumCircuit(qubits, [clbit])
expected_if_body.while_loop((clbit, True), inner_expected, [0, 1, 2, 3], [0])
expected = QuantumCircuit(qubits, [clbit])
expected.for_loop(range(2), None, inner_expected, [0, 1, 2, 3], [0])
expected.if_else((clbit, True), expected_if_body, None, [0, 1, 2, 3], [0])
self.assertEqual(pass_(test), expected)

@mtreinish mtreinish added the Changelog: Bugfix Include in the "Fixed" section of the changelog label Nov 20, 2024
@haimengz
Copy link

Thanks for doing this!

Can you also add some tests? You can probably just more or less copy the tests we had for CXCancellation's control flow handling into test_inverse_cancellation.py.

def test_if_else(self):
"""Test that the pass recurses in a simple if-else."""
with self.assertWarns(DeprecationWarning):
pass_ = CXCancellation()
inner_test = QuantumCircuit(4, 1)
inner_test.cx(0, 1)
inner_test.cx(0, 1)
inner_test.cx(2, 3)
inner_expected = QuantumCircuit(4, 1)
inner_expected.cx(2, 3)
test = QuantumCircuit(4, 1)
test.h(0)
test.measure(0, 0)
test.if_else((0, True), inner_test.copy(), inner_test.copy(), range(4), [0])
expected = QuantumCircuit(4, 1)
expected.h(0)
expected.measure(0, 0)
expected.if_else((0, True), inner_expected, inner_expected, range(4), [0])
self.assertEqual(pass_(test), expected)
def test_nested_control_flow(self):
"""Test that collection recurses into nested control flow."""
with self.assertWarns(DeprecationWarning):
pass_ = CXCancellation()
qubits = [Qubit() for _ in [None] * 4]
clbit = Clbit()
inner_test = QuantumCircuit(qubits, [clbit])
inner_test.cx(0, 1)
inner_test.cx(0, 1)
inner_test.cx(2, 3)
inner_expected = QuantumCircuit(qubits, [clbit])
inner_expected.cx(2, 3)
true_body = QuantumCircuit(qubits, [clbit])
true_body.while_loop((clbit, True), inner_test.copy(), [0, 1, 2, 3], [0])
test = QuantumCircuit(qubits, [clbit])
test.for_loop(range(2), None, inner_test.copy(), [0, 1, 2, 3], [0])
test.if_else((clbit, True), true_body, None, [0, 1, 2, 3], [0])
expected_if_body = QuantumCircuit(qubits, [clbit])
expected_if_body.while_loop((clbit, True), inner_expected, [0, 1, 2, 3], [0])
expected = QuantumCircuit(qubits, [clbit])
expected.for_loop(range(2), None, inner_expected, [0, 1, 2, 3], [0])
expected.if_else((clbit, True), expected_if_body, None, [0, 1, 2, 3], [0])
self.assertEqual(pass_(test), expected)

Done! Please kindly review.

Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

The code and test LGTM, thanks for doing this! Before we merge it though do you mind adding a release note to document this fix was included in the next release? The process to do this is documented here: https://github.com/Qiskit/qiskit/blob/main/CONTRIBUTING.md#release-notes

Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for adding the release note so quickly.

@@ -0,0 +1,3 @@
fixes:
- |
The transpilation pass :class`.InverseCancellation` now runs inside of flow controlled blocks. Previously, it ignores the pairs of gates in classical blocks that can be cancelled. Refer to `#13437 <https://github.com/Qiskit/qiskit/issues/13437>` for more details.
Copy link
Member

Choose a reason for hiding this comment

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

I'd probably word this as "recurse into control flow blocks in the circuit". But we can also do this as part of the release prep PR #13447

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the suggestion! Is there a way that I can still fix this, either in this PR or the release prep PR #13447?

Copy link
Member

Choose a reason for hiding this comment

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

If you have a suggested alternative wording you can let @raynelfss know, I asked him to just update the text as part of the other release note editing he's working on in #13447.

In general we're not too picky about the exact release note content in PR reviews and as there are always a fair amount of edits needed before we can publish them. So it's simple for whoever is driving a given release to just copy-edit the text of a release note all at once in that final PR for a release.

@mtreinish mtreinish enabled auto-merge November 26, 2024 21:09
@mtreinish mtreinish added this pull request to the merge queue Nov 26, 2024
Merged via the queue into Qiskit:main with commit 6025b7c Nov 26, 2024
17 checks passed
mergify bot pushed a commit that referenced this pull request Nov 26, 2024
* Fix an issue that `InverseCancellation` does not run in classical blocks (#13437)

* Add test functions for  pass running in classical blocks.

* Reformat test function file

* Add a release note

(cherry picked from commit 6025b7c)
mtreinish pushed a commit that referenced this pull request Nov 27, 2024
* Fix an issue that `InverseCancellation` does not run in classical blocks (#13437)

* Add test functions for  pass running in classical blocks.

* Reformat test function file

* Add a release note

(cherry picked from commit 6025b7c)
github-merge-queue bot pushed a commit that referenced this pull request Nov 27, 2024
* Fix an issue that `InverseCancellation` does not run in classical blocks (#13437)

* Add test functions for  pass running in classical blocks.

* Reformat test function file

* Add a release note

(cherry picked from commit 6025b7c)

Co-authored-by: haimeng-zhang <33587226+haimeng-zhang@users.noreply.github.com>
@1ucian0
Copy link
Member

1ucian0 commented Dec 4, 2024

@Mergifyio backport stable/1.4

Copy link
Contributor

mergify bot commented Dec 4, 2024

backport stable/1.4

❌ No backport have been created

  • Backport to branch stable/1.4 failed

GitHub error: Branch not found

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: Bugfix Include in the "Fixed" section of the changelog Community PR PRs from contributors that are not 'members' of the Qiskit repo stable backport potential The bug might be minimal and/or import enough to be port to stable
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

InverseCancellation does not run in classical blocks like CXCancellation
8 participants