-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fully port FilterOpNodes to Rust (#13052)
* Fully port FilterOpNodes to Rust This commit ports the FilterOpNodes pass to rust. This pass is exceedingly simple and just runs a filter function over all the op nodes and removes nodes that match the filter. However, the API for the class exposes that filter function interface as a user provided Python callable. So for the current pass we need to retain that python callback. This limits the absolute performance of this pass because we're bottlenecked by calling python. Looking to the future, this commit adds a rust native method to DAGCircuit to perform this filtering with a rust predicate FnMut. It isn't leveraged by the python implementation because of layer mismatch for the efficient rust interface and Python working with `DAGOpNode` objects. A function using that interface is added to filter labeled nodes. In the preset pass manager we only use FilterOpNodes to remove nodes with a specific label (which is used to identify temporary barriers created by qiskit). In a follow up we should consider leveraging this new function to build a new pass specifically for this use case. Fixes #12263 Part of #12208 * Make filter_op_nodes() infallible The filter_op_nodes() method originally returned a Result<()> to handle a predicate that was fallible. This was because the original intent for the method was to use it with Python callbacks in the predicate. But because of differences between the rust API and the Python API this wasn't feasible as was originally planned. So this Result<()> return wasn't used anymore. This commit reworks it to make the filter_op_nodes() infallible and the predicate a user provides also only returns `bool` and not `Result<bool>`. * Rename filter_labelled_op to filter_labeled_op
- Loading branch information
Showing
8 changed files
with
101 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// This code is part of Qiskit. | ||
// | ||
// (C) Copyright IBM 2024 | ||
// | ||
// 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. | ||
|
||
use pyo3::prelude::*; | ||
use pyo3::wrap_pyfunction; | ||
|
||
use qiskit_circuit::dag_circuit::DAGCircuit; | ||
use qiskit_circuit::packed_instruction::PackedInstruction; | ||
use rustworkx_core::petgraph::stable_graph::NodeIndex; | ||
|
||
#[pyfunction] | ||
#[pyo3(name = "filter_op_nodes")] | ||
pub fn py_filter_op_nodes( | ||
py: Python, | ||
dag: &mut DAGCircuit, | ||
predicate: &Bound<PyAny>, | ||
) -> PyResult<()> { | ||
let callable = |node: NodeIndex| -> PyResult<bool> { | ||
let dag_op_node = dag.get_node(py, node)?; | ||
predicate.call1((dag_op_node,))?.extract() | ||
}; | ||
let mut remove_nodes: Vec<NodeIndex> = Vec::new(); | ||
for node in dag.op_nodes(true) { | ||
if !callable(node)? { | ||
remove_nodes.push(node); | ||
} | ||
} | ||
for node in remove_nodes { | ||
dag.remove_op_node(node); | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Remove any nodes that have the provided label set | ||
/// | ||
/// Args: | ||
/// dag (DAGCircuit): The dag circuit to filter the ops from | ||
/// label (str): The label to filter nodes on | ||
#[pyfunction] | ||
pub fn filter_labeled_op(dag: &mut DAGCircuit, label: String) { | ||
let predicate = |node: &PackedInstruction| -> bool { | ||
match node.label() { | ||
Some(inst_label) => inst_label != label, | ||
None => false, | ||
} | ||
}; | ||
dag.filter_op_nodes(predicate); | ||
} | ||
|
||
pub fn filter_op_nodes_mod(m: &Bound<PyModule>) -> PyResult<()> { | ||
m.add_wrapped(wrap_pyfunction!(py_filter_op_nodes))?; | ||
m.add_wrapped(wrap_pyfunction!(filter_labeled_op))?; | ||
Ok(()) | ||
} |
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
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