Skip to content

Commit

Permalink
Correct mypy and pylint errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
johngarvin-quantinuum committed Sep 28, 2023
1 parent 27e21f7 commit e77b33b
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions pytket/phir/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,35 @@
from __future__ import annotations


class TransportError(Exception):
def __init__(self, a: list[int], b: list[int]):
super().__init__(f"Traps different sizes: {len(a)} vs. {len(b)}")


class PermutationError(Exception):
def __init__(self, lst: list[int]):
super().__init__(f"List {lst} is not a permutation of range({len(lst)})")


def inverse(lst: list[int]) -> list[int]:
"""Inverse of a permutation list. If a[i] = x, then inverse(a)[x] = i."""
inv = [-1] * len(lst)
for (i, elem) in enumerate(lst):
if not 0 <= elem < len(lst):
raise ValueError(f"List contains element not in range: {elem}")
if inv[elem] != -1:
raise ValueError(f"List contains duplicate elements: {lst}")
for i, elem in enumerate(lst):
if not 0 <= elem < len(lst) or inv[elem] != -1:
raise PermutationError(lst)
inv[elem] = i
return inv


def transport_cost(init: list[int], goal: list[int], swap_cost: float) -> float:
"""Cost of transport from init to goal.
This is based on the number of parallel swaps performed by Odd-Even
Transposition Sort, which is the maximum distance that any qubit travels.
"""
if len(init) != len(goal):
raise ValueError(
f"init and goal lists have different lengths: {len(init)} vs. {len(goal)}"
)
n_swaps = max(abs(g - i) for (i, g) in zip(inverse(init), inverse(goal)))
raise TransportError(init, goal)
n_swaps = max(
abs(g - i) for (i, g) in zip(inverse(init), inverse(goal)) # noqa: B905
)
return n_swaps * swap_cost

0 comments on commit e77b33b

Please sign in to comment.