mqt.qecc.cococo.utils_routing

Routing Routines for Lattice Surgery Compilation.

Classes

BasicRouter

Basic Routing for CNOT + T gates based on shortest-first VDP solving.

TeleportationRouter

Compilation routine that exploits CNOT + teleportation steps.

Module Contents

class BasicRouter(g: networkx.Graph, logical_pos: list[mqt.qecc.cococo.types.pos], factory_pos: list[mqt.qecc.cococo.types.pos], valid_path: str, t: int, metric: str, use_dag: bool)[source]

Basic Routing for CNOT + T gates based on shortest-first VDP solving.

static path_sc(g: networkx.Graph, control: mqt.qecc.cococo.types.pos, target: mqt.qecc.cococo.types.pos) → list[mqt.qecc.cococo.types.pos][source]

Find the shortest path with Dijkstra but with constraints for standard sc.

This means, that control qubits are only entered horizontally, targets vertically. thus horizontal bdrys are Z_L and vertical X_L.

This method is actually useless because the Lookahead optimization in this class is only valid for color code connectivity.

static path_cc(g: networkx.Graph, control: mqt.qecc.cococo.types.pos, target: mqt.qecc.cococo.types.pos) → list[mqt.qecc.cococo.types.pos][source]

Find the shortest path with Dijkstra for a color code architecture.

There is only constraint: directly neighboring control/target cannot be directly connected. Hence, length of path must at least be 3.

valid_path_method() → Callable[[networkx.Graph, mqt.qecc.cococo.types.pos, mqt.qecc.cococo.types.pos], list[mqt.qecc.cococo.types.pos]][source]

Calls the correct version of Dijkstra depending on self.valid_path.

static split_layer_terminal_pairs(terminal_pairs: Sequence[mqt.qecc.cococo.types.pos | tuple[mqt.qecc.cococo.types.pos, mqt.qecc.cococo.types.pos]]) → list[Sequence[mqt.qecc.cococo.types.pos | tuple[mqt.qecc.cococo.types.pos, mqt.qecc.cococo.types.pos]]][source]

Split terminal_pairs into layers of disjoint qubit support.

Only really needed if self.use_dag=False. If true, it is often computed uselessly.

find_max_vdp_set(layer: Sequence[mqt.qecc.cococo.types.pos | tuple[mqt.qecc.cococo.types.pos, mqt.qecc.cococo.types.pos]], logical_pos: list[mqt.qecc.cococo.types.pos] | None, factory_times: dict[mqt.qecc.cococo.types.pos, int]) → tuple[mqt.qecc.cococo.types.VdpDict, Sequence[mqt.qecc.cococo.types.pos | tuple[mqt.qecc.cococo.types.pos, mqt.qecc.cococo.types.pos]], dict[mqt.qecc.cococo.types.pos, int]][source]

Finds the approximation for a vdp set for some given layer and returns the routing as well as a potential remainder.

If logical_pos is given as input, you use the input instead of the self.logical_pos

push_remainder_into_layers(layers: list[Sequence[mqt.qecc.cococo.types.pos | tuple[mqt.qecc.cococo.types.pos, mqt.qecc.cococo.types.pos]]], remainder: Sequence[mqt.qecc.cococo.types.pos | tuple[mqt.qecc.cococo.types.pos, mqt.qecc.cococo.types.pos]], delete_layer_zero: bool = True) → list[Sequence[mqt.qecc.cococo.types.pos | tuple[mqt.qecc.cococo.types.pos, mqt.qecc.cococo.types.pos]]][source]

Updates a copy of layers_cnot_t (removed used stuff and takes remainder of previous layer, pushes through).

Only really needed if self.use_dag=False. If true, it is often computed uselessly.

Parameters:
  • layers – given layers

  • remainder – remaining gates which could not be routed so far in current layer.

  • delete_layer_zero – whether 0th layer is deleted or not.

Returns:

layered gates with remainder being pushed into next layer.

find_total_vdp_layers_dyn(next_layers: list[Sequence[mqt.qecc.cococo.types.pos | tuple[mqt.qecc.cococo.types.pos, mqt.qecc.cococo.types.pos]]], logical_pos: list[mqt.qecc.cococo.types.pos], factory_times: dict[mqt.qecc.cococo.types.pos, int], layout: dict[int, mqt.qecc.cococo.types.pos], testing: bool = False) → tuple[list[mqt.qecc.cococo.types.VdpDict] | None, dict[mqt.qecc.cococo.types.pos, int]][source]

Finds all routes for given logical layers called next_layers.

This is only used if self.use_dag = False

Dynamically pushes remaining gates into the next layers.

