diff --git a/cirq-core/cirq/protocols/circuit_diagram_info_protocol.py b/cirq-core/cirq/protocols/circuit_diagram_info_protocol.py index e476a4ef7aa..dfb6fcdc235 100644 --- a/cirq-core/cirq/protocols/circuit_diagram_info_protocol.py +++ b/cirq-core/cirq/protocols/circuit_diagram_info_protocol.py @@ -194,7 +194,7 @@ def __init__( known_qubit_count: int | None, use_unicode_characters: bool, precision: int | None, - label_map: dict[cirq.LabelEntity, int] | None, + label_map: dict[LabelEntity, int] | None, include_tags: bool = True, transpose: bool = False, ) -> None: diff --git a/cirq-core/cirq/transformers/transformer_api.py b/cirq-core/cirq/transformers/transformer_api.py index a299bf08de6..3fbe43f5b75 100644 --- a/cirq-core/cirq/transformers/transformer_api.py +++ b/cirq-core/cirq/transformers/transformer_api.py @@ -231,7 +231,7 @@ class TRANSFORMER(Protocol): >>> def convert_to_cz( ... circuit: cirq.AbstractCircuit, ... *, - ... context: 'Optional[cirq.TransformerContext]' = None, + ... context: cirq.TransformerContext | None = None, ... atol: float = 1e-8, ... ) -> cirq.Circuit: ... ... @@ -245,7 +245,7 @@ class TRANSFORMER(Protocol): ... self, ... circuit: cirq.AbstractCircuit, ... *, - ... context: 'Optional[cirq.TransformerContext]' = None, + ... context: cirq.TransformerContext | None = None, ... ) -> cirq.AbstractCircuit: ... ... """ @@ -288,7 +288,7 @@ def transformer(cls_or_func: Any = None, *, add_deep_support: bool = False) -> A >>> @cirq.transformer ... def convert_to_cz( - ... circuit: cirq.AbstractCircuit, *, context: 'Optional[cirq.TransformerContext]' = None + ... circuit: cirq.AbstractCircuit, *, context: cirq.TransformerContext | None = None ... ) -> cirq.Circuit: ... ... @@ -302,7 +302,7 @@ def transformer(cls_or_func: Any = None, *, add_deep_support: bool = False) -> A ... self, ... circuit: cirq.AbstractCircuit, ... *, - ... context: 'Optional[cirq.TransformerContext]' = None, + ... context: cirq.TransformerContext | None = None, ... ) -> cirq.Circuit: ... ... @@ -313,7 +313,7 @@ def transformer(cls_or_func: Any = None, *, add_deep_support: bool = False) -> A ... def convert_to_sqrt_iswap( ... circuit: cirq.AbstractCircuit, ... *, - ... context: 'Optional[cirq.TransformerContext]' = None, + ... context: cirq.TransformerContext | None = None, ... atol: float = 1e-8, ... sqrt_iswap_gate: cirq.ISwapPowGate = cirq.SQRT_ISWAP_INV, ... cleanup_operations: bool = True, diff --git a/docs/dev/plotting.md b/docs/dev/plotting.md index 48f2047bd15..99a5aafd202 100644 --- a/docs/dev/plotting.md +++ b/docs/dev/plotting.md @@ -28,12 +28,12 @@ interactive session. The recommended way to achieve that is illustrated in the example below. ```python -from typing import Any, List, Optional +from typing import Any import matplotlib.pyplot as plt class Foo: ... - def plot(self, ax: Optional[plt.Axes]=None, **plot_kwargs: Any) -> plt.Axes: + def plot(self, ax: plt.Axes | None = None, **plot_kwargs: Any) -> plt.Axes: show_plot = not ax if ax is None: 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 ```python class Foo: ... - def plot(self, axes: Optional[List[plt.Axes]]=None, - **plot_kwargs: Any) -> List[plt.Axes]: + def plot(self, axes: list[plt.Axes] | None = None, + **plot_kwargs: Any) -> list[plt.Axes]: show_plot = not axes if axes is None: fig, axes = plt.subplots(1, 2) # or your favorite figure setup diff --git a/docs/dev/style.md b/docs/dev/style.md index a291182ad39..68ad0e0f237 100644 --- a/docs/dev/style.md +++ b/docs/dev/style.md @@ -37,7 +37,7 @@ qubit = cirq.NamedQubit('a') ``` The one exception to this is for the typing code, where we prefer the direct import ```python -from typing import List +from typing import Mapping ``` This exception allows typing hints to be more compact. diff --git a/docs/experiments/shor.ipynb b/docs/experiments/shor.ipynb index de98c88ae89..f178780d1d1 100644 --- a/docs/experiments/shor.ipynb +++ b/docs/experiments/shor.ipynb @@ -105,7 +105,7 @@ "\n", "import numpy as np\n", "import sympy\n", - "from typing import Callable, Iterable, List, Optional, Sequence\n", + "from typing import Callable, Iterable, Sequence\n", "\n", "import cirq" ] @@ -169,7 +169,7 @@ "\"\"\"Function to compute the elements of Z_n.\"\"\"\n", "\n", "\n", - "def multiplicative_group(n: int) -> List[int]:\n", + "def multiplicative_group(n: int) -> list[int]:\n", " \"\"\"Returns the multiplicative group modulo n.\n", "\n", " Args:\n", @@ -252,7 +252,7 @@ "\"\"\"Function for classically computing the order of an element of Z_n.\"\"\"\n", "\n", "\n", - "def classical_order_finder(x: int, n: int) -> Optional[int]:\n", + "def classical_order_finder(x: int, n: int) -> int | None:\n", " \"\"\"Computes smallest positive r such that x**r mod n == 1.\n", "\n", " Args:\n", @@ -826,7 +826,7 @@ }, "outputs": [], "source": [ - "def process_measurement(result: cirq.Result, x: int, n: int) -> Optional[int]:\n", + "def process_measurement(result: cirq.Result, x: int, n: int) -> int | None:\n", " \"\"\"Interprets the output of the order finding circuit.\n", "\n", " Specifically, it determines s/r such that exp(2πis/r) is an eigenvalue\n", @@ -935,7 +935,7 @@ }, "outputs": [], "source": [ - "def quantum_order_finder(x: int, n: int) -> Optional[int]:\n", + "def quantum_order_finder(x: int, n: int) -> int | None:\n", " \"\"\"Computes smallest positive r such that x**r mod n == 1.\n", "\n", " Args:\n", @@ -1005,7 +1005,7 @@ "\"\"\"Functions for factoring from start to finish.\"\"\"\n", "\n", "\n", - "def find_factor_of_prime_power(n: int) -> Optional[int]:\n", + "def find_factor_of_prime_power(n: int) -> int | None:\n", " \"\"\"Returns non-trivial factor of n if n is a prime power, else None.\"\"\"\n", " for k in range(2, math.floor(math.log2(n)) + 1):\n", " c = math.pow(n, 1 / k)\n", @@ -1020,9 +1020,9 @@ "\n", "def find_factor(\n", " n: int,\n", - " order_finder: Callable[[int, int], Optional[int]] = quantum_order_finder,\n", + " order_finder: Callable[[int, int], int | None] = quantum_order_finder,\n", " max_attempts: int = 30,\n", - ") -> Optional[int]:\n", + ") -> int | None:\n", " \"\"\"Returns a non-trivial factor of composite integer n.\n", "\n", " Args:\n", diff --git a/docs/named_topologies.ipynb b/docs/named_topologies.ipynb index b9b31318265..ec4ca50b794 100644 --- a/docs/named_topologies.ipynb +++ b/docs/named_topologies.ipynb @@ -90,8 +90,6 @@ }, "outputs": [], "source": [ - "from typing import Iterable, List, Optional, Sequence\n", - "\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import networkx as nx" diff --git a/docs/noise/qcvv/xeb_calibration_example.ipynb b/docs/noise/qcvv/xeb_calibration_example.ipynb index 826c8c1ebc5..e3f22ada767 100644 --- a/docs/noise/qcvv/xeb_calibration_example.ipynb +++ b/docs/noise/qcvv/xeb_calibration_example.ipynb @@ -169,14 +169,14 @@ "outputs": [], "source": [ "# @title Helper functions\n", - "from typing import Optional, Sequence\n", + "from typing import Sequence\n", "\n", "\n", "def create_random_circuit(\n", " qubits: Sequence[cirq.GridQubit],\n", " cycles: int,\n", " twoq_gate: cirq.Gate = cirq.FSimGate(np.pi / 4, 0.0),\n", - " seed: Optional[int] = None,\n", + " seed: int | None = None,\n", ") -> cirq.Circuit:\n", " return rqcg.random_rotations_between_grid_interaction_layers_circuit(\n", " qubits,\n", @@ -194,7 +194,7 @@ " qubits: Sequence[cirq.GridQubit],\n", " cycles: int,\n", " twoq_gate: cirq.Gate = cirq.FSimGate(np.pi / 4, 0.0),\n", - " seed: Optional[int] = None,\n", + " seed: int | None = None,\n", ") -> cirq.Circuit:\n", " \"\"\"Returns a Loschmidt echo circuit using a random unitary U.\n", "\n", diff --git a/docs/tutorials/google/echoes.ipynb b/docs/tutorials/google/echoes.ipynb index 5e024430dfb..aa4802f6f1c 100644 --- a/docs/tutorials/google/echoes.ipynb +++ b/docs/tutorials/google/echoes.ipynb @@ -124,7 +124,7 @@ }, "outputs": [], "source": [ - "from typing import Optional, Sequence\n", + "from typing import Sequence\n", "\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", @@ -202,8 +202,8 @@ " qubits: Sequence[cirq.GridQubit],\n", " cycles: int,\n", " twoq_gate: cirq.Gate = cirq.FSimGate(np.pi / 4, 0.0),\n", - " pause: Optional[cirq.Duration] = None,\n", - " seed: Optional[int] = None,\n", + " pause: cirq.Duration | None = None,\n", + " seed: int | None = None,\n", ") -> cirq.Circuit:\n", " \"\"\"Returns a Loschmidt echo circuit using a random unitary U.\n", "\n", diff --git a/docs/tutorials/google/spin_echoes.ipynb b/docs/tutorials/google/spin_echoes.ipynb index 75a1f5ee886..9d32ff00201 100644 --- a/docs/tutorials/google/spin_echoes.ipynb +++ b/docs/tutorials/google/spin_echoes.ipynb @@ -159,7 +159,7 @@ "outputs": [], "source": [ "# @markdown Helper functions.\n", - "from typing import Optional, Sequence\n", + "from typing import Sequence\n", "from cirq.experiments import random_rotations_between_grid_interaction_layers_circuit\n", "\n", "\n", @@ -171,7 +171,7 @@ " qubits: Sequence[cirq.GridQubit],\n", " cycles: int,\n", " twoq_gate: cirq.Gate = cirq.SQRT_ISWAP,\n", - " seed: Optional[int] = None,\n", + " seed: int | None = None,\n", " with_optimization: bool = False,\n", " with_alignment: bool = False,\n", " with_spin_echoes: bool = False,\n",