Skip to content

Commit

Permalink
Avoid allocation in loop.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinhartman committed Nov 28, 2023
1 parent 43f0167 commit 1ab85c4
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions crates/accelerate/src/sabre_swap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,12 @@ fn populate_extended_set(
let mut decremented: IndexMap<usize, u32, ahash::RandomState> =
IndexMap::with_hasher(ahash::RandomState::default());
let mut i = 0;
let mut visit_now: Vec<NodeIndex> = Vec::new();
while i < to_visit.len() && extended_set.len() < EXTENDED_SET_SIZE {
// Visit runs of non-2Q gates fully before moving on to children
// of 2Q gates. This way, traversal order is a BFS of 2Q gates rather
// than of all gates.
let mut visit_now = vec![to_visit[i]];
visit_now.push(to_visit[i]);
let mut j = 0;
while let Some(node) = visit_now.get(j) {
for edge in dag.dag.edges_directed(*node, Direction::Outgoing) {
Expand All @@ -185,7 +186,8 @@ fn populate_extended_set(
required_predecessors[successor_index] -= 1;
if required_predecessors[successor_index] == 0 {
if !dag.dag[successor_node].directive
&& !dag.node_blocks.contains_key(&successor_index) {
&& !dag.node_blocks.contains_key(&successor_index)
{
if let [a, b] = dag.dag[successor_node].qubits[..] {
extended_set.push([a.to_phys(layout), b.to_phys(layout)]);
to_visit.push(successor_node);
Expand All @@ -197,6 +199,7 @@ fn populate_extended_set(
}
j += 1;
}
visit_now.clear();
i += 1;
}
for (node, amount) in decremented.iter() {
Expand Down

0 comments on commit 1ab85c4

Please sign in to comment.