Skip to content

Commit

Permalink
Edge weights to floats
Browse files Browse the repository at this point in the history
  • Loading branch information
edyounis committed Sep 8, 2024
1 parent 2df5770 commit 6c18c1f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 31 deletions.
4 changes: 2 additions & 2 deletions bqskit/passes/mapping/pam.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def _get_best_perm(
cg: CouplingGraph,
F: set[CircuitPoint],
pi: list[int],
D: list[list[int]],
D: list[list[float]],
E: set[CircuitPoint],
qudits: Sequence[int],
) -> tuple[tuple[int, ...], Circuit, tuple[int, ...]]:
Expand Down Expand Up @@ -366,7 +366,7 @@ def _score_perm(
circuit: Circuit,
F: set[CircuitPoint],
pi: list[int],
D: list[list[int]],
D: list[list[float]],
perm: tuple[Sequence[int], Sequence[int]],
E: set[CircuitPoint],
) -> float:
Expand Down
8 changes: 4 additions & 4 deletions bqskit/passes/mapping/sabre.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ def _get_best_swap(
circuit: Circuit,
F: set[CircuitPoint],
E: set[CircuitPoint],
D: list[list[int]],
D: list[list[float]],
cg: CouplingGraph,
pi: list[int],
decay: list[float],
Expand Down Expand Up @@ -416,7 +416,7 @@ def _score_swap(
circuit: Circuit,
F: set[CircuitPoint],
pi: list[int],
D: list[list[int]],
D: list[list[float]],
swap: tuple[int, int],
decay: list[float],
E: set[CircuitPoint],
Expand Down Expand Up @@ -475,7 +475,7 @@ def _get_distance(
self,
logical_qudits: Sequence[int],
pi: list[int],
D: list[list[int]],
D: list[list[float]],
) -> float:
"""Calculate the expected number of swaps to connect logical qudits."""
min_term = np.inf
Expand All @@ -493,7 +493,7 @@ def _uphill_swaps(
logical_qudits: Sequence[int],
cg: CouplingGraph,
pi: list[int],
D: list[list[int]],
D: list[list[float]],
) -> Iterator[tuple[int, int]]:
"""Yield the swaps necessary to bring some of the qudits together."""
center_qudit = min(
Expand Down
48 changes: 23 additions & 25 deletions bqskit/qis/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
import logging
from random import shuffle
from typing import Any
from typing import cast
from typing import Collection
from typing import Iterable
from typing import Iterator
from typing import List
from typing import Mapping
from typing import Tuple
from typing import TYPE_CHECKING
Expand All @@ -24,7 +22,7 @@
from bqskit.ir.location import CircuitLocation
from bqskit.ir.location import CircuitLocationLike
from bqskit.utils.typing import is_integer
from bqskit.utils.typing import is_iterable, is_mapping
from bqskit.utils.typing import is_iterable, is_mapping, is_real_number

_logger = logging.getLogger(__name__)

Expand All @@ -37,9 +35,9 @@ def __init__(
graph: CouplingGraphLike,
num_qudits: int | None = None,
remote_edges: Iterable[tuple[int, int]] = [],
default_weight: int = 1,
default_remote_weight: int = 100,
edge_weights_overrides: Mapping[tuple[int, int], int] = {},
default_weight: float = 1.0,
default_remote_weight: float = 100.0,
edge_weights_overrides: Mapping[tuple[int, int], float] = {},
) -> None:
"""
Construct a new CouplingGraph.
Expand All @@ -56,13 +54,13 @@ def __init__(
connect them. Notes, remote edges must specified both in
`graph` and here. (Default: [])
default_weight (int): The default weight of an edge in the
graph. (Default: 1)
default_weight (float): The default weight of an edge in the
graph. (Default: 1.0)
default_remote_weight (int): The default weight of a remote
edge in the graph. (Default: 100)
default_remote_weight (float): The default weight of a remote
edge in the graph. (Default: 100.0)
edge_weights_overrides (Mapping[tuple[int, int], int]): A mapping
edge_weights_overrides (Mapping[tuple[int, int], float]): A mapping
of edges to their weights. These override the defaults on
a case-by-case basis. (Default: {})
Expand Down Expand Up @@ -101,13 +99,13 @@ def __init__(
' All remote links must also be specified in the graph input.',
)

if not isinstance(default_weight, int):
if not is_real_number(default_weight):
raise TypeError(
'Expected integer for default_weight,'
f' got {type(default_weight)}',
)

if not isinstance(default_remote_weight, int):
if not is_real_number(default_remote_weight):
raise TypeError(
'Expected integer for default_remote_weight,'
f' got {type(default_remote_weight)}',
Expand All @@ -120,12 +118,12 @@ def __init__(
)

if any(
not isinstance(v, int)
not is_real_number(v)
for v in edge_weights_overrides.values()
):
invalids = [
v for v in edge_weights_overrides.values()
if not isinstance(v, int)
if not is_real_number(v)
]
raise TypeError(
'Expected integer values for edge_weights_overrides,'
Expand Down Expand Up @@ -158,9 +156,9 @@ def __init__(
self._edges: set[tuple[int, int]] = graph._edges
self._remote_edges: set[tuple[int, int]] = graph._remote_edges
self._adj: list[set[int]] = graph._adj
self._mat: list[list[int]] = graph._mat
self.default_weight: int = graph.default_weight
self.default_remote_weight: int = graph.default_remote_weight
self._mat: list[list[float]] = graph._mat
self.default_weight: float = graph.default_weight
self.default_remote_weight: float = graph.default_remote_weight
return

self.num_qudits = calc_num_qudits if num_qudits is None else num_qudits
Expand All @@ -185,7 +183,7 @@ def __init__(
self._mat[q1][q2] = default_weight
self._mat[q2][q1] = default_weight

for q1, q2 in self._remote_links:
for q1, q2 in self._remote_edges:
self._mat[q1][q2] = default_remote_weight
self._mat[q2][q1] = default_remote_weight

Expand Down Expand Up @@ -235,7 +233,7 @@ def get_qpu_to_qudit_map(self) -> list[list[int]]:
if len(seen) != self.num_qudits:
raise RuntimeError(
'Graph is not fully connected and pathological'
' for distributed subroutines.'
' for distributed subroutines.',
)

return qpus
Expand All @@ -249,11 +247,11 @@ def get_qudit_to_qpu_map(self) -> dict[int, int]:
qudit_to_qpu[qudit] = qpu
return qudit_to_qpu

def get_qpu_connectivity(self) -> list[list[int]]:
def get_qpu_connectivity(self) -> list[set[int]]:
"""Return the adjacency list of the QPUs."""
qpu_to_qudit = self.get_qpu_to_qudit_map()
qudit_to_qpu = self.get_qudit_to_qpu_map()
qpu_adj = [set() for _ in range(len(qpu_to_qudit))]
qpu_adj: list[set[int]] = [set() for _ in range(len(qpu_to_qudit))]
for q1, q2 in self._remote_edges:
qpu1 = qudit_to_qpu[q1]
qpu2 = qudit_to_qpu[q2]
Expand Down Expand Up @@ -339,20 +337,20 @@ def __repr__(self) -> str:
def get_qudit_degrees(self) -> list[int]:
return [len(l) for l in self._adj]

def all_pairs_shortest_path(self) -> list[list[int]]:
def all_pairs_shortest_path(self) -> list[list[float]]:
"""
Calculate all pairs shortest path matrix using Floyd-Warshall.
Returns:
D (list[list[int]]): D[i][j] is the length of the shortest
D (list[list[float]]): D[i][j] is the length of the shortest
path from i to j.
"""
D = copy.deepcopy(self._mat)
for k in range(self.num_qudits):
for i in range(self.num_qudits):
for j in range(self.num_qudits):
D[i][j] = min(D[i][j], D[i][k] + D[k][j])
return cast(List[List[int]], D)
return D

def get_shortest_path_tree(self, source: int) -> list[tuple[int, ...]]:
"""Return shortest path from `source` to every node in `self`."""
Expand Down

0 comments on commit 6c18c1f

Please sign in to comment.