Skip to content

Commit

Permalink
unwrapped DAGCircuit::collect_runs() to not return an Option (#13222)
Browse files Browse the repository at this point in the history
* changed collect runs to unwrap Option and route error to DAGCircuit

* changes by @kevinhartman

* updated py_collect_runs

* update collect_runs calls and specified lifteime of return

---------

Co-authored-by: Kevin Hartman <kevin@hart.mn>
  • Loading branch information
cameron-d28 and kevinhartman authored Sep 27, 2024
1 parent 43feab3 commit 9778462
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
4 changes: 2 additions & 2 deletions crates/accelerate/src/inverse_cancellation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fn run_on_self_inverse(
}
let mut collect_set: HashSet<String> = HashSet::with_capacity(1);
collect_set.insert(gate.operation.name().to_string());
let gate_runs: Vec<Vec<NodeIndex>> = dag.collect_runs(collect_set).unwrap().collect();
let gate_runs: Vec<Vec<NodeIndex>> = dag.collect_runs(collect_set).collect();
for gate_cancel_run in gate_runs {
let mut partitions: Vec<Vec<NodeIndex>> = Vec::new();
let mut chunk: Vec<NodeIndex> = Vec::new();
Expand Down Expand Up @@ -128,7 +128,7 @@ fn run_on_inverse_pairs(
.iter()
.map(|x| x.operation.name().to_string())
.collect();
let runs: Vec<Vec<NodeIndex>> = dag.collect_runs(names).unwrap().collect();
let runs: Vec<Vec<NodeIndex>> = dag.collect_runs(names).collect();
for nodes in runs {
let mut i = 0;
while i < nodes.len() - 1 {
Expand Down
40 changes: 17 additions & 23 deletions crates/circuit/src/dag_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4388,27 +4388,18 @@ def _format(operand):
for name in namelist.iter() {
name_list_set.insert(name.extract::<String>()?);
}
match self.collect_runs(name_list_set) {
Some(runs) => {
let run_iter = runs.map(|node_indices| {
PyTuple::new_bound(
py,
node_indices
.into_iter()
.map(|node_index| self.get_node(py, node_index).unwrap()),
)
.unbind()
});
let out_set = PySet::empty_bound(py)?;
for run_tuple in run_iter {
out_set.add(run_tuple)?;
}
Ok(out_set.unbind())
}
None => Err(PyRuntimeError::new_err(
"Invalid DAGCircuit, cycle encountered",
)),

let out_set = PySet::empty_bound(py)?;

for run in self.collect_runs(name_list_set) {
let run_tuple = PyTuple::new_bound(
py,
run.into_iter()
.map(|node_index| self.get_node(py, node_index).unwrap()),
);
out_set.add(run_tuple)?;
}
Ok(out_set.unbind())
}

/// Return a set of non-conditional runs of 1q "op" nodes.
Expand Down Expand Up @@ -4970,7 +4961,7 @@ impl DAGCircuit {
pub fn collect_runs(
&self,
namelist: HashSet<String>,
) -> Option<impl Iterator<Item = Vec<NodeIndex>> + '_> {
) -> impl Iterator<Item = Vec<NodeIndex>> + '_ {
let filter_fn = move |node_index: NodeIndex| -> Result<bool, Infallible> {
let node = &self.dag[node_index];
match node {
Expand All @@ -4980,8 +4971,11 @@ impl DAGCircuit {
_ => Ok(false),
}
};
rustworkx_core::dag_algo::collect_runs(&self.dag, filter_fn)
.map(|node_iter| node_iter.map(|x| x.unwrap()))

match rustworkx_core::dag_algo::collect_runs(&self.dag, filter_fn) {
Some(iter) => iter.map(|result| result.unwrap()),
None => panic!("invalid DAG: cycle(s) detected!"),
}
}

/// Return a set of non-conditional runs of 1q "op" nodes.
Expand Down

0 comments on commit 9778462

Please sign in to comment.