Skip to content

astar_router

AStarRouter

Bases: Router

Source code in opensquirrel/passes/router/astar_router.py
class AStarRouter(Router):
    def __init__(
        self, connectivity: Connectivity, distance_metric: DistanceMetric | None = None, **kwargs: Any
    ) -> None:
        super().__init__(connectivity, **kwargs)
        self._distance_metric = distance_metric

    def route(self, ir: IR, qubit_register_size: int) -> IR:
        """Route the input IR using the [A*](https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.shortest_paths.astar.astar_path.html)
        search algorithm.

        Args:
            ir (IR): The input IR to be routed.
            qubit_register_size (int): Size of the qubit register.

        Returns:
            The routed IR.

        """
        pathfinder: PathFinderType = self._astar_pathfinder
        return ProcessSwaps.process_swaps(ir, qubit_register_size, self._connectivity, pathfinder)

    def _astar_pathfinder(self, graph: nx.Graph, source: int, target: int) -> Any:
        num_available_qubits = max(graph.nodes) + 1
        num_columns = math.ceil(math.sqrt(num_available_qubits))
        return nx.astar_path(
            graph,
            source=source,
            target=target,
            heuristic=lambda q0_index, q1_index: (
                calculate_distance(q0_index, q1_index, num_columns, self._distance_metric)
                if self._distance_metric
                else None
            ),
        )

route

route(ir: IR, qubit_register_size: int) -> IR

Route the input IR using the A* search algorithm.

Parameters:

Name Type Description Default
ir IR

The input IR to be routed.

required
qubit_register_size int

Size of the qubit register.

required

Returns:

Type Description
IR

The routed IR.

Source code in opensquirrel/passes/router/astar_router.py
def route(self, ir: IR, qubit_register_size: int) -> IR:
    """Route the input IR using the [A*](https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.shortest_paths.astar.astar_path.html)
    search algorithm.

    Args:
        ir (IR): The input IR to be routed.
        qubit_register_size (int): Size of the qubit register.

    Returns:
        The routed IR.

    """
    pathfinder: PathFinderType = self._astar_pathfinder
    return ProcessSwaps.process_swaps(ir, qubit_register_size, self._connectivity, pathfinder)