Skip to content

Commit

Permalink
use pytket 1.19 and fix resulting mypy issues (#46)
Browse files Browse the repository at this point in the history
* use pytket 1.19 and fix resulting mypy issues

* remove more type ignores
  • Loading branch information
trvto authored Sep 6, 2023
1 parent 08b201d commit e0a4e6f
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 96 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ classifiers= [

[tool.poetry.dependencies]
python = ">=3.10, <3.12"
pytket = "^1.17"
pytket = "^1.19"
requests = "^2.22"
types-requests = "*"
pydantic = "^2.0"
Expand Down
49 changes: 24 additions & 25 deletions pytket/extensions/aqt/backends/aqt.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,25 @@
from pytket.backends.backendresult import BackendResult
from pytket.backends.resulthandle import _ResultIdTuple
from pytket.circuit import Circuit
from pytket.circuit import Node # type: ignore
from pytket.circuit import OpType # type: ignore
from pytket.circuit import Qubit # type: ignore
from pytket.passes import auto_rebase_pass # type: ignore
from pytket.passes import BasePass # type: ignore
from pytket.passes import DecomposeBoxes # type: ignore
from pytket.passes import EulerAngleReduction # type: ignore
from pytket.passes import FlattenRegisters # type: ignore
from pytket.passes import FullPeepholeOptimise # type: ignore
from pytket.passes import RebaseCustom # type: ignore
from pytket.passes import RenameQubitsPass # type: ignore
from pytket.passes import SequencePass # type: ignore
from pytket.passes import SimplifyInitial # type: ignore
from pytket.passes import SynthesiseTket # type: ignore
from pytket.predicates import GateSetPredicate # type: ignore
from pytket.predicates import MaxNQubitsPredicate # type: ignore
from pytket.predicates import NoClassicalControlPredicate # type: ignore
from pytket.predicates import NoFastFeedforwardPredicate # type: ignore
from pytket.predicates import NoMidMeasurePredicate # type: ignore
from pytket.predicates import NoSymbolsPredicate # type: ignore
from pytket.predicates import Predicate # type: ignore
from pytket.circuit import OpType
from pytket.circuit import Qubit
from pytket.passes import auto_rebase_pass
from pytket.passes import BasePass
from pytket.passes import DecomposeBoxes
from pytket.passes import EulerAngleReduction
from pytket.passes import FlattenRegisters
from pytket.passes import FullPeepholeOptimise
from pytket.passes import RenameQubitsPass
from pytket.passes import SequencePass
from pytket.passes import SimplifyInitial
from pytket.passes import SynthesiseTket
from pytket.predicates import GateSetPredicate
from pytket.predicates import MaxNQubitsPredicate
from pytket.predicates import NoClassicalControlPredicate
from pytket.predicates import NoFastFeedforwardPredicate
from pytket.predicates import NoMidMeasurePredicate
from pytket.predicates import NoSymbolsPredicate
from pytket.predicates import Predicate
from pytket.utils import prepare_circuit
from pytket.utils.outcomearray import OutcomeArray

Expand Down Expand Up @@ -138,7 +136,7 @@ def __init__(
super().__init__()
self._url = AQT_URL_PREFIX + device_name
self._label = label
config = AQTConfig.from_default_config_file()
config = cast(AQTConfig, AQTConfig.from_default_config_file())

if access_token is None:
access_token = config.access_token
Expand All @@ -147,7 +145,7 @@ def __init__(

self._header = {"Ocp-Apim-Subscription-Key": access_token, "SDK": "pytket"}
self._backend_info: Optional[BackendInfo] = None
self._qm: Dict[Qubit, Node] = {}
self._qm: Dict[Qubit, Qubit] = {}
if device_name in _DEVICE_INFO:
self._backend_info = fully_connected_backendinfo(
type(self).__name__,
Expand All @@ -157,7 +155,8 @@ def __init__(
_GATE_SET,
)
self._qm = {
Qubit(i): node for i, node in enumerate(self._backend_info.nodes)
Qubit(i): cast(Qubit, node)
for i, node in enumerate(self._backend_info.nodes)
}
self._MACHINE_DEBUG = False

Expand Down Expand Up @@ -395,7 +394,7 @@ def _translate_aqt(circ: Circuit) -> Tuple[List[List], str]:
return (gates, json.dumps(measures))


def _aqt_rebase() -> RebaseCustom:
def _aqt_rebase() -> BasePass:
return auto_rebase_pass({OpType.XXPhase, OpType.Rx, OpType.Ry})


Expand Down
70 changes: 39 additions & 31 deletions pytket/extensions/aqt/backends/aqt_multi_zone.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.
import json
from copy import deepcopy
from typing import Any
from typing import Any, cast
from typing import Dict
from typing import List
from typing import Optional
Expand All @@ -31,27 +31,25 @@
from pytket.backends.backendresult import BackendResult
from pytket.backends.resulthandle import _ResultIdTuple
from pytket.circuit import Circuit
from pytket.circuit import Node # type: ignore
from pytket.circuit import OpType # type: ignore
from pytket.circuit import Qubit # type: ignore
from pytket.circuit import OpType
from pytket.circuit import Qubit
from pytket.passes import auto_rebase_pass
from pytket.passes import BasePass # type: ignore
from pytket.passes import DecomposeBoxes # type: ignore
from pytket.passes import EulerAngleReduction # type: ignore
from pytket.passes import FlattenRegisters # type: ignore
from pytket.passes import FullPeepholeOptimise # type: ignore
from pytket.passes import RebaseCustom # type: ignore
from pytket.passes import RenameQubitsPass # type: ignore
from pytket.passes import SequencePass # type: ignore
from pytket.passes import SimplifyInitial # type: ignore
from pytket.passes import SynthesiseTket # type: ignore
from pytket.predicates import GateSetPredicate # type: ignore
from pytket.predicates import MaxNQubitsPredicate # type: ignore
from pytket.predicates import NoClassicalControlPredicate # type: ignore
from pytket.predicates import NoFastFeedforwardPredicate # type: ignore
from pytket.predicates import NoMidMeasurePredicate # type: ignore
from pytket.predicates import NoSymbolsPredicate # type: ignore
from pytket.predicates import Predicate # type: ignore
from pytket.passes import BasePass
from pytket.passes import DecomposeBoxes
from pytket.passes import EulerAngleReduction
from pytket.passes import FlattenRegisters
from pytket.passes import FullPeepholeOptimise
from pytket.passes import RenameQubitsPass
from pytket.passes import SequencePass
from pytket.passes import SimplifyInitial
from pytket.passes import SynthesiseTket
from pytket.predicates import GateSetPredicate
from pytket.predicates import MaxNQubitsPredicate
from pytket.predicates import NoClassicalControlPredicate
from pytket.predicates import NoFastFeedforwardPredicate
from pytket.predicates import NoMidMeasurePredicate
from pytket.predicates import NoSymbolsPredicate
from pytket.predicates import Predicate

from ..backends.config import AQTConfig
from ..multi_zone_architecture.architecture import MultiZoneArchitecture
Expand Down Expand Up @@ -126,7 +124,7 @@ def __init__(
super().__init__()
self._url = AQT_URL_PREFIX + device_name
self._label = label
config = AQTConfig.from_default_config_file()
config = cast(AQTConfig, AQTConfig.from_default_config_file())

if access_token is None:
access_token = config.access_token
Expand All @@ -135,15 +133,18 @@ def __init__(

self._header = {"Ocp-Apim-Subscription-Key": access_token, "SDK": "pytket"}
self._backend_info: Optional[BackendInfo] = None
self._qm: Dict[Qubit, Node] = {}
self._qm: Dict[Qubit, Qubit] = {}
self._backend_info = fully_connected_backendinfo(
type(self).__name__,
device_name,
__extension_version__,
architecture.n_qubits_max,
_GATE_SET,
)
self._qm = {Qubit(i): node for i, node in enumerate(self._backend_info.nodes)}
self._qm = {
Qubit(i): cast(Qubit, node)
for i, node in enumerate(self._backend_info.nodes)
}
self._MACHINE_DEBUG = True

def rebase_pass(self) -> BasePass:
Expand Down Expand Up @@ -174,7 +175,7 @@ def required_predicates(self) -> List[Predicate]:
preds.append(MaxNQubitsPredicate(self._backend_info.n_nodes))
return preds

def default_compilation_pass(self, optimisation_level: int = 2) -> BasePass:
def default_compilation_pass(self, optimisation_level: int = 2) -> SequencePass:
assert optimisation_level in range(3)
if optimisation_level == 0:
return SequencePass(
Expand Down Expand Up @@ -247,6 +248,11 @@ def get_result(self, handle: ResultHandle, **kwargs: KwargTypes) -> BackendResul
raise NotImplementedError

def get_compiled_circuit(
self, circuit: Circuit, optimisation_level: int = 2
) -> Circuit:
raise NotImplementedError("Use get_compiled_circuit_mz for Multizone Circuits")

def get_compiled_circuit_mz(
self, circuit: MultiZoneCircuit, optimisation_level: int = 2
) -> MultiZoneCircuit:
"""Compile a MultiZoneCircuit to run on an AQT multi-zone architecture
Expand All @@ -264,10 +270,12 @@ def get_compiled_circuit(
new_circuit = MultiZoneCircuit(
circuit.architecture,
new_initial_zone_to_qubits,
circuit.n_qubits,
circuit.n_bits,
circuit.pytket_circuit.n_qubits,
circuit.pytket_circuit.n_bits,
)
compiled_circuit = super().get_compiled_circuit(
circuit.pytket_circuit, optimisation_level
)
compiled_circuit = super().get_compiled_circuit(circuit)

new_circuit.zone_to_qubits = deepcopy(circuit.zone_to_qubits)
new_circuit.multi_zone_operations = deepcopy(circuit.multi_zone_operations)
Expand All @@ -290,7 +298,7 @@ def get_compiled_circuit(
current_multiop_index_per_qubit[qubit] = current_multiop_index + 1
else:
qubits = [q.index[0] for q in cmd.args]
new_circuit.add_gate(cmd.op.type, op.params, qubits)
new_circuit.add_gate(cmd.op.type, qubits, op.params)

new_circuit.is_compiled = True
return new_circuit
Expand Down Expand Up @@ -329,7 +337,7 @@ def swap_position(qubit_1_: int, qubit_2_: int) -> None:
qubit_to_zone_position[qubit_1_] = (zone_2, position_2)
qubit_to_zone_position[qubit_2_] = (zone_1, position_1)

for cmd in circ.get_commands():
for cmd in circ.pytket_circuit.get_commands():
op = cmd.op
optype = op.type
op_string = f"{op}"
Expand Down Expand Up @@ -413,7 +421,7 @@ def swap_position(qubit_1_: int, qubit_2_: int) -> None:
return gates, json.dumps(measures)


def _aqt_rebase() -> RebaseCustom:
def _aqt_rebase() -> BasePass:
return auto_rebase_pass({OpType.XXPhase, OpType.Rx, OpType.Ry})


Expand Down
4 changes: 2 additions & 2 deletions pytket/extensions/aqt/backends/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.
"""AQT config."""
from dataclasses import dataclass
from typing import Any
from typing import Any, cast
from typing import ClassVar
from typing import Dict
from typing import Optional
Expand Down Expand Up @@ -41,7 +41,7 @@ def set_aqt_config(
access_token: Optional[str] = None,
) -> None:
"""Set default value for AQT API token."""
config = AQTConfig.from_default_config_file()
config = cast(AQTConfig, AQTConfig.from_default_config_file())
if access_token is not None:
config.access_token = access_token
config.update_default_config_file()
Loading

0 comments on commit e0a4e6f

Please sign in to comment.