count_crossings(layers: Sequence[Sequence[tuple[mqt.qecc.cococo.types.pos, mqt.qecc.cococo.types.pos]]], logical_pos_temp: list[mqt.qecc.cococo.types.pos]) → int[source]

Heuristic energy function to minimize.

Computes number of crossings of Dijkstra paths without constraints. If there is a shortest path which is not possible at all (due to locking), then add high, hard coded penalty. This only works for CNOT circuits only.

count_crossings_per_layer(layers: Sequence[Sequence[mqt.qecc.cococo.types.pos | tuple[mqt.qecc.cococo.types.pos, mqt.qecc.cococo.types.pos]]], t_crossings: bool = False) → list[int][source]

Counts the crossings of the simple paths between cnots and between shortest factory to qubit path (respecting terminals and factory positions) per layer.

Parameters:
  • layers – circuit layers.

  • t_crossings – decides whether the crossings to the factory are included (true) or not (false).

Returns:

Number of crossings per initial layer. len is len(self.layers_cnot_t_orig)

class TeleportationRouter(g: networkx.Graph, logical_pos: list[mqt.qecc.cococo.types.pos], factory_pos: list[mqt.qecc.cococo.types.pos], valid_path: str, t: int, metric: str, use_dag: bool, seed: int)[source]

Bases: BasicRouter

Compilation routine that exploits CNOT + teleportation steps.

initialize_steiner(vdp_dict: mqt.qecc.cococo.types.VdpDict, steiner_init_type: str, layers: list[Sequence[mqt.qecc.cococo.types.pos | tuple[mqt.qecc.cococo.types.pos, mqt.qecc.cococo.types.pos]]] | None = None, k_lookahead: int | None = None) → SteinerType[source]

Initialize a random steiner tree per path which are non-overlapping.

This initialization depends on the vdp_dict you already have for your current layer. This is fixed, it only tries to find additional terminal of a 3-terminal steiner tree.

If layers and k_lookahead are not None, only a limited number of trees is initialized; namely only for the qubits which are actually used in layers[:k_lookahead]. Other qubits are not moved. However, this turned out not to be really useful, because movements can be relevant even if this constraint does not hold. Therefore, layers and k_lookahead default to None.

perturbation(steiner_dct: SteinerType, radius: int, vdp_dict: dict[str | mqt.qecc.cococo.types.pos | tuple[mqt.qecc.cococo.types.pos, mqt.qecc.cococo.types.pos], list[mqt.qecc.cococo.types.pos]]) → tuple[SteinerType, networkx.Graph][source]

Computes a perturbation of a given collection of trees within a given radius of edges around the current terminal.

For each tree in steiner_dct a new location of the 3rd terminal is updated randomly.

static replace_pos(lst: Sequence[mqt.qecc.cococo.types.pos | tuple[mqt.qecc.cococo.types.pos, mqt.qecc.cococo.types.pos]], old: mqt.qecc.cococo.types.pos, new: mqt.qecc.cococo.types.pos) → Sequence[mqt.qecc.cococo.types.pos | tuple[mqt.qecc.cococo.types.pos, mqt.qecc.cococo.types.pos]][source]

Helper function to replace pos values in lists.

This is needed to update logical_pos etc. during the optimization.

run_annealing(next_layers: list[Sequence[mqt.qecc.cococo.types.pos | tuple[mqt.qecc.cococo.types.pos, mqt.qecc.cococo.types.pos]]], init_steiner_dct: SteinerType, max_iters: int, T_start: float, T_end: float, alpha: float, k_lookahead: int, radius: int, vdp_dict: mqt.qecc.cococo.types.VdpDict, layout: dict[int, mqt.qecc.cococo.types.pos]) → tuple[SteinerType | None, int, list[mqt.qecc.cococo.types.VdpDict] | None, list[tuple[int, list[Sequence[mqt.qecc.cococo.types.pos | tuple[mqt.qecc.cococo.types.pos, mqt.qecc.cococo.types.pos]]]]], dict[tuple[mqt.qecc.cococo.types.pos, ...], str], list[SteinerType], list[networkx.Graph]][source]

Plug together all previous methods to run annealing for k future layers.

Parameters:
  • next_layers – upcoming layers to draw into consideration

  • init_steiner_dct – current steiner

  • max_iters – max number of iterations of each simulated annealing

  • T_start – start temperature of simulated annealing

  • T_end – end temperature of simulated annealing

  • alpha – factor to reduce temp

  • k_lookahead – number of logical layers to draw into account

  • radius – number of edges within which one can choose a new ancilla position

  • vdp_dict – routing of current layer

  • layout – layout.

Returns:

