-
Notifications
You must be signed in to change notification settings - Fork 2.9k
swap_cancellation pass #5902
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
Closed
Closed
swap_cancellation pass #5902
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
54 changes: 54 additions & 0 deletions
54
qiskit/transpiler/passes/optimization/swap_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,54 @@ | ||
| # This code is part of Qiskit. | ||
| # | ||
| # (C) Copyright IBM 2021. | ||
| # | ||
| # 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. | ||
|
|
||
| """Cancel back-to-back `swap` gates in dag.""" | ||
|
|
||
| from qiskit.transpiler.basepasses import TransformationPass | ||
|
|
||
|
|
||
| class SWAPCancellation(TransformationPass): | ||
| """Cancel back-to-back `swap` gates in dag.""" | ||
|
|
||
| def run(self, dag): | ||
| """Run the SWAPCancellation pass on `dag`. | ||
|
|
||
| Args: | ||
| dag (DAGCircuit): the directed acyclic graph to run on. | ||
|
|
||
| Returns: | ||
| DAGCircuit: Transformed DAG. | ||
| """ | ||
| swap_runs = dag.collect_runs(["swap"]) | ||
| for swap_run in swap_runs: | ||
| # Partition the swap_run into chunks with equal gate arguments | ||
| partition = [] | ||
| chunk = [] | ||
| for i in range(len(swap_run) - 1): | ||
| chunk.append(swap_run[i]) | ||
|
|
||
| qargs0 = swap_run[i].qargs | ||
| qargs1 = swap_run[i + 1].qargs | ||
|
|
||
| if set(qargs0) != set(qargs1): | ||
| partition.append(chunk) | ||
| chunk = [] | ||
| chunk.append(swap_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,52 @@ | ||
| # This code is part of Qiskit. | ||
| # | ||
| # (C) Copyright IBM 2021. | ||
| # | ||
| # 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. | ||
|
|
||
| """Tests for pass cancelling 2 consecutive SWAPs on the same qubits.""" | ||
|
|
||
| from qiskit import QuantumCircuit | ||
| from qiskit.transpiler.passes import SWAPCancellation | ||
| from qiskit.test import QiskitTestCase | ||
|
|
||
|
|
||
| class TestSWAPCancellation(QiskitTestCase): | ||
| """Test the SWAPCancellation pass.""" | ||
|
|
||
| def test_pass_swap_cancellation_simple(self): | ||
| """Test a simple swap cancellation pass, symmetric wires """ | ||
| circuit = QuantumCircuit(2) | ||
| circuit.swap(0, 1) | ||
| circuit.swap(1, 0) | ||
|
|
||
| out_circuit = SWAPCancellation()(circuit) | ||
| resources_after = out_circuit.count_ops() | ||
|
|
||
| self.assertNotIn('swap', resources_after) | ||
|
|
||
| def test_pass_swap_cancellation_many(self): | ||
| """Test the swap cancellation pass. | ||
|
|
||
| It should cancel consecutive swap pairs on same qubits. | ||
| """ | ||
| circuit = QuantumCircuit(2) | ||
| circuit.h(0) | ||
| circuit.h(0) | ||
| circuit.swap(0, 1) | ||
| circuit.swap(0, 1) | ||
| circuit.swap(0, 1) | ||
| circuit.swap(0, 1) | ||
| circuit.swap(1, 0) | ||
| circuit.swap(1, 0) | ||
|
|
||
| out_circuit = SWAPCancellation()(circuit) | ||
| resources_after = out_circuit.count_ops() | ||
|
|
||
| self.assertNotIn('swap', resources_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.
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.
Does this actually work as expected? The
collect_runsmethod was mostly for 1q gates I don't think we have any tests of using it with >1q gates. Looking at the rustworkx code for the function (https://github.com/Qiskit/rustworkx/blob/main/src/dag_algo/mod.rs#L460-L506 ) it should work I think, but having a testing with partially overlapping swaps (eg, swap(0,1) -> swap(1, 2) -> swap (2, 0)) to make sure the behavior is sound