Skip to content

Commit

Permalink
fix(typing): Layer -> ShardLayer, and introduce another Layer type
Browse files Browse the repository at this point in the history
  • Loading branch information
qartik committed Nov 9, 2023
1 parent 177d1bb commit a773d44
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
4 changes: 2 additions & 2 deletions pytket/phir/phirgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pytket.circuit.logic_exp import RegWiseOp
from pytket.unit_id import UnitID

from .sharding.shard import Cost, Layer, Ordering
from .sharding.shard import Cost, Ordering, ShardLayer

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -235,7 +235,7 @@ def append_cmd(cmd: tk.Command, ops: list[dict[str, Any]]) -> None:


def genphir(
inp: list[tuple[Ordering, Layer, Cost]], *, machine_ops: bool = True
inp: list[tuple[Ordering, ShardLayer, Cost]], *, machine_ops: bool = True
) -> str:
"""Convert a list of shards to the equivalent PHIR.
Expand Down
4 changes: 2 additions & 2 deletions pytket/phir/place_and_route.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from .machine import Machine
from .placement import optimized_place
from .routing import transport_cost
from .sharding.shard import Cost, Layer, Ordering, Shard
from .sharding.shard import Cost, Ordering, Shard, ShardLayer
from .sharding.shards2ops import parse_shards_naive


def place_and_route(
shards: list[Shard],
machine: Machine | None = None,
) -> list[tuple[Ordering, Layer, Cost]]:
) -> list[tuple[Ordering, ShardLayer, Cost]]:
"""Get all the routing info needed for PHIR generation."""
shard_set = set(shards)
circuit_rep, shard_layers = parse_shards_naive(shard_set)
Expand Down
2 changes: 1 addition & 1 deletion pytket/phir/placement.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def placement_check(

# If there are no operations to place, it does not matter where the
# qubits are and any placement is valid
if len(ops) == 0:
if not ops:
return True

# assume ops look like this [[1,2],[3],[4],[5,6],[7],[8],[9,10]]
Expand Down
2 changes: 1 addition & 1 deletion pytket/phir/sharding/shard.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@ def pretty_print(self) -> str:


Cost: TypeAlias = float
Layer: TypeAlias = list[Shard]
ShardLayer: TypeAlias = list[Shard]
Ordering: TypeAlias = list[int]
19 changes: 13 additions & 6 deletions pytket/phir/sharding/shards2ops.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
from .shard import Shard
from typing import TypeAlias

from .shard import Shard, ShardLayer

Layer: TypeAlias = list[list[int]]


def parse_shards_naive(
shards: set[Shard],
) -> tuple[list[list[list[int]]], list[list[Shard]]]:
) -> tuple[list[Layer], list[ShardLayer]]:
"""Parse a set of shards and return a circuit representation for placement."""
layers: list[list[list[int]]] = []
shards_in_layer: list[list[Shard]] = []
layers: list[Layer] = []
shards_in_layer: list[ShardLayer] = []
scheduled: set[int] = set()
num_shards: int = len(shards)

while len(scheduled) < num_shards:
layer: list[list[int]] = []
layer: Layer = []

# Iterate the shards, looking for shards whose dependencies have been
# satisfied, or initially, shards with no dependencies
to_schedule = [s for s in shards if s.depends_upon.issubset(scheduled)]
to_schedule: ShardLayer = [
s for s in shards if s.depends_upon.issubset(scheduled)
]
shards_in_layer.append(to_schedule)
shards.difference_update(to_schedule)

Expand Down

0 comments on commit a773d44

Please sign in to comment.