Skip to content

Commit 9778462

Browse files
unwrapped DAGCircuit::collect_runs() to not return an Option (Qiskit#13222)
* 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 <[email protected]>
1 parent 43feab3 commit 9778462

File tree

2 files changed

+19
-25
lines changed

2 files changed

+19
-25
lines changed

crates/accelerate/src/inverse_cancellation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ fn run_on_self_inverse(
5959
}
6060
let mut collect_set: HashSet<String> = HashSet::with_capacity(1);
6161
collect_set.insert(gate.operation.name().to_string());
62-
let gate_runs: Vec<Vec<NodeIndex>> = dag.collect_runs(collect_set).unwrap().collect();
62+
let gate_runs: Vec<Vec<NodeIndex>> = dag.collect_runs(collect_set).collect();
6363
for gate_cancel_run in gate_runs {
6464
let mut partitions: Vec<Vec<NodeIndex>> = Vec::new();
6565
let mut chunk: Vec<NodeIndex> = Vec::new();
@@ -128,7 +128,7 @@ fn run_on_inverse_pairs(
128128
.iter()
129129
.map(|x| x.operation.name().to_string())
130130
.collect();
131-
let runs: Vec<Vec<NodeIndex>> = dag.collect_runs(names).unwrap().collect();
131+
let runs: Vec<Vec<NodeIndex>> = dag.collect_runs(names).collect();
132132
for nodes in runs {
133133
let mut i = 0;
134134
while i < nodes.len() - 1 {

crates/circuit/src/dag_circuit.rs

+17-23
Original file line numberDiff line numberDiff line change
@@ -4388,27 +4388,18 @@ def _format(operand):
43884388
for name in namelist.iter() {
43894389
name_list_set.insert(name.extract::<String>()?);
43904390
}
4391-
match self.collect_runs(name_list_set) {
4392-
Some(runs) => {
4393-
let run_iter = runs.map(|node_indices| {
4394-
PyTuple::new_bound(
4395-
py,
4396-
node_indices
4397-
.into_iter()
4398-
.map(|node_index| self.get_node(py, node_index).unwrap()),
4399-
)
4400-
.unbind()
4401-
});
4402-
let out_set = PySet::empty_bound(py)?;
4403-
for run_tuple in run_iter {
4404-
out_set.add(run_tuple)?;
4405-
}
4406-
Ok(out_set.unbind())
4407-
}
4408-
None => Err(PyRuntimeError::new_err(
4409-
"Invalid DAGCircuit, cycle encountered",
4410-
)),
4391+
4392+
let out_set = PySet::empty_bound(py)?;
4393+
4394+
for run in self.collect_runs(name_list_set) {
4395+
let run_tuple = PyTuple::new_bound(
4396+
py,
4397+
run.into_iter()
4398+
.map(|node_index| self.get_node(py, node_index).unwrap()),
4399+
);
4400+
out_set.add(run_tuple)?;
44114401
}
4402+
Ok(out_set.unbind())
44124403
}
44134404

44144405
/// Return a set of non-conditional runs of 1q "op" nodes.
@@ -4970,7 +4961,7 @@ impl DAGCircuit {
49704961
pub fn collect_runs(
49714962
&self,
49724963
namelist: HashSet<String>,
4973-
) -> Option<impl Iterator<Item = Vec<NodeIndex>> + '_> {
4964+
) -> impl Iterator<Item = Vec<NodeIndex>> + '_ {
49744965
let filter_fn = move |node_index: NodeIndex| -> Result<bool, Infallible> {
49754966
let node = &self.dag[node_index];
49764967
match node {
@@ -4980,8 +4971,11 @@ impl DAGCircuit {
49804971
_ => Ok(false),
49814972
}
49824973
};
4983-
rustworkx_core::dag_algo::collect_runs(&self.dag, filter_fn)
4984-
.map(|node_iter| node_iter.map(|x| x.unwrap()))
4974+
4975+
match rustworkx_core::dag_algo::collect_runs(&self.dag, filter_fn) {
4976+
Some(iter) => iter.map(|result| result.unwrap()),
4977+
None => panic!("invalid DAG: cycle(s) detected!"),
4978+
}
49854979
}
49864980

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

0 commit comments

Comments
 (0)