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

unwrapped DAGCircuit::collect_runs() to not return an Option #13222

Merged
merged 5 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
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
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
Loading