@@ -149,15 +149,15 @@ def _add_qubits_to_mapping_smart_init(current_mapping, graph,
149149
150150 Returns: A new mapping
151151 """
152- qubit_interaction_subgraphs = \
153- commands_dag .calculate_qubit_interaction_subgraphs (max_order = 2 )
152+ if not current_mapping :
153+ qubit_interaction_subgraphs = \
154+ commands_dag .calculate_qubit_interaction_subgraphs (max_order = 2 )
154155
155- # Interaction subgraph list can be empty if only single qubit gates are
156- # present
157- if not qubit_interaction_subgraphs :
158- qubit_interaction_subgraphs = [list (new_logical_qubit_ids )]
156+ # Interaction subgraph list can be empty if only single qubit gates are
157+ # present
158+ if not qubit_interaction_subgraphs :
159+ qubit_interaction_subgraphs = [list (new_logical_qubit_ids )]
159160
160- if not current_mapping :
161161 return _generate_mapping_minimize_swaps (graph ,
162162 qubit_interaction_subgraphs )
163163 return _add_qubits_to_mapping_fcfs (current_mapping , graph ,
@@ -185,15 +185,15 @@ def _add_qubits_to_mapping(current_mapping, graph, new_logical_qubit_ids,
185185
186186 Returns: A new mapping
187187 """
188- qubit_interaction_subgraphs = \
189- commands_dag .calculate_qubit_interaction_subgraphs (max_order = 2 )
188+ if not current_mapping :
189+ qubit_interaction_subgraphs = \
190+ commands_dag .calculate_qubit_interaction_subgraphs (max_order = 2 )
190191
191- # Interaction subgraph list can be empty if only single qubit gates are
192- # present
193- if not qubit_interaction_subgraphs :
194- qubit_interaction_subgraphs = [list (new_logical_qubit_ids )]
192+ # Interaction subgraph list can be empty if only single qubit gates are
193+ # present
194+ if not qubit_interaction_subgraphs :
195+ qubit_interaction_subgraphs = [list (new_logical_qubit_ids )]
195196
196- if not current_mapping :
197197 return _generate_mapping_minimize_swaps (graph ,
198198 qubit_interaction_subgraphs )
199199
@@ -213,17 +213,24 @@ def _add_qubits_to_mapping(current_mapping, graph, new_logical_qubit_ids,
213213 backend_id = None
214214
215215 if len (qubit_interactions ) == 1 :
216+ # If there's only a single qubit interacting and it is already
217+ # present within the mapping, find the neighbour with the highest
218+ # degree
219+
216220 qubit = qubit_interactions [0 ]
217221
218222 if qubit in mapping :
219- candidates = sorted ([
220- n for n in graph [mapping [qubit ]]
221- if n not in currently_used_nodes
222- ],
223- key = lambda n : len (graph [n ]))
223+ candidates = sorted (
224+ [n for n in graph [mapping [qubit ]] if n in available_nodes ],
225+ key = lambda n : len (graph [n ]))
224226 if candidates :
225227 backend_id = candidates [- 1 ]
226228 elif qubit_interactions :
229+ # If there are multiple qubits interacting, find out all the
230+ # neighbouring nodes for each interaction. Then within those
231+ # nodes, try to find the one that maximizes the number of
232+ # interactions without swapping
233+
227234 neighbours = []
228235 for qubit in qubit_interactions :
229236 if qubit in mapping :
@@ -233,14 +240,18 @@ def _add_qubits_to_mapping(current_mapping, graph, new_logical_qubit_ids,
233240 else :
234241 break
235242
243+ # Try to find an intersection that maximizes the number of
244+ # interactions by iteratively reducing the number of considered
245+ # interactions
246+
236247 intersection = set ()
237- while neighbours :
248+ while neighbours and not intersection :
238249 intersection = neighbours [0 ].intersection (* neighbours [1 :])
239- if intersection :
240- backend_id = intersection .pop ()
241- break
242250 neighbours .pop ()
243251
252+ if intersection :
253+ backend_id = intersection .pop ()
254+
244255 if backend_id is None :
245256 backend_id = available_nodes .pop ()
246257 else :
@@ -342,6 +353,11 @@ def __init__(self,
342353 - | Extra options to pass onto the cost function
343354 | (see :py:meth:`.MultiQubitGateManager.generate_swaps`)
344355 | Defaults to ``{'W': 0.5}``.
356+ * - max_swap_steps
357+ - ``int``
358+ - | Maximum number of swap steps per mapping
359+ | (see :py:meth:`.MultiQubitGateManager.generate_swaps`)
360+ | Defaults to 30
345361 """
346362 BasicMapperEngine .__init__ (self )
347363
@@ -483,7 +499,7 @@ def _send_possible_commands(self):
483499 if allocate_cmds and num_available_qubits > 0 :
484500
485501 def rank_allocate_cmds (cmds_list , dag ):
486- #pylint: disable=unused-argument
502+ # pylint: disable=unused-argument
487503 return cmds_list
488504
489505 allocate_cmds = rank_allocate_cmds (
@@ -663,7 +679,7 @@ def __str__(self):
663679
664680 Returns:
665681 A summary (string) of resources used, including depth of swaps and
666- statistics about the paths generated
682+ statistics about the swaps themselves
667683 """
668684
669685 depth_of_swaps_str = ""
0 commit comments