forked from Qiskit/qiskit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pass to filter ops and label inserted barriers (Qiskit#10323)
* Add pass to remove labeled ops and label inserted barriers This commit adds a new transpiler pass RemoveLabeledOps which is used to remove any op nodes that match a given label. This is paired with a new label option for BarrierBeforeFinalMeasurements. These are combined in the preset pass managers to ensure we're not always adding a barrier before output measurements in the output of the transpiler. Fixes Qiskit#10321 * Fix rebase issues * Handle pre-existing adjacent barriers In the case when a label is set to trigger the removal of the labelled barrier the merge step would lose the context around which barrier instruction was transpiler inserted and which was user provided. To address this issue this commit skips the MergeAdjacentBarrier step so that the transpiler barrier is kept separate from any user inserted barriers which need to be preserved. * Generalize RemoveLabeledOps to FilterOpNodes This commit generalizes the new RemoveLabeledOps pass to be a generic node filtering pass that given a filter function it will remove any matching op nodes in the circuit. * Add release note * Add tests for new pass * Add release note for new barrier pass kwarg * Fix type hint * Fix docs typo * Invert predicate usage This commit inverts the predicate usage to be consistent with Python's built in filter() function. Now if the predicate returns True the dag node is retained and if it returns false it is removed. This is also explicitly documented to make it clear how the pass is to be used. * Update pass manager drawer reference images
- Loading branch information
1 parent
76d4e9f
commit ea06a80
Showing
12 changed files
with
276 additions
and
30 deletions.
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
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,65 @@ | ||
# This code is part of Qiskit. | ||
# | ||
# (C) Copyright IBM 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. | ||
|
||
"""Filter ops from a circuit""" | ||
|
||
from typing import Callable | ||
|
||
from qiskit.dagcircuit import DAGCircuit, DAGOpNode | ||
from qiskit.transpiler.basepasses import TransformationPass | ||
from qiskit.transpiler.passes.utils import control_flow | ||
|
||
|
||
class FilterOpNodes(TransformationPass): | ||
"""Remove all operations that match a filter function | ||
This transformation pass is used to remove any operations that matches a | ||
the provided filter function. | ||
Args: | ||
predicate: A given callable that will be passed the :class:`.DAGOpNode` | ||
for each node in the :class:`.DAGCircuit`. If the callable returns | ||
``True`` the :class:`.DAGOpNode` is retained in the circuit and if it | ||
returns ``False`` it is removed from the circuit. | ||
Example: | ||
Filter out operations that are labelled ``"foo"`` | ||
.. plot:: | ||
:include-source: | ||
from qiskit import QuantumCircuit | ||
from qiskit.transpiler.passes import FilterOpNodes | ||
circuit = QuantumCircuit(1) | ||
circuit.x(0, label='foo') | ||
circuit.barrier() | ||
circuit.h(0) | ||
circuit = FilterOpNodes( | ||
lambda node: getattr(node.op, "label") != "foo" | ||
)(circuit) | ||
circuit.draw('mpl') | ||
""" | ||
|
||
def __init__(self, predicate: Callable[[DAGOpNode], bool]): | ||
super().__init__() | ||
self.predicate = predicate | ||
|
||
@control_flow.trivial_recurse | ||
def run(self, dag: DAGCircuit) -> DAGCircuit: | ||
"""Run the RemoveBarriers pass on `dag`.""" | ||
for node in dag.op_nodes(): | ||
if not self.predicate(node): | ||
dag.remove_op_node(node) | ||
return 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
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
16 changes: 16 additions & 0 deletions
16
releasenotes/notes/add-filter-op-nodes-aa024a0f1058e4b7.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,16 @@ | ||
--- | ||
features: | ||
- | | ||
Added a new transpiler pass :class:`.FilterOpNodes` which is used to filter | ||
:class:`.DAGOpNode`\s in a :class:`.DAGCircuit`. | ||
- | | ||
Added a new keyword argument, ``label``, to the constructor on the | ||
:class:`.BarrierBeforeFinalMeasurements` transpiler pass. If specified the | ||
inserted barrier will be assigned the specified label. This also prevents | ||
the inserted barrier from being merged with any any other pre-existing | ||
adjacent barriers. | ||
other: | ||
- | | ||
The preset pass managers used by :func:`.transpile` and returned with | ||
:class:`.generate_preset_pass_manager` will no longer insert barriers | ||
before final measurements in the output circuits. |
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,78 @@ | ||
# This code is part of Qiskit. | ||
# | ||
# (C) Copyright IBM 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. | ||
|
||
"""FilterOpNodes pass testing""" | ||
|
||
|
||
from qiskit import QuantumCircuit | ||
from qiskit.transpiler.passes import FilterOpNodes | ||
from qiskit.test import QiskitTestCase | ||
|
||
|
||
class TestFilterOpNodes(QiskitTestCase): | ||
"""Tests for FilterOpNodes transformation pass.""" | ||
|
||
def test_empty_circuit(self): | ||
"""Empty DAG has does nothing.""" | ||
circuit = QuantumCircuit() | ||
self.assertEqual(FilterOpNodes(lambda x: False)(circuit), circuit) | ||
|
||
def test_remove_x_gate(self): | ||
"""Test filter removes matching gates.""" | ||
circuit = QuantumCircuit(2) | ||
circuit.x(0) | ||
circuit.x(1) | ||
circuit.cx(0, 1) | ||
circuit.cx(1, 0) | ||
circuit.cx(0, 1) | ||
circuit.measure_all() | ||
|
||
filter_pass = FilterOpNodes(lambda node: node.op.name != "x") | ||
|
||
expected = QuantumCircuit(2) | ||
expected.cx(0, 1) | ||
expected.cx(1, 0) | ||
expected.cx(0, 1) | ||
expected.measure_all() | ||
|
||
self.assertEqual(filter_pass(circuit), expected) | ||
|
||
def test_filter_exception(self): | ||
"""Test a filter function exception passes through.""" | ||
circuit = QuantumCircuit(2) | ||
circuit.x(0) | ||
circuit.x(1) | ||
circuit.cx(0, 1) | ||
circuit.cx(1, 0) | ||
circuit.cx(0, 1) | ||
circuit.measure_all() | ||
|
||
def filter_fn(node): | ||
raise TypeError("Failure") | ||
|
||
filter_pass = FilterOpNodes(filter_fn) | ||
with self.assertRaises(TypeError): | ||
filter_pass(circuit) | ||
|
||
def test_no_matches(self): | ||
"""Test the pass does nothing if there are no filter matches.""" | ||
circuit = QuantumCircuit(2) | ||
circuit.x(0) | ||
circuit.x(1) | ||
circuit.cx(0, 1) | ||
circuit.cx(1, 0) | ||
circuit.cx(0, 1) | ||
circuit.measure_all() | ||
|
||
filter_pass = FilterOpNodes(lambda node: node.op.name != "cz") | ||
|
||
self.assertEqual(filter_pass(circuit), circuit) |
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.