Skip to content

Commit

Permalink
refactor: cleanup code a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
ss2165 committed Dec 15, 2023
1 parent e8c9c65 commit 244067b
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions tket2/src/passes/commutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ fn available_slice(
) -> Option<(usize, HashMap<Qb, Rc<ComCommand>>)> {
let mut available = None;
let mut prev_nodes: HashMap<Qb, Rc<ComCommand>> = HashMap::new();

for slice_index in (0..starting_index + 1).rev() {
for slice_index in (0..=starting_index).rev() {
// if all qubit slots are empty here the command can be moved here
if command
.qubits()
Expand Down Expand Up @@ -304,26 +303,25 @@ pub fn apply_greedy_commutation(circ: &mut Hugr) -> Result<u32, PullForwardError
.collect();

for command in slice_commands {
if let Some((destination, new_nexts)) =
let Some((destination, new_nexts)) =
available_slice(&circ, &slice_vec, slice_index, &command)
{
debug_assert!(
destination < slice_index,
"Avoid mutating slices we haven't got to yet."
);
for q in command.qubits() {
let com = slice_vec[slice_index][q.index()].take();
slice_vec[destination][q.index()] = com;
}
let rewrite = PullForward { command, new_nexts };
circ.apply_rewrite(rewrite)?;
count += 1;
else {
continue;
};

debug_assert!(
destination < slice_index,
"Avoid mutating slices we haven't got to yet."
);
for q in command.qubits() {
let com = slice_vec[slice_index][q.index()].take();
slice_vec[destination][q.index()] = com;
}
let rewrite = PullForward { command, new_nexts };
circ.apply_rewrite(rewrite)?;
count += 1;
}
}

// TODO remove empty slices and return
// and full slices at start?
Ok(count)
}

Expand Down Expand Up @@ -546,10 +544,7 @@ mod test {
slices[1][1].as_ref().unwrap().clone()
);

assert_eq!(
*prev_nodes.get(&Qb::new(3)).unwrap(),
slices[2][3].as_ref().unwrap().clone()
);
assert!(prev_nodes.get(&Qb::new(3)).is_none());
}

#[rstest]
Expand Down

0 comments on commit 244067b

Please sign in to comment.