From 36f0f0bd2f3b1ee3a2c470236914395b17c5c6bf Mon Sep 17 00:00:00 2001 From: Agustin Borgna Date: Fri, 7 Jun 2024 15:59:14 +0100 Subject: [PATCH] feat: `Circuit::operations` --- tket2/src/circuit.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tket2/src/circuit.rs b/tket2/src/circuit.rs index 5d3afff8..10728089 100644 --- a/tket2/src/circuit.rs +++ b/tket2/src/circuit.rs @@ -231,6 +231,22 @@ impl Circuit { CommandIterator::new(self) } + /// Returns the top-level operations in the circuit, in some topological + /// order. + /// + /// This is a subset of the commands returned by [`Circuit::commands`], only + /// including [`Tk2Op`]s, pytket ops, and any other custom operations. + /// + /// [`Tk2Op`]: crate::Tk2Op + #[inline] + pub fn operations(&self) -> impl Iterator> + '_ + where + Self: Sized, + { + // Traverse the circuit in topological order. + self.commands().filter(|cmd| cmd.optype().is_custom_op()) + } + /// Compute the cost of the circuit based on a per-operation cost function. #[inline] pub fn circuit_cost(&self, op_cost: F) -> C @@ -570,6 +586,7 @@ mod tests { ); assert_eq!(circ.qubit_count(), qubits); assert_eq!(circ.num_operations(), 3); + assert_eq!(circ.operations().count(), 3); assert_eq!(circ.units().count(), qubits + bits); assert_eq!(circ.nonlinear_units().count(), 0);