Skip to content

Commit

Permalink
Use VF2 to find a partial layout for seeding a SabreLayout trial
Browse files Browse the repository at this point in the history
This commit builds on the VF2PartialLayout pass which was an experiment
available as an external plugin here:

https://github.com/mtreinish/vf2_partial_layout

That pass used the vf2 algorithm in rustworkx to find the deepest
partial interaction graph of a circuit which is isomorphic with the
coupling graph and uses that mapping to apply an initial layout. The
issue with the performance of that pass was the selection of the qubits
outside the partial interaction graph. Selecting the mapping for those
qubits is similar to the same heuristic layout that SabreLayout is
trying to solve, just for a subset of qubits. In VF2PartialLayout a
simple nearest neighbor based approach was used for selecting qubits
from the coupling graph for any virtual qubits outside the partial
layout. In practice this ended up performing worse than SabreLayout.

To address the shortcomings of that pass this commit combines the
partial layout selection from that external plugin with SabreLayout.
The sabre layout algorithm starts by randomly selecting a layout and
then progressively working forwards and backwards across the circuit
and swap mapping it to find the permutation caused by inserted swaps.
Those permutations are then used to modify the random layout and
eventual an initial layout that minimizes the number of swaps needed is
selected. With this commit instead of using a completely random layout
for all the initial guesses this starts a single trial with the partial
layout found in the same way as VF2PartialLayout. Then the remaining
qubits are selected at random and the Sabrelayout algorithm is run in
the same manner as before. This hopefully should improve the quality
of the results because we're starting from a partial layout that
doesn't require swaps for those qubits.

A similar (almost identical approach) was tried in Qiskit#9174 except instead
of seeding a single trial with the partial layout it used the partial
layout for all the the trials. In that case the results were not
generally better and the results were mixed. At the time my guess was
that using the partial layout constrained the search space too much and
was inducing more swaps to be needed. However, looking at the details in
issue Qiskit#10160 this adapts Qiskit#9174 to see if doing the partial layout in a
more limited manner has any impact there.
  • Loading branch information
