cirq-core 1.7.0.dev20250915211227__py3-none-any.whl → 1.7.0.dev20250917002151__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of cirq-core might be problematic. Click here for more details.
- cirq/_version.py +1 -1
- cirq/_version_test.py +1 -1
- cirq/circuits/circuit_operation_test.py +5 -0
- cirq/ops/classically_controlled_operation.py +3 -7
- cirq/ops/measure_util.py +7 -6
- cirq/ops/pauli_string_test.py +2 -2
- cirq/ops/raw_types.py +19 -2
- cirq/protocols/has_stabilizer_effect_protocol_test.py +11 -9
- cirq/protocols/has_unitary_protocol_test.py +3 -3
- cirq/protocols/json_serialization.py +3 -3
- cirq/protocols/json_serialization_test.py +31 -31
- cirq/protocols/kraus_protocol_test.py +5 -5
- cirq/protocols/measurement_key_protocol.py +31 -8
- cirq/protocols/mixture_protocol.py +1 -1
- cirq/protocols/mixture_protocol_test.py +7 -7
- cirq/protocols/mul_protocol_test.py +4 -4
- cirq/protocols/phase_protocol.py +13 -4
- cirq/protocols/pow_protocol_test.py +5 -5
- cirq/protocols/resolve_parameters.py +1 -1
- cirq/protocols/unitary_protocol_test.py +31 -19
- cirq/qis/clifford_tableau.py +14 -14
- cirq/qis/clifford_tableau_test.py +17 -17
- cirq/qis/entropy.py +1 -1
- cirq/qis/entropy_test.py +1 -1
- cirq/qis/states_test.py +54 -54
- cirq/sim/classical_simulator_test.py +56 -28
- cirq/sim/clifford/clifford_simulator.py +5 -5
- cirq/sim/clifford/clifford_simulator_test.py +50 -49
- cirq/sim/clifford/stabilizer_state_ch_form.py +9 -9
- cirq/sim/density_matrix_simulation_state.py +6 -6
- cirq/sim/density_matrix_simulator.py +1 -1
- cirq/sim/density_matrix_simulator_test.py +94 -84
- cirq/sim/density_matrix_utils_test.py +1 -1
- cirq/sim/mux_test.py +26 -26
- cirq/sim/simulation_product_state_test.py +7 -7
- cirq/sim/simulation_state.py +4 -4
- cirq/sim/simulation_state_base.py +1 -1
- cirq/sim/simulation_state_test.py +5 -5
- cirq/sim/simulator.py +2 -2
- cirq/sim/simulator_base_test.py +49 -35
- cirq/sim/simulator_test.py +39 -35
- cirq/sim/sparse_simulator.py +1 -1
- cirq/sim/sparse_simulator_test.py +92 -82
- cirq/sim/state_vector.py +1 -1
- cirq/sim/state_vector_simulation_state.py +7 -7
- cirq/sim/state_vector_simulator_test.py +9 -9
- cirq/sim/state_vector_test.py +37 -37
- cirq/study/result_test.py +20 -20
- cirq/study/sweepable_test.py +20 -20
- cirq/study/sweeps_test.py +43 -43
- cirq/testing/circuit_compare_test.py +16 -14
- cirq/testing/consistent_channels.py +2 -2
- cirq/testing/consistent_controlled_gate_op.py +1 -1
- cirq/testing/consistent_decomposition.py +4 -2
- cirq/testing/consistent_phase_by.py +1 -1
- cirq/testing/consistent_qasm.py +2 -2
- cirq/testing/consistent_qasm_test.py +3 -3
- cirq/transformers/eject_z.py +1 -0
- {cirq_core-1.7.0.dev20250915211227.dist-info → cirq_core-1.7.0.dev20250917002151.dist-info}/METADATA +1 -1
- {cirq_core-1.7.0.dev20250915211227.dist-info → cirq_core-1.7.0.dev20250917002151.dist-info}/RECORD +63 -63
- {cirq_core-1.7.0.dev20250915211227.dist-info → cirq_core-1.7.0.dev20250917002151.dist-info}/WHEEL +0 -0
- {cirq_core-1.7.0.dev20250915211227.dist-info → cirq_core-1.7.0.dev20250917002151.dist-info}/licenses/LICENSE +0 -0
- {cirq_core-1.7.0.dev20250915211227.dist-info → cirq_core-1.7.0.dev20250917002151.dist-info}/top_level.txt +0 -0
|
@@ -216,7 +216,7 @@ def test_measure_density_matrix_partial_indices_all_orders() -> None:
|
|
|
216
216
|
assert bits == [bool(1 & (x >> (2 - p))) for p in perm]
|
|
217
217
|
|
|
218
218
|
|
|
219
|
-
def matrix_000_plus_010():
|
|
219
|
+
def matrix_000_plus_010() -> np.ndarray:
|
|
220
220
|
state = np.zeros(8, dtype=np.complex64)
|
|
221
221
|
state[0] = 1 / np.sqrt(2)
|
|
222
222
|
state[2] = 1j / np.sqrt(2)
|
cirq/sim/mux_test.py
CHANGED
|
@@ -26,7 +26,7 @@ import cirq
|
|
|
26
26
|
import cirq.testing
|
|
27
27
|
|
|
28
28
|
|
|
29
|
-
def test_sample():
|
|
29
|
+
def test_sample() -> None:
|
|
30
30
|
q = cirq.NamedQubit('q')
|
|
31
31
|
|
|
32
32
|
with pytest.raises(ValueError, match="no measurements"):
|
|
@@ -49,7 +49,7 @@ def test_sample():
|
|
|
49
49
|
assert results.histogram(key=q) == collections.Counter({0: 1})
|
|
50
50
|
|
|
51
51
|
|
|
52
|
-
def test_sample_seed_unitary():
|
|
52
|
+
def test_sample_seed_unitary() -> None:
|
|
53
53
|
q = cirq.NamedQubit('q')
|
|
54
54
|
circuit = cirq.Circuit(cirq.X(q) ** 0.2, cirq.measure(q))
|
|
55
55
|
result = cirq.sample(circuit, repetitions=10, seed=1234)
|
|
@@ -60,7 +60,7 @@ def test_sample_seed_unitary():
|
|
|
60
60
|
)
|
|
61
61
|
|
|
62
62
|
|
|
63
|
-
def test_sample_seed_non_unitary():
|
|
63
|
+
def test_sample_seed_non_unitary() -> None:
|
|
64
64
|
q = cirq.NamedQubit('q')
|
|
65
65
|
circuit = cirq.Circuit(cirq.depolarize(0.5).on(q), cirq.measure(q))
|
|
66
66
|
result = cirq.sample(circuit, repetitions=10, seed=1234)
|
|
@@ -70,7 +70,7 @@ def test_sample_seed_non_unitary():
|
|
|
70
70
|
)
|
|
71
71
|
|
|
72
72
|
|
|
73
|
-
def test_sample_sweep():
|
|
73
|
+
def test_sample_sweep() -> None:
|
|
74
74
|
q = cirq.NamedQubit('q')
|
|
75
75
|
c = cirq.Circuit(cirq.X(q), cirq.Y(q) ** sympy.Symbol('t'), cirq.measure(q))
|
|
76
76
|
|
|
@@ -102,7 +102,7 @@ def test_sample_sweep():
|
|
|
102
102
|
assert results[1].histogram(key=q) == collections.Counter({0: 3})
|
|
103
103
|
|
|
104
104
|
|
|
105
|
-
def test_sample_sweep_seed():
|
|
105
|
+
def test_sample_sweep_seed() -> None:
|
|
106
106
|
q = cirq.NamedQubit('q')
|
|
107
107
|
circuit = cirq.Circuit(cirq.X(q) ** sympy.Symbol('t'), cirq.measure(q))
|
|
108
108
|
|
|
@@ -124,7 +124,7 @@ def test_sample_sweep_seed():
|
|
|
124
124
|
assert np.all(results[2].measurements['q'] == [[True], [False]])
|
|
125
125
|
|
|
126
126
|
|
|
127
|
-
def test_final_state_vector_different_program_types():
|
|
127
|
+
def test_final_state_vector_different_program_types() -> None:
|
|
128
128
|
a, b = cirq.LineQubit.range(2)
|
|
129
129
|
|
|
130
130
|
np.testing.assert_allclose(cirq.final_state_vector(cirq.X), [0, 1], atol=1e-8)
|
|
@@ -140,7 +140,7 @@ def test_final_state_vector_different_program_types():
|
|
|
140
140
|
)
|
|
141
141
|
|
|
142
142
|
|
|
143
|
-
def test_final_state_vector_initial_state():
|
|
143
|
+
def test_final_state_vector_initial_state() -> None:
|
|
144
144
|
np.testing.assert_allclose(cirq.final_state_vector(cirq.X, initial_state=0), [0, 1], atol=1e-8)
|
|
145
145
|
|
|
146
146
|
np.testing.assert_allclose(cirq.final_state_vector(cirq.X, initial_state=1), [1, 0], atol=1e-8)
|
|
@@ -152,7 +152,7 @@ def test_final_state_vector_initial_state():
|
|
|
152
152
|
)
|
|
153
153
|
|
|
154
154
|
|
|
155
|
-
def test_final_state_vector_dtype_insensitive_to_initial_state():
|
|
155
|
+
def test_final_state_vector_dtype_insensitive_to_initial_state() -> None:
|
|
156
156
|
assert cirq.final_state_vector(cirq.X).dtype == np.complex64
|
|
157
157
|
|
|
158
158
|
assert cirq.final_state_vector(cirq.X, initial_state=0).dtype == np.complex64
|
|
@@ -181,7 +181,7 @@ def test_final_state_vector_dtype_insensitive_to_initial_state():
|
|
|
181
181
|
)
|
|
182
182
|
|
|
183
183
|
|
|
184
|
-
def test_final_state_vector_param_resolver():
|
|
184
|
+
def test_final_state_vector_param_resolver() -> None:
|
|
185
185
|
s = sympy.Symbol('s')
|
|
186
186
|
|
|
187
187
|
with pytest.raises(ValueError, match='not unitary'):
|
|
@@ -192,7 +192,7 @@ def test_final_state_vector_param_resolver():
|
|
|
192
192
|
)
|
|
193
193
|
|
|
194
194
|
|
|
195
|
-
def test_final_state_vector_qubit_order():
|
|
195
|
+
def test_final_state_vector_qubit_order() -> None:
|
|
196
196
|
a, b = cirq.LineQubit.range(2)
|
|
197
197
|
|
|
198
198
|
np.testing.assert_allclose(
|
|
@@ -206,7 +206,7 @@ def test_final_state_vector_qubit_order():
|
|
|
206
206
|
)
|
|
207
207
|
|
|
208
208
|
|
|
209
|
-
def test_final_state_vector_ignore_terminal_measurement():
|
|
209
|
+
def test_final_state_vector_ignore_terminal_measurement() -> None:
|
|
210
210
|
a, b = cirq.LineQubit.range(2)
|
|
211
211
|
|
|
212
212
|
np.testing.assert_allclose(
|
|
@@ -226,7 +226,7 @@ def test_final_state_vector_ignore_terminal_measurement():
|
|
|
226
226
|
|
|
227
227
|
|
|
228
228
|
@pytest.mark.parametrize('repetitions', (0, 1, 100))
|
|
229
|
-
def test_repetitions(repetitions):
|
|
229
|
+
def test_repetitions(repetitions) -> None:
|
|
230
230
|
a = cirq.LineQubit(0)
|
|
231
231
|
c = cirq.Circuit(cirq.H(a), cirq.measure(a, key='m'))
|
|
232
232
|
r = cirq.sample(c, repetitions=repetitions)
|
|
@@ -235,7 +235,7 @@ def test_repetitions(repetitions):
|
|
|
235
235
|
assert np.issubdtype(samples.dtype, np.integer)
|
|
236
236
|
|
|
237
237
|
|
|
238
|
-
def test_final_density_matrix_different_program_types():
|
|
238
|
+
def test_final_density_matrix_different_program_types() -> None:
|
|
239
239
|
a, b = cirq.LineQubit.range(2)
|
|
240
240
|
|
|
241
241
|
np.testing.assert_allclose(cirq.final_density_matrix(cirq.X), [[0, 0], [0, 1]], atol=1e-8)
|
|
@@ -244,12 +244,12 @@ def test_final_density_matrix_different_program_types():
|
|
|
244
244
|
|
|
245
245
|
np.testing.assert_allclose(
|
|
246
246
|
cirq.final_density_matrix(cirq.Circuit(ops)),
|
|
247
|
-
[[0.5, 0, 0, 0.5], [0, 0, 0, 0], [0, 0, 0, 0], [0.5, 0, 0, 0.5]],
|
|
247
|
+
np.asarray([[0.5, 0, 0, 0.5], [0, 0, 0, 0], [0, 0, 0, 0], [0.5, 0, 0, 0.5]]),
|
|
248
248
|
atol=1e-8,
|
|
249
249
|
)
|
|
250
250
|
|
|
251
251
|
|
|
252
|
-
def test_final_density_matrix_initial_state():
|
|
252
|
+
def test_final_density_matrix_initial_state() -> None:
|
|
253
253
|
np.testing.assert_allclose(
|
|
254
254
|
cirq.final_density_matrix(cirq.X, initial_state=0), [[0, 0], [0, 1]], atol=1e-8
|
|
255
255
|
)
|
|
@@ -265,7 +265,7 @@ def test_final_density_matrix_initial_state():
|
|
|
265
265
|
)
|
|
266
266
|
|
|
267
267
|
|
|
268
|
-
def test_final_density_matrix_dtype_insensitive_to_initial_state():
|
|
268
|
+
def test_final_density_matrix_dtype_insensitive_to_initial_state() -> None:
|
|
269
269
|
assert cirq.final_density_matrix(cirq.X).dtype == np.complex64
|
|
270
270
|
|
|
271
271
|
assert cirq.final_density_matrix(cirq.X, initial_state=0).dtype == np.complex64
|
|
@@ -296,7 +296,7 @@ def test_final_density_matrix_dtype_insensitive_to_initial_state():
|
|
|
296
296
|
)
|
|
297
297
|
|
|
298
298
|
|
|
299
|
-
def test_final_density_matrix_param_resolver():
|
|
299
|
+
def test_final_density_matrix_param_resolver() -> None:
|
|
300
300
|
s = sympy.Symbol('s')
|
|
301
301
|
|
|
302
302
|
with pytest.raises(ValueError, match='not specified in parameter sweep'):
|
|
@@ -308,17 +308,17 @@ def test_final_density_matrix_param_resolver():
|
|
|
308
308
|
)
|
|
309
309
|
|
|
310
310
|
|
|
311
|
-
def test_final_density_matrix_qubit_order():
|
|
311
|
+
def test_final_density_matrix_qubit_order() -> None:
|
|
312
312
|
a, b = cirq.LineQubit.range(2)
|
|
313
313
|
|
|
314
314
|
np.testing.assert_allclose(
|
|
315
315
|
cirq.final_density_matrix([cirq.X(a), cirq.X(b) ** 0.5], qubit_order=[a, b]),
|
|
316
|
-
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0.5, 0.5j], [0, 0, -0.5j, 0.5]],
|
|
316
|
+
np.asarray([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0.5, 0.5j], [0, 0, -0.5j, 0.5]]),
|
|
317
317
|
)
|
|
318
318
|
|
|
319
319
|
np.testing.assert_allclose(
|
|
320
320
|
cirq.final_density_matrix([cirq.X(a), cirq.X(b) ** 0.5], qubit_order=[b, a]),
|
|
321
|
-
[[0, 0, 0, 0], [0, 0.5, 0, 0.5j], [0, 0, 0, 0], [0, -0.5j, 0, 0.5]],
|
|
321
|
+
np.asarray([[0, 0, 0, 0], [0, 0.5, 0, 0.5j], [0, 0, 0, 0], [0, -0.5j, 0, 0.5]]),
|
|
322
322
|
)
|
|
323
323
|
|
|
324
324
|
np.testing.assert_allclose(
|
|
@@ -331,7 +331,7 @@ def test_final_density_matrix_qubit_order():
|
|
|
331
331
|
)
|
|
332
332
|
|
|
333
333
|
|
|
334
|
-
def test_final_density_matrix_seed_with_dephasing():
|
|
334
|
+
def test_final_density_matrix_seed_with_dephasing() -> None:
|
|
335
335
|
a = cirq.LineQubit(0)
|
|
336
336
|
np.testing.assert_allclose(
|
|
337
337
|
cirq.final_density_matrix([cirq.X(a) ** 0.5, cirq.measure(a)], seed=123),
|
|
@@ -345,7 +345,7 @@ def test_final_density_matrix_seed_with_dephasing():
|
|
|
345
345
|
)
|
|
346
346
|
|
|
347
347
|
|
|
348
|
-
def test_final_density_matrix_seed_with_collapsing():
|
|
348
|
+
def test_final_density_matrix_seed_with_collapsing() -> None:
|
|
349
349
|
a = cirq.LineQubit(0)
|
|
350
350
|
np.testing.assert_allclose(
|
|
351
351
|
cirq.final_density_matrix(
|
|
@@ -363,7 +363,7 @@ def test_final_density_matrix_seed_with_collapsing():
|
|
|
363
363
|
)
|
|
364
364
|
|
|
365
365
|
|
|
366
|
-
def test_final_density_matrix_noise():
|
|
366
|
+
def test_final_density_matrix_noise() -> None:
|
|
367
367
|
a = cirq.LineQubit(0)
|
|
368
368
|
np.testing.assert_allclose(
|
|
369
369
|
cirq.final_density_matrix([cirq.H(a), cirq.Z(a), cirq.H(a), cirq.measure(a)]),
|
|
@@ -380,7 +380,7 @@ def test_final_density_matrix_noise():
|
|
|
380
380
|
)
|
|
381
381
|
|
|
382
382
|
|
|
383
|
-
def test_final_density_matrix_classical_control():
|
|
383
|
+
def test_final_density_matrix_classical_control() -> None:
|
|
384
384
|
q0, q1 = cirq.LineQubit.range(2)
|
|
385
385
|
circuit = cirq.Circuit(
|
|
386
386
|
cirq.H(q0),
|
|
@@ -394,7 +394,7 @@ def test_final_density_matrix_classical_control():
|
|
|
394
394
|
)
|
|
395
395
|
|
|
396
396
|
|
|
397
|
-
def test_ps_initial_state_wfn():
|
|
397
|
+
def test_ps_initial_state_wfn() -> None:
|
|
398
398
|
q0, q1 = cirq.LineQubit.range(2)
|
|
399
399
|
s00 = cirq.KET_ZERO(q0) * cirq.KET_ZERO(q1)
|
|
400
400
|
sp0 = cirq.KET_PLUS(q0) * cirq.KET_ZERO(q1)
|
|
@@ -410,7 +410,7 @@ def test_ps_initial_state_wfn():
|
|
|
410
410
|
)
|
|
411
411
|
|
|
412
412
|
|
|
413
|
-
def test_ps_initial_state_dmat():
|
|
413
|
+
def test_ps_initial_state_dmat() -> None:
|
|
414
414
|
q0, q1 = cirq.LineQubit.range(2)
|
|
415
415
|
s00 = cirq.KET_ZERO(q0) * cirq.KET_ZERO(q1)
|
|
416
416
|
sp0 = cirq.KET_PLUS(q0) * cirq.KET_ZERO(q1)
|
|
@@ -14,29 +14,29 @@
|
|
|
14
14
|
|
|
15
15
|
from __future__ import annotations
|
|
16
16
|
|
|
17
|
-
from typing import Any, Sequence
|
|
17
|
+
from typing import Any, Self, Sequence
|
|
18
18
|
|
|
19
19
|
import cirq
|
|
20
20
|
|
|
21
21
|
|
|
22
22
|
class EmptyQuantumState(cirq.QuantumStateRepresentation):
|
|
23
|
-
def copy(self, deep_copy_buffers=True):
|
|
23
|
+
def copy(self, deep_copy_buffers=True) -> Self:
|
|
24
24
|
return self
|
|
25
25
|
|
|
26
|
-
def measure(self, axes, seed=None):
|
|
26
|
+
def measure(self, axes, seed=None) -> list[int]:
|
|
27
27
|
return [0] * len(axes)
|
|
28
28
|
|
|
29
29
|
@property
|
|
30
|
-
def supports_factor(self):
|
|
30
|
+
def supports_factor(self) -> bool:
|
|
31
31
|
return True
|
|
32
32
|
|
|
33
|
-
def kron(self, other):
|
|
33
|
+
def kron(self, other) -> Self:
|
|
34
34
|
return self
|
|
35
35
|
|
|
36
|
-
def factor(self, axes, *, validate=True, atol=1e-07):
|
|
36
|
+
def factor(self, axes, *, validate=True, atol=1e-07) -> tuple[Self, Self]:
|
|
37
37
|
return self, self
|
|
38
38
|
|
|
39
|
-
def reindex(self, axes):
|
|
39
|
+
def reindex(self, axes) -> Self:
|
|
40
40
|
return self
|
|
41
41
|
|
|
42
42
|
|
cirq/sim/simulation_state.py
CHANGED
|
@@ -73,7 +73,7 @@ class SimulationState(SimulationStateBase, Generic[TState], metaclass=abc.ABCMet
|
|
|
73
73
|
key: str,
|
|
74
74
|
invert_mask: Sequence[bool],
|
|
75
75
|
confusion_map: dict[tuple[int, ...], np.ndarray],
|
|
76
|
-
):
|
|
76
|
+
) -> None:
|
|
77
77
|
"""Measures the qubits and records to `log_of_measurement_results`.
|
|
78
78
|
|
|
79
79
|
Any bitmasks will be applied to the measurement record.
|
|
@@ -209,7 +209,7 @@ class SimulationState(SimulationStateBase, Generic[TState], metaclass=abc.ABCMet
|
|
|
209
209
|
return extracted, remainder
|
|
210
210
|
|
|
211
211
|
@property
|
|
212
|
-
def allows_factoring(self):
|
|
212
|
+
def allows_factoring(self) -> bool:
|
|
213
213
|
"""Subclasses that allow factorization should override this."""
|
|
214
214
|
return self._state.supports_factor if self._state is not None else False
|
|
215
215
|
|
|
@@ -238,7 +238,7 @@ class SimulationState(SimulationStateBase, Generic[TState], metaclass=abc.ABCMet
|
|
|
238
238
|
def qubits(self) -> tuple[cirq.Qid, ...]:
|
|
239
239
|
return self._qubits
|
|
240
240
|
|
|
241
|
-
def swap(self, q1: cirq.Qid, q2: cirq.Qid, *, inplace=False):
|
|
241
|
+
def swap(self, q1: cirq.Qid, q2: cirq.Qid, *, inplace=False) -> Self:
|
|
242
242
|
"""Swaps two qubits.
|
|
243
243
|
|
|
244
244
|
This only affects the index, and does not modify the underlying
|
|
@@ -269,7 +269,7 @@ class SimulationState(SimulationStateBase, Generic[TState], metaclass=abc.ABCMet
|
|
|
269
269
|
args._set_qubits(qubits)
|
|
270
270
|
return args
|
|
271
271
|
|
|
272
|
-
def rename(self, q1: cirq.Qid, q2: cirq.Qid, *, inplace=False):
|
|
272
|
+
def rename(self, q1: cirq.Qid, q2: cirq.Qid, *, inplace=False) -> Self:
|
|
273
273
|
"""Renames `q1` to `q2`.
|
|
274
274
|
|
|
275
275
|
Args:
|
|
@@ -81,7 +81,7 @@ class SimulationStateBase(Generic[TSimulationState], metaclass=abc.ABCMeta):
|
|
|
81
81
|
Returns:
|
|
82
82
|
True if the fallback applies, else NotImplemented."""
|
|
83
83
|
|
|
84
|
-
def apply_operation(self, op: cirq.Operation):
|
|
84
|
+
def apply_operation(self, op: cirq.Operation) -> None:
|
|
85
85
|
protocols.act_on(op, self)
|
|
86
86
|
|
|
87
87
|
@abc.abstractmethod
|
|
@@ -25,13 +25,13 @@ from cirq.testing import PhaseUsingCleanAncilla, PhaseUsingDirtyAncilla
|
|
|
25
25
|
|
|
26
26
|
|
|
27
27
|
class ExampleQuantumState(cirq.QuantumStateRepresentation):
|
|
28
|
-
def copy(self, deep_copy_buffers=True):
|
|
29
|
-
|
|
28
|
+
def copy(self, deep_copy_buffers=True) -> ExampleQuantumState:
|
|
29
|
+
raise NotImplementedError()
|
|
30
30
|
|
|
31
|
-
def measure(self, axes, seed=None):
|
|
31
|
+
def measure(self, axes, seed=None) -> list[int]:
|
|
32
32
|
return [5, 3]
|
|
33
33
|
|
|
34
|
-
def reindex(self, axes):
|
|
34
|
+
def reindex(self, axes) -> ExampleQuantumState:
|
|
35
35
|
return self
|
|
36
36
|
|
|
37
37
|
|
|
@@ -44,7 +44,7 @@ class ExampleSimulationState(cirq.SimulationState):
|
|
|
44
44
|
) -> bool:
|
|
45
45
|
return True
|
|
46
46
|
|
|
47
|
-
def add_qubits(self, qubits):
|
|
47
|
+
def add_qubits(self, qubits) -> ExampleSimulationState:
|
|
48
48
|
super().add_qubits(qubits)
|
|
49
49
|
return self
|
|
50
50
|
|
cirq/sim/simulator.py
CHANGED
|
@@ -217,7 +217,7 @@ class SimulatesAmplitudes(metaclass=value.ABCMetaImplementAnyOneOf):
|
|
|
217
217
|
def sample_from_amplitudes(
|
|
218
218
|
self,
|
|
219
219
|
circuit: cirq.AbstractCircuit,
|
|
220
|
-
param_resolver: cirq.
|
|
220
|
+
param_resolver: cirq.ParamResolverOrSimilarType,
|
|
221
221
|
seed: cirq.RANDOM_STATE_OR_SEED_LIKE,
|
|
222
222
|
repetitions: int = 1,
|
|
223
223
|
qubit_order: cirq.QubitOrderOrList = ops.QubitOrder.DEFAULT,
|
|
@@ -938,7 +938,7 @@ def _qubit_map_to_shape(qubit_map: Mapping[cirq.Qid, int]) -> tuple[int, ...]:
|
|
|
938
938
|
return tuple(qid_shape)
|
|
939
939
|
|
|
940
940
|
|
|
941
|
-
def check_all_resolved(circuit):
|
|
941
|
+
def check_all_resolved(circuit) -> None:
|
|
942
942
|
"""Raises if the circuit contains unresolved symbols."""
|
|
943
943
|
if protocols.is_parameterized(circuit):
|
|
944
944
|
unresolved = [op for moment in circuit for op in moment if protocols.is_parameterized(op)]
|
cirq/sim/simulator_base_test.py
CHANGED
|
@@ -73,25 +73,25 @@ class CountingSimulationState(cirq.SimulationState[CountingState]):
|
|
|
73
73
|
return True
|
|
74
74
|
|
|
75
75
|
@property
|
|
76
|
-
def data(self):
|
|
76
|
+
def data(self) -> Any:
|
|
77
77
|
return self._state.data
|
|
78
78
|
|
|
79
79
|
@property
|
|
80
|
-
def gate_count(self):
|
|
80
|
+
def gate_count(self) -> int:
|
|
81
81
|
return self._state.gate_count
|
|
82
82
|
|
|
83
83
|
@property
|
|
84
|
-
def measurement_count(self):
|
|
84
|
+
def measurement_count(self) -> int:
|
|
85
85
|
return self._state.measurement_count
|
|
86
86
|
|
|
87
87
|
@property
|
|
88
|
-
def copy_count(self):
|
|
88
|
+
def copy_count(self) -> int:
|
|
89
89
|
return self._state.copy_count
|
|
90
90
|
|
|
91
91
|
|
|
92
92
|
class SplittableCountingSimulationState(CountingSimulationState):
|
|
93
93
|
@property
|
|
94
|
-
def allows_factoring(self):
|
|
94
|
+
def allows_factoring(self) -> bool:
|
|
95
95
|
return True
|
|
96
96
|
|
|
97
97
|
|
|
@@ -167,59 +167,64 @@ entangled_state_repr = np.array([[math.sqrt(0.5), 0], [0, math.sqrt(0.5)]])
|
|
|
167
167
|
|
|
168
168
|
|
|
169
169
|
class TestOp(cirq.Operation):
|
|
170
|
-
def with_qubits(self, *new_qubits):
|
|
171
|
-
|
|
170
|
+
def with_qubits(self, *new_qubits) -> cirq.Operation:
|
|
171
|
+
raise NotImplementedError()
|
|
172
172
|
|
|
173
173
|
@property
|
|
174
|
-
def qubits(self):
|
|
175
|
-
return
|
|
174
|
+
def qubits(self) -> tuple[cirq.Qid, ...]:
|
|
175
|
+
return (q0,)
|
|
176
176
|
|
|
177
177
|
|
|
178
|
-
def test_simulate_empty_circuit():
|
|
178
|
+
def test_simulate_empty_circuit() -> None:
|
|
179
179
|
sim = CountingSimulator()
|
|
180
180
|
r = sim.simulate(cirq.Circuit())
|
|
181
|
+
assert isinstance(r._final_simulator_state, CountingSimulationState)
|
|
181
182
|
assert r._final_simulator_state.gate_count == 0
|
|
182
183
|
assert r._final_simulator_state.measurement_count == 0
|
|
183
184
|
assert r._final_simulator_state.copy_count == 0
|
|
184
185
|
|
|
185
186
|
|
|
186
|
-
def test_simulate_one_gate_circuit():
|
|
187
|
+
def test_simulate_one_gate_circuit() -> None:
|
|
187
188
|
sim = CountingSimulator()
|
|
188
189
|
r = sim.simulate(cirq.Circuit(cirq.X(q0)))
|
|
190
|
+
assert isinstance(r._final_simulator_state, CountingSimulationState)
|
|
189
191
|
assert r._final_simulator_state.gate_count == 1
|
|
190
192
|
assert r._final_simulator_state.copy_count == 0
|
|
191
193
|
|
|
192
194
|
|
|
193
|
-
def test_simulate_one_measurement_circuit():
|
|
195
|
+
def test_simulate_one_measurement_circuit() -> None:
|
|
194
196
|
sim = CountingSimulator()
|
|
195
197
|
r = sim.simulate(cirq.Circuit(cirq.measure(q0)))
|
|
198
|
+
assert isinstance(r._final_simulator_state, CountingSimulationState)
|
|
196
199
|
assert r._final_simulator_state.gate_count == 0
|
|
197
200
|
assert r._final_simulator_state.measurement_count == 1
|
|
198
201
|
assert r._final_simulator_state.copy_count == 0
|
|
199
202
|
|
|
200
203
|
|
|
201
|
-
def test_empty_circuit_simulation_has_moment():
|
|
204
|
+
def test_empty_circuit_simulation_has_moment() -> None:
|
|
202
205
|
sim = CountingSimulator()
|
|
203
206
|
steps = list(sim.simulate_moment_steps(cirq.Circuit()))
|
|
204
207
|
assert len(steps) == 1
|
|
205
208
|
|
|
206
209
|
|
|
207
|
-
def test_noise_applied():
|
|
210
|
+
def test_noise_applied() -> None:
|
|
208
211
|
sim = CountingSimulator(noise=cirq.X)
|
|
209
212
|
r = sim.simulate(cirq.Circuit(cirq.X(q0)))
|
|
213
|
+
assert isinstance(r._final_simulator_state, CountingSimulationState)
|
|
210
214
|
assert r._final_simulator_state.gate_count == 2
|
|
211
215
|
assert r._final_simulator_state.copy_count == 0
|
|
212
216
|
|
|
213
217
|
|
|
214
|
-
def test_noise_applied_measurement_gate():
|
|
218
|
+
def test_noise_applied_measurement_gate() -> None:
|
|
215
219
|
sim = CountingSimulator(noise=cirq.X)
|
|
216
220
|
r = sim.simulate(cirq.Circuit(cirq.measure(q0)))
|
|
221
|
+
assert isinstance(r._final_simulator_state, CountingSimulationState)
|
|
217
222
|
assert r._final_simulator_state.gate_count == 1
|
|
218
223
|
assert r._final_simulator_state.measurement_count == 1
|
|
219
224
|
assert r._final_simulator_state.copy_count == 0
|
|
220
225
|
|
|
221
226
|
|
|
222
|
-
def test_parameterized_copies_all_but_last():
|
|
227
|
+
def test_parameterized_copies_all_but_last() -> None:
|
|
223
228
|
sim = CountingSimulator()
|
|
224
229
|
n = 4
|
|
225
230
|
rs = sim.simulate_sweep(
|
|
@@ -227,12 +232,13 @@ def test_parameterized_copies_all_but_last():
|
|
|
227
232
|
)
|
|
228
233
|
for i in range(n):
|
|
229
234
|
r = rs[i]
|
|
235
|
+
assert isinstance(r._final_simulator_state, CountingSimulationState)
|
|
230
236
|
assert r._final_simulator_state.gate_count == 1
|
|
231
237
|
assert r._final_simulator_state.measurement_count == 0
|
|
232
238
|
assert r._final_simulator_state.copy_count == 0 if i == n - 1 else 1
|
|
233
239
|
|
|
234
240
|
|
|
235
|
-
def test_cannot_act():
|
|
241
|
+
def test_cannot_act() -> None:
|
|
236
242
|
class BadOp(TestOp):
|
|
237
243
|
def _act_on_(self, sim_state):
|
|
238
244
|
raise TypeError()
|
|
@@ -242,25 +248,25 @@ def test_cannot_act():
|
|
|
242
248
|
sim.simulate(cirq.Circuit(BadOp()))
|
|
243
249
|
|
|
244
250
|
|
|
245
|
-
def test_run_one_gate_circuit():
|
|
251
|
+
def test_run_one_gate_circuit() -> None:
|
|
246
252
|
sim = CountingSimulator()
|
|
247
253
|
r = sim.run(cirq.Circuit(cirq.X(q0), cirq.measure(q0)), repetitions=2)
|
|
248
254
|
assert np.allclose(r.measurements['q(0)'], [[1], [1]])
|
|
249
255
|
|
|
250
256
|
|
|
251
|
-
def test_run_one_gate_circuit_noise():
|
|
257
|
+
def test_run_one_gate_circuit_noise() -> None:
|
|
252
258
|
sim = CountingSimulator(noise=cirq.X)
|
|
253
259
|
r = sim.run(cirq.Circuit(cirq.X(q0), cirq.measure(q0)), repetitions=2)
|
|
254
260
|
assert np.allclose(r.measurements['q(0)'], [[2], [2]])
|
|
255
261
|
|
|
256
262
|
|
|
257
|
-
def test_run_non_unitary_circuit():
|
|
263
|
+
def test_run_non_unitary_circuit() -> None:
|
|
258
264
|
sim = CountingSimulator()
|
|
259
265
|
r = sim.run(cirq.Circuit(cirq.phase_damp(1).on(q0), cirq.measure(q0)), repetitions=2)
|
|
260
266
|
assert np.allclose(r.measurements['q(0)'], [[1], [1]])
|
|
261
267
|
|
|
262
268
|
|
|
263
|
-
def test_run_non_unitary_circuit_non_unitary_state():
|
|
269
|
+
def test_run_non_unitary_circuit_non_unitary_state() -> None:
|
|
264
270
|
class DensityCountingSimulator(CountingSimulator):
|
|
265
271
|
def _can_be_in_run_prefix(self, val):
|
|
266
272
|
return not cirq.is_measurement(val)
|
|
@@ -270,15 +276,16 @@ def test_run_non_unitary_circuit_non_unitary_state():
|
|
|
270
276
|
assert np.allclose(r.measurements['q(0)'], [[1], [1]])
|
|
271
277
|
|
|
272
278
|
|
|
273
|
-
def test_run_non_terminal_measurement():
|
|
279
|
+
def test_run_non_terminal_measurement() -> None:
|
|
274
280
|
sim = CountingSimulator()
|
|
275
281
|
r = sim.run(cirq.Circuit(cirq.X(q0), cirq.measure(q0), cirq.X(q0)), repetitions=2)
|
|
276
282
|
assert np.allclose(r.measurements['q(0)'], [[1], [1]])
|
|
277
283
|
|
|
278
284
|
|
|
279
|
-
def test_integer_initial_state_is_split():
|
|
285
|
+
def test_integer_initial_state_is_split() -> None:
|
|
280
286
|
sim = SplittableCountingSimulator()
|
|
281
287
|
state = sim._create_simulation_state(2, (q0, q1))
|
|
288
|
+
assert isinstance(state, cirq.SimulationProductState)
|
|
282
289
|
assert len(set(state.values())) == 3
|
|
283
290
|
assert state[q0] is not state[q1]
|
|
284
291
|
assert state[q0].data == 1
|
|
@@ -286,7 +293,7 @@ def test_integer_initial_state_is_split():
|
|
|
286
293
|
assert state[None].data == 0
|
|
287
294
|
|
|
288
295
|
|
|
289
|
-
def test_integer_initial_state_is_not_split_if_disabled():
|
|
296
|
+
def test_integer_initial_state_is_not_split_if_disabled() -> None:
|
|
290
297
|
sim = SplittableCountingSimulator(split_untangled_states=False)
|
|
291
298
|
state = sim._create_simulation_state(2, (q0, q1))
|
|
292
299
|
assert isinstance(state, SplittableCountingSimulationState)
|
|
@@ -294,7 +301,7 @@ def test_integer_initial_state_is_not_split_if_disabled():
|
|
|
294
301
|
assert state.data == 2
|
|
295
302
|
|
|
296
303
|
|
|
297
|
-
def test_integer_initial_state_is_not_split_if_impossible():
|
|
304
|
+
def test_integer_initial_state_is_not_split_if_impossible() -> None:
|
|
298
305
|
sim = CountingSimulator()
|
|
299
306
|
state = sim._create_simulation_state(2, (q0, q1))
|
|
300
307
|
assert isinstance(state, CountingSimulationState)
|
|
@@ -303,18 +310,20 @@ def test_integer_initial_state_is_not_split_if_impossible():
|
|
|
303
310
|
assert state.data == 2
|
|
304
311
|
|
|
305
312
|
|
|
306
|
-
def test_non_integer_initial_state_is_not_split():
|
|
313
|
+
def test_non_integer_initial_state_is_not_split() -> None:
|
|
307
314
|
sim = SplittableCountingSimulator()
|
|
308
315
|
state = sim._create_simulation_state(entangled_state_repr, (q0, q1))
|
|
316
|
+
assert isinstance(state, cirq.SimulationProductState)
|
|
309
317
|
assert len(set(state.values())) == 2
|
|
310
318
|
assert (state[q0].data == entangled_state_repr).all()
|
|
311
319
|
assert state[q1] is state[q0]
|
|
312
320
|
assert state[None].data == 0
|
|
313
321
|
|
|
314
322
|
|
|
315
|
-
def test_entanglement_causes_join():
|
|
323
|
+
def test_entanglement_causes_join() -> None:
|
|
316
324
|
sim = SplittableCountingSimulator()
|
|
317
325
|
state = sim._create_simulation_state(2, (q0, q1))
|
|
326
|
+
assert isinstance(state, cirq.SimulationProductState)
|
|
318
327
|
assert len(set(state.values())) == 3
|
|
319
328
|
state.apply_operation(cirq.CNOT(q0, q1))
|
|
320
329
|
assert len(set(state.values())) == 2
|
|
@@ -322,9 +331,10 @@ def test_entanglement_causes_join():
|
|
|
322
331
|
assert state[None] is not state[q0]
|
|
323
332
|
|
|
324
333
|
|
|
325
|
-
def test_measurement_causes_split():
|
|
334
|
+
def test_measurement_causes_split() -> None:
|
|
326
335
|
sim = SplittableCountingSimulator()
|
|
327
336
|
state = sim._create_simulation_state(entangled_state_repr, (q0, q1))
|
|
337
|
+
assert isinstance(state, cirq.SimulationProductState)
|
|
328
338
|
assert len(set(state.values())) == 2
|
|
329
339
|
state.apply_operation(cirq.measure(q0))
|
|
330
340
|
assert len(set(state.values())) == 3
|
|
@@ -332,7 +342,7 @@ def test_measurement_causes_split():
|
|
|
332
342
|
assert state[q0] is not state[None]
|
|
333
343
|
|
|
334
344
|
|
|
335
|
-
def test_measurement_does_not_split_if_disabled():
|
|
345
|
+
def test_measurement_does_not_split_if_disabled() -> None:
|
|
336
346
|
sim = SplittableCountingSimulator(split_untangled_states=False)
|
|
337
347
|
state = sim._create_simulation_state(2, (q0, q1))
|
|
338
348
|
assert isinstance(state, SplittableCountingSimulationState)
|
|
@@ -341,7 +351,7 @@ def test_measurement_does_not_split_if_disabled():
|
|
|
341
351
|
assert state[q0] is state[q1]
|
|
342
352
|
|
|
343
353
|
|
|
344
|
-
def test_measurement_does_not_split_if_impossible():
|
|
354
|
+
def test_measurement_does_not_split_if_impossible() -> None:
|
|
345
355
|
sim = CountingSimulator()
|
|
346
356
|
state = sim._create_simulation_state(2, (q0, q1))
|
|
347
357
|
assert isinstance(state, CountingSimulationState)
|
|
@@ -352,7 +362,7 @@ def test_measurement_does_not_split_if_impossible():
|
|
|
352
362
|
assert state[q0] is state[q1]
|
|
353
363
|
|
|
354
364
|
|
|
355
|
-
def test_reorder_succeeds():
|
|
365
|
+
def test_reorder_succeeds() -> None:
|
|
356
366
|
sim = SplittableCountingSimulator()
|
|
357
367
|
state = sim._create_simulation_state(entangled_state_repr, (q0, q1))
|
|
358
368
|
reordered = state[q0].transpose_to_qubit_order([q1, q0])
|
|
@@ -360,7 +370,7 @@ def test_reorder_succeeds():
|
|
|
360
370
|
|
|
361
371
|
|
|
362
372
|
@pytest.mark.parametrize('split', [True, False])
|
|
363
|
-
def test_sim_state_instance_unchanged_during_normal_sim(split: bool):
|
|
373
|
+
def test_sim_state_instance_unchanged_during_normal_sim(split: bool) -> None:
|
|
364
374
|
sim = SplittableCountingSimulator(split_untangled_states=split)
|
|
365
375
|
state = sim._create_simulation_state(0, (q0, q1))
|
|
366
376
|
circuit = cirq.Circuit(cirq.H(q0), cirq.CNOT(q0, q1), cirq.reset(q1))
|
|
@@ -369,7 +379,7 @@ def test_sim_state_instance_unchanged_during_normal_sim(split: bool):
|
|
|
369
379
|
assert (step._merged_sim_state is not state) == split
|
|
370
380
|
|
|
371
381
|
|
|
372
|
-
def test_measurements_retained_in_step_results():
|
|
382
|
+
def test_measurements_retained_in_step_results() -> None:
|
|
373
383
|
sim = SplittableCountingSimulator()
|
|
374
384
|
circuit = cirq.Circuit(
|
|
375
385
|
cirq.measure(q0, key='a'), cirq.measure(q0, key='b'), cirq.measure(q0, key='c')
|
|
@@ -381,7 +391,7 @@ def test_measurements_retained_in_step_results():
|
|
|
381
391
|
assert not any(iterator)
|
|
382
392
|
|
|
383
393
|
|
|
384
|
-
def test_sweep_unparameterized_prefix_not_repeated_iff_unitary():
|
|
394
|
+
def test_sweep_unparameterized_prefix_not_repeated_iff_unitary() -> None:
|
|
385
395
|
q = cirq.LineQubit(0)
|
|
386
396
|
|
|
387
397
|
class TestOp(cirq.Operation):
|
|
@@ -410,6 +420,8 @@ def test_sweep_unparameterized_prefix_not_repeated_iff_unitary():
|
|
|
410
420
|
op2 = TestOp(has_unitary=True)
|
|
411
421
|
circuit = cirq.Circuit(op1, cirq.XPowGate(exponent=sympy.Symbol('a'))(q), op2)
|
|
412
422
|
rs = simulator.simulate_sweep(program=circuit, params=params)
|
|
423
|
+
assert isinstance(rs[0]._final_simulator_state, CountingSimulationState)
|
|
424
|
+
assert isinstance(rs[1]._final_simulator_state, CountingSimulationState)
|
|
413
425
|
assert rs[0]._final_simulator_state.copy_count == 1
|
|
414
426
|
assert rs[1]._final_simulator_state.copy_count == 0
|
|
415
427
|
assert op1.count == 1
|
|
@@ -419,13 +431,15 @@ def test_sweep_unparameterized_prefix_not_repeated_iff_unitary():
|
|
|
419
431
|
op2 = TestOp(has_unitary=False)
|
|
420
432
|
circuit = cirq.Circuit(op1, cirq.XPowGate(exponent=sympy.Symbol('a'))(q), op2)
|
|
421
433
|
rs = simulator.simulate_sweep(program=circuit, params=params)
|
|
434
|
+
assert isinstance(rs[0]._final_simulator_state, CountingSimulationState)
|
|
435
|
+
assert isinstance(rs[1]._final_simulator_state, CountingSimulationState)
|
|
422
436
|
assert rs[0]._final_simulator_state.copy_count == 1
|
|
423
437
|
assert rs[1]._final_simulator_state.copy_count == 0
|
|
424
438
|
assert op1.count == 2
|
|
425
439
|
assert op2.count == 2
|
|
426
440
|
|
|
427
441
|
|
|
428
|
-
def test_inhomogeneous_measurement_count_padding():
|
|
442
|
+
def test_inhomogeneous_measurement_count_padding() -> None:
|
|
429
443
|
q = cirq.LineQubit(0)
|
|
430
444
|
key = cirq.MeasurementKey('m')
|
|
431
445
|
sim = cirq.Simulator()
|