-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
base: main
Are you sure you want to change the base?
Fix InverseCancellation
to run in classical blocks
#13454
Conversation
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:
|
Pull Request Test Coverage Report for Build 11965151140Details
💛 - Coveralls |
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.
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
.
qiskit/test/python/transpiler/test_cx_cancellation.py
Lines 146 to 199 in 94cea41
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. |
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.