From 50aab71321fd11fe517379130143da058f239dfc Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Fri, 18 Nov 2022 17:28:40 -0500 Subject: [PATCH] Add helper function for layout creation in VF2Layout scoring loop --- qiskit/transpiler/passes/layout/vf2_layout.py | 25 ++++++++----------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/qiskit/transpiler/passes/layout/vf2_layout.py b/qiskit/transpiler/passes/layout/vf2_layout.py index 4dc34da6e2fe..bdeaacb8399f 100644 --- a/qiskit/transpiler/passes/layout/vf2_layout.py +++ b/qiskit/transpiler/passes/layout/vf2_layout.py @@ -168,25 +168,26 @@ def run(self, dag): os.getenv("QISKIT_IN_PARALLEL", "FALSE").upper() != "TRUE" or os.getenv("QISKIT_FORCE_THREADS", "FALSE").upper() == "TRUE" ) + + def mapping_to_layout(layout_mapping): + return Layout({reverse_im_graph_node_map[k]: v for k, v in layout_mapping.items()}) + for mapping in mappings: trials += 1 logger.debug("Running trial: %s", trials) stop_reason = VF2LayoutStopReason.SOLUTION_FOUND 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): - chosen_layout = Layout( - {reverse_im_graph_node_map[k]: v for k, v in layout_mapping.items()} - ) + chosen_layout = mapping_to_layout(layout_mapping) break # If there is no error map avilable we can just skip the scoring stage as there # is nothing to score with, so any match is the best we can find. if self.avg_error_map is None: - chosen_layout = Layout( - {reverse_im_graph_node_map[k]: v for k, v in layout_mapping.items()} - ) + chosen_layout = mapping_to_layout(layout_mapping) break layout_score = vf2_utils.score_layout( self.avg_error_map, @@ -201,20 +202,14 @@ def run(self, dag): # waste time finding additional mappings that will at best match # the performance, so exit early in this case if layout_score == 0.0: - chosen_layout = Layout( - {reverse_im_graph_node_map[k]: v for k, v in layout_mapping.items()} - ) + chosen_layout = mapping_to_layout(layout_mapping) break logger.debug("Trial %s has score %s", trials, layout_score) if chosen_layout is None: - chosen_layout = Layout( - {reverse_im_graph_node_map[k]: v for k, v in layout_mapping.items()} - ) + chosen_layout = mapping_to_layout(layout_mapping) chosen_layout_score = layout_score elif layout_score < chosen_layout_score: - layout = Layout( - {reverse_im_graph_node_map[k]: v for k, v in layout_mapping.items()} - ) + layout = mapping_to_layout(layout_mapping) logger.debug( "Found layout %s has a lower score (%s) than previous best %s (%s)", layout,