mtreinish committed May 26, 2023
1 parent 5013fe2 commit ca4c3bd
Show file tree
Hide file tree
Showing 5 changed files with 260 additions and 9 deletions.
47 changes: 43 additions & 4 deletions crates/accelerate/src/sabre_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub fn sabre_layout_and_routing(
num_swap_trials: usize,
num_layout_trials: usize,
seed: Option<u64>,
partial_layout: Option<Vec<Option<usize>>>,
) -> ([NLayout; 2], SwapMap, PyObject) {
let run_in_parallel = getenv_use_multiple_threads();
let outer_rng = match seed {
Expand All @@ -57,6 +58,7 @@ pub fn sabre_layout_and_routing(
.into_par_iter()
.enumerate()
.map(|(index, seed_trial)| {
let partial = if index > 0 { &partial_layout } else { &None };
(
index,
layout_trial(
Expand All @@ -69,6 +71,7 @@ pub fn sabre_layout_and_routing(
max_iterations,
num_swap_trials,
run_in_parallel,
partial.clone(),
),
)
})
Expand All @@ -83,7 +86,9 @@ pub fn sabre_layout_and_routing(
} else {
seed_vec
.into_iter()
.map(|seed_trial| {
.enumerate()
.map(|(index, seed_trial)| {
let partial = if index > 0 { &partial_layout } else { &None };
layout_trial(
num_clbits,
&mut dag_nodes,
Expand All @@ -94,6 +99,7 @@ pub fn sabre_layout_and_routing(
max_iterations,
num_swap_trials,
run_in_parallel,
partial.clone(),
)
})
.min_by_key(|result| result.1.map.values().map(|x| x.len()).sum::<usize>())
Expand All @@ -112,13 +118,46 @@ fn layout_trial(
max_iterations: usize,
num_swap_trials: usize,
run_swap_in_parallel: bool,
partial_layout: Option<Vec<Option<usize>>>,
) -> ([NLayout; 2], SwapMap, Vec<usize>) {
// Pick a random initial layout and fully populate ancillas in that layout too
let num_physical_qubits = distance_matrix.shape()[0];
let mut rng = Pcg64Mcg::seed_from_u64(seed);
let mut physical_qubits: Vec<usize> = (0..num_physical_qubits).collect();
physical_qubits.shuffle(&mut rng);
let mut initial_layout = NLayout::from_logical_to_physical(physical_qubits);
let mut physical_qubits: Vec<usize>;
match partial_layout {
Some(partial_layout_bits) => {
let used_bits: HashSet<usize> = partial_layout_bits
.iter()
.filter_map(|x| x.as_ref())
.copied()
.collect();
let mut free_bits: Vec<usize> = (0..num_physical_qubits)
.filter(|x| !used_bits.contains(x))
.collect();
free_bits.shuffle(&mut rng);
physical_qubits = partial_layout_bits
.iter()
.map(|x| match x {
Some(phys) => *phys,
None => free_bits.pop().unwrap(),
})
.collect();
}
None => {
physical_qubits = (0..num_physical_qubits).collect();
physical_qubits.shuffle(&mut rng);
}
};
let mut phys_to_logic = vec![0; num_physical_qubits];
physical_qubits
.iter()
.enumerate()
.for_each(|(logic, phys)| phys_to_logic[*phys] = logic);

let mut initial_layout = NLayout {
logic_to_phys: physical_qubits,
phys_to_logic,
};
let mut rev_dag_nodes: Vec<(usize, Vec<usize>, HashSet<usize>)> =
dag_nodes.iter().rev().cloned().collect();
for _iter in 0..max_iterations {
Expand Down
218 changes: 213 additions & 5 deletions qiskit/transpiler/passes/layout/sabre_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
"""Layout selection using the SABRE bidirectional search approach from Li et al.
"""

from collections import defaultdict
import copy
import logging
import time

import numpy as np
import rustworkx as rx

Expand All @@ -24,6 +27,7 @@
from qiskit.transpiler.passes.layout.enlarge_with_ancilla import EnlargeWithAncilla
from qiskit.transpiler.passes.layout.apply_layout import ApplyLayout
from qiskit.transpiler.passes.layout import disjoint_utils
from qiskit.transpiler.passes.layout import vf2_utils
from qiskit.transpiler.passmanager import PassManager
from qiskit.transpiler.layout import Layout
from qiskit.transpiler.basepasses import TransformationPass
Expand All @@ -38,18 +42,25 @@
from qiskit.transpiler.target import Target
from qiskit.transpiler.coupling import CouplingMap
from qiskit.tools.parallel import CPU_COUNT
from qiskit.circuit.controlflow import ControlFlowOp, ForLoopOp
from qiskit.converters import circuit_to_dag

logger = logging.getLogger(__name__)


class SabreLayout(TransformationPass):
"""Choose a Layout via iterative bidirectional routing of the input circuit.
Starting with a random initial `Layout`, the algorithm does a full routing
of the circuit (via the `routing_pass` method) to end up with a
`final_layout`. This final_layout is then used as the initial_layout for
routing the reverse circuit. The algorithm iterates a number of times until
it finds an initial_layout that reduces full routing cost.
The algorithm does a full routing of the circuit (via the `routing_pass`
method) to end up with a `final_layout`. This final_layout is then used as
the initial_layout for routing the reverse circuit. The algorithm iterates a
number of times until it finds an initial_layout that reduces full routing cost.
Prior to running the SABRE algorithm this transpiler pass will try to find the layout
for deepest layer that is has an isomorphic subgraph in the coupling graph. This is
done by progressively using the algorithm from :class:`~.VF2Layout` on the circuit
until a mapping is not found. This partial layout is then used to seed the SABRE algorithm
and then random physical bits are selected for the remaining elements in the mapping.
This method exploits the reversibility of quantum circuits, and tries to
include global circuit information in the choice of initial_layout.
Expand Down Expand Up @@ -85,6 +96,10 @@ def __init__(
swap_trials=None,
layout_trials=None,
skip_routing=False,
vf2_partial_layout=True,
vf2_call_limit=None,
vf2_time_limit=None,
vf2_max_trials=None,
):
"""SabreLayout initializer.
Expand Down Expand Up @@ -121,6 +136,16 @@ def __init__(
will be returned in the property set. This is a tradeoff to run custom
routing with multiple layout trials, as using this option will cause
SabreLayout to run the routing stage internally but not use that result.
vf2_partial_layout (bool): Run vf2 partial layout
vf2_call_limit (int): The number of state visits to attempt in each execution of
VF2 to attempt to find a partial layout.
vf2_time_limit (float): The total time limit in seconds to run VF2 to find a partial
layout
vf2_max_trials (int): The maximum number of trials to run VF2 to find
a partial layout. If this is not specified the number of trials will be limited
based on the number of edges in the interaction graph or the coupling graph
(whichever is larger) if no other limits are set. If set to a value <= 0 no
limit on the number of trials will be set.
Raises:
TranspilerError: If both ``routing_pass`` and ``swap_trials`` or
Expand Down Expand Up @@ -158,6 +183,11 @@ def __init__(
self.coupling_map = copy.deepcopy(self.coupling_map)
self.coupling_map.make_symmetric()
self._neighbor_table = NeighborTable(rx.adjacency_matrix(self.coupling_map.graph))
self.avg_error_map = None
self.vf2_partial_layout = vf2_partial_layout
self.call_limit = vf2_call_limit
self.time_limit = vf2_time_limit
self.max_trials = vf2_max_trials

def run(self, dag):
"""Run the SabreLayout pass on `dag`.
Expand Down Expand Up @@ -321,6 +351,13 @@ def _inner_run(self, dag, coupling_map):
cargs,
)
)
partial_layout = None
if self.vf2_partial_layout:
partial_layout_virtual_bits = self._vf2_partial_layout(
dag, coupling_map
).get_virtual_bits()
partial_layout = [partial_layout_virtual_bits.get(i, None) for i in dag.qubits]

((initial_layout, final_layout), swap_map, gate_order) = sabre_layout_and_routing(
len(dag.clbits),
dag_list,
Expand All @@ -331,6 +368,7 @@ def _inner_run(self, dag, coupling_map):
self.swap_trials,
self.layout_trials,
self.seed,
partial_layout,
)
# Apply initial layout selected.
layout_dict = {}
Expand Down Expand Up @@ -385,3 +423,173 @@ def _compose_layouts(self, initial_layout, pass_final_layout, qregs):
qubit_map = Layout.combine_into_edge_map(initial_layout, trivial_layout)
final_layout = {v: pass_final_layout._v2p[qubit_map[v]] for v in initial_layout._v2p}
return Layout(final_layout)

# TODO: Migrate this to rust as part of sabre_layout.rs after
# https://github.com/Qiskit/rustworkx/issues/741 is implemented and released
def _vf2_partial_layout(self, dag, coupling_map):
"""Find a partial layout using vf2 on the deepest subgraph that is isomorphic to
the coupling graph."""
im_graph_node_map = {}
reverse_im_graph_node_map = {}
im_graph = rx.PyGraph(multigraph=False)
logger.debug("Buidling interaction graphs")
largest_im_graph = None
best_mapping = None
first_mapping = None
if self.avg_error_map is None:
self.avg_error_map = vf2_utils.build_average_error_map(self.target, None, coupling_map)

cm_graph, cm_nodes = vf2_utils.shuffle_coupling_graph(coupling_map, self.seed, False)
# To avoid trying to over optimize the result by default limit the number
# of trials based on the size of the graphs. For circuits with simple layouts
# like an all 1q circuit we don't want to sit forever trying every possible
# mapping in the search space if no other limits are set
if self.max_trials is None and self.call_limit is None and self.time_limit is None:
im_graph_edge_count = len(im_graph.edge_list())
cm_graph_edge_count = len(coupling_map.graph.edge_list())
self.max_trials = max(im_graph_edge_count, cm_graph_edge_count) + 15

start_time = time.time()

# A more efficient search pattern would be to do a binary search
# and find, but to conserve memory and avoid a large number of
# unecessary graphs this searchs from the beginning and continues
# until there is no vf2 match
def _visit(dag, weight, wire_map):
for node in dag.topological_op_nodes():
nonlocal largest_im_graph
largest_im_graph = im_graph.copy()
if getattr(node.op, "_directive", False):
continue
if isinstance(node.op, ControlFlowOp):
if isinstance(node.op, ForLoopOp):
inner_weight = len(node.op.params[0]) * weight
else:
inner_weight = weight
for block in node.op.blocks:
inner_wire_map = {
inner: wire_map[outer] for outer, inner in zip(node.qargs, block.qubits)
}
_visit(circuit_to_dag(block), inner_weight, inner_wire_map)
continue
len_args = len(node.qargs)
qargs = [wire_map[q] for q in node.qargs]
if len_args == 1:
if qargs[0] not in im_graph_node_map:
weights = defaultdict(int)
weights[node.name] += weight
im_graph_node_map[qargs[0]] = im_graph.add_node(weights)
reverse_im_graph_node_map[im_graph_node_map[qargs[0]]] = qargs[0]
else:
im_graph[im_graph_node_map[qargs[0]]][node.op.name] += weight
if len_args == 2:
if qargs[0] not in im_graph_node_map:
im_graph_node_map[qargs[0]] = im_graph.add_node(defaultdict(int))
reverse_im_graph_node_map[im_graph_node_map[qargs[0]]] = qargs[0]
if qargs[1] not in im_graph_node_map:
im_graph_node_map[qargs[1]] = im_graph.add_node(defaultdict(int))
reverse_im_graph_node_map[im_graph_node_map[qargs[1]]] = qargs[1]
edge = (im_graph_node_map[qargs[0]], im_graph_node_map[qargs[1]])
if im_graph.has_edge(*edge):
im_graph.get_edge_data(*edge)[node.name] += weight
else:
weights = defaultdict(int)
weights[node.name] += weight
im_graph.add_edge(*edge, weights)
if len_args > 2:
raise TranspilerError(
"Encountered an instruction operating on more than 2 qubits, this pass "
"only functions with 1 or 2 qubit operations."
)
vf2_mapping = rx.vf2_mapping(
cm_graph,
im_graph,
subgraph=True,
id_order=False,
induced=False,
call_limit=self.call_limit,
)
try:
nonlocal first_mapping
first_mapping = next(vf2_mapping)
except StopIteration:
break
nonlocal best_mapping
best_mapping = vf2_mapping
elapsed_time = time.time() - start_time
if (
self.time_limit is not None
and best_mapping is not None
and elapsed_time >= self.time_limit
):
logger.debug(
"SabreLayout VF2 heuristic has taken %s which exceeds configured max time: %s",
elapsed_time,
self.time_limit,
)
break

_visit(dag, 1, {bit: bit for bit in dag.qubits})
logger.debug("Finding best mappings of largest partial subgraph")
im_graph = largest_im_graph

def mapping_to_layout(layout_mapping):
return Layout({reverse_im_graph_node_map[k]: v for k, v in layout_mapping.items()})

layout_mapping = {im_i: cm_nodes[cm_i] for cm_i, im_i in first_mapping.items()}
chosen_layout = mapping_to_layout(layout_mapping)
chosen_layout_score = vf2_utils.score_layout(
self.avg_error_map,
layout_mapping,
im_graph_node_map,
reverse_im_graph_node_map,
im_graph,
False,
)
trials = 1
for mapping in best_mapping: # pylint: disable=not-an-iterable
trials += 1
logger.debug("Running trial: %s", trials)
layout_mapping = {im_i: cm_nodes[cm_i] for cm_i, im_i in mapping.items()}
# If the graphs have the same number of nodes we don't need to score or do multiple
# trials as the score heuristic currently doesn't weigh nodes based on gates on a
# qubit so the scores will always all be the same
if len(cm_graph) == len(im_graph):
break
layout_score = vf2_utils.score_layout(
self.avg_error_map,
layout_mapping,
im_graph_node_map,
reverse_im_graph_node_map,
im_graph,
False,
)
logger.debug("Trial %s has score %s", trials, layout_score)
if chosen_layout is None:
chosen_layout = mapping_to_layout(layout_mapping)
chosen_layout_score = layout_score
elif layout_score < chosen_layout_score:
layout = mapping_to_layout(layout_mapping)
logger.debug(
"Found layout %s has a lower score (%s) than previous best %s (%s)",
layout,
layout_score,
chosen_layout,
chosen_layout_score,
)
chosen_layout = layout
chosen_layout_score = layout_score
if self.max_trials and trials >= self.max_trials:
logger.debug("Trial %s is >= configured max trials %s", trials, self.max_trials)
break
elapsed_time = time.time() - start_time
if self.time_limit is not None and elapsed_time >= self.time_limit:
logger.debug(
"VF2Layout has taken %s which exceeds configured max time: %s",
elapsed_time,
self.time_limit,
)
break
for reg in dag.qregs.values():
chosen_layout.add_register(reg)
return chosen_layout
2 changes: 2 additions & 0 deletions qiskit/transpiler/preset_passmanagers/level1.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ def _vf2_match_not_found(property_set):
layout_trials=5,
skip_routing=pass_manager_config.routing_method is not None
and routing_method != "sabre",
vf2_call_limit=int(5e4),
)
elif layout_method is None:
_improve_layout = common.if_has_control_flow_else(
Expand All @@ -168,6 +169,7 @@ def _vf2_match_not_found(property_set):
layout_trials=5,
skip_routing=pass_manager_config.routing_method is not None
and routing_method != "sabre",
vf2_call_limit=int(5e4),
),
).to_flow_controller()

Expand Down
Loading

0 comments on commit ca4c3bd

Please sign in to comment.