Skip to content
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

Unbox the DAGCircuit::op_nodes iterator #13680

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 7 additions & 20 deletions crates/circuit/src/dag_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5668,28 +5668,15 @@ impl DAGCircuit {
}

/// Returns an iterator over all the indices that refer to an `Operation` node in the `DAGCircuit.`
pub fn op_nodes<'a>(
&'a self,
include_directives: bool,
) -> Box<dyn Iterator<Item = NodeIndex> + 'a> {
let node_ops_iter = self
.dag
pub fn op_nodes(&self, include_directives: bool) -> impl Iterator<Item = NodeIndex> + '_ {
self.dag
.node_references()
.filter_map(|(node_index, node_type)| match node_type {
NodeType::Operation(ref node) => Some((node_index, node)),
_ => None,
});
if !include_directives {
Box::new(node_ops_iter.filter_map(|(index, node)| {
if !node.op.directive() {
Some(index)
} else {
None
.filter_map(move |(node_index, node_type)| match node_type {
NodeType::Operation(ref node) => {
(include_directives || !node.op.directive()).then_some(node_index)
}
}))
} else {
Box::new(node_ops_iter.map(|(index, _)| index))
}
_ => None,
})
}

/// Return an iterator of 2 qubit operations. Ignore directives like snapshot and barrier.
Expand Down
Loading