best_steiner best_cost best_schedule cost_history best_move_type_lst steiner_history graph_history

reduce_steiner_moves(steiner_dct: SteinerType, move_type_lst: dict[tuple[mqt.qecc.cococo.types.pos, ...], str], next_layers: list[Sequence[mqt.qecc.cococo.types.pos | tuple[mqt.qecc.cococo.types.pos, mqt.qecc.cococo.types.pos]]], best_cost: int, k_lookahead: int, layout: dict[int, mqt.qecc.cococo.types.pos]) → tuple[SteinerType, dict[tuple[mqt.qecc.cococo.types.pos, ...], str], list[mqt.qecc.cococo.types.VdpDict] | None][source]

Given some tree solution, make sure that you effectively use as least movements as possible.

This means, we go through small subsets of the tree solution and check at what point we reach the same optimized cost. This can scale horribly if you have too many trees in your solution.

idle_move_back(schedule: Any, danger_qubits: dict[mqt.qecc.cococo.types.pos, int], available_gaps: list[mqt.qecc.cococo.types.pos], danger_qubits_temp: dict[mqt.qecc.cococo.types.pos, int], available_gaps_temp: list[mqt.qecc.cococo.types.pos], layout: dict[int, mqt.qecc.cococo.types.pos], layers: list[Sequence[mqt.qecc.cococo.types.pos | tuple[mqt.qecc.cococo.types.pos, mqt.qecc.cococo.types.pos]]], reduce_time_stamp: bool, jump_harvesting: bool, best_schedule: list[mqt.qecc.cococo.types.VdpDict] | None) → tuple[Any, dict[mqt.qecc.cococo.types.pos, int], list[mqt.qecc.cococo.types.pos], dict[int, mqt.qecc.cococo.types.pos], list[Sequence[mqt.qecc.cococo.types.pos | tuple[mqt.qecc.cococo.types.pos, mqt.qecc.cococo.types.pos]]]][source]

Subroutine of optimize_layers to move back qubits in dangerous positions asap.

optimize_layers(terminal_pairs: Sequence[mqt.qecc.cococo.types.pos | tuple[mqt.qecc.cococo.types.pos, mqt.qecc.cococo.types.pos]], layout: dict[int, mqt.qecc.cococo.types.pos], max_iters: int, T_start: float, T_end: float, alpha: float, radius: int, k_lookahead: int, steiner_init_type: str, jump_harvesting: bool, reduce_steiner: bool, idle_move_type: str, reduce_init_steiner: bool = False, stimtest: bool = False) → tuple[Any, list[tuple[int, Any]]][source]

Optimize the positions in batches of size k_lookahead.

This means we do the following:

  1. find an initial layer structure

  2. route the first layer, and push remainder into next layers

  3. run SA with k_lookahead layers (i.e. k layers are counted in the metric)

4. move idling qubits back (in different points in time depending on idle_move_type.) Repeat this layer by layer.

Parameters:
  • terminal_pairs – Circuit of CNOT + T gates in terms of qubit positions pos on the lattice.

  • layout – mapping between logical qubit labels int and their positions pos on the lattice.

  • max_iters – Maximum number of iterations per simulated annealing run

  • T_start – Start temperature of each simulated annealing run

  • T_end – End temperature of each simulated annealing run

  • alpha – Factor by which the temperature is decreased per iteration in a simulated annealing run. T*alpha per iteration.

  • radius – Radius in which the 3rd terminal of the tree can be randomly placed (radius in terms of edges on the macroscopic routing graph)

  • k_lookahead – Number of logical lookahead layers on which the optimization is done per simulated annealing.

  • steiner_init_type – recommended to use full_random. on_path_random: terminal placement on the routing path - or full_random: fully random placement on the macroscopic routing graph.

  • jump_harvesting – Recommended to use True. False: Default sliding window method. True: 1 layer is used for steiner search of k future layers. but then you do not just iterate through EACH layer but you skip the k layers, because you do not want to destroy the optimization by optimizing too much!

  • reduce_steiner – Recommended to use True. Decides whether the trees are reduced again (True), such that possibly useless movements are removed. Using False may reduce rutnime however.

  • idle_move_type – Recommended to use later. asap: moving back is done as frequent as possible. this may destroy however structure of the predicted routing from the steiner search. later: means that moving back is only done when the steiner search is done. if a locking occurs, extra layers with moving back are necessary, but no moving back during the routing of the k_lookahead layers with jump_harvesting = True.

  • reduce_init_steiner – Defaults to False and is recommended not to be changed.

  • stimtest – If True, at the end of the computation the schedule is sanity checked. Defaults to False but recommended to use.

Returns:

schedule and improvement_history.