cirq-core 1.7.0.dev20250915211227__py3-none-any.whl → 1.7.0.dev20250917023732__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/transformers/gauge_compiling/__init__.py +5 -0
- cirq/transformers/gauge_compiling/idle_moments_gauge.py +222 -0
- cirq/transformers/gauge_compiling/idle_moments_gauge_test.py +193 -0
- {cirq_core-1.7.0.dev20250915211227.dist-info → cirq_core-1.7.0.dev20250917023732.dist-info}/METADATA +1 -1
- {cirq_core-1.7.0.dev20250915211227.dist-info → cirq_core-1.7.0.dev20250917023732.dist-info}/RECORD +66 -64
- {cirq_core-1.7.0.dev20250915211227.dist-info → cirq_core-1.7.0.dev20250917023732.dist-info}/WHEEL +0 -0
- {cirq_core-1.7.0.dev20250915211227.dist-info → cirq_core-1.7.0.dev20250917023732.dist-info}/licenses/LICENSE +0 -0
- {cirq_core-1.7.0.dev20250915211227.dist-info → cirq_core-1.7.0.dev20250917023732.dist-info}/top_level.txt +0 -0
cirq/sim/state_vector.py
CHANGED
|
@@ -105,7 +105,7 @@ class StateVectorMixin:
|
|
|
105
105
|
and non-zero floats of the specified accuracy."""
|
|
106
106
|
return qis.dirac_notation(self.state_vector(), decimals, qid_shape=self._qid_shape)
|
|
107
107
|
|
|
108
|
-
def density_matrix_of(self, qubits:
|
|
108
|
+
def density_matrix_of(self, qubits: Sequence[cirq.Qid] | None = None) -> np.ndarray:
|
|
109
109
|
r"""Returns the density matrix of the state.
|
|
110
110
|
|
|
111
111
|
Calculate the density matrix for the system on the qubits provided.
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
|
|
17
17
|
from __future__ import annotations
|
|
18
18
|
|
|
19
|
-
from typing import Any, Callable, Sequence, TYPE_CHECKING
|
|
19
|
+
from typing import Any, Callable, Self, Sequence, TYPE_CHECKING
|
|
20
20
|
|
|
21
21
|
import numpy as np
|
|
22
22
|
|
|
@@ -356,7 +356,7 @@ class StateVectorSimulationState(SimulationState[_BufferedStateVector]):
|
|
|
356
356
|
)
|
|
357
357
|
super().__init__(state=state, prng=prng, qubits=qubits, classical_data=classical_data)
|
|
358
358
|
|
|
359
|
-
def add_qubits(self, qubits: Sequence[cirq.Qid]):
|
|
359
|
+
def add_qubits(self, qubits: Sequence[cirq.Qid]) -> Self:
|
|
360
360
|
ret = super().add_qubits(qubits)
|
|
361
361
|
return (
|
|
362
362
|
self.kronecker_product(type(self)(qubits=qubits), inplace=True)
|
|
@@ -364,7 +364,7 @@ class StateVectorSimulationState(SimulationState[_BufferedStateVector]):
|
|
|
364
364
|
else ret
|
|
365
365
|
)
|
|
366
366
|
|
|
367
|
-
def remove_qubits(self, qubits: Sequence[cirq.Qid]):
|
|
367
|
+
def remove_qubits(self, qubits: Sequence[cirq.Qid]) -> Self:
|
|
368
368
|
ret = super().remove_qubits(qubits)
|
|
369
369
|
if ret is not NotImplemented:
|
|
370
370
|
return ret
|
|
@@ -406,11 +406,11 @@ class StateVectorSimulationState(SimulationState[_BufferedStateVector]):
|
|
|
406
406
|
)
|
|
407
407
|
|
|
408
408
|
@property
|
|
409
|
-
def target_tensor(self):
|
|
409
|
+
def target_tensor(self) -> np.ndarray:
|
|
410
410
|
return self._state._state_vector
|
|
411
411
|
|
|
412
412
|
@property
|
|
413
|
-
def available_buffer(self):
|
|
413
|
+
def available_buffer(self) -> np.ndarray:
|
|
414
414
|
return self._state._buffer
|
|
415
415
|
|
|
416
416
|
|
|
@@ -429,7 +429,7 @@ def _strat_act_on_state_vector_from_mixture(
|
|
|
429
429
|
if index is None:
|
|
430
430
|
return NotImplemented
|
|
431
431
|
if protocols.is_measurement(action):
|
|
432
|
-
key = protocols.
|
|
432
|
+
key = protocols.measurement_key_obj(action)
|
|
433
433
|
args._classical_data.record_channel_measurement(key, index)
|
|
434
434
|
return True
|
|
435
435
|
|
|
@@ -441,6 +441,6 @@ def _strat_act_on_state_vector_from_channel(
|
|
|
441
441
|
if index is None:
|
|
442
442
|
return NotImplemented
|
|
443
443
|
if protocols.is_measurement(action):
|
|
444
|
-
key = protocols.
|
|
444
|
+
key = protocols.measurement_key_obj(action)
|
|
445
445
|
args._classical_data.record_channel_measurement(key, index)
|
|
446
446
|
return True
|
|
@@ -20,7 +20,7 @@ import cirq
|
|
|
20
20
|
import cirq.testing
|
|
21
21
|
|
|
22
22
|
|
|
23
|
-
def test_state_vector_trial_result_repr():
|
|
23
|
+
def test_state_vector_trial_result_repr() -> None:
|
|
24
24
|
q0 = cirq.NamedQubit('a')
|
|
25
25
|
final_simulator_state = cirq.StateVectorSimulationState(
|
|
26
26
|
available_buffer=np.array([0, 1], dtype=np.complex64),
|
|
@@ -47,7 +47,7 @@ def test_state_vector_trial_result_repr():
|
|
|
47
47
|
assert eval(expected_repr) == trial_result
|
|
48
48
|
|
|
49
49
|
|
|
50
|
-
def test_state_vector_trial_result_equality():
|
|
50
|
+
def test_state_vector_trial_result_equality() -> None:
|
|
51
51
|
eq = cirq.testing.EqualsTester()
|
|
52
52
|
final_simulator_state = cirq.StateVectorSimulationState(initial_state=np.array([]))
|
|
53
53
|
eq.add_equality_group(
|
|
@@ -86,7 +86,7 @@ def test_state_vector_trial_result_equality():
|
|
|
86
86
|
)
|
|
87
87
|
|
|
88
88
|
|
|
89
|
-
def test_state_vector_trial_result_state_mixin():
|
|
89
|
+
def test_state_vector_trial_result_state_mixin() -> None:
|
|
90
90
|
qubits = cirq.LineQubit.range(2)
|
|
91
91
|
final_simulator_state = cirq.StateVectorSimulationState(
|
|
92
92
|
qubits=qubits, initial_state=np.array([0, 1, 0, 0])
|
|
@@ -103,7 +103,7 @@ def test_state_vector_trial_result_state_mixin():
|
|
|
103
103
|
assert result.dirac_notation() == '|01⟩'
|
|
104
104
|
|
|
105
105
|
|
|
106
|
-
def test_state_vector_trial_result_qid_shape():
|
|
106
|
+
def test_state_vector_trial_result_qid_shape() -> None:
|
|
107
107
|
final_simulator_state = cirq.StateVectorSimulationState(
|
|
108
108
|
qubits=[cirq.NamedQubit('a')], initial_state=np.array([0, 1])
|
|
109
109
|
)
|
|
@@ -125,7 +125,7 @@ def test_state_vector_trial_result_qid_shape():
|
|
|
125
125
|
assert cirq.qid_shape(trial_result) == (3, 2)
|
|
126
126
|
|
|
127
127
|
|
|
128
|
-
def test_state_vector_trial_state_vector_is_copy():
|
|
128
|
+
def test_state_vector_trial_state_vector_is_copy() -> None:
|
|
129
129
|
final_state_vector = np.array([0, 1], dtype=np.complex64)
|
|
130
130
|
qubit_map = {cirq.NamedQubit('a'): 0}
|
|
131
131
|
final_simulator_state = cirq.StateVectorSimulationState(
|
|
@@ -137,7 +137,7 @@ def test_state_vector_trial_state_vector_is_copy():
|
|
|
137
137
|
assert trial_result.state_vector(copy=True) is not final_simulator_state.target_tensor
|
|
138
138
|
|
|
139
139
|
|
|
140
|
-
def test_state_vector_trial_result_no_qubits():
|
|
140
|
+
def test_state_vector_trial_result_no_qubits() -> None:
|
|
141
141
|
initial_state_vector = np.array([1], dtype=np.complex64)
|
|
142
142
|
initial_state = initial_state_vector.reshape((2,) * 0) # reshape as tensor for 0 qubits
|
|
143
143
|
final_simulator_state = cirq.StateVectorSimulationState(qubits=[], initial_state=initial_state)
|
|
@@ -149,7 +149,7 @@ def test_state_vector_trial_result_no_qubits():
|
|
|
149
149
|
assert np.array_equal(state_vector, initial_state_vector)
|
|
150
150
|
|
|
151
151
|
|
|
152
|
-
def test_str_big():
|
|
152
|
+
def test_str_big() -> None:
|
|
153
153
|
qs = cirq.LineQubit.range(10)
|
|
154
154
|
final_simulator_state = cirq.StateVectorSimulationState(
|
|
155
155
|
prng=np.random.RandomState(0),
|
|
@@ -161,7 +161,7 @@ def test_str_big():
|
|
|
161
161
|
assert 'output vector: [0.03125+0.j 0.03125+0.j 0.03125+0.j ..' in str(result)
|
|
162
162
|
|
|
163
163
|
|
|
164
|
-
def test_str_qudit():
|
|
164
|
+
def test_str_qudit() -> None:
|
|
165
165
|
qutrit = cirq.LineQid(0, dimension=3)
|
|
166
166
|
final_simulator_state = cirq.StateVectorSimulationState(
|
|
167
167
|
prng=np.random.RandomState(0),
|
|
@@ -183,7 +183,7 @@ def test_str_qudit():
|
|
|
183
183
|
assert "|1⟩" in str(result)
|
|
184
184
|
|
|
185
185
|
|
|
186
|
-
def test_pretty_print():
|
|
186
|
+
def test_pretty_print() -> None:
|
|
187
187
|
final_simulator_state = cirq.StateVectorSimulationState(
|
|
188
188
|
available_buffer=np.array([1]),
|
|
189
189
|
prng=np.random.RandomState(0),
|
cirq/sim/state_vector_test.py
CHANGED
|
@@ -35,7 +35,7 @@ def use_np_transpose(request) -> Iterator[bool]:
|
|
|
35
35
|
yield value
|
|
36
36
|
|
|
37
37
|
|
|
38
|
-
def test_state_mixin():
|
|
38
|
+
def test_state_mixin() -> None:
|
|
39
39
|
class TestClass(cirq.StateVectorMixin):
|
|
40
40
|
def state_vector(self, copy: bool | None = None) -> np.ndarray:
|
|
41
41
|
return np.array([0, 0, 1, 0])
|
|
@@ -60,7 +60,7 @@ def test_state_mixin():
|
|
|
60
60
|
_ = TestClass({qubits[0]: -1, qubits[1]: 1})
|
|
61
61
|
|
|
62
62
|
|
|
63
|
-
def test_sample_state_big_endian():
|
|
63
|
+
def test_sample_state_big_endian() -> None:
|
|
64
64
|
results = []
|
|
65
65
|
for x in range(8):
|
|
66
66
|
state = cirq.to_valid_state_vector(x, 3)
|
|
@@ -71,7 +71,7 @@ def test_sample_state_big_endian():
|
|
|
71
71
|
np.testing.assert_equal(result, expected)
|
|
72
72
|
|
|
73
73
|
|
|
74
|
-
def test_sample_state_partial_indices():
|
|
74
|
+
def test_sample_state_partial_indices() -> None:
|
|
75
75
|
for index in range(3):
|
|
76
76
|
for x in range(8):
|
|
77
77
|
state = cirq.to_valid_state_vector(x, 3)
|
|
@@ -80,14 +80,14 @@ def test_sample_state_partial_indices():
|
|
|
80
80
|
)
|
|
81
81
|
|
|
82
82
|
|
|
83
|
-
def test_sample_state_partial_indices_oder():
|
|
83
|
+
def test_sample_state_partial_indices_oder() -> None:
|
|
84
84
|
for x in range(8):
|
|
85
85
|
state = cirq.to_valid_state_vector(x, 3)
|
|
86
86
|
expected = [[bool(1 & (x >> 0)), bool(1 & (x >> 1))]]
|
|
87
87
|
np.testing.assert_equal(cirq.sample_state_vector(state, [2, 1]), expected)
|
|
88
88
|
|
|
89
89
|
|
|
90
|
-
def test_sample_state_partial_indices_all_orders():
|
|
90
|
+
def test_sample_state_partial_indices_all_orders() -> None:
|
|
91
91
|
for perm in itertools.permutations([0, 1, 2]):
|
|
92
92
|
for x in range(8):
|
|
93
93
|
state = cirq.to_valid_state_vector(x, 3)
|
|
@@ -95,7 +95,7 @@ def test_sample_state_partial_indices_all_orders():
|
|
|
95
95
|
np.testing.assert_equal(cirq.sample_state_vector(state, perm), expected)
|
|
96
96
|
|
|
97
97
|
|
|
98
|
-
def test_sample_state():
|
|
98
|
+
def test_sample_state() -> None:
|
|
99
99
|
state = np.zeros(8, dtype=np.complex64)
|
|
100
100
|
state[0] = 1 / np.sqrt(2)
|
|
101
101
|
state[2] = 1 / np.sqrt(2)
|
|
@@ -110,12 +110,12 @@ def test_sample_state():
|
|
|
110
110
|
np.testing.assert_equal(cirq.sample_state_vector(state, [0]), [[False]])
|
|
111
111
|
|
|
112
112
|
|
|
113
|
-
def test_sample_empty_state():
|
|
113
|
+
def test_sample_empty_state() -> None:
|
|
114
114
|
state = np.array([1.0])
|
|
115
115
|
np.testing.assert_almost_equal(cirq.sample_state_vector(state, []), np.zeros(shape=(1, 0)))
|
|
116
116
|
|
|
117
117
|
|
|
118
|
-
def test_sample_no_repetitions():
|
|
118
|
+
def test_sample_no_repetitions() -> None:
|
|
119
119
|
state = cirq.to_valid_state_vector(0, 3)
|
|
120
120
|
np.testing.assert_almost_equal(
|
|
121
121
|
cirq.sample_state_vector(state, [1], repetitions=0), np.zeros(shape=(0, 1))
|
|
@@ -125,7 +125,7 @@ def test_sample_no_repetitions():
|
|
|
125
125
|
)
|
|
126
126
|
|
|
127
127
|
|
|
128
|
-
def test_sample_state_repetitions():
|
|
128
|
+
def test_sample_state_repetitions() -> None:
|
|
129
129
|
for perm in itertools.permutations([0, 1, 2]):
|
|
130
130
|
for x in range(8):
|
|
131
131
|
state = cirq.to_valid_state_vector(x, 3)
|
|
@@ -135,7 +135,7 @@ def test_sample_state_repetitions():
|
|
|
135
135
|
np.testing.assert_equal(result, expected)
|
|
136
136
|
|
|
137
137
|
|
|
138
|
-
def test_sample_state_seed():
|
|
138
|
+
def test_sample_state_seed() -> None:
|
|
139
139
|
state = np.ones(2) / np.sqrt(2)
|
|
140
140
|
|
|
141
141
|
samples = cirq.sample_state_vector(state, [0], repetitions=10, seed=1234)
|
|
@@ -151,20 +151,20 @@ def test_sample_state_seed():
|
|
|
151
151
|
)
|
|
152
152
|
|
|
153
153
|
|
|
154
|
-
def test_sample_state_negative_repetitions():
|
|
154
|
+
def test_sample_state_negative_repetitions() -> None:
|
|
155
155
|
state = cirq.to_valid_state_vector(0, 3)
|
|
156
156
|
with pytest.raises(ValueError, match='-1'):
|
|
157
157
|
cirq.sample_state_vector(state, [1], repetitions=-1)
|
|
158
158
|
|
|
159
159
|
|
|
160
|
-
def test_sample_state_not_power_of_two():
|
|
160
|
+
def test_sample_state_not_power_of_two() -> None:
|
|
161
161
|
with pytest.raises(ValueError, match='3'):
|
|
162
162
|
cirq.sample_state_vector(np.array([1, 0, 0]), [1])
|
|
163
163
|
with pytest.raises(ValueError, match='5'):
|
|
164
164
|
cirq.sample_state_vector(np.array([0, 1, 0, 0, 0]), [1])
|
|
165
165
|
|
|
166
166
|
|
|
167
|
-
def test_sample_state_index_out_of_range():
|
|
167
|
+
def test_sample_state_index_out_of_range() -> None:
|
|
168
168
|
state = cirq.to_valid_state_vector(0, 3)
|
|
169
169
|
with pytest.raises(IndexError, match='-2'):
|
|
170
170
|
cirq.sample_state_vector(state, [-2])
|
|
@@ -172,12 +172,12 @@ def test_sample_state_index_out_of_range():
|
|
|
172
172
|
cirq.sample_state_vector(state, [3])
|
|
173
173
|
|
|
174
174
|
|
|
175
|
-
def test_sample_no_indices():
|
|
175
|
+
def test_sample_no_indices() -> None:
|
|
176
176
|
state = cirq.to_valid_state_vector(0, 3)
|
|
177
177
|
np.testing.assert_almost_equal(cirq.sample_state_vector(state, []), np.zeros(shape=(1, 0)))
|
|
178
178
|
|
|
179
179
|
|
|
180
|
-
def test_sample_no_indices_repetitions():
|
|
180
|
+
def test_sample_no_indices_repetitions() -> None:
|
|
181
181
|
state = cirq.to_valid_state_vector(0, 3)
|
|
182
182
|
np.testing.assert_almost_equal(
|
|
183
183
|
cirq.sample_state_vector(state, [], repetitions=2), np.zeros(shape=(2, 0))
|
|
@@ -185,7 +185,7 @@ def test_sample_no_indices_repetitions():
|
|
|
185
185
|
|
|
186
186
|
|
|
187
187
|
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
188
|
-
def test_measure_state_computational_basis(use_np_transpose: bool):
|
|
188
|
+
def test_measure_state_computational_basis(use_np_transpose: bool) -> None:
|
|
189
189
|
# verify patching of can_numpy_support_shape in the use_np_transpose fixture
|
|
190
190
|
assert linalg.can_numpy_support_shape([1]) is use_np_transpose
|
|
191
191
|
results = []
|
|
@@ -199,7 +199,7 @@ def test_measure_state_computational_basis(use_np_transpose: bool):
|
|
|
199
199
|
|
|
200
200
|
|
|
201
201
|
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
202
|
-
def test_measure_state_reshape(use_np_transpose: bool):
|
|
202
|
+
def test_measure_state_reshape(use_np_transpose: bool) -> None:
|
|
203
203
|
results = []
|
|
204
204
|
for x in range(8):
|
|
205
205
|
initial_state = np.reshape(cirq.to_valid_state_vector(x, 3), [2] * 3)
|
|
@@ -211,7 +211,7 @@ def test_measure_state_reshape(use_np_transpose: bool):
|
|
|
211
211
|
|
|
212
212
|
|
|
213
213
|
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
214
|
-
def test_measure_state_partial_indices(use_np_transpose: bool):
|
|
214
|
+
def test_measure_state_partial_indices(use_np_transpose: bool) -> None:
|
|
215
215
|
for index in range(3):
|
|
216
216
|
for x in range(8):
|
|
217
217
|
initial_state = cirq.to_valid_state_vector(x, 3)
|
|
@@ -221,7 +221,7 @@ def test_measure_state_partial_indices(use_np_transpose: bool):
|
|
|
221
221
|
|
|
222
222
|
|
|
223
223
|
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
224
|
-
def test_measure_state_partial_indices_order(use_np_transpose: bool):
|
|
224
|
+
def test_measure_state_partial_indices_order(use_np_transpose: bool) -> None:
|
|
225
225
|
for x in range(8):
|
|
226
226
|
initial_state = cirq.to_valid_state_vector(x, 3)
|
|
227
227
|
bits, state = cirq.measure_state_vector(initial_state, [2, 1])
|
|
@@ -230,7 +230,7 @@ def test_measure_state_partial_indices_order(use_np_transpose: bool):
|
|
|
230
230
|
|
|
231
231
|
|
|
232
232
|
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
233
|
-
def test_measure_state_partial_indices_all_orders(use_np_transpose: bool):
|
|
233
|
+
def test_measure_state_partial_indices_all_orders(use_np_transpose: bool) -> None:
|
|
234
234
|
for perm in itertools.permutations([0, 1, 2]):
|
|
235
235
|
for x in range(8):
|
|
236
236
|
initial_state = cirq.to_valid_state_vector(x, 3)
|
|
@@ -240,7 +240,7 @@ def test_measure_state_partial_indices_all_orders(use_np_transpose: bool):
|
|
|
240
240
|
|
|
241
241
|
|
|
242
242
|
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
243
|
-
def test_measure_state_collapse(use_np_transpose: bool):
|
|
243
|
+
def test_measure_state_collapse(use_np_transpose: bool) -> None:
|
|
244
244
|
initial_state = np.zeros(8, dtype=np.complex64)
|
|
245
245
|
initial_state[0] = 1 / np.sqrt(2)
|
|
246
246
|
initial_state[2] = 1 / np.sqrt(2)
|
|
@@ -264,7 +264,7 @@ def test_measure_state_collapse(use_np_transpose: bool):
|
|
|
264
264
|
|
|
265
265
|
|
|
266
266
|
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
267
|
-
def test_measure_state_seed(use_np_transpose: bool):
|
|
267
|
+
def test_measure_state_seed(use_np_transpose: bool) -> None:
|
|
268
268
|
n = 10
|
|
269
269
|
initial_state = np.ones(2**n) / 2 ** (n / 2)
|
|
270
270
|
|
|
@@ -284,7 +284,7 @@ def test_measure_state_seed(use_np_transpose: bool):
|
|
|
284
284
|
|
|
285
285
|
|
|
286
286
|
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
287
|
-
def test_measure_state_out_is_state(use_np_transpose: bool):
|
|
287
|
+
def test_measure_state_out_is_state(use_np_transpose: bool) -> None:
|
|
288
288
|
initial_state = np.zeros(8, dtype=np.complex64)
|
|
289
289
|
initial_state[0] = 1 / np.sqrt(2)
|
|
290
290
|
initial_state[2] = 1 / np.sqrt(2)
|
|
@@ -296,7 +296,7 @@ def test_measure_state_out_is_state(use_np_transpose: bool):
|
|
|
296
296
|
|
|
297
297
|
|
|
298
298
|
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
299
|
-
def test_measure_state_out_is_not_state(use_np_transpose: bool):
|
|
299
|
+
def test_measure_state_out_is_not_state(use_np_transpose: bool) -> None:
|
|
300
300
|
initial_state = np.zeros(8, dtype=np.complex64)
|
|
301
301
|
initial_state[0] = 1 / np.sqrt(2)
|
|
302
302
|
initial_state[2] = 1 / np.sqrt(2)
|
|
@@ -307,7 +307,7 @@ def test_measure_state_out_is_not_state(use_np_transpose: bool):
|
|
|
307
307
|
|
|
308
308
|
|
|
309
309
|
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
310
|
-
def test_measure_state_not_power_of_two(use_np_transpose: bool):
|
|
310
|
+
def test_measure_state_not_power_of_two(use_np_transpose: bool) -> None:
|
|
311
311
|
with pytest.raises(ValueError, match='3'):
|
|
312
312
|
_, _ = cirq.measure_state_vector(np.array([1, 0, 0]), [1])
|
|
313
313
|
with pytest.raises(ValueError, match='5'):
|
|
@@ -315,7 +315,7 @@ def test_measure_state_not_power_of_two(use_np_transpose: bool):
|
|
|
315
315
|
|
|
316
316
|
|
|
317
317
|
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
318
|
-
def test_measure_state_index_out_of_range(use_np_transpose: bool):
|
|
318
|
+
def test_measure_state_index_out_of_range(use_np_transpose: bool) -> None:
|
|
319
319
|
state = cirq.to_valid_state_vector(0, 3)
|
|
320
320
|
with pytest.raises(IndexError, match='-2'):
|
|
321
321
|
cirq.measure_state_vector(state, [-2])
|
|
@@ -324,7 +324,7 @@ def test_measure_state_index_out_of_range(use_np_transpose: bool):
|
|
|
324
324
|
|
|
325
325
|
|
|
326
326
|
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
327
|
-
def test_measure_state_no_indices(use_np_transpose: bool):
|
|
327
|
+
def test_measure_state_no_indices(use_np_transpose: bool) -> None:
|
|
328
328
|
initial_state = cirq.to_valid_state_vector(0, 3)
|
|
329
329
|
bits, state = cirq.measure_state_vector(initial_state, [])
|
|
330
330
|
assert [] == bits
|
|
@@ -332,7 +332,7 @@ def test_measure_state_no_indices(use_np_transpose: bool):
|
|
|
332
332
|
|
|
333
333
|
|
|
334
334
|
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
335
|
-
def test_measure_state_no_indices_out_is_state(use_np_transpose: bool):
|
|
335
|
+
def test_measure_state_no_indices_out_is_state(use_np_transpose: bool) -> None:
|
|
336
336
|
initial_state = cirq.to_valid_state_vector(0, 3)
|
|
337
337
|
bits, state = cirq.measure_state_vector(initial_state, [], out=initial_state)
|
|
338
338
|
assert [] == bits
|
|
@@ -341,7 +341,7 @@ def test_measure_state_no_indices_out_is_state(use_np_transpose: bool):
|
|
|
341
341
|
|
|
342
342
|
|
|
343
343
|
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
344
|
-
def test_measure_state_no_indices_out_is_not_state(use_np_transpose: bool):
|
|
344
|
+
def test_measure_state_no_indices_out_is_not_state(use_np_transpose: bool) -> None:
|
|
345
345
|
initial_state = cirq.to_valid_state_vector(0, 3)
|
|
346
346
|
out = np.zeros_like(initial_state)
|
|
347
347
|
bits, state = cirq.measure_state_vector(initial_state, [], out=out)
|
|
@@ -352,7 +352,7 @@ def test_measure_state_no_indices_out_is_not_state(use_np_transpose: bool):
|
|
|
352
352
|
|
|
353
353
|
|
|
354
354
|
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
355
|
-
def test_measure_state_empty_state(use_np_transpose: bool):
|
|
355
|
+
def test_measure_state_empty_state(use_np_transpose: bool) -> None:
|
|
356
356
|
initial_state = np.array([1.0])
|
|
357
357
|
bits, state = cirq.measure_state_vector(initial_state, [])
|
|
358
358
|
assert [] == bits
|
|
@@ -364,12 +364,12 @@ class BasicStateVector(cirq.StateVectorMixin):
|
|
|
364
364
|
return np.array([0, 1, 0, 0])
|
|
365
365
|
|
|
366
366
|
|
|
367
|
-
def test_step_result_pretty_state():
|
|
367
|
+
def test_step_result_pretty_state() -> None:
|
|
368
368
|
step_result = BasicStateVector()
|
|
369
369
|
assert step_result.dirac_notation() == '|01⟩'
|
|
370
370
|
|
|
371
371
|
|
|
372
|
-
def test_step_result_density_matrix():
|
|
372
|
+
def test_step_result_density_matrix() -> None:
|
|
373
373
|
q0, q1 = cirq.LineQubit.range(2)
|
|
374
374
|
|
|
375
375
|
step_result = BasicStateVector({q0: 0, q1: 1})
|
|
@@ -385,7 +385,7 @@ def test_step_result_density_matrix():
|
|
|
385
385
|
np.testing.assert_array_almost_equal(single_rho, step_result.density_matrix_of([q1]))
|
|
386
386
|
|
|
387
387
|
|
|
388
|
-
def test_step_result_density_matrix_invalid():
|
|
388
|
+
def test_step_result_density_matrix_invalid() -> None:
|
|
389
389
|
q0, q1 = cirq.LineQubit.range(2)
|
|
390
390
|
|
|
391
391
|
step_result = BasicStateVector({q0: 0})
|
|
@@ -393,12 +393,12 @@ def test_step_result_density_matrix_invalid():
|
|
|
393
393
|
with pytest.raises(KeyError):
|
|
394
394
|
step_result.density_matrix_of([q1])
|
|
395
395
|
with pytest.raises(KeyError):
|
|
396
|
-
step_result.density_matrix_of('junk')
|
|
396
|
+
step_result.density_matrix_of('junk') # type: ignore[arg-type]
|
|
397
397
|
with pytest.raises(TypeError):
|
|
398
|
-
step_result.density_matrix_of(0)
|
|
398
|
+
step_result.density_matrix_of(0) # type: ignore[arg-type]
|
|
399
399
|
|
|
400
400
|
|
|
401
|
-
def test_step_result_bloch_vector():
|
|
401
|
+
def test_step_result_bloch_vector() -> None:
|
|
402
402
|
q0, q1 = cirq.LineQubit.range(2)
|
|
403
403
|
step_result = BasicStateVector({q0: 0, q1: 1})
|
|
404
404
|
bloch1 = np.array([0, 0, -1])
|
|
@@ -409,7 +409,7 @@ def test_step_result_bloch_vector():
|
|
|
409
409
|
step_result.bloch_vector_of(cirq.LineQubit(2))
|
|
410
410
|
|
|
411
411
|
|
|
412
|
-
def test_factor_validation():
|
|
412
|
+
def test_factor_validation() -> None:
|
|
413
413
|
args = cirq.Simulator()._create_simulation_state(0, qubits=cirq.LineQubit.range(2))
|
|
414
414
|
args.apply_operation(cirq.H(cirq.LineQubit(0)) ** 0.7)
|
|
415
415
|
t = args.create_merged_state().target_tensor
|
cirq/study/result_test.py
CHANGED
|
@@ -25,12 +25,12 @@ import cirq.testing
|
|
|
25
25
|
from cirq.study.result import _pack_digits
|
|
26
26
|
|
|
27
27
|
|
|
28
|
-
def test_result_init():
|
|
28
|
+
def test_result_init() -> None:
|
|
29
29
|
assert cirq.ResultDict(params=cirq.ParamResolver({}), measurements=None).repetitions == 0
|
|
30
30
|
assert cirq.ResultDict(params=cirq.ParamResolver({}), measurements={}).repetitions == 0
|
|
31
31
|
|
|
32
32
|
|
|
33
|
-
def test_default_repetitions():
|
|
33
|
+
def test_default_repetitions() -> None:
|
|
34
34
|
class MyResult(cirq.Result):
|
|
35
35
|
def __init__(self, records):
|
|
36
36
|
self._records = records
|
|
@@ -55,7 +55,7 @@ def test_default_repetitions():
|
|
|
55
55
|
assert MyResult({'a': np.zeros((5, 2, 3))}).repetitions == 5
|
|
56
56
|
|
|
57
57
|
|
|
58
|
-
def test_repr():
|
|
58
|
+
def test_repr() -> None:
|
|
59
59
|
v = cirq.ResultDict(
|
|
60
60
|
params=cirq.ParamResolver({'a': 2}), measurements={'xy': np.array([[1, 0], [0, 1]])}
|
|
61
61
|
)
|
|
@@ -68,7 +68,7 @@ def test_repr():
|
|
|
68
68
|
cirq.testing.assert_equivalent_repr(v)
|
|
69
69
|
|
|
70
70
|
|
|
71
|
-
def test_construct_from_measurements():
|
|
71
|
+
def test_construct_from_measurements() -> None:
|
|
72
72
|
r = cirq.ResultDict(
|
|
73
73
|
params=None,
|
|
74
74
|
measurements={'a': np.array([[0, 0], [1, 1]]), 'b': np.array([[0, 0, 0], [1, 1, 1]])},
|
|
@@ -79,7 +79,7 @@ def test_construct_from_measurements():
|
|
|
79
79
|
assert np.all(r.records['b'] == np.array([[[0, 0, 0]], [[1, 1, 1]]]))
|
|
80
80
|
|
|
81
81
|
|
|
82
|
-
def test_construct_from_repeated_measurements():
|
|
82
|
+
def test_construct_from_repeated_measurements() -> None:
|
|
83
83
|
r = cirq.ResultDict(
|
|
84
84
|
params=None,
|
|
85
85
|
records={
|
|
@@ -104,13 +104,13 @@ def test_construct_from_repeated_measurements():
|
|
|
104
104
|
assert r2.repetitions == 2
|
|
105
105
|
|
|
106
106
|
|
|
107
|
-
def test_empty_measurements():
|
|
107
|
+
def test_empty_measurements() -> None:
|
|
108
108
|
assert cirq.ResultDict(params=None).repetitions == 0
|
|
109
109
|
assert cirq.ResultDict(params=None, measurements={}).repetitions == 0
|
|
110
110
|
assert cirq.ResultDict(params=None, records={}).repetitions == 0
|
|
111
111
|
|
|
112
112
|
|
|
113
|
-
def test_str():
|
|
113
|
+
def test_str() -> None:
|
|
114
114
|
result = cirq.ResultDict(
|
|
115
115
|
params=cirq.ParamResolver({}),
|
|
116
116
|
measurements={
|
|
@@ -136,7 +136,7 @@ def test_str():
|
|
|
136
136
|
assert str(result) == 'c=1, 0\nc=0, 1'
|
|
137
137
|
|
|
138
138
|
|
|
139
|
-
def test_df():
|
|
139
|
+
def test_df() -> None:
|
|
140
140
|
result = cirq.ResultDict(
|
|
141
141
|
params=cirq.ParamResolver({}),
|
|
142
142
|
measurements={
|
|
@@ -156,7 +156,7 @@ def test_df():
|
|
|
156
156
|
assert df.c.value_counts().to_dict() == {0: 3, 1: 2}
|
|
157
157
|
|
|
158
158
|
|
|
159
|
-
def test_df_large():
|
|
159
|
+
def test_df_large() -> None:
|
|
160
160
|
result = cirq.ResultDict(
|
|
161
161
|
params=cirq.ParamResolver({}),
|
|
162
162
|
measurements={
|
|
@@ -171,7 +171,7 @@ def test_df_large():
|
|
|
171
171
|
assert result.data['d'].dtype == object
|
|
172
172
|
|
|
173
173
|
|
|
174
|
-
def test_histogram():
|
|
174
|
+
def test_histogram() -> None:
|
|
175
175
|
result = cirq.ResultDict(
|
|
176
176
|
params=cirq.ParamResolver({}),
|
|
177
177
|
measurements={
|
|
@@ -188,7 +188,7 @@ def test_histogram():
|
|
|
188
188
|
assert result.histogram(key='c') == collections.Counter({0: 3, 1: 2})
|
|
189
189
|
|
|
190
190
|
|
|
191
|
-
def test_multi_measurement_histogram():
|
|
191
|
+
def test_multi_measurement_histogram() -> None:
|
|
192
192
|
result = cirq.ResultDict(
|
|
193
193
|
params=cirq.ParamResolver({}),
|
|
194
194
|
measurements={
|
|
@@ -222,7 +222,7 @@ def test_multi_measurement_histogram():
|
|
|
222
222
|
)
|
|
223
223
|
|
|
224
224
|
|
|
225
|
-
def test_result_equality():
|
|
225
|
+
def test_result_equality() -> None:
|
|
226
226
|
et = cirq.testing.EqualsTester()
|
|
227
227
|
et.add_equality_group(
|
|
228
228
|
cirq.ResultDict(params=cirq.ParamResolver({}), measurements={'a': np.array([[0]] * 5)}),
|
|
@@ -239,7 +239,7 @@ def test_result_equality():
|
|
|
239
239
|
)
|
|
240
240
|
|
|
241
241
|
|
|
242
|
-
def test_result_addition_valid():
|
|
242
|
+
def test_result_addition_valid() -> None:
|
|
243
243
|
a = cirq.ResultDict(
|
|
244
244
|
params=cirq.ParamResolver({'ax': 1}),
|
|
245
245
|
measurements={
|
|
@@ -278,7 +278,7 @@ def test_result_addition_valid():
|
|
|
278
278
|
)
|
|
279
279
|
|
|
280
280
|
|
|
281
|
-
def test_result_addition_invalid():
|
|
281
|
+
def test_result_addition_invalid() -> None:
|
|
282
282
|
a = cirq.ResultDict(
|
|
283
283
|
params=cirq.ParamResolver({'ax': 1}),
|
|
284
284
|
measurements={
|
|
@@ -324,10 +324,10 @@ def test_result_addition_invalid():
|
|
|
324
324
|
with pytest.raises(ValueError, match='different measurement shapes'):
|
|
325
325
|
_ = a + e
|
|
326
326
|
with pytest.raises(TypeError):
|
|
327
|
-
_ = a + 'junk'
|
|
327
|
+
_ = a + 'junk' # type: ignore[operator]
|
|
328
328
|
|
|
329
329
|
|
|
330
|
-
def test_qubit_keys_for_histogram():
|
|
330
|
+
def test_qubit_keys_for_histogram() -> None:
|
|
331
331
|
a, b, c = cirq.LineQubit.range(3)
|
|
332
332
|
circuit = cirq.Circuit(cirq.measure(a, b), cirq.X(c), cirq.measure(c))
|
|
333
333
|
results = cirq.Simulator().run(program=circuit, repetitions=100)
|
|
@@ -339,7 +339,7 @@ def test_qubit_keys_for_histogram():
|
|
|
339
339
|
assert results.histogram(key=[c]) == collections.Counter({1: 100})
|
|
340
340
|
|
|
341
341
|
|
|
342
|
-
def test_text_diagram_jupyter():
|
|
342
|
+
def test_text_diagram_jupyter() -> None:
|
|
343
343
|
result = cirq.ResultDict(
|
|
344
344
|
params=cirq.ParamResolver({}),
|
|
345
345
|
measurements={
|
|
@@ -397,12 +397,12 @@ def test_json_bit_packing_and_dtype(use_records: bool) -> None:
|
|
|
397
397
|
np.testing.assert_allclose(len(bits_json), len(digits_json) / 8, rtol=0.02)
|
|
398
398
|
|
|
399
399
|
|
|
400
|
-
def test_json_bit_packing_error():
|
|
400
|
+
def test_json_bit_packing_error() -> None:
|
|
401
401
|
with pytest.raises(ValueError):
|
|
402
402
|
_pack_digits(np.ones(10), pack_bits='hi mom')
|
|
403
403
|
|
|
404
404
|
|
|
405
|
-
def test_json_bit_packing_force():
|
|
405
|
+
def test_json_bit_packing_force() -> None:
|
|
406
406
|
assert _pack_digits(np.ones(10, dtype=int), pack_bits='force') == _pack_digits(
|
|
407
407
|
np.ones(10), pack_bits='auto'
|
|
408
408
|
)
|
|
@@ -418,7 +418,7 @@ def test_json_bit_packing_force():
|
|
|
418
418
|
)
|
|
419
419
|
|
|
420
420
|
|
|
421
|
-
def test_json_unpack_compat():
|
|
421
|
+
def test_json_unpack_compat() -> None:
|
|
422
422
|
"""Test reading old json with serialized measurements array."""
|
|
423
423
|
old_json = """
|
|
424
424
|
{
|