Skip to content

Commit 78ceed5

Browse files
committed
Switch to unquoted type annotations files part 5/5
1 parent 8a106dd commit 78ceed5

10 files changed

+88
-85
lines changed

cirq-core/cirq/contrib/routing/utils.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from __future__ import annotations
16+
1517
import operator
1618
import re
1719
from typing import Callable, Iterable, List, TYPE_CHECKING
@@ -86,7 +88,7 @@ def is_valid_routing(
8688
raise
8789

8890

89-
def get_circuit_connectivity(circuit: 'cirq.Circuit') -> nx.Graph:
91+
def get_circuit_connectivity(circuit: cirq.Circuit) -> nx.Graph:
9092
"""Return a graph of all 2q interactions in a circuit.
9193
9294
Nodes are qubits and undirected edges correspond to any two-qubit

cirq-core/cirq/contrib/svg/svg.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# pylint: disable=wrong-or-nonexistent-copyright-notice
2+
from __future__ import annotations
3+
24
from typing import cast, Dict, List, Tuple, TYPE_CHECKING
35

46
import matplotlib.font_manager
@@ -59,7 +61,7 @@ def _text(x: float, y: float, text: str, fontsize: int = 14):
5961

6062

6163
def _fit_horizontal(
62-
tdd: 'cirq.TextDiagramDrawer', ref_boxwidth: float, col_padding: float
64+
tdd: cirq.TextDiagramDrawer, ref_boxwidth: float, col_padding: float
6365
) -> Tuple[List[float], List[float]]:
6466
"""Figure out the horizontal spacing of columns to fit everything in.
6567
@@ -89,7 +91,7 @@ def _fit_horizontal(
8991

9092

9193
def _fit_vertical(
92-
tdd: 'cirq.TextDiagramDrawer', ref_boxheight: float, row_padding: float
94+
tdd: cirq.TextDiagramDrawer, ref_boxheight: float, row_padding: float
9395
) -> Tuple[List[float], List[float], Dict[float, int]]:
9496
"""Return data structures used to turn tdd vertical coordinates into
9597
well-spaced SVG coordinates.
@@ -164,7 +166,7 @@ def _debug_spacing(col_starts, row_starts): # pragma: no cover
164166

165167

166168
def tdd_to_svg(
167-
tdd: 'cirq.TextDiagramDrawer',
169+
tdd: cirq.TextDiagramDrawer,
168170
ref_boxwidth: float = 40,
169171
ref_boxheight: float = 40,
170172
col_padding: float = 20,
@@ -246,7 +248,7 @@ def tdd_to_svg(
246248
return t
247249

248250

249-
def _validate_circuit(circuit: 'cirq.Circuit'):
251+
def _validate_circuit(circuit: cirq.Circuit):
250252
if len(circuit) == 0:
251253
raise ValueError("Can't draw SVG diagram for empty circuits")
252254

@@ -260,14 +262,14 @@ class SVGCircuit:
260262
which will cause the circuit to be displayed as an SVG image.
261263
"""
262264

263-
def __init__(self, circuit: 'cirq.Circuit'):
265+
def __init__(self, circuit: cirq.Circuit):
264266
self.circuit = circuit
265267

266268
def _repr_svg_(self) -> str:
267269
return circuit_to_svg(self.circuit)
268270

269271

270-
def circuit_to_svg(circuit: 'cirq.Circuit') -> str:
272+
def circuit_to_svg(circuit: cirq.Circuit) -> str:
271273
"""Render a circuit as SVG."""
272274
_validate_circuit(circuit)
273275
tdd = circuit.to_text_diagram_drawer(transpose=False)

cirq-core/cirq/devices/device.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from __future__ import annotations
16+
1517
import abc
1618
from typing import FrozenSet, Iterable, Optional, TYPE_CHECKING
1719

@@ -65,7 +67,7 @@ def metadata(self) -> Optional['DeviceMetadata']:
6567
"""
6668
return None
6769

68-
def validate_operation(self, operation: 'cirq.Operation') -> None:
70+
def validate_operation(self, operation: cirq.Operation) -> None:
6971
"""Raises an exception if an operation is not valid.
7072
7173
Args:
@@ -75,7 +77,7 @@ def validate_operation(self, operation: 'cirq.Operation') -> None:
7577
ValueError: The operation isn't valid for this device.
7678
"""
7779

78-
def validate_circuit(self, circuit: 'cirq.AbstractCircuit') -> None:
80+
def validate_circuit(self, circuit: cirq.AbstractCircuit) -> None:
7981
"""Raises an exception if a circuit is not valid.
8082
8183
Args:
@@ -87,7 +89,7 @@ def validate_circuit(self, circuit: 'cirq.AbstractCircuit') -> None:
8789
for moment in circuit:
8890
self.validate_moment(moment)
8991

90-
def validate_moment(self, moment: 'cirq.Moment') -> None:
92+
def validate_moment(self, moment: cirq.Moment) -> None:
9193
"""Raises an exception if a moment is not valid.
9294
9395
Args:
@@ -104,7 +106,7 @@ def validate_moment(self, moment: 'cirq.Moment') -> None:
104106
class DeviceMetadata:
105107
"""Parent type for all device specific metadata classes."""
106108

107-
def __init__(self, qubits: Iterable['cirq.Qid'], nx_graph: 'nx.Graph'):
109+
def __init__(self, qubits: Iterable[cirq.Qid], nx_graph: nx.Graph):
108110
"""Construct a DeviceMetadata object.
109111
110112
Args:
@@ -114,11 +116,11 @@ def __init__(self, qubits: Iterable['cirq.Qid'], nx_graph: 'nx.Graph'):
114116
directional coupling, undirected edges indicate bi-directional
115117
coupling.
116118
"""
117-
self._qubits_set: FrozenSet['cirq.Qid'] = frozenset(qubits)
119+
self._qubits_set: FrozenSet[cirq.Qid] = frozenset(qubits)
118120
self._nx_graph = nx_graph
119121

120122
@property
121-
def qubit_set(self) -> FrozenSet['cirq.Qid']:
123+
def qubit_set(self) -> FrozenSet[cirq.Qid]:
122124
"""Returns the set of qubits on the device.
123125
124126
Returns:
@@ -127,7 +129,7 @@ def qubit_set(self) -> FrozenSet['cirq.Qid']:
127129
return self._qubits_set
128130

129131
@property
130-
def nx_graph(self) -> 'nx.Graph':
132+
def nx_graph(self) -> nx.Graph:
131133
"""Returns a nx.Graph where nodes are qubits and edges are couple-able qubits.
132134
133135
Returns:
@@ -150,6 +152,6 @@ def _json_dict_(self):
150152
return {'qubits': qubits_payload, 'nx_graph': graph_payload}
151153

152154
@classmethod
153-
def _from_json_dict_(cls, qubits: Iterable['cirq.Qid'], nx_graph: 'nx.Graph', **kwargs):
155+
def _from_json_dict_(cls, qubits: Iterable[cirq.Qid], nx_graph: nx.Graph, **kwargs):
154156
graph_obj = nx.readwrite.json_graph.node_link_graph(nx_graph)
155157
return cls(qubits, graph_obj)

cirq-core/cirq/devices/grid_device_metadata.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
1415
"""Metadata subtype for 2D Homogenous devices."""
1516

17+
from __future__ import annotations
18+
1619
from typing import cast, FrozenSet, Iterable, Mapping, Optional, Tuple, TYPE_CHECKING
1720

1821
import networkx as nx
@@ -30,11 +33,11 @@ class GridDeviceMetadata(device.DeviceMetadata):
3033

3134
def __init__(
3235
self,
33-
qubit_pairs: Iterable[Tuple['cirq.GridQubit', 'cirq.GridQubit']],
34-
gateset: 'cirq.Gateset',
35-
gate_durations: Optional[Mapping['cirq.GateFamily', 'cirq.Duration']] = None,
36-
all_qubits: Optional[Iterable['cirq.GridQubit']] = None,
37-
compilation_target_gatesets: Iterable['cirq.CompilationTargetGateset'] = (),
36+
qubit_pairs: Iterable[Tuple[cirq.GridQubit, cirq.GridQubit]],
37+
gateset: cirq.Gateset,
38+
gate_durations: Optional[Mapping[cirq.GateFamily, cirq.Duration]] = None,
39+
all_qubits: Optional[Iterable[cirq.GridQubit]] = None,
40+
compilation_target_gatesets: Iterable[cirq.CompilationTargetGateset] = (),
3841
):
3942
"""Create a GridDeviceMetadata object.
4043
@@ -115,7 +118,7 @@ def __init__(
115118
self._gate_durations = gate_durations
116119

117120
@property
118-
def qubit_set(self) -> FrozenSet['cirq.GridQubit']:
121+
def qubit_set(self) -> FrozenSet[cirq.GridQubit]:
119122
"""Returns the set of grid qubits on the device.
120123
121124
Returns:
@@ -124,7 +127,7 @@ def qubit_set(self) -> FrozenSet['cirq.GridQubit']:
124127
return cast(FrozenSet['cirq.GridQubit'], super().qubit_set)
125128

126129
@property
127-
def qubit_pairs(self) -> FrozenSet[FrozenSet['cirq.GridQubit']]:
130+
def qubit_pairs(self) -> FrozenSet[FrozenSet[cirq.GridQubit]]:
128131
"""Returns the set of all couple-able qubits on the device.
129132
130133
Each element in the outer frozenset is a 2-element frozenset representing a bidirectional
@@ -133,22 +136,22 @@ def qubit_pairs(self) -> FrozenSet[FrozenSet['cirq.GridQubit']]:
133136
return self._qubit_pairs
134137

135138
@property
136-
def isolated_qubits(self) -> FrozenSet['cirq.GridQubit']:
139+
def isolated_qubits(self) -> FrozenSet[cirq.GridQubit]:
137140
"""Returns the set of all isolated qubits on the device (if applicable)."""
138141
return self._isolated_qubits
139142

140143
@property
141-
def gateset(self) -> 'cirq.Gateset':
144+
def gateset(self) -> cirq.Gateset:
142145
"""Returns the `cirq.Gateset` of supported gates on this device."""
143146
return self._gateset
144147

145148
@property
146-
def compilation_target_gatesets(self) -> Tuple['cirq.CompilationTargetGateset', ...]:
149+
def compilation_target_gatesets(self) -> Tuple[cirq.CompilationTargetGateset, ...]:
147150
"""Returns a sequence of valid `cirq.CompilationTargetGateset`s for this device."""
148151
return self._compilation_target_gatesets
149152

150153
@property
151-
def gate_durations(self) -> Optional[Mapping['cirq.GateFamily', 'cirq.Duration']]:
154+
def gate_durations(self) -> Optional[Mapping[cirq.GateFamily, cirq.Duration]]:
152155
"""Get a dictionary mapping from gate family to duration for gates.
153156
154157
To look up the duration of a specific gate instance / gate type / operation which is part of

cirq-core/cirq/devices/grid_qubit.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from __future__ import annotations
16+
1517
import abc
1618
import functools
1719
import weakref
@@ -107,7 +109,7 @@ def dimension(self) -> int:
107109
def with_dimension(self, dimension: int) -> 'GridQid':
108110
return GridQid(self._row, self._col, dimension=dimension)
109111

110-
def is_adjacent(self, other: 'cirq.Qid') -> bool:
112+
def is_adjacent(self, other: cirq.Qid) -> bool:
111113
"""Determines if two qubits are adjacent qubits."""
112114
return (
113115
isinstance(other, GridQubit)
@@ -204,7 +206,7 @@ class GridQid(_BaseGridQid):
204206
# Holds weak references so instances can still be garbage collected.
205207
_cache = weakref.WeakValueDictionary[Tuple[int, int, int], 'cirq.GridQid']()
206208

207-
def __new__(cls, row: int, col: int, *, dimension: int) -> 'cirq.GridQid':
209+
def __new__(cls, row: int, col: int, *, dimension: int) -> cirq.GridQid:
208210
"""Creates a grid qid at the given row, col coordinate
209211
210212
Args:
@@ -337,9 +339,7 @@ def __repr__(self) -> str:
337339
def __str__(self) -> str:
338340
return f"q({self._row}, {self._col}) (d={self._dimension})"
339341

340-
def _circuit_diagram_info_(
341-
self, args: 'cirq.CircuitDiagramInfoArgs'
342-
) -> 'cirq.CircuitDiagramInfo':
342+
def _circuit_diagram_info_(self, args: cirq.CircuitDiagramInfoArgs) -> cirq.CircuitDiagramInfo:
343343
return protocols.CircuitDiagramInfo(
344344
wire_symbols=(f"({self._row}, {self._col}) (d={self._dimension})",)
345345
)
@@ -371,7 +371,7 @@ class GridQubit(_BaseGridQid):
371371
# Holds weak references so instances can still be garbage collected.
372372
_cache = weakref.WeakValueDictionary[Tuple[int, int], 'cirq.GridQubit']()
373373

374-
def __new__(cls, row: int, col: int) -> 'cirq.GridQubit':
374+
def __new__(cls, row: int, col: int) -> cirq.GridQubit:
375375
"""Creates a grid qubit at the given row, col coordinate
376376
377377
Args:
@@ -490,9 +490,7 @@ def __repr__(self) -> str:
490490
def __str__(self) -> str:
491491
return f"q({self._row}, {self._col})"
492492

493-
def _circuit_diagram_info_(
494-
self, args: 'cirq.CircuitDiagramInfoArgs'
495-
) -> 'cirq.CircuitDiagramInfo':
493+
def _circuit_diagram_info_(self, args: cirq.CircuitDiagramInfoArgs) -> cirq.CircuitDiagramInfo:
496494
return protocols.CircuitDiagramInfo(wire_symbols=(f"({self._row}, {self._col})",))
497495

498496
def _json_dict_(self) -> Dict[str, Any]:

cirq-core/cirq/devices/grid_qubit_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def test_grid_qid_pickled_hash():
5858
_test_qid_pickled_hash(q, q_bad)
5959

6060

61-
def _test_qid_pickled_hash(q: 'cirq.Qid', q_bad: 'cirq.Qid') -> None:
61+
def _test_qid_pickled_hash(q: cirq.Qid, q_bad: cirq.Qid) -> None:
6262
"""Test that hashes are not pickled with Qid instances."""
6363
assert q_bad is not q
6464
_ = hash(q_bad) # compute hash to ensure it is cached.

cirq-core/cirq/devices/insertion_noise_model.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from __future__ import annotations
16+
1517
import dataclasses
1618
from typing import Any, Dict, List, Optional, Sequence, TYPE_CHECKING
1719

@@ -42,16 +44,14 @@ class InsertionNoiseModel(devices.NoiseModel):
4244
with PHYSICAL_GATE_TAG.
4345
"""
4446

45-
ops_added: Dict[noise_utils.OpIdentifier, 'cirq.Operation'] = dataclasses.field(
47+
ops_added: Dict[noise_utils.OpIdentifier, cirq.Operation] = dataclasses.field(
4648
default_factory=dict
4749
)
4850
prepend: bool = False
4951
require_physical_tag: bool = True
5052

51-
def noisy_moment(
52-
self, moment: 'cirq.Moment', system_qubits: Sequence['cirq.Qid']
53-
) -> 'cirq.OP_TREE':
54-
noise_ops: List['cirq.Operation'] = []
53+
def noisy_moment(self, moment: cirq.Moment, system_qubits: Sequence[cirq.Qid]) -> cirq.OP_TREE:
54+
noise_ops: List[cirq.Operation] = []
5555
candidate_ops = [
5656
op
5757
for op in moment

cirq-core/cirq/devices/line_qubit.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from __future__ import annotations
16+
1517
import abc
1618
import functools
1719
import weakref
@@ -96,7 +98,7 @@ def dimension(self) -> int:
9698
def with_dimension(self, dimension: int) -> 'LineQid':
9799
return LineQid(self._x, dimension)
98100

99-
def is_adjacent(self, other: 'cirq.Qid') -> bool:
101+
def is_adjacent(self, other: cirq.Qid) -> bool:
100102
"""Determines if two qubits are adjacent line qubits.
101103
102104
Args:
@@ -184,7 +186,7 @@ class LineQid(_BaseLineQid):
184186
# Holds weak references so instances can still be garbage collected.
185187
_cache = weakref.WeakValueDictionary[Tuple[int, int], 'cirq.LineQid']()
186188

187-
def __new__(cls, x: int, dimension: int) -> 'cirq.LineQid':
189+
def __new__(cls, x: int, dimension: int) -> cirq.LineQid:
188190
"""Initializes a line qid at the given x coordinate.
189191
190192
Args:
@@ -264,9 +266,7 @@ def __repr__(self) -> str:
264266
def __str__(self) -> str:
265267
return f"q({self._x}) (d={self._dimension})"
266268

267-
def _circuit_diagram_info_(
268-
self, args: 'cirq.CircuitDiagramInfoArgs'
269-
) -> 'cirq.CircuitDiagramInfo':
269+
def _circuit_diagram_info_(self, args: cirq.CircuitDiagramInfoArgs) -> cirq.CircuitDiagramInfo:
270270
return protocols.CircuitDiagramInfo(wire_symbols=(f"{self._x} (d={self._dimension})",))
271271

272272
def _json_dict_(self) -> Dict[str, Any]:
@@ -296,7 +296,7 @@ class LineQubit(_BaseLineQid):
296296
# Holds weak references so instances can still be garbage collected.
297297
_cache = weakref.WeakValueDictionary[int, 'cirq.LineQubit']()
298298

299-
def __new__(cls, x: int) -> 'cirq.LineQubit':
299+
def __new__(cls, x: int) -> cirq.LineQubit:
300300
"""Initializes a line qid at the given x coordinate.
301301
302302
Args:
@@ -339,9 +339,7 @@ def __repr__(self) -> str:
339339
def __str__(self) -> str:
340340
return f"q({self._x})"
341341

342-
def _circuit_diagram_info_(
343-
self, args: 'cirq.CircuitDiagramInfoArgs'
344-
) -> 'cirq.CircuitDiagramInfo':
342+
def _circuit_diagram_info_(self, args: cirq.CircuitDiagramInfoArgs) -> cirq.CircuitDiagramInfo:
345343
return protocols.CircuitDiagramInfo(wire_symbols=(f"{self._x}",))
346344

347345
def _json_dict_(self) -> Dict[str, Any]:

0 commit comments

Comments
 (0)