Skip to content

Commit

Permalink
Add helper function for layout creation in VF2Layout scoring loop
Browse files Browse the repository at this point in the history
  • Loading branch information
mtreinish committed Nov 18, 2022
1 parent 7c38723 commit 50aab71
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions qiskit/transpiler/passes/layout/vf2_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 50aab71

Please sign in to comment.