Skip to content

Commit 09b44c0

Browse files
authored
Take care of leftover conversions to PEP 585 style (#7382)
Clean old-style type annotations and unused import in notebooks. Related to #7339
1 parent 12eab31 commit 09b44c0

9 files changed

Lines changed: 27 additions & 29 deletions

File tree

cirq-core/cirq/protocols/circuit_diagram_info_protocol.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def __init__(
194194
known_qubit_count: int | None,
195195
use_unicode_characters: bool,
196196
precision: int | None,
197-
label_map: dict[cirq.LabelEntity, int] | None,
197+
label_map: dict[LabelEntity, int] | None,
198198
include_tags: bool = True,
199199
transpose: bool = False,
200200
) -> None:

cirq-core/cirq/transformers/transformer_api.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ class TRANSFORMER(Protocol):
231231
>>> def convert_to_cz(
232232
... circuit: cirq.AbstractCircuit,
233233
... *,
234-
... context: 'Optional[cirq.TransformerContext]' = None,
234+
... context: cirq.TransformerContext | None = None,
235235
... atol: float = 1e-8,
236236
... ) -> cirq.Circuit:
237237
... ...
@@ -245,7 +245,7 @@ class TRANSFORMER(Protocol):
245245
... self,
246246
... circuit: cirq.AbstractCircuit,
247247
... *,
248-
... context: 'Optional[cirq.TransformerContext]' = None,
248+
... context: cirq.TransformerContext | None = None,
249249
... ) -> cirq.AbstractCircuit:
250250
... ...
251251
"""
@@ -288,7 +288,7 @@ def transformer(cls_or_func: Any = None, *, add_deep_support: bool = False) -> A
288288
289289
>>> @cirq.transformer
290290
... def convert_to_cz(
291-
... circuit: cirq.AbstractCircuit, *, context: 'Optional[cirq.TransformerContext]' = None
291+
... circuit: cirq.AbstractCircuit, *, context: cirq.TransformerContext | None = None
292292
... ) -> cirq.Circuit:
293293
... ...
294294
@@ -302,7 +302,7 @@ def transformer(cls_or_func: Any = None, *, add_deep_support: bool = False) -> A
302302
... self,
303303
... circuit: cirq.AbstractCircuit,
304304
... *,
305-
... context: 'Optional[cirq.TransformerContext]' = None,
305+
... context: cirq.TransformerContext | None = None,
306306
... ) -> cirq.Circuit:
307307
... ...
308308
@@ -313,7 +313,7 @@ def transformer(cls_or_func: Any = None, *, add_deep_support: bool = False) -> A
313313
... def convert_to_sqrt_iswap(
314314
... circuit: cirq.AbstractCircuit,
315315
... *,
316-
... context: 'Optional[cirq.TransformerContext]' = None,
316+
... context: cirq.TransformerContext | None = None,
317317
... atol: float = 1e-8,
318318
... sqrt_iswap_gate: cirq.ISwapPowGate = cirq.SQRT_ISWAP_INV,
319319
... cleanup_operations: bool = True,

docs/dev/plotting.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ interactive session. The recommended way to achieve that is illustrated in the
2828
example below.
2929

3030
```python
31-
from typing import Any, List, Optional
31+
from typing import Any
3232
import matplotlib.pyplot as plt
3333

3434
class Foo:
3535
...
36-
def plot(self, ax: Optional[plt.Axes]=None, **plot_kwargs: Any) -> plt.Axes:
36+
def plot(self, ax: plt.Axes | None = None, **plot_kwargs: Any) -> plt.Axes:
3737
show_plot = not ax
3838
if ax is None:
3939
fig, ax = plt.subplots(1, 1) # or your favorite figure setup
@@ -78,8 +78,8 @@ not sufficient. The `plot` method of such a class should take an optional
7878
```python
7979
class Foo:
8080
...
81-
def plot(self, axes: Optional[List[plt.Axes]]=None,
82-
**plot_kwargs: Any) -> List[plt.Axes]:
81+
def plot(self, axes: list[plt.Axes] | None = None,
82+
**plot_kwargs: Any) -> list[plt.Axes]:
8383
show_plot = not axes
8484
if axes is None:
8585
fig, axes = plt.subplots(1, 2) # or your favorite figure setup

docs/dev/style.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ qubit = cirq.NamedQubit('a')
3737
```
3838
The one exception to this is for the typing code, where we prefer the direct import
3939
```python
40-
from typing import List
40+
from typing import Mapping
4141
```
4242
This exception allows typing hints to be more compact.
4343

docs/experiments/shor.ipynb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
"\n",
106106
"import numpy as np\n",
107107
"import sympy\n",
108-
"from typing import Callable, Iterable, List, Optional, Sequence\n",
108+
"from typing import Callable, Iterable, Sequence\n",
109109
"\n",
110110
"import cirq"
111111
]
@@ -169,7 +169,7 @@
169169
"\"\"\"Function to compute the elements of Z_n.\"\"\"\n",
170170
"\n",
171171
"\n",
172-
"def multiplicative_group(n: int) -> List[int]:\n",
172+
"def multiplicative_group(n: int) -> list[int]:\n",
173173
" \"\"\"Returns the multiplicative group modulo n.\n",
174174
"\n",
175175
" Args:\n",
@@ -252,7 +252,7 @@
252252
"\"\"\"Function for classically computing the order of an element of Z_n.\"\"\"\n",
253253
"\n",
254254
"\n",
255-
"def classical_order_finder(x: int, n: int) -> Optional[int]:\n",
255+
"def classical_order_finder(x: int, n: int) -> int | None:\n",
256256
" \"\"\"Computes smallest positive r such that x**r mod n == 1.\n",
257257
"\n",
258258
" Args:\n",
@@ -826,7 +826,7 @@
826826
},
827827
"outputs": [],
828828
"source": [
829-
"def process_measurement(result: cirq.Result, x: int, n: int) -> Optional[int]:\n",
829+
"def process_measurement(result: cirq.Result, x: int, n: int) -> int | None:\n",
830830
" \"\"\"Interprets the output of the order finding circuit.\n",
831831
"\n",
832832
" Specifically, it determines s/r such that exp(2πis/r) is an eigenvalue\n",
@@ -935,7 +935,7 @@
935935
},
936936
"outputs": [],
937937
"source": [
938-
"def quantum_order_finder(x: int, n: int) -> Optional[int]:\n",
938+
"def quantum_order_finder(x: int, n: int) -> int | None:\n",
939939
" \"\"\"Computes smallest positive r such that x**r mod n == 1.\n",
940940
"\n",
941941
" Args:\n",
@@ -1005,7 +1005,7 @@
10051005
"\"\"\"Functions for factoring from start to finish.\"\"\"\n",
10061006
"\n",
10071007
"\n",
1008-
"def find_factor_of_prime_power(n: int) -> Optional[int]:\n",
1008+
"def find_factor_of_prime_power(n: int) -> int | None:\n",
10091009
" \"\"\"Returns non-trivial factor of n if n is a prime power, else None.\"\"\"\n",
10101010
" for k in range(2, math.floor(math.log2(n)) + 1):\n",
10111011
" c = math.pow(n, 1 / k)\n",
@@ -1020,9 +1020,9 @@
10201020
"\n",
10211021
"def find_factor(\n",
10221022
" n: int,\n",
1023-
" order_finder: Callable[[int, int], Optional[int]] = quantum_order_finder,\n",
1023+
" order_finder: Callable[[int, int], int | None] = quantum_order_finder,\n",
10241024
" max_attempts: int = 30,\n",
1025-
") -> Optional[int]:\n",
1025+
") -> int | None:\n",
10261026
" \"\"\"Returns a non-trivial factor of composite integer n.\n",
10271027
"\n",
10281028
" Args:\n",

docs/named_topologies.ipynb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@
9090
},
9191
"outputs": [],
9292
"source": [
93-
"from typing import Iterable, List, Optional, Sequence\n",
94-
"\n",
9593
"import matplotlib.pyplot as plt\n",
9694
"import numpy as np\n",
9795
"import networkx as nx"

docs/noise/qcvv/xeb_calibration_example.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,14 @@
169169
"outputs": [],
170170
"source": [
171171
"# @title Helper functions\n",
172-
"from typing import Optional, Sequence\n",
172+
"from typing import Sequence\n",
173173
"\n",
174174
"\n",
175175
"def create_random_circuit(\n",
176176
" qubits: Sequence[cirq.GridQubit],\n",
177177
" cycles: int,\n",
178178
" twoq_gate: cirq.Gate = cirq.FSimGate(np.pi / 4, 0.0),\n",
179-
" seed: Optional[int] = None,\n",
179+
" seed: int | None = None,\n",
180180
") -> cirq.Circuit:\n",
181181
" return rqcg.random_rotations_between_grid_interaction_layers_circuit(\n",
182182
" qubits,\n",
@@ -194,7 +194,7 @@
194194
" qubits: Sequence[cirq.GridQubit],\n",
195195
" cycles: int,\n",
196196
" twoq_gate: cirq.Gate = cirq.FSimGate(np.pi / 4, 0.0),\n",
197-
" seed: Optional[int] = None,\n",
197+
" seed: int | None = None,\n",
198198
") -> cirq.Circuit:\n",
199199
" \"\"\"Returns a Loschmidt echo circuit using a random unitary U.\n",
200200
"\n",

docs/tutorials/google/echoes.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124
},
125125
"outputs": [],
126126
"source": [
127-
"from typing import Optional, Sequence\n",
127+
"from typing import Sequence\n",
128128
"\n",
129129
"import matplotlib.pyplot as plt\n",
130130
"import numpy as np\n",
@@ -202,8 +202,8 @@
202202
" qubits: Sequence[cirq.GridQubit],\n",
203203
" cycles: int,\n",
204204
" twoq_gate: cirq.Gate = cirq.FSimGate(np.pi / 4, 0.0),\n",
205-
" pause: Optional[cirq.Duration] = None,\n",
206-
" seed: Optional[int] = None,\n",
205+
" pause: cirq.Duration | None = None,\n",
206+
" seed: int | None = None,\n",
207207
") -> cirq.Circuit:\n",
208208
" \"\"\"Returns a Loschmidt echo circuit using a random unitary U.\n",
209209
"\n",

docs/tutorials/google/spin_echoes.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@
159159
"outputs": [],
160160
"source": [
161161
"# @markdown Helper functions.\n",
162-
"from typing import Optional, Sequence\n",
162+
"from typing import Sequence\n",
163163
"from cirq.experiments import random_rotations_between_grid_interaction_layers_circuit\n",
164164
"\n",
165165
"\n",
@@ -171,7 +171,7 @@
171171
" qubits: Sequence[cirq.GridQubit],\n",
172172
" cycles: int,\n",
173173
" twoq_gate: cirq.Gate = cirq.SQRT_ISWAP,\n",
174-
" seed: Optional[int] = None,\n",
174+
" seed: int | None = None,\n",
175175
" with_optimization: bool = False,\n",
176176
" with_alignment: bool = False,\n",
177177
" with_spin_echoes: bool = False,\n",

0 commit comments

Comments
 (0)