-
Notifications
You must be signed in to change notification settings - Fork 3k
Generalize CXCancellation optimization pass to generic gate-inverse pairs
#6855
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
Merged
mergify
merged 27 commits into
Qiskit:main
from
vadebayo49:issue-6576/general-cancellation-pass
Aug 26, 2021
Merged
Changes from 5 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
3c59717
create new cancellation file
vadebayo49 81b755c
added cancellation pass
vadebayo49 3df475a
added inverse-cancellation pass
vadebayo49 6bf4c0b
fixed lints
vadebayo49 4bfb368
add test
vadebayo49 e842746
update tests
vadebayo49 20b25c3
add fixes
vadebayo49 53f7a36
Make some updates and write out some TODOs
lcapelluto b175100
Revert debugging statements/changes
lcapelluto 2fd283d
update self and inverse gates
vadebayo49 7c2c580
Changes from pair programming aug 13
lcapelluto 89b5bc5
added error checking
vadebayo49 692bb7f
release notes
vadebayo49 e869d1a
respond to code review
vadebayo49 224b502
updated code review
vadebayo49 3aef2db
comment update
vadebayo49 5884574
update InverseCancellation
vadebayo49 01327b0
update Inversecancellation initialization
vadebayo49 70192a3
updated self.attribute
vadebayo49 b4e3d20
run method
vadebayo49 561af5e
Update qiskit/transpiler/passes/optimization/inverse_cancellation.py
lcapelluto e0cad12
Update test/python/transpiler/test_inverse_cancellation.py
lcapelluto 77e8663
PR update
vadebayo49 962a730
Merge branch 'main' into issue-6576/general-cancellation-pass
lcapelluto c010d3c
Fix lint errors
lcapelluto ed6376c
Merge pull request #1 from lcapelluto/issue-6576/general-cancellation…
vadebayo49 b786923
Merge branch 'main' into issue-6576/general-cancellation-pass
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
63 changes: 63 additions & 0 deletions
63
qiskit/transpiler/passes/optimization/inverse_cancellation.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # This code is part of Qiskit. | ||
| # | ||
| # (C) Copyright IBM 2020. | ||
|
lcapelluto marked this conversation as resolved.
Outdated
|
||
| # | ||
| # This code is licensed under the Apache License, Version 2.0. You may | ||
| # obtain a copy of this license in the LICENSE.txt file in the root directory | ||
| # of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
| # | ||
| # Any modifications or derivative works of this code must retain this | ||
| # copyright notice, and modified files need to carry a notice indicating | ||
| # that they have been altered from the originals. | ||
|
|
||
| """ | ||
| A generic gate-inverse cancellation pass for a broad set of gate-inverse pairs. | ||
|
lcapelluto marked this conversation as resolved.
Outdated
|
||
| """ | ||
|
|
||
| from qiskit.transpiler.basepasses import TransformationPass | ||
|
|
||
|
|
||
| class Cancellation(TransformationPass): | ||
|
lcapelluto marked this conversation as resolved.
Outdated
|
||
| """Cancel back-to-back `gates`in dag.""" | ||
|
lcapelluto marked this conversation as resolved.
Outdated
|
||
|
|
||
| def __init__(self, gate_cancel): | ||
| """Initialize gate_cancel""" | ||
|
lcapelluto marked this conversation as resolved.
Outdated
|
||
| self.gate_cancel = gate_cancel | ||
| print(self.gate_cancel) | ||
|
lcapelluto marked this conversation as resolved.
Outdated
|
||
| super().__init__() | ||
|
|
||
| def run(self, dag): | ||
| """Run the Cancellation pass on `dag`. | ||
|
|
||
| Args: | ||
| dag (DAGCircuit): the directed acyclic graph to run on. | ||
|
|
||
| Returns: | ||
| DAGCircuit: Transformed DAG. | ||
| """ | ||
| gate_cancel_runs = dag.collect_runs(self.gate_cancel) | ||
| # Generalize input using self.gate_cancel | ||
| for gate_cancel_run in gate_cancel_runs: | ||
| # Partition the cx_run into chunks with equal gate arguments | ||
|
lcapelluto marked this conversation as resolved.
Outdated
|
||
| partition = [] | ||
| chunk = [] | ||
| for i in range(len(gate_cancel_run) - 1): | ||
| chunk.append(gate_cancel_run[i]) | ||
|
|
||
| qargs0 = gate_cancel_run[i].qargs | ||
| qargs1 = gate_cancel_run[i + 1].qargs | ||
|
|
||
| if qargs0 != qargs1: | ||
| partition.append(chunk) | ||
| chunk = [] | ||
| chunk.append(gate_cancel_run[-1]) | ||
| partition.append(chunk) | ||
| # Simplify each chunk in the partition | ||
| for chunk in partition: | ||
| if len(chunk) % 2 == 0: | ||
| for n in chunk: | ||
| dag.remove_op_node(n) | ||
| else: | ||
| for n in chunk[1:]: | ||
| dag.remove_op_node(n) | ||
| return dag | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # This code is part of Qiskit. | ||
| # | ||
| # (C) Copyright IBM 2020. | ||
| # | ||
| # This code is licensed under the Apache License, Version 2.0. You may | ||
| # obtain a copy of this license in the LICENSE.txt file in the root directory | ||
| # of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
| # | ||
| # Any modifications or derivative works of this code must retain this | ||
| # copyright notice, and modified files need to carry a notice indicating | ||
| # that they have been altered from the originals. | ||
|
|
||
| """ | ||
| Testing inverse_cancellation | ||
| """ | ||
|
|
||
| from qiskit import QuantumCircuit | ||
| from qiskit.transpiler.basepasses import TransformationPass | ||
| from qiskit.transpiler.passes import CXCancellation, Cancellation | ||
| from qiskit.test import QiskitTestCase | ||
| from qiskit.transpiler import PassManager | ||
| from numpy import pi | ||
|
|
||
| class TestCancellation(QiskitTestCase): | ||
|
|
||
| def test_inverse_cancellation(self): | ||
|
lcapelluto marked this conversation as resolved.
Outdated
|
||
| qc = QuantumCircuit(2,2) | ||
| qc.u(pi/2, 0, pi, 0) | ||
| qc.u(pi/2, 0, pi, 0) | ||
| pass_ = Cancellation(["u"]) | ||
| pm = PassManager(pass_) | ||
| new_circ = pm.run(qc) | ||
| cir_after = new_circ.count_ops() | ||
| self.assertNotIn("u", cir_after) | ||
|
|
||
|
|
||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.