2424from copy import deepcopy
2525
2626import random
27- import itertools
27+ import sys
2828
2929from projectq .cengines import (BasicMapperEngine , return_swap_depth )
3030from projectq .meta import LogicalQubitIDTag
3737# ------------------------------------------------------------------------------
3838
3939# https://www.peterbe.com/plog/fastest-way-to-uniquify-a-list-in-python-3.6
40- import sys
4140if sys .version_info [0 ] >= 3 and sys .version_info [1 ] > 6 : # pragma: no cover
4241
4342 def uniquify_list (seq ):
43+ #pylint: disable=missing-function-docstring
4444 return list (dict .fromkeys (seq ))
4545else : # pragma: no cover
4646
4747 def uniquify_list (seq ):
48+ #pylint: disable=missing-function-docstring
4849 seen = set ()
4950 seen_add = seen .add
5051 return [x for x in seq if x not in seen and not seen_add (x )]
@@ -78,6 +79,8 @@ def _add_qubits_to_mapping_fcfs(current_mapping, graph, new_logical_qubit_ids,
7879
7980 Returns: A new mapping
8081 """
82+ #pylint: disable=unused-argument
83+
8184 mapping = deepcopy (current_mapping )
8285 currently_used_nodes = sorted ([v for _ , v in mapping .items ()])
8386 available_nodes = [n for n in graph if n not in currently_used_nodes ]
@@ -199,11 +202,11 @@ def _add_qubits_to_mapping(current_mapping, graph, new_logical_qubit_ids,
199202 available_nodes = sorted (
200203 [n for n in graph if n not in currently_used_nodes ],
201204 key = lambda n : len (graph [n ]))
202- interactions = commands_dag .calculate_interaction_list ()
203205
204206 for logical_id in uniquify_list (new_logical_qubit_ids ):
205207 qubit_interactions = uniquify_list ([
206- i [0 ] if i [0 ] != logical_id else i [1 ] for i in interactions
208+ i [0 ] if i [0 ] != logical_id else i [1 ]
209+ for i in commands_dag .calculate_interaction_list ()
207210 if logical_id in i
208211 ])
209212
@@ -300,7 +303,7 @@ def __init__(self,
300303 graph ,
301304 storage = 1000 ,
302305 add_qubits_to_mapping = _add_qubits_to_mapping ,
303- opts = {} ):
306+ opts = None ):
304307 """
305308 Initialize a GraphMapper compiler engine.
306309
@@ -342,8 +345,13 @@ def __init__(self,
342345 """
343346 BasicMapperEngine .__init__ (self )
344347
348+ if opts is None :
349+ self ._opts = {}
350+ else :
351+ self ._opts = opts
352+
345353 self .qubit_manager = MultiQubitGateManager (graph = graph ,
346- decay_opts = opts .get (
354+ decay_opts = self . _opts .get (
347355 'decay_opts' , {
348356 'delta' : 0.001 ,
349357 'max_lifetime' : 5
@@ -365,10 +373,6 @@ def __init__(self,
365373 # Function to add new logical qubits ids to the mapping
366374 self .set_add_qubits_to_mapping (add_qubits_to_mapping )
367375
368- self ._cost_fun = opts .get ('cost_fun' , look_ahead_parallelism_cost_fun )
369- self ._opts = opts .get ('opts' , {'W' : 0.5 })
370- self ._max_swap_steps = opts .get ('max_swap_steps' , 30 )
371-
372376 # Statistics:
373377 self .num_mappings = 0
374378 self .depth_of_swaps = dict ()
@@ -393,6 +397,17 @@ def current_mapping(self, current_mapping):
393397 }
394398
395399 def set_add_qubits_to_mapping (self , add_qubits_to_mapping ):
400+ """
401+ Modify the callback function used to add qubits to an existing mapping
402+
403+ Args:
404+ add_qubits_to_mapping (function): Callback function
405+
406+ Note:
407+ Signature for callback function is:
408+ ``add_qubits_to_mapping(current_mapping, graph,
409+ new_logical_qubit_ids, command_dag)``
410+ """
396411 if isinstance (add_qubits_to_mapping , str ):
397412 if add_qubits_to_mapping .lower () == "fcfs" :
398413 self ._add_qubits_to_mapping = _add_qubits_to_mapping_fcfs
@@ -467,17 +482,18 @@ def _send_possible_commands(self):
467482 num_available_qubits = self .num_qubits - len (self ._current_mapping )
468483 if allocate_cmds and num_available_qubits > 0 :
469484
470- def rank_allocate_cmds (l , dag ):
471- return l
485+ def rank_allocate_cmds (cmds_list , dag ):
486+ #pylint: disable=unused-argument
487+ return cmds_list
472488
473489 allocate_cmds = rank_allocate_cmds (
474- allocate_cmds , self .qubit_manager ._dag )[:num_available_qubits ]
490+ allocate_cmds , self .qubit_manager .dag )[:num_available_qubits ]
475491 not_in_mapping_qubits = [node .logical_id for node in allocate_cmds ]
476492
477493 new_mapping = self ._add_qubits_to_mapping (self ._current_mapping ,
478494 self .qubit_manager .graph ,
479495 not_in_mapping_qubits ,
480- self .qubit_manager ._dag )
496+ self .qubit_manager .dag )
481497
482498 self .current_mapping = new_mapping
483499
@@ -509,19 +525,18 @@ def _run(self):
509525
510526 swaps , all_swapped_qubits = self .qubit_manager .generate_swaps (
511527 self ._current_mapping ,
512- cost_fun = self ._cost_fun ,
513- opts = self ._opts ,
514- max_steps = self ._max_swap_steps )
515-
516- if swaps : # first mapping requires no swaps
517- backend_ids_used = {
518- self ._current_mapping [logical_id ]
519- for logical_id in self ._currently_allocated_ids
520- }
528+ cost_fun = self ._opts .get ('cost_fun' ,
529+ look_ahead_parallelism_cost_fun ),
530+ opts = self ._opts .get ('opts' , {'W' : 0.5 }),
531+ max_steps = self ._opts .get ('max_swap_steps' , 30 ))
521532
533+ if swaps :
522534 # Get a list of the qubits we need to allocate just to perform the
523535 # swaps
524- not_allocated_ids = all_swapped_qubits .difference (backend_ids_used )
536+ not_allocated_ids = all_swapped_qubits .difference ({
537+ self ._current_mapping [logical_id ]
538+ for logical_id in self ._currently_allocated_ids
539+ })
525540
526541 # Calculate temporary internal reverse mapping
527542 new_internal_mapping = deepcopy (self ._reverse_current_mapping )
@@ -628,8 +643,8 @@ def receive(self, command_list):
628643 ]
629644
630645 if len (qubit_ids ) > 2 or not qubit_ids :
631- raise Exception ("Invalid command (number of qubits): " +
632- str (cmd ))
646+ raise Exception ("Invalid command (number of qubits): "
647+ + str (cmd ))
633648
634649 if isinstance (cmd .gate , FlushGate ):
635650 while self .qubit_manager .size () > 0 :
@@ -663,7 +678,7 @@ def __str__(self):
663678 num_swaps_per_mapping_str += "\n {:3d}: {:3d}" .format (
664679 num_swaps_per_mapping , num_mapping )
665680
666- return ("Number of mappings: {}\n " + "Depth of swaps: {}\n \n " +
667- "Number of swaps per mapping:{}\n \n {}\n \n " ).format (
681+ return ("Number of mappings: {}\n " + "Depth of swaps: {}\n \n "
682+ + "Number of swaps per mapping:{}\n \n {}\n \n " ).format (
668683 self .num_mappings , depth_of_swaps_str ,
669684 num_swaps_per_mapping_str , str (self .qubit_manager ))
0 commit comments