2525
2626random .seed (45 )
2727
28+
2829class HistoryTemp (TypedDict , total = False ):
2930 """Type for history dictionaries."""
31+
3032 scores : list [int ]
3133 layout_init : dict [int | str , tuple [int , int ] | list [tuple [int , int ]]]
3234 layout_final : dict [int | str , tuple [int , int ] | list [tuple [int , int ]]]
@@ -55,7 +57,7 @@ def __init__(
5557 free_rows : list [str ] | None = None ,
5658 t : int | None = None ,
5759 optimize_factories : bool = False ,
58- custom_layout : list [list [tuple [int ,int ]] | nx .Graph ] | None = None ,
60+ custom_layout : list [list [tuple [int , int ]] | nx .Graph ] | None = None ,
5961 routing : str = "static" ,
6062 ) -> None :
6163 """Initializes the Hill Climbing with Random Restarts algorithm.
@@ -133,7 +135,7 @@ def __init__(
133135 elif layout_type == "hex" :
134136 data_qubit_locs = lat .gen_layout_hex ()
135137 elif layout_type == "custom" :
136- if custom_layout is not None : # only for mypy
138+ if custom_layout is not None : # only for mypy
137139 data_qubit_locs = custom_layout [0 ]
138140 self .lat .G = custom_layout [1 ]
139141 else :
@@ -148,7 +150,9 @@ def __init__(
148150 num for tup in self .circuit for num in (tup if isinstance (tup , tuple ) else (tup ,))
149151 ]
150152 else :
151- flattened_qubit_labels = [num for tup in self .circuit if isinstance (tup , tuple ) for num in tup ] #isinstance only added for mypy
153+ flattened_qubit_labels = [
154+ num for tup in self .circuit if isinstance (tup , tuple ) for num in tup
155+ ] # isinstance only added for mypy
152156 self .q = max (flattened_qubit_labels ) + 1
153157 if self .q < len (self .data_qubit_locs ):
154158 self .data_qubit_locs = self .data_qubit_locs [: self .q ] # cut-off unnecessary qubit spots.
@@ -234,16 +238,16 @@ def add_left_g(g: nx.Graph) -> nx.Graph:
234238
235239 return g
236240
237- def evaluate_solution (self , layout : dict [int | str , tuple [int , int ] | list [tuple [int ,int ]]]) -> int :
241+ def evaluate_solution (self , layout : dict [int | str , tuple [int , int ] | list [tuple [int , int ]]]) -> int :
238242 """Evaluates the layout=solution according to self.metric."""
239243 terminal_pairs = translate_layout_circuit (self .circuit , layout )
240- factory_positions : list [tuple [int ,int ]]
241- #if type(layout["factory_positions"]) is list[tuple[int,int]] or list:
244+ factory_positions : list [tuple [int , int ]]
245+ # if type(layout["factory_positions"]) is list[tuple[int,int]] or list:
242246 factory_positions = layout ["factory_positions" ]
243- #else:
247+ # else:
244248 # msg = f"factory positions of layout must be list[tuple[int,int]]. But you got {type(layout['factory_positions'])}"
245249 # raise TypeError(msg)
246- router : ShortestFirstRouter | ShortestFirstRouterTGatesDyn | ShortestFirstRouterTGates
250+ router : ShortestFirstRouter | ShortestFirstRouterTGatesDyn | ShortestFirstRouterTGates
247251 if any (type (el ) is int for el in self .circuit ):
248252 if self .t is not None :
249253 if self .routing == "static" :
@@ -258,7 +262,7 @@ def evaluate_solution(self, layout: dict[int | str, tuple[int, int] | list[tuple
258262 router .G = self .lat .G .copy ()
259263 else : # if not custom, update self.lat.G by the router's G because m,n might differ from initial values.
260264 self .lat .G = router .G # also add to self
261- if self .free_rows is not None : # for mypy # noqa: SIM102
265+ if self .free_rows is not None : # for mypy # noqa: SIM102
262266 if "left" in self .free_rows :
263267 router .G = self .add_left_g (router .G )
264268 self .lat .G = router .G # also add to self
@@ -281,15 +285,15 @@ def evaluate_solution(self, layout: dict[int | str, tuple[int, int] | list[tuple
281285 cost : int
282286 if self .metric == "crossing" :
283287 if self .optimize_factories and any (type (el ) is int for el in self .circuit ):
284- router = cast ("ShortestFirstRouterTGates | ShortestFirstRouterTGatesDyn" , router )
288+ router = cast ("ShortestFirstRouterTGates | ShortestFirstRouterTGatesDyn" , router )
285289 cost = np .sum (router .count_crossings_per_layer (t_crossings = True ))
286290 elif self .optimize_factories is False and any (type (el ) is int for el in self .circuit ):
287- router = cast ("ShortestFirstRouterTGates | ShortestFirstRouterTGatesDyn" , router )
291+ router = cast ("ShortestFirstRouterTGates | ShortestFirstRouterTGatesDyn" , router )
288292 cost = np .sum (router .count_crossings_per_layer (t_crossings = False ))
289293 else :
290294 cost = np .sum (router .count_crossings_per_layer ())
291295 elif self .metric == "distance" :
292- router = cast ("ShortestFirstRouter" , router )
296+ router = cast ("ShortestFirstRouter" , router )
293297 distances = router .measure_terminal_pair_distances ()
294298 cost = np .sum (distances )
295299 if any (type (el ) is int for el in self .circuit ):
@@ -298,14 +302,14 @@ def evaluate_solution(self, layout: dict[int | str, tuple[int, int] | list[tuple
298302 if self .routing == "static" :
299303 vdp_layers = router .find_total_vdp_layers ()
300304 elif self .routing == "dynamic" :
301- router = cast ("ShortestFirstRouterTGatesDyn" , router )
305+ router = cast ("ShortestFirstRouterTGatesDyn" , router )
302306 vdp_layers = router .find_total_vdp_layers_dyn ()
303307 cost = len (vdp_layers )
304308 return cost
305309
306- def gen_random_qubit_assignment (self ) -> dict [int | str , tuple [int , int ] | list [tuple [int ,int ]]]:
310+ def gen_random_qubit_assignment (self ) -> dict [int | str , tuple [int , int ] | list [tuple [int , int ]]]:
307311 """Yields a random qubit assignment given the `data_qubit_locs`."""
308- layout : dict [int | str , tuple [int , int ] | list [tuple [int ,int ]]] = {}
312+ layout : dict [int | str , tuple [int , int ] | list [tuple [int , int ]]] = {}
309313 perm = list (range (self .q ))
310314 random .shuffle (perm )
311315 for i , j in zip (
@@ -323,7 +327,9 @@ def gen_random_qubit_assignment(self) -> dict[int | str, tuple[int, int] | list[
323327
324328 return layout
325329
326- def gen_neighborhood (self , layout : dict [int | str , tuple [int , int ] | list [tuple [int ,int ]]]) -> list [dict [int | str , tuple [int , int ] | list [tuple [int ,int ]]]]:
330+ def gen_neighborhood (
331+ self , layout : dict [int | str , tuple [int , int ] | list [tuple [int , int ]]]
332+ ) -> list [dict [int | str , tuple [int , int ] | list [tuple [int , int ]]]]:
327333 """Creates the Neighborhood of a given layout by going through each terminal pair and swapping their positions.
328334
329335 If there are no T gates, there will be l=len(terminal_pairs) elements in the neighborhood.
@@ -364,12 +370,9 @@ def gen_neighborhood(self, layout: dict[int | str, tuple[int, int] | list[tuple[
364370
365371 return neighborhood
366372
367- def _parallel_hill_climbing (self , restart : int ) -> tuple [
368- int ,
369- dict [int | str , tuple [int , int ] | list [tuple [int ,int ]]],
370- int ,
371- HistoryTemp
372- ]:
373+ def _parallel_hill_climbing (
374+ self , restart : int
375+ ) -> tuple [int , dict [int | str , tuple [int , int ] | list [tuple [int , int ]]], int , HistoryTemp ]:
373376 """Helper method for parallel execution of hill climbing restarts.
374377
375378 Args:
@@ -408,7 +411,9 @@ def _parallel_hill_climbing(self, restart: int) -> tuple[
408411 history_temp .update ({"layout_final" : current_solution .copy ()})
409412 return restart , current_solution , current_score , history_temp
410413
411- def run (self , prefix : str , suffix : str , parallel : bool , processes : int = 8 ) -> tuple [dict [int | str , tuple [int , int ] | list [tuple [int , int ]]], int , int , dict [int ,HistoryTemp ]]:
414+ def run (
415+ self , prefix : str , suffix : str , parallel : bool , processes : int = 8
416+ ) -> tuple [dict [int | str , tuple [int , int ] | list [tuple [int , int ]]], int , int , dict [int , HistoryTemp ]]:
412417 """Executes the Hill Climbing algorithm with random restarts.
413418
414419 Args:
0 commit comments