Skip to content

Commit

Permalink
Reuse scratch space for Sabre best-swap choice (Qiskit#10783)
Browse files Browse the repository at this point in the history
* Reuse scratch space for Sabre best-swap choice

This re-uses the same growable scratch space for storing the temporary
swaps for each choice.  This particular space typically needed to grow
several times for each swap choice, which was taking up a non-negligible
amount of the time we spent in Rust space, especially for large circuits
on big coupling maps.

* Add comment on scratch space
  • Loading branch information
jakelishman authored Sep 6, 2023
1 parent 180f19a commit 7252db3
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion crates/accelerate/src/sabre_swap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,10 @@ fn swap_map_trial(
// Main logic loop; the front layer only becomes empty when all nodes have been routed. At
// each iteration of this loop, we route either one or two gates.
let mut routable_nodes = Vec::<NodeIndex>::with_capacity(2);
// Reusable allocated storage space for choosing the best swap. This is owned outside of the
// `choose_best_swap` function so that we don't need to reallocate and then re-grow the
// collection on every entry.
let mut swap_scratch = Vec::<[VirtualQubit; 2]>::new();
while !front_layer.is_empty() {
let mut current_swaps: Vec<[VirtualQubit; 2]> = Vec::new();
// Swap-mapping loop. This is the main part of the algorithm, which we repeat until we
Expand All @@ -397,6 +401,7 @@ fn swap_map_trial(
&qubits_decay,
heuristic,
&mut rng,
&mut swap_scratch,
);
front_layer.routable_after(&mut routable_nodes, &best_swap, &layout, coupling_graph);
current_swaps.push(best_swap);
Expand Down Expand Up @@ -688,9 +693,10 @@ fn choose_best_swap(
qubits_decay: &[f64],
heuristic: &Heuristic,
rng: &mut Pcg64Mcg,
best_swaps: &mut Vec<[VirtualQubit; 2]>,
) -> [VirtualQubit; 2] {
best_swaps.clear();
let mut min_score = f64::MAX;
let mut best_swaps: Vec<[VirtualQubit; 2]> = Vec::new();
// The decay heuristic is the only one that actually needs the absolute score.
let absolute_score = match heuristic {
Heuristic::Decay => {
Expand Down

0 comments on commit 7252db3

Please sign in to comment.