-
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
Add RemoveFinalReset pass #11266
Merged
Merged
Add RemoveFinalReset pass #11266
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7dfb754
Add RemoveFinalReset pass
garrison 01ddf91
Fix lint
garrison f358d46
Look at predecessors of `output_map`
garrison 4adc048
Remove `RemoveFinalReset` from preset passmanagers
garrison 00f40d2
Merge branch 'main' into remove-final-reset
mtreinish 92ca2da
Update remove-final-reset-488247c01c4e147d.yaml
garrison d88605c
Fix `QiskitTestCase` import
garrison ed91caa
Update to use `DoWhileController`
garrison 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
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
37 changes: 37 additions & 0 deletions
37
qiskit/transpiler/passes/optimization/remove_final_reset.py
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# This code is part of Qiskit. | ||
# | ||
# (C) Copyright IBM 2017, 2023. | ||
# | ||
# 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. | ||
|
||
"""Remove reset when it is the final instruction on a qubit.""" | ||
|
||
from qiskit.circuit import Reset, Qubit | ||
from qiskit.dagcircuit import DAGOpNode | ||
from qiskit.transpiler.basepasses import TransformationPass | ||
|
||
|
||
class RemoveFinalReset(TransformationPass): | ||
"""Remove reset when it is the final instruction on a qubit wire.""" | ||
|
||
def run(self, dag): | ||
"""Run the RemoveFinalReset pass on `dag`. | ||
|
||
Args: | ||
dag (DAGCircuit): the DAG to be optimized. | ||
|
||
Returns: | ||
DAGCircuit: the optimized DAG. | ||
""" | ||
for output_node in dag.output_map.values(): | ||
if isinstance(output_node.wire, Qubit): | ||
pred = next(dag.predecessors(output_node)) | ||
if isinstance(pred, DAGOpNode) and isinstance(pred.op, Reset): | ||
dag.remove_op_node(pred) | ||
return dag |
35 changes: 35 additions & 0 deletions
35
releasenotes/notes/remove-final-reset-488247c01c4e147d.yaml
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- | ||
features: | ||
- | | ||
Added a new transpiler pass, :class:`.RemoveFinalReset`, which | ||
will remove any :class:`.Reset` operation which is the final | ||
instruction on a qubit wire. For example, taking a circuit with | ||
final :class:`.Reset`\ s: | ||
|
||
.. plot:: | ||
|
||
from qiskit.circuit import QuantumCircuit | ||
|
||
qc = QuantumCircuit(3, 1) | ||
qc.reset(0) | ||
qc.h(range(3)) | ||
qc.cx(1, 0) | ||
qc.measure(0, 0) | ||
qc.reset(range(3)) | ||
qc.draw("mpl") | ||
|
||
will remove the final resets when the pass is run: | ||
|
||
.. plot:: | ||
:include-source: | ||
|
||
from qiskit.transpiler.passes import RemoveFinalReset | ||
from qiskit.circuit import QuantumCircuit | ||
|
||
qc = QuantumCircuit(3, 1) | ||
qc.reset(0) | ||
qc.h(range(3)) | ||
qc.cx(1, 0) | ||
qc.measure(0, 0) | ||
qc.reset(range(3)) | ||
RemoveFinalReset()(qc).draw("mpl") |
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
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 |
---|---|---|
@@ -0,0 +1,117 @@ | ||
# This code is part of Qiskit. | ||
# | ||
# (C) Copyright IBM 2017, 2023. | ||
# | ||
# 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. | ||
|
||
"""Test RemoveFinalReset pass""" | ||
|
||
import unittest | ||
|
||
from qiskit import QuantumRegister, QuantumCircuit | ||
from qiskit.passmanager.flow_controllers import DoWhileController | ||
from qiskit.transpiler import PassManager | ||
from qiskit.transpiler.passes import RemoveFinalReset, DAGFixedPoint | ||
from qiskit.converters import circuit_to_dag | ||
from test import QiskitTestCase # pylint: disable=wrong-import-order | ||
|
||
|
||
class TestRemoveFinalReset(QiskitTestCase): | ||
"""Test remove-reset-in-zero-state optimizations.""" | ||
|
||
def test_optimize_single_reset(self): | ||
"""Remove a single final reset | ||
qr0:--[H]--|0>-- ==> qr0:--[H]-- | ||
""" | ||
qr = QuantumRegister(1, "qr") | ||
circuit = QuantumCircuit(qr) | ||
circuit.h(0) | ||
circuit.reset(qr) | ||
dag = circuit_to_dag(circuit) | ||
|
||
expected = QuantumCircuit(qr) | ||
expected.h(0) | ||
|
||
pass_ = RemoveFinalReset() | ||
after = pass_.run(dag) | ||
|
||
self.assertEqual(circuit_to_dag(expected), after) | ||
|
||
def test_dont_optimize_non_final_reset(self): | ||
"""Do not remove reset if not final instruction | ||
qr0:--|0>--[H]-- ==> qr0:--|0>--[H]-- | ||
""" | ||
qr = QuantumRegister(1, "qr") | ||
circuit = QuantumCircuit(qr) | ||
circuit.reset(qr) | ||
circuit.h(qr) | ||
dag = circuit_to_dag(circuit) | ||
|
||
expected = QuantumCircuit(qr) | ||
expected.reset(qr) | ||
expected.h(qr) | ||
|
||
pass_ = RemoveFinalReset() | ||
after = pass_.run(dag) | ||
|
||
self.assertEqual(circuit_to_dag(expected), after) | ||
|
||
def test_optimize_single_reset_in_diff_qubits(self): | ||
"""Remove a single final reset in different qubits | ||
qr0:--[H]--|0>-- qr0:--[H]-- | ||
==> | ||
qr1:--[X]--|0>-- qr1:--[X]---- | ||
""" | ||
qr = QuantumRegister(2, "qr") | ||
circuit = QuantumCircuit(qr) | ||
circuit.h(0) | ||
circuit.x(1) | ||
circuit.reset(qr) | ||
dag = circuit_to_dag(circuit) | ||
|
||
expected = QuantumCircuit(qr) | ||
expected.h(0) | ||
expected.x(1) | ||
|
||
pass_ = RemoveFinalReset() | ||
after = pass_.run(dag) | ||
|
||
self.assertEqual(circuit_to_dag(expected), after) | ||
|
||
|
||
class TestRemoveFinalResetFixedPoint(QiskitTestCase): | ||
"""Test RemoveFinalReset in a transpiler, using fixed point.""" | ||
|
||
def test_two_resets(self): | ||
"""Remove two final resets | ||
qr0:--[H]-|0>-|0>-- ==> qr0:--[H]-- | ||
""" | ||
qr = QuantumRegister(1, "qr") | ||
circuit = QuantumCircuit(qr) | ||
circuit.h(qr[0]) | ||
circuit.reset(qr[0]) | ||
circuit.reset(qr[0]) | ||
|
||
expected = QuantumCircuit(qr) | ||
expected.h(qr[0]) | ||
|
||
pass_manager = PassManager() | ||
pass_manager.append( | ||
DoWhileController( | ||
[RemoveFinalReset(), DAGFixedPoint()], | ||
do_while=lambda property_set: not property_set["dag_fixed_point"], | ||
) | ||
) | ||
after = pass_manager.run(circuit) | ||
|
||
self.assertEqual(expected, after) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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
Oops, something went wrong.
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.
FWIW, you could make pylint happy if you added a new line between the
qiskit
import section and this import.