Skip to content

Commit d374427

Browse files
authored
Unbox the DAGCircuit::op_nodes iterator (Qiskit#13680)
This allows the iterator to be (reliably) properly monomorphised into its usage sites. Since the dynamic `include_directives` call is almost certainly given as a literal, I would strongly expect that even the trivial overhead of the short-circuiting `||` operator will be compiled out in all cases.
1 parent dffc2df commit d374427

File tree

1 file changed

+7
-20
lines changed

1 file changed

+7
-20
lines changed

crates/circuit/src/dag_circuit.rs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5668,28 +5668,15 @@ impl DAGCircuit {
56685668
}
56695669

56705670
/// Returns an iterator over all the indices that refer to an `Operation` node in the `DAGCircuit.`
5671-
pub fn op_nodes<'a>(
5672-
&'a self,
5673-
include_directives: bool,
5674-
) -> Box<dyn Iterator<Item = NodeIndex> + 'a> {
5675-
let node_ops_iter = self
5676-
.dag
5671+
pub fn op_nodes(&self, include_directives: bool) -> impl Iterator<Item = NodeIndex> + '_ {
5672+
self.dag
56775673
.node_references()
5678-
.filter_map(|(node_index, node_type)| match node_type {
5679-
NodeType::Operation(ref node) => Some((node_index, node)),
5680-
_ => None,
5681-
});
5682-
if !include_directives {
5683-
Box::new(node_ops_iter.filter_map(|(index, node)| {
5684-
if !node.op.directive() {
5685-
Some(index)
5686-
} else {
5687-
None
5674+
.filter_map(move |(node_index, node_type)| match node_type {
5675+
NodeType::Operation(ref node) => {
5676+
(include_directives || !node.op.directive()).then_some(node_index)
56885677
}
5689-
}))
5690-
} else {
5691-
Box::new(node_ops_iter.map(|(index, _)| index))
5692-
}
5678+
_ => None,
5679+
})
56935680
}
56945681

56955682
/// Return an iterator of 2 qubit operations. Ignore directives like snapshot and barrier.

0 commit comments

Comments
 (0)