cirq-core 1.5.0.dev20240823014143__py3-none-any.whl → 1.5.0.dev20240830211827__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.py +1 -1
- cirq/circuits/circuit_operation.py +2 -2
- cirq/circuits/qasm_output.py +2 -2
- cirq/contrib/acquaintance/bipartite.py +3 -3
- cirq/contrib/acquaintance/executor.py +2 -2
- cirq/contrib/acquaintance/gates.py +12 -2
- cirq/contrib/acquaintance/inspection_utils.py +2 -2
- cirq/contrib/acquaintance/permutation.py +3 -2
- cirq/contrib/acquaintance/shift.py +2 -2
- cirq/contrib/acquaintance/shift_swap_network.py +2 -2
- cirq/contrib/bayesian_network/bayesian_network_gate.py +2 -2
- cirq/contrib/paulistring/separate.py +2 -2
- cirq/experiments/random_quantum_circuit_generation.py +2 -1
- cirq/interop/quirk/cells/control_cells.py +1 -1
- cirq/interop/quirk/cells/parse.py +3 -1
- cirq/ops/diagonal_gate.py +1 -1
- cirq/ops/fsim_gate.py +3 -3
- cirq/ops/linear_combinations.py +1 -1
- cirq/ops/parity_gates.py +3 -3
- cirq/ops/pauli_interaction_gate.py +2 -2
- cirq/ops/pauli_measurement_gate.py +2 -1
- cirq/ops/pauli_string_phasor.py +1 -1
- cirq/ops/permutation_gate.py +2 -2
- cirq/ops/phased_iswap_gate.py +2 -2
- cirq/ops/phased_x_z_gate.py +3 -2
- cirq/ops/projector.py +2 -1
- cirq/ops/raw_types.py +1 -1
- cirq/ops/three_qubit_gates.py +3 -2
- cirq/ops/two_qubit_diagonal_gate.py +2 -2
- cirq/ops/uniform_superposition_gate.py +2 -2
- cirq/sim/state_vector_test.py +27 -32
- cirq/transformers/eject_phased_paulis.py +2 -2
- cirq/transformers/eject_z.py +2 -2
- cirq/transformers/noise_adding.py +1 -1
- cirq/transformers/noise_adding_test.py +4 -4
- cirq/transformers/transformer_primitives_test.py +3 -3
- cirq/value/linear_dict.py +6 -4
- cirq/vis/heatmap.py +9 -1
- cirq/vis/state_histogram.py +2 -1
- {cirq_core-1.5.0.dev20240823014143.dist-info → cirq_core-1.5.0.dev20240830211827.dist-info}/METADATA +1 -1
- {cirq_core-1.5.0.dev20240823014143.dist-info → cirq_core-1.5.0.dev20240830211827.dist-info}/RECORD +46 -46
- {cirq_core-1.5.0.dev20240823014143.dist-info → cirq_core-1.5.0.dev20240830211827.dist-info}/LICENSE +0 -0
- {cirq_core-1.5.0.dev20240823014143.dist-info → cirq_core-1.5.0.dev20240830211827.dist-info}/WHEEL +0 -0
- {cirq_core-1.5.0.dev20240823014143.dist-info → cirq_core-1.5.0.dev20240830211827.dist-info}/top_level.txt +0 -0
cirq/sim/state_vector_test.py
CHANGED
|
@@ -14,16 +14,24 @@
|
|
|
14
14
|
"""Tests for state_vector.py"""
|
|
15
15
|
|
|
16
16
|
import itertools
|
|
17
|
-
from typing import Optional
|
|
18
|
-
import
|
|
17
|
+
from typing import Iterator, Optional
|
|
18
|
+
from unittest import mock
|
|
19
19
|
|
|
20
20
|
import numpy as np
|
|
21
|
+
import pytest
|
|
21
22
|
|
|
22
23
|
import cirq
|
|
23
24
|
import cirq.testing
|
|
24
25
|
from cirq import linalg
|
|
25
26
|
|
|
26
27
|
|
|
28
|
+
@pytest.fixture
|
|
29
|
+
def use_np_transpose(request) -> Iterator[bool]:
|
|
30
|
+
value: bool = request.param
|
|
31
|
+
with mock.patch.object(linalg, 'can_numpy_support_shape', lambda shape: value):
|
|
32
|
+
yield value
|
|
33
|
+
|
|
34
|
+
|
|
27
35
|
def test_state_mixin():
|
|
28
36
|
class TestClass(cirq.StateVectorMixin):
|
|
29
37
|
def state_vector(self, copy: Optional[bool] = None) -> np.ndarray:
|
|
@@ -173,9 +181,10 @@ def test_sample_no_indices_repetitions():
|
|
|
173
181
|
)
|
|
174
182
|
|
|
175
183
|
|
|
176
|
-
@pytest.mark.parametrize('use_np_transpose', [False, True])
|
|
184
|
+
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
177
185
|
def test_measure_state_computational_basis(use_np_transpose: bool):
|
|
178
|
-
|
|
186
|
+
# verify patching of can_numpy_support_shape in the use_np_transpose fixture
|
|
187
|
+
assert linalg.can_numpy_support_shape([1]) is use_np_transpose
|
|
179
188
|
results = []
|
|
180
189
|
for x in range(8):
|
|
181
190
|
initial_state = cirq.to_valid_state_vector(x, 3)
|
|
@@ -186,9 +195,8 @@ def test_measure_state_computational_basis(use_np_transpose: bool):
|
|
|
186
195
|
assert results == expected
|
|
187
196
|
|
|
188
197
|
|
|
189
|
-
@pytest.mark.parametrize('use_np_transpose', [False, True])
|
|
198
|
+
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
190
199
|
def test_measure_state_reshape(use_np_transpose: bool):
|
|
191
|
-
linalg.can_numpy_support_shape = lambda s: use_np_transpose
|
|
192
200
|
results = []
|
|
193
201
|
for x in range(8):
|
|
194
202
|
initial_state = np.reshape(cirq.to_valid_state_vector(x, 3), [2] * 3)
|
|
@@ -199,9 +207,8 @@ def test_measure_state_reshape(use_np_transpose: bool):
|
|
|
199
207
|
assert results == expected
|
|
200
208
|
|
|
201
209
|
|
|
202
|
-
@pytest.mark.parametrize('use_np_transpose', [False, True])
|
|
210
|
+
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
203
211
|
def test_measure_state_partial_indices(use_np_transpose: bool):
|
|
204
|
-
linalg.can_numpy_support_shape = lambda s: use_np_transpose
|
|
205
212
|
for index in range(3):
|
|
206
213
|
for x in range(8):
|
|
207
214
|
initial_state = cirq.to_valid_state_vector(x, 3)
|
|
@@ -210,9 +217,8 @@ def test_measure_state_partial_indices(use_np_transpose: bool):
|
|
|
210
217
|
assert bits == [bool(1 & (x >> (2 - index)))]
|
|
211
218
|
|
|
212
219
|
|
|
213
|
-
@pytest.mark.parametrize('use_np_transpose', [False, True])
|
|
220
|
+
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
214
221
|
def test_measure_state_partial_indices_order(use_np_transpose: bool):
|
|
215
|
-
linalg.can_numpy_support_shape = lambda s: use_np_transpose
|
|
216
222
|
for x in range(8):
|
|
217
223
|
initial_state = cirq.to_valid_state_vector(x, 3)
|
|
218
224
|
bits, state = cirq.measure_state_vector(initial_state, [2, 1])
|
|
@@ -220,9 +226,8 @@ def test_measure_state_partial_indices_order(use_np_transpose: bool):
|
|
|
220
226
|
assert bits == [bool(1 & (x >> 0)), bool(1 & (x >> 1))]
|
|
221
227
|
|
|
222
228
|
|
|
223
|
-
@pytest.mark.parametrize('use_np_transpose', [False, True])
|
|
229
|
+
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
224
230
|
def test_measure_state_partial_indices_all_orders(use_np_transpose: bool):
|
|
225
|
-
linalg.can_numpy_support_shape = lambda s: use_np_transpose
|
|
226
231
|
for perm in itertools.permutations([0, 1, 2]):
|
|
227
232
|
for x in range(8):
|
|
228
233
|
initial_state = cirq.to_valid_state_vector(x, 3)
|
|
@@ -231,9 +236,8 @@ def test_measure_state_partial_indices_all_orders(use_np_transpose: bool):
|
|
|
231
236
|
assert bits == [bool(1 & (x >> (2 - p))) for p in perm]
|
|
232
237
|
|
|
233
238
|
|
|
234
|
-
@pytest.mark.parametrize('use_np_transpose', [False, True])
|
|
239
|
+
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
235
240
|
def test_measure_state_collapse(use_np_transpose: bool):
|
|
236
|
-
linalg.can_numpy_support_shape = lambda s: use_np_transpose
|
|
237
241
|
initial_state = np.zeros(8, dtype=np.complex64)
|
|
238
242
|
initial_state[0] = 1 / np.sqrt(2)
|
|
239
243
|
initial_state[2] = 1 / np.sqrt(2)
|
|
@@ -256,9 +260,8 @@ def test_measure_state_collapse(use_np_transpose: bool):
|
|
|
256
260
|
assert bits == [False]
|
|
257
261
|
|
|
258
262
|
|
|
259
|
-
@pytest.mark.parametrize('use_np_transpose', [False, True])
|
|
263
|
+
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
260
264
|
def test_measure_state_seed(use_np_transpose: bool):
|
|
261
|
-
linalg.can_numpy_support_shape = lambda s: use_np_transpose
|
|
262
265
|
n = 10
|
|
263
266
|
initial_state = np.ones(2**n) / 2 ** (n / 2)
|
|
264
267
|
|
|
@@ -277,9 +280,8 @@ def test_measure_state_seed(use_np_transpose: bool):
|
|
|
277
280
|
np.testing.assert_allclose(state1, state2)
|
|
278
281
|
|
|
279
282
|
|
|
280
|
-
@pytest.mark.parametrize('use_np_transpose', [False, True])
|
|
283
|
+
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
281
284
|
def test_measure_state_out_is_state(use_np_transpose: bool):
|
|
282
|
-
linalg.can_numpy_support_shape = lambda s: use_np_transpose
|
|
283
285
|
initial_state = np.zeros(8, dtype=np.complex64)
|
|
284
286
|
initial_state[0] = 1 / np.sqrt(2)
|
|
285
287
|
initial_state[2] = 1 / np.sqrt(2)
|
|
@@ -290,9 +292,8 @@ def test_measure_state_out_is_state(use_np_transpose: bool):
|
|
|
290
292
|
assert state is initial_state
|
|
291
293
|
|
|
292
294
|
|
|
293
|
-
@pytest.mark.parametrize('use_np_transpose', [False, True])
|
|
295
|
+
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
294
296
|
def test_measure_state_out_is_not_state(use_np_transpose: bool):
|
|
295
|
-
linalg.can_numpy_support_shape = lambda s: use_np_transpose
|
|
296
297
|
initial_state = np.zeros(8, dtype=np.complex64)
|
|
297
298
|
initial_state[0] = 1 / np.sqrt(2)
|
|
298
299
|
initial_state[2] = 1 / np.sqrt(2)
|
|
@@ -302,18 +303,16 @@ def test_measure_state_out_is_not_state(use_np_transpose: bool):
|
|
|
302
303
|
assert out is state
|
|
303
304
|
|
|
304
305
|
|
|
305
|
-
@pytest.mark.parametrize('use_np_transpose', [False, True])
|
|
306
|
+
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
306
307
|
def test_measure_state_not_power_of_two(use_np_transpose: bool):
|
|
307
|
-
linalg.can_numpy_support_shape = lambda s: use_np_transpose
|
|
308
308
|
with pytest.raises(ValueError, match='3'):
|
|
309
309
|
_, _ = cirq.measure_state_vector(np.array([1, 0, 0]), [1])
|
|
310
310
|
with pytest.raises(ValueError, match='5'):
|
|
311
311
|
cirq.measure_state_vector(np.array([0, 1, 0, 0, 0]), [1])
|
|
312
312
|
|
|
313
313
|
|
|
314
|
-
@pytest.mark.parametrize('use_np_transpose', [False, True])
|
|
314
|
+
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
315
315
|
def test_measure_state_index_out_of_range(use_np_transpose: bool):
|
|
316
|
-
linalg.can_numpy_support_shape = lambda s: use_np_transpose
|
|
317
316
|
state = cirq.to_valid_state_vector(0, 3)
|
|
318
317
|
with pytest.raises(IndexError, match='-2'):
|
|
319
318
|
cirq.measure_state_vector(state, [-2])
|
|
@@ -321,18 +320,16 @@ def test_measure_state_index_out_of_range(use_np_transpose: bool):
|
|
|
321
320
|
cirq.measure_state_vector(state, [3])
|
|
322
321
|
|
|
323
322
|
|
|
324
|
-
@pytest.mark.parametrize('use_np_transpose', [False, True])
|
|
323
|
+
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
325
324
|
def test_measure_state_no_indices(use_np_transpose: bool):
|
|
326
|
-
linalg.can_numpy_support_shape = lambda s: use_np_transpose
|
|
327
325
|
initial_state = cirq.to_valid_state_vector(0, 3)
|
|
328
326
|
bits, state = cirq.measure_state_vector(initial_state, [])
|
|
329
327
|
assert [] == bits
|
|
330
328
|
np.testing.assert_almost_equal(state, initial_state)
|
|
331
329
|
|
|
332
330
|
|
|
333
|
-
@pytest.mark.parametrize('use_np_transpose', [False, True])
|
|
331
|
+
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
334
332
|
def test_measure_state_no_indices_out_is_state(use_np_transpose: bool):
|
|
335
|
-
linalg.can_numpy_support_shape = lambda s: use_np_transpose
|
|
336
333
|
initial_state = cirq.to_valid_state_vector(0, 3)
|
|
337
334
|
bits, state = cirq.measure_state_vector(initial_state, [], out=initial_state)
|
|
338
335
|
assert [] == bits
|
|
@@ -340,9 +337,8 @@ def test_measure_state_no_indices_out_is_state(use_np_transpose: bool):
|
|
|
340
337
|
assert state is initial_state
|
|
341
338
|
|
|
342
339
|
|
|
343
|
-
@pytest.mark.parametrize('use_np_transpose', [False, True])
|
|
340
|
+
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
344
341
|
def test_measure_state_no_indices_out_is_not_state(use_np_transpose: bool):
|
|
345
|
-
linalg.can_numpy_support_shape = lambda s: use_np_transpose
|
|
346
342
|
initial_state = cirq.to_valid_state_vector(0, 3)
|
|
347
343
|
out = np.zeros_like(initial_state)
|
|
348
344
|
bits, state = cirq.measure_state_vector(initial_state, [], out=out)
|
|
@@ -352,9 +348,8 @@ def test_measure_state_no_indices_out_is_not_state(use_np_transpose: bool):
|
|
|
352
348
|
assert out is not initial_state
|
|
353
349
|
|
|
354
350
|
|
|
355
|
-
@pytest.mark.parametrize('use_np_transpose', [False, True])
|
|
351
|
+
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
356
352
|
def test_measure_state_empty_state(use_np_transpose: bool):
|
|
357
|
-
linalg.can_numpy_support_shape = lambda s: use_np_transpose
|
|
358
353
|
initial_state = np.array([1.0])
|
|
359
354
|
bits, state = cirq.measure_state_vector(initial_state, [])
|
|
360
355
|
assert [] == bits
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
|
|
15
15
|
"""Transformer pass that pushes 180° rotations around axes in the XY plane later in the circuit."""
|
|
16
16
|
|
|
17
|
-
from typing import Optional, cast, TYPE_CHECKING, Iterable, Tuple, Dict
|
|
17
|
+
from typing import Optional, cast, TYPE_CHECKING, Iterable, Iterator, Tuple, Dict
|
|
18
18
|
import sympy
|
|
19
19
|
import numpy as np
|
|
20
20
|
|
|
@@ -127,7 +127,7 @@ def _absorb_z_into_w(
|
|
|
127
127
|
|
|
128
128
|
def _dump_held(
|
|
129
129
|
qubits: Iterable[ops.Qid], held_w_phases: Dict[ops.Qid, value.TParamVal]
|
|
130
|
-
) -> 'cirq.OP_TREE':
|
|
130
|
+
) -> Iterator['cirq.OP_TREE']:
|
|
131
131
|
# Note: sorting is to avoid non-determinism in the insertion order.
|
|
132
132
|
for q in sorted(qubits):
|
|
133
133
|
p = held_w_phases.get(q)
|
cirq/transformers/eject_z.py
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
|
|
15
15
|
"""Transformer pass that pushes Z gates later and later in the circuit."""
|
|
16
16
|
|
|
17
|
-
from typing import Dict, Iterable, Optional, Tuple, TYPE_CHECKING
|
|
17
|
+
from typing import Dict, Iterable, Iterator, Optional, Tuple, TYPE_CHECKING
|
|
18
18
|
from collections import defaultdict
|
|
19
19
|
import numpy as np
|
|
20
20
|
|
|
@@ -76,7 +76,7 @@ def eject_z(
|
|
|
76
76
|
lambda: None
|
|
77
77
|
)
|
|
78
78
|
|
|
79
|
-
def dump_tracked_phase(qubits: Iterable[ops.Qid]) -> 'cirq.OP_TREE':
|
|
79
|
+
def dump_tracked_phase(qubits: Iterable[ops.Qid]) -> Iterator['cirq.OP_TREE']:
|
|
80
80
|
"""Zeroes qubit_phase entries by emitting Z gates."""
|
|
81
81
|
for q in qubits:
|
|
82
82
|
p, key = qubit_phase[q], last_phased_xz_op[q]
|
|
@@ -26,7 +26,7 @@ def _gate_in_moment(gate: ops.Gate, moment: circuits.Moment) -> bool:
|
|
|
26
26
|
|
|
27
27
|
|
|
28
28
|
@transformer_api.transformer
|
|
29
|
-
class
|
|
29
|
+
class DepolarizingNoiseTransformer:
|
|
30
30
|
"""Add local depolarizing noise after two-qubit gates in a specified circuit. More specifically,
|
|
31
31
|
with probability p, append a random non-identity two-qubit Pauli operator after each specified
|
|
32
32
|
two-qubit gate.
|
|
@@ -23,16 +23,16 @@ def test_noise_adding():
|
|
|
23
23
|
circuit = one_layer * 10
|
|
24
24
|
|
|
25
25
|
# test that p=0 does nothing
|
|
26
|
-
transformed_circuit_p0 = na.
|
|
26
|
+
transformed_circuit_p0 = na.DepolarizingNoiseTransformer(0.0)(circuit)
|
|
27
27
|
assert transformed_circuit_p0 == circuit
|
|
28
28
|
|
|
29
29
|
# test that p=1 doubles the circuit depth
|
|
30
|
-
transformed_circuit_p1 = na.
|
|
30
|
+
transformed_circuit_p1 = na.DepolarizingNoiseTransformer(1.0)(circuit)
|
|
31
31
|
assert len(transformed_circuit_p1) == 20
|
|
32
32
|
|
|
33
33
|
# test that we get a deterministic result when using a specific rng
|
|
34
34
|
rng = np.random.default_rng(0)
|
|
35
|
-
transformed_circuit_p0_03 = na.
|
|
35
|
+
transformed_circuit_p0_03 = na.DepolarizingNoiseTransformer(0.03)(circuit, rng=rng)
|
|
36
36
|
expected_circuit = (
|
|
37
37
|
one_layer * 2
|
|
38
38
|
+ circuits.Circuit(ops.I(qubits[2]), ops.Z(qubits[3]))
|
|
@@ -44,7 +44,7 @@ def test_noise_adding():
|
|
|
44
44
|
assert transformed_circuit_p0_03 == expected_circuit
|
|
45
45
|
|
|
46
46
|
# test that supplying a dictionary for p works
|
|
47
|
-
transformed_circuit_p_dict = na.
|
|
47
|
+
transformed_circuit_p_dict = na.DepolarizingNoiseTransformer(
|
|
48
48
|
{tuple(qubits[:2]): 1.0, tuple(qubits[2:]): 0.0}
|
|
49
49
|
)(circuit)
|
|
50
50
|
assert len(transformed_circuit_p_dict) == 20 # depth should be doubled
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
# See the License for the specific language governing permissions and
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
|
|
15
|
-
from typing import
|
|
15
|
+
from typing import Iterator, List, Optional
|
|
16
16
|
import pytest
|
|
17
17
|
|
|
18
18
|
import cirq
|
|
@@ -64,7 +64,7 @@ def test_map_operations_does_not_insert_too_many_moments():
|
|
|
64
64
|
q = cirq.LineQubit.range(5)
|
|
65
65
|
c_orig = cirq.Circuit(cirq.CX(q[0], q[1]), cirq.CX(q[3], q[2]), cirq.CX(q[3], q[4]))
|
|
66
66
|
|
|
67
|
-
def map_func(op: cirq.Operation, _: int) -> cirq.OP_TREE:
|
|
67
|
+
def map_func(op: cirq.Operation, _: int) -> Iterator[cirq.OP_TREE]:
|
|
68
68
|
yield cirq.Z.on_each(*op.qubits)
|
|
69
69
|
yield cirq.CX(*op.qubits)
|
|
70
70
|
yield cirq.Z.on_each(*op.qubits)
|
|
@@ -130,7 +130,7 @@ def test_map_operations_deep_subcircuits():
|
|
|
130
130
|
.with_tags("external")
|
|
131
131
|
)
|
|
132
132
|
|
|
133
|
-
def map_func(op: cirq.Operation, _: int) -> cirq.OP_TREE:
|
|
133
|
+
def map_func(op: cirq.Operation, _: int) -> Iterator[cirq.OP_TREE]:
|
|
134
134
|
yield (
|
|
135
135
|
[cirq.Z.on_each(*op.qubits), cirq.CX(*op.qubits), cirq.Z.on_each(*op.qubits)]
|
|
136
136
|
if op.gate == cirq.CX
|
cirq/value/linear_dict.py
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
|
|
15
15
|
"""Linear combination represented as mapping of things to coefficients."""
|
|
16
|
-
|
|
16
|
+
|
|
17
17
|
from typing import (
|
|
18
18
|
Any,
|
|
19
19
|
Callable,
|
|
@@ -34,7 +34,9 @@ from typing import (
|
|
|
34
34
|
)
|
|
35
35
|
from typing_extensions import Self
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
import numpy as np
|
|
38
|
+
|
|
39
|
+
Scalar = Union[complex, np.number]
|
|
38
40
|
TVector = TypeVar('TVector')
|
|
39
41
|
|
|
40
42
|
TDefault = TypeVar('TDefault')
|
|
@@ -124,7 +126,7 @@ class LinearDict(Generic[TVector], MutableMapping[TVector, Scalar]):
|
|
|
124
126
|
|
|
125
127
|
def clean(self, *, atol: float = 1e-9) -> Self:
|
|
126
128
|
"""Remove terms with coefficients of absolute value atol or less."""
|
|
127
|
-
negligible = [v for v, c in self._terms.items() if abs(c) <= atol]
|
|
129
|
+
negligible = [v for v, c in self._terms.items() if abs(complex(c)) <= atol]
|
|
128
130
|
for v in negligible:
|
|
129
131
|
del self._terms[v]
|
|
130
132
|
return self
|
|
@@ -245,7 +247,7 @@ class LinearDict(Generic[TVector], MutableMapping[TVector, Scalar]):
|
|
|
245
247
|
result *= a
|
|
246
248
|
return result
|
|
247
249
|
|
|
248
|
-
def __rmul__(self, a: Scalar) -> Self:
|
|
250
|
+
def __rmul__(self, a: Scalar) -> Self: # type: ignore
|
|
249
251
|
return self.__mul__(a)
|
|
250
252
|
|
|
251
253
|
def __truediv__(self, a: Scalar) -> Self:
|
cirq/vis/heatmap.py
CHANGED
|
@@ -11,6 +11,9 @@
|
|
|
11
11
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
12
|
# See the License for the specific language governing permissions and
|
|
13
13
|
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
14
17
|
import copy
|
|
15
18
|
from dataclasses import astuple, dataclass
|
|
16
19
|
from typing import (
|
|
@@ -25,6 +28,7 @@ from typing import (
|
|
|
25
28
|
SupportsFloat,
|
|
26
29
|
Tuple,
|
|
27
30
|
Union,
|
|
31
|
+
TYPE_CHECKING,
|
|
28
32
|
)
|
|
29
33
|
|
|
30
34
|
import matplotlib as mpl
|
|
@@ -36,6 +40,9 @@ from mpl_toolkits import axes_grid1
|
|
|
36
40
|
from cirq.devices import grid_qubit
|
|
37
41
|
from cirq.vis import vis_utils
|
|
38
42
|
|
|
43
|
+
if TYPE_CHECKING:
|
|
44
|
+
from numpy.typing import ArrayLike
|
|
45
|
+
|
|
39
46
|
QubitTuple = Tuple[grid_qubit.GridQubit, ...]
|
|
40
47
|
|
|
41
48
|
Polygon = Sequence[Tuple[float, float]]
|
|
@@ -233,13 +240,14 @@ class Heatmap:
|
|
|
233
240
|
ax: plt.Axes,
|
|
234
241
|
) -> None:
|
|
235
242
|
"""Writes annotations to the center of cells. Internal."""
|
|
243
|
+
facecolor: ArrayLike
|
|
236
244
|
for (center, annotation), facecolor in zip(centers_and_annot, collection.get_facecolor()):
|
|
237
245
|
# Calculate the center of the cell, assuming that it is a square
|
|
238
246
|
# centered at (x=col, y=row).
|
|
239
247
|
if not annotation:
|
|
240
248
|
continue
|
|
241
249
|
x, y = center
|
|
242
|
-
face_luminance = vis_utils.relative_luminance(facecolor)
|
|
250
|
+
face_luminance = vis_utils.relative_luminance(facecolor)
|
|
243
251
|
text_color = 'black' if face_luminance > 0.4 else 'white'
|
|
244
252
|
text_kwargs: Dict[str, Any] = dict(color=text_color, ha="center", va="center")
|
|
245
253
|
text_kwargs.update(self._config.get('annotation_text_kwargs', {}))
|
cirq/vis/state_histogram.py
CHANGED
|
@@ -90,7 +90,8 @@ def plot_state_histogram(
|
|
|
90
90
|
if isinstance(data, result.Result):
|
|
91
91
|
values = get_state_histogram(data)
|
|
92
92
|
elif isinstance(data, collections.Counter):
|
|
93
|
-
tick_label,
|
|
93
|
+
tick_label, counts = zip(*sorted(data.items()))
|
|
94
|
+
values = np.asarray(counts)
|
|
94
95
|
else:
|
|
95
96
|
values = np.array(data)
|
|
96
97
|
if tick_label is None:
|
{cirq_core-1.5.0.dev20240823014143.dist-info → cirq_core-1.5.0.dev20240830211827.dist-info}/METADATA
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: cirq-core
|
|
3
|
-
Version: 1.5.0.
|
|
3
|
+
Version: 1.5.0.dev20240830211827
|
|
4
4
|
Summary: A framework for creating, editing, and invoking Noisy Intermediate Scale Quantum (NISQ) circuits.
|
|
5
5
|
Home-page: http://github.com/quantumlib/cirq
|
|
6
6
|
Author: The Cirq Developers
|