-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
OptimizeSwapBeforeMeasure only when last measurements #5906
Closed
Closed
Changes from 5 commits
Commits
Show all changes
6 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 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 |
---|---|---|
|
@@ -13,9 +13,9 @@ | |
|
||
"""Remove the swaps followed by measurement (and adapt the measurement).""" | ||
|
||
from qiskit.circuit import Measure | ||
from qiskit.circuit.library.standard_gates import SwapGate | ||
from qiskit.transpiler.basepasses import TransformationPass | ||
from qiskit.transpiler import Layout | ||
from qiskit.dagcircuit import DAGCircuit | ||
|
||
|
||
|
@@ -35,29 +35,33 @@ def run(self, dag): | |
Returns: | ||
DAGCircuit: the optimized DAG. | ||
""" | ||
swaps = dag.op_nodes(SwapGate) | ||
for swap in swaps[::-1]: | ||
final_successor = [] | ||
for successor in dag.successors(swap): | ||
final_successor.append(successor.type == 'out' or (successor.type == 'op' and | ||
successor.op.name == 'measure')) | ||
if all(final_successor): | ||
# the node swap needs to be removed and, if a measure follows, needs to be adapted | ||
swap_qargs = swap.qargs | ||
measure_layer = DAGCircuit() | ||
for qreg in dag.qregs.values(): | ||
measure_layer.add_qreg(qreg) | ||
for creg in dag.cregs.values(): | ||
measure_layer.add_creg(creg) | ||
for successor in list(dag.successors(swap)): | ||
if successor.type == 'op' and successor.op.name == 'measure': | ||
# replace measure node with a new one, where qargs is set with the "other" | ||
# swap qarg. | ||
dag.remove_op_node(successor) | ||
old_measure_qarg = successor.qargs[0] | ||
new_measure_qarg = swap_qargs[swap_qargs.index(old_measure_qarg) - 1] | ||
measure_layer.apply_operation_back(Measure(), [new_measure_qarg], | ||
[successor.cargs[0]]) | ||
dag.compose(measure_layer) | ||
dag.remove_op_node(swap) | ||
return dag | ||
|
||
new_dag = DAGCircuit() | ||
new_dag.metadata = dag.metadata | ||
new_dag._global_phase = dag._global_phase | ||
for creg in dag.cregs.values(): | ||
new_dag.add_creg(creg) | ||
for qreg in dag.qregs.values(): | ||
new_dag.add_qreg(qreg) | ||
|
||
_layout = Layout.generate_trivial_layout(*dag.qregs.values()) | ||
_trivial_layout = Layout.generate_trivial_layout(*dag.qregs.values()) | ||
|
||
for node in dag.topological_op_nodes(): | ||
if node.type == 'op': | ||
qargs = [_trivial_layout[_layout[qarg]] for qarg in node.qargs] | ||
if isinstance(node.op, SwapGate): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this apply for all swaps or only those added by routing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does not distinguish from where is the SWAP coming from. |
||
swap = node | ||
final_successor = [] | ||
for successor in dag.successors(swap): | ||
if successor.type == 'op' and successor.op.name == 'measure': | ||
is_final_measure = all([s.type == 'out' | ||
for s in dag.successors(successor)]) | ||
else: | ||
is_final_measure = False | ||
final_successor.append(successor.type == 'out' or is_final_measure) | ||
if all(final_successor): | ||
_layout.swap(*qargs) | ||
continue | ||
new_dag.apply_operation_back(node.op, qargs, node.cargs) | ||
return new_dag |
This file contains 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
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.
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.
nice one. Why is
_copy_circuit_metadata
a private method?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.
Initially because it was used internally, but then was useful to passes that needed to rebuild the dag (but preserve metadata) like the layout routing passes. Making it public seems like a good idea.