Skip to content

Commit 1be4d9a

Browse files
authored
Add return-type to public functions, mostly tests part 2 (#7568)
Add return-type to public functions, mostly tests part 2 No change in the effective code. A batch of ~50 files. Modified files pass ruff check --select=ANN201 Partially implements #4393
1 parent fe0dc21 commit 1be4d9a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+562
-497
lines changed

cirq-core/cirq/contrib/noise_models/noise_models.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def __init__(self, depol_prob: float, prepend: bool = False):
4545
self._prepend = prepend
4646

4747
@property
48-
def depol_prob(self):
48+
def depol_prob(self) -> float:
4949
"""The depolarizing probability."""
5050
return self._depol_prob
5151

@@ -100,7 +100,7 @@ def __init__(self, bitflip_prob: float, prepend: bool = True):
100100
self._prepend = prepend
101101

102102
@property
103-
def bitflip_prob(self):
103+
def bitflip_prob(self) -> float:
104104
"""The probability of a bit-flip during measurement."""
105105
return self._bitflip_prob
106106

@@ -154,7 +154,7 @@ def __init__(self, decay_prob: float, prepend: bool = True):
154154
self._prepend = prepend
155155

156156
@property
157-
def decay_prob(self):
157+
def decay_prob(self) -> float:
158158
"""The probability of T1 decay during measurement."""
159159
return self._decay_prob
160160

@@ -207,12 +207,12 @@ def __init__(self, depol_prob: float, bitflip_prob: float):
207207
self.readout_noise_gate = ops.BitFlipChannel(bitflip_prob)
208208

209209
@property
210-
def depol_prob(self):
210+
def depol_prob(self) -> float:
211211
"""The depolarizing probability."""
212212
return self._depol_prob
213213

214214
@property
215-
def bitflip_prob(self):
215+
def bitflip_prob(self) -> float:
216216
"""The probability of a bit-flip during measurement."""
217217
return self._bitflip_prob
218218

@@ -262,17 +262,17 @@ def __init__(self, depol_prob: float, bitflip_prob: float, decay_prob: float):
262262
self.readout_decay_gate = ops.AmplitudeDampingChannel(decay_prob)
263263

264264
@property
265-
def depol_prob(self):
265+
def depol_prob(self) -> float:
266266
"""The depolarizing probability."""
267267
return self._depol_prob
268268

269269
@property
270-
def bitflip_prob(self):
270+
def bitflip_prob(self) -> float:
271271
"""Probability of a bit-flip during measurement."""
272272
return self._bitflip_prob
273273

274274
@property
275-
def decay_prob(self):
275+
def decay_prob(self) -> float:
276276
"""The probability of T1 decay during measurement."""
277277
return self._decay_prob
278278

cirq-core/cirq/contrib/quimb/grid_circuits.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def _interaction(
105105
)
106106

107107

108-
def simplify_expectation_value_circuit(circuit_sand: cirq.Circuit):
108+
def simplify_expectation_value_circuit(circuit_sand: cirq.Circuit) -> None:
109109
"""For low weight operators on low-degree circuits, we can simplify
110110
the circuit representation of an expectation value.
111111

cirq-core/cirq/contrib/quimb/grid_circuits_test.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from __future__ import annotations
44

5+
from typing import Sequence
6+
57
import networkx as nx
68
import numpy as np
79
import pytest
@@ -26,12 +28,13 @@ def _get_circuit(width, height, rs, p=2):
2628
return circuit, qubits
2729

2830

29-
def test_simplify_sandwich():
31+
def test_simplify_sandwich() -> None:
3032
rs = np.random.RandomState(52)
3133
for width in [2, 3]:
3234
for height in [1, 3]:
3335
for p in [1, 2]:
3436
circuit, qubits = _get_circuit(width=width, height=height, p=p, rs=rs)
37+
operator: cirq.PauliString
3538
operator = cirq.PauliString(
3639
{q: cirq.Z for q in rs.choice(qubits, size=2, replace=False)}
3740
)
@@ -47,11 +50,12 @@ def test_simplify_sandwich():
4750

4851

4952
@pytest.mark.parametrize('simplify', [False, True])
50-
def test_circuit_to_tensors(simplify):
51-
rs = np.random.RandomState(52)
53+
def test_circuit_to_tensors(simplify) -> None:
54+
qubits: Sequence[cirq.Qid]
5255
qubits = cirq.LineQubit.range(2)
5356
circuit = cirq.testing.random_circuit(qubits=qubits, n_moments=10, op_density=0.8)
54-
operator = cirq.PauliString({q: cirq.Z for q in rs.choice(qubits, size=2, replace=False)})
57+
operator: cirq.PauliString
58+
operator = cirq.PauliString(dict.fromkeys(qubits, cirq.Z))
5559

5660
circuit_sand = ccq.circuit_for_expectation_value(circuit, operator)
5761
if simplify:
@@ -62,13 +66,14 @@ def test_circuit_to_tensors(simplify):
6266
np.testing.assert_allclose(u_tn, u_cirq, atol=1e-6)
6367

6468

65-
def test_tensor_expectation_value():
69+
def test_tensor_expectation_value() -> None:
6670
for _ in range(10):
6771
for width in [2, 3]:
6872
for height in [1, 3]:
6973
for p in [1, 2]:
7074
rs = np.random.RandomState(52)
7175
circuit, qubits = _get_circuit(width=width, height=height, p=p, rs=rs)
76+
operator: cirq.PauliString
7277
operator = cirq.PauliString(
7378
{q: cirq.Z for q in rs.choice(qubits, size=2, replace=False)},
7479
coefficient=rs.uniform(-1, 1),

cirq-core/cirq/contrib/quimb/mps_simulator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def __init__(self, sim_state: cirq.SimulationStateBase[MPSState]):
183183
super().__init__(sim_state)
184184

185185
@property
186-
def state(self):
186+
def state(self) -> MPSState:
187187
return self._merged_sim_state
188188

189189
def __str__(self) -> str:
@@ -646,10 +646,10 @@ def _act_on_fallback_(
646646
"""Delegates the action to self.apply_op"""
647647
return self._state.apply_op(action, self.get_axes(qubits), self.prng)
648648

649-
def estimation_stats(self):
649+
def estimation_stats(self) -> dict[str, float]:
650650
"""Returns some statistics about the memory usage and quality of the approximation."""
651651
return self._state.estimation_stats()
652652

653653
@property
654-
def M(self):
654+
def M(self) -> list[qtn.Tensor]:
655655
return self._state._M

0 commit comments

Comments
 (0)