Skip to content

Commit 080ec36

Browse files
authored
Update Type Hints (#7348)
* update type hints for files in cirq-aqt * fix formatting
1 parent e58f3ad commit 080ec36

6 files changed

Lines changed: 21 additions & 29 deletions

File tree

cirq-aqt/cirq_aqt/aqt_device.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
import json
3030
from enum import Enum
31-
from typing import Any, cast, Dict, Iterable, List, Optional, Sequence, Set, Tuple, Union
31+
from typing import Any, cast, Iterable, Sequence
3232

3333
import networkx as nx
3434
import numpy as np
@@ -89,7 +89,7 @@ def __init__(self):
8989

9090
def noisy_moment(
9191
self, moment: cirq.Moment, system_qubits: Sequence[cirq.Qid]
92-
) -> List[cirq.Operation]:
92+
) -> list[cirq.Operation]:
9393
"""Returns a list of noisy moments.
9494
9595
The model includes
@@ -117,7 +117,7 @@ def noisy_moment(
117117

118118
def get_crosstalk_operation(
119119
self, operation: cirq.Operation, system_qubits: Sequence[cirq.Qid]
120-
) -> List[cirq.Operation]:
120+
) -> list[cirq.Operation]:
121121
"""Returns a list of operations including crosstalk
122122
123123
Args:
@@ -127,7 +127,7 @@ def get_crosstalk_operation(
127127
Returns:
128128
List of operations including crosstalk
129129
"""
130-
cast(Tuple[cirq.LineQubit], system_qubits)
130+
cast(tuple[cirq.LineQubit], system_qubits)
131131
num_qubits = len(system_qubits)
132132
xtlk_arr = np.zeros(num_qubits)
133133
idx_list = []
@@ -169,7 +169,7 @@ def __init__(
169169
num_qubits: int,
170170
circuit: cirq.Circuit = cirq.Circuit(),
171171
simulate_ideal: bool = False,
172-
noise_dict: Optional[Dict] = None,
172+
noise_dict: dict | None = None,
173173
):
174174
"""Initializes the AQT simulator.
175175
@@ -200,7 +200,7 @@ def generate_circuit_from_list(self, json_string: str):
200200
"""
201201
self.circuit = cirq.Circuit()
202202
json_obj = json.loads(json_string)
203-
gate: Union[cirq.PhasedXPowGate, cirq.EigenGate]
203+
gate: cirq.PhasedXPowGate | cirq.EigenGate
204204
for circuit_list in json_obj:
205205
op_str = circuit_list[0]
206206
if op_str == 'R':
@@ -308,7 +308,7 @@ def validate_circuit(self, circuit: cirq.AbstractCircuit):
308308
super().validate_circuit(circuit)
309309
_verify_unique_measurement_keys(circuit.all_operations())
310310

311-
def at(self, position: int) -> Optional[cirq.LineQubit]:
311+
def at(self, position: int) -> cirq.LineQubit | None:
312312
"""Returns the qubit at the given position, if there is one, else None."""
313313
q = cirq.LineQubit(position)
314314
return q if q in self.qubits else None
@@ -341,7 +341,7 @@ def _repr_pretty_(self, p: Any, cycle: bool):
341341
p.text("AQTDevice(...)" if cycle else self.__str__())
342342

343343

344-
def get_aqt_device(num_qubits: int) -> Tuple[AQTDevice, List[cirq.LineQubit]]:
344+
def get_aqt_device(num_qubits: int) -> tuple[AQTDevice, list[cirq.LineQubit]]:
345345
"""Returns an AQT ion device
346346
347347
Args:
@@ -361,7 +361,7 @@ def get_aqt_device(num_qubits: int) -> Tuple[AQTDevice, List[cirq.LineQubit]]:
361361
return ion_device, qubit_list
362362

363363

364-
def get_default_noise_dict() -> Dict[str, Any]:
364+
def get_default_noise_dict() -> dict[str, Any]:
365365
"""Returns the current noise parameters"""
366366
default_noise_dict = {
367367
OperationString.R.value: cirq.depolarize(1e-3),
@@ -373,7 +373,7 @@ def get_default_noise_dict() -> Dict[str, Any]:
373373

374374

375375
def _verify_unique_measurement_keys(operations: Iterable[cirq.Operation]):
376-
seen: Set[str] = set()
376+
seen: set[str] = set()
377377
for op in operations:
378378
if isinstance(op.gate, cirq.MeasurementGate):
379379
meas = op.gate

cirq-aqt/cirq_aqt/aqt_device_metadata_test.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
from __future__ import annotations
1818

19-
from typing import List
20-
2119
import pytest
2220

2321
import cirq
@@ -26,7 +24,7 @@
2624

2725

2826
@pytest.fixture
29-
def qubits() -> List[cirq.LineQubit]:
27+
def qubits() -> list[cirq.LineQubit]:
3028
return cirq.LineQubit.range(5)
3129

3230

cirq-aqt/cirq_aqt/aqt_device_test.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from __future__ import annotations
1616

1717
from datetime import timedelta
18-
from typing import List
1918

2019
import pytest
2120

@@ -24,7 +23,7 @@
2423

2524

2625
@pytest.fixture
27-
def qubits() -> List[cirq.LineQubit]:
26+
def qubits() -> list[cirq.LineQubit]:
2827
return cirq.LineQubit.range(3)
2928

3029

cirq-aqt/cirq_aqt/aqt_sampler.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import json
2828
import time
2929
import uuid
30-
from typing import Callable, cast, Dict, List, Literal, Sequence, Tuple, TypedDict, Union
30+
from typing import Callable, cast, Literal, Sequence, TypedDict
3131
from urllib.parse import urljoin
3232

3333
import numpy as np
@@ -251,16 +251,14 @@ def _generate_json(
251251
RuntimeError: If the circuit is empty.
252252
"""
253253

254-
seq_list: List[Union[Tuple[str, float, List[int]], Tuple[str, float, float, List[int]]]] = (
255-
[]
256-
)
254+
seq_list: list[tuple[str, float, list[int]] | tuple[str, float, float, list[int]]] = []
257255
circuit = cirq.resolve_parameters(circuit, param_resolver)
258256
for op in circuit.all_operations():
259-
line_qubit = cast(Tuple[cirq.LineQubit], op.qubits)
257+
line_qubit = cast(tuple[cirq.LineQubit], op.qubits)
260258
op = cast(cirq.GateOperation, op)
261259
qubit_idx = [obj.x for obj in line_qubit]
262260
op_str = get_op_string(op)
263-
gate: Union[cirq.EigenGate, cirq.PhasedXPowGate]
261+
gate: cirq.EigenGate | cirq.PhasedXPowGate
264262
if op_str == 'R':
265263
gate = cast(cirq.PhasedXPowGate, op.gate)
266264
seq_list.append(
@@ -369,7 +367,7 @@ def _send_json(
369367

370368
response = post(submission_url, json=submission_data, headers=headers)
371369
response = response.json()
372-
data = cast(Dict, response)
370+
data = cast(dict, response)
373371

374372
if 'response' not in data.keys() or 'status' not in data['response'].keys():
375373
raise RuntimeError('Got unexpected return data from server: \n' + str(data))
@@ -384,7 +382,7 @@ def _send_json(
384382
while True:
385383
response = get(result_url, headers=headers)
386384
response = response.json()
387-
data = cast(Dict, response)
385+
data = cast(dict, response)
388386

389387
if 'response' not in data.keys() or 'status' not in data['response'].keys():
390388
raise RuntimeError('Got unexpected return data from AQT server: \n' + str(data))
@@ -426,7 +424,7 @@ def run_sweep(
426424
# TODO: Use measurement name from circuit.
427425
# Github issue: https://github.com/quantumlib/Cirq/issues/2199
428426
meas_name = 'm'
429-
trial_results: List[cirq.Result] = []
427+
trial_results: list[cirq.Result] = []
430428
for param_resolver in cirq.to_resolvers(params):
431429
id_str = str(uuid.uuid1())
432430
num_qubits = len(program.all_qubits())

cirq-aqt/cirq_aqt/aqt_target_gateset.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
from __future__ import annotations
1818

19-
from typing import List
20-
2119
import numpy as np
2220

2321
import cirq
@@ -67,6 +65,6 @@ def _decompose_two_qubit_operation(self, op: cirq.Operation, _) -> DecomposeResu
6765
return NotImplemented
6866

6967
@property
70-
def postprocess_transformers(self) -> List[cirq.TRANSFORMER]:
68+
def postprocess_transformers(self) -> list[cirq.TRANSFORMER]:
7169
"""List of transformers which should be run after decomposing individual operations."""
7270
return [cirq.drop_negligible_operations, cirq.drop_empty_moments]

cirq-aqt/cirq_aqt/json_resolver_cache.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
from __future__ import annotations
1616

1717
import functools
18-
from typing import Dict
1918

2019
from cirq.protocols.json_serialization import ObjectFactory
2120

2221

2322
@functools.lru_cache()
24-
def _class_resolver_dictionary() -> Dict[str, ObjectFactory]:
23+
def _class_resolver_dictionary() -> dict[str, ObjectFactory]:
2524
return {}

0 commit comments

Comments
 (0)