cirq-core 1.7.0.dev20250911180440__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/gate_operation.py +15 -0
- cirq/ops/linear_combinations.py +4 -14
- cirq/ops/measure_util.py +7 -6
- cirq/ops/pauli_string_test.py +7 -6
- 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.dev20250911180440.dist-info → cirq_core-1.7.0.dev20250917002151.dist-info}/METADATA +1 -1
- {cirq_core-1.7.0.dev20250911180440.dist-info → cirq_core-1.7.0.dev20250917002151.dist-info}/RECORD +65 -65
- {cirq_core-1.7.0.dev20250911180440.dist-info → cirq_core-1.7.0.dev20250917002151.dist-info}/WHEEL +0 -0
- {cirq_core-1.7.0.dev20250911180440.dist-info → cirq_core-1.7.0.dev20250917002151.dist-info}/licenses/LICENSE +0 -0
- {cirq_core-1.7.0.dev20250911180440.dist-info → cirq_core-1.7.0.dev20250917002151.dist-info}/top_level.txt +0 -0
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
from __future__ import annotations
|
|
16
16
|
|
|
17
17
|
import itertools
|
|
18
|
+
import random
|
|
18
19
|
|
|
19
20
|
import numpy as np
|
|
20
21
|
import pytest
|
|
@@ -24,7 +25,7 @@ import cirq
|
|
|
24
25
|
import cirq.testing
|
|
25
26
|
|
|
26
27
|
|
|
27
|
-
def test_simulate_no_circuit():
|
|
28
|
+
def test_simulate_no_circuit() -> None:
|
|
28
29
|
q0, q1 = cirq.LineQubit.range(2)
|
|
29
30
|
simulator = cirq.CliffordSimulator()
|
|
30
31
|
circuit = cirq.Circuit()
|
|
@@ -33,7 +34,7 @@ def test_simulate_no_circuit():
|
|
|
33
34
|
assert len(result.measurements) == 0
|
|
34
35
|
|
|
35
36
|
|
|
36
|
-
def test_run_no_repetitions():
|
|
37
|
+
def test_run_no_repetitions() -> None:
|
|
37
38
|
q0 = cirq.LineQubit(0)
|
|
38
39
|
simulator = cirq.CliffordSimulator()
|
|
39
40
|
circuit = cirq.Circuit(cirq.H(q0), cirq.measure(q0))
|
|
@@ -41,7 +42,7 @@ def test_run_no_repetitions():
|
|
|
41
42
|
assert sum(result.measurements['q(0)']) == 0
|
|
42
43
|
|
|
43
44
|
|
|
44
|
-
def test_run_hadamard():
|
|
45
|
+
def test_run_hadamard() -> None:
|
|
45
46
|
q0 = cirq.LineQubit(0)
|
|
46
47
|
simulator = cirq.CliffordSimulator()
|
|
47
48
|
circuit = cirq.Circuit(cirq.H(q0), cirq.measure(q0))
|
|
@@ -50,7 +51,7 @@ def test_run_hadamard():
|
|
|
50
51
|
assert sum(result.measurements['q(0)'])[0] > 20
|
|
51
52
|
|
|
52
53
|
|
|
53
|
-
def test_run_GHZ():
|
|
54
|
+
def test_run_GHZ() -> None:
|
|
54
55
|
(q0, q1) = (cirq.LineQubit(0), cirq.LineQubit(1))
|
|
55
56
|
simulator = cirq.CliffordSimulator()
|
|
56
57
|
circuit = cirq.Circuit(cirq.H(q0), cirq.H(q1), cirq.measure(q0))
|
|
@@ -59,7 +60,7 @@ def test_run_GHZ():
|
|
|
59
60
|
assert sum(result.measurements['q(0)'])[0] > 20
|
|
60
61
|
|
|
61
62
|
|
|
62
|
-
def test_run_correlations():
|
|
63
|
+
def test_run_correlations() -> None:
|
|
63
64
|
q0, q1 = cirq.LineQubit.range(2)
|
|
64
65
|
simulator = cirq.CliffordSimulator()
|
|
65
66
|
circuit = cirq.Circuit(cirq.H(q0), cirq.CNOT(q0, q1), cirq.measure(q0, q1))
|
|
@@ -69,7 +70,7 @@ def test_run_correlations():
|
|
|
69
70
|
assert bits[0] == bits[1]
|
|
70
71
|
|
|
71
72
|
|
|
72
|
-
def test_run_parameters_not_resolved():
|
|
73
|
+
def test_run_parameters_not_resolved() -> None:
|
|
73
74
|
a = cirq.LineQubit(0)
|
|
74
75
|
simulator = cirq.CliffordSimulator()
|
|
75
76
|
circuit = cirq.Circuit(cirq.XPowGate(exponent=sympy.Symbol('a'))(a), cirq.measure(a))
|
|
@@ -77,7 +78,7 @@ def test_run_parameters_not_resolved():
|
|
|
77
78
|
_ = simulator.run_sweep(circuit, cirq.ParamResolver({}))
|
|
78
79
|
|
|
79
80
|
|
|
80
|
-
def test_simulate_parameters_not_resolved():
|
|
81
|
+
def test_simulate_parameters_not_resolved() -> None:
|
|
81
82
|
a = cirq.LineQubit(0)
|
|
82
83
|
simulator = cirq.CliffordSimulator()
|
|
83
84
|
circuit = cirq.Circuit(cirq.XPowGate(exponent=sympy.Symbol('a'))(a), cirq.measure(a))
|
|
@@ -85,7 +86,7 @@ def test_simulate_parameters_not_resolved():
|
|
|
85
86
|
_ = simulator.simulate_sweep(circuit, cirq.ParamResolver({}))
|
|
86
87
|
|
|
87
88
|
|
|
88
|
-
def test_simulate():
|
|
89
|
+
def test_simulate() -> None:
|
|
89
90
|
q0, q1 = cirq.LineQubit.range(2)
|
|
90
91
|
simulator = cirq.CliffordSimulator()
|
|
91
92
|
circuit = cirq.Circuit(cirq.H(q0), cirq.H(q1))
|
|
@@ -94,7 +95,7 @@ def test_simulate():
|
|
|
94
95
|
assert len(result.measurements) == 0
|
|
95
96
|
|
|
96
97
|
|
|
97
|
-
def test_simulate_initial_state():
|
|
98
|
+
def test_simulate_initial_state() -> None:
|
|
98
99
|
q0, q1 = cirq.LineQubit.range(2)
|
|
99
100
|
simulator = cirq.CliffordSimulator()
|
|
100
101
|
for b0 in [0, 1]:
|
|
@@ -119,7 +120,7 @@ def test_simulate_initial_state():
|
|
|
119
120
|
)
|
|
120
121
|
|
|
121
122
|
|
|
122
|
-
def test_simulation_state():
|
|
123
|
+
def test_simulation_state() -> None:
|
|
123
124
|
q0, q1 = cirq.LineQubit.range(2)
|
|
124
125
|
simulator = cirq.CliffordSimulator()
|
|
125
126
|
for b0 in [0, 1]:
|
|
@@ -140,7 +141,7 @@ def test_simulation_state():
|
|
|
140
141
|
)
|
|
141
142
|
|
|
142
143
|
|
|
143
|
-
def test_simulate_qubit_order():
|
|
144
|
+
def test_simulate_qubit_order() -> None:
|
|
144
145
|
q0, q1 = cirq.LineQubit.range(2)
|
|
145
146
|
simulator = cirq.CliffordSimulator()
|
|
146
147
|
for b0 in [0, 1]:
|
|
@@ -160,7 +161,7 @@ def test_simulate_qubit_order():
|
|
|
160
161
|
)
|
|
161
162
|
|
|
162
163
|
|
|
163
|
-
def test_run_measure_multiple_qubits():
|
|
164
|
+
def test_run_measure_multiple_qubits() -> None:
|
|
164
165
|
q0, q1 = cirq.LineQubit.range(2)
|
|
165
166
|
simulator = cirq.CliffordSimulator()
|
|
166
167
|
for b0 in [0, 1]:
|
|
@@ -175,7 +176,7 @@ def test_run_measure_multiple_qubits():
|
|
|
175
176
|
np.testing.assert_equal(result.measurements, {'q(0),q(1)': [[b0, b1]] * 3})
|
|
176
177
|
|
|
177
178
|
|
|
178
|
-
def test_simulate_moment_steps():
|
|
179
|
+
def test_simulate_moment_steps() -> None:
|
|
179
180
|
q0, q1 = cirq.LineQubit.range(2)
|
|
180
181
|
circuit = cirq.Circuit(cirq.H(q0), cirq.H(q1), cirq.H(q0), cirq.H(q1))
|
|
181
182
|
simulator = cirq.CliffordSimulator()
|
|
@@ -186,7 +187,7 @@ def test_simulate_moment_steps():
|
|
|
186
187
|
np.testing.assert_almost_equal(step.state.to_numpy(), np.array([1, 0, 0, 0]))
|
|
187
188
|
|
|
188
189
|
|
|
189
|
-
def test_simulate_moment_steps_sample():
|
|
190
|
+
def test_simulate_moment_steps_sample() -> None:
|
|
190
191
|
q0, q1 = cirq.LineQubit.range(2)
|
|
191
192
|
circuit = cirq.Circuit(cirq.H(q0), cirq.CNOT(q0, q1))
|
|
192
193
|
simulator = cirq.CliffordSimulator()
|
|
@@ -206,7 +207,7 @@ def test_simulate_moment_steps_sample():
|
|
|
206
207
|
|
|
207
208
|
|
|
208
209
|
@pytest.mark.parametrize('split', [True, False])
|
|
209
|
-
def test_simulate_moment_steps_intermediate_measurement(split):
|
|
210
|
+
def test_simulate_moment_steps_intermediate_measurement(split) -> None:
|
|
210
211
|
q0 = cirq.LineQubit(0)
|
|
211
212
|
circuit = cirq.Circuit(cirq.H(q0), cirq.measure(q0), cirq.H(q0))
|
|
212
213
|
simulator = cirq.CliffordSimulator(split_untangled_states=split)
|
|
@@ -221,7 +222,7 @@ def test_simulate_moment_steps_intermediate_measurement(split):
|
|
|
221
222
|
np.testing.assert_almost_equal(step.state.to_numpy(), expected)
|
|
222
223
|
|
|
223
224
|
|
|
224
|
-
def test_clifford_state_initial_state():
|
|
225
|
+
def test_clifford_state_initial_state() -> None:
|
|
225
226
|
q0 = cirq.LineQubit(0)
|
|
226
227
|
with pytest.raises(ValueError, match='Out of range'):
|
|
227
228
|
_ = cirq.CliffordState(qubit_map={q0: 0}, initial_state=2)
|
|
@@ -231,7 +232,7 @@ def test_clifford_state_initial_state():
|
|
|
231
232
|
assert state.copy() == state
|
|
232
233
|
|
|
233
234
|
|
|
234
|
-
def test_clifford_trial_result_repr():
|
|
235
|
+
def test_clifford_trial_result_repr() -> None:
|
|
235
236
|
q0 = cirq.LineQubit(0)
|
|
236
237
|
final_simulator_state = cirq.StabilizerChFormSimulationState(qubits=[q0])
|
|
237
238
|
assert (
|
|
@@ -251,7 +252,7 @@ def test_clifford_trial_result_repr():
|
|
|
251
252
|
)
|
|
252
253
|
|
|
253
254
|
|
|
254
|
-
def test_clifford_trial_result_str():
|
|
255
|
+
def test_clifford_trial_result_str() -> None:
|
|
255
256
|
q0 = cirq.LineQubit(0)
|
|
256
257
|
final_simulator_state = cirq.StabilizerChFormSimulationState(qubits=[q0])
|
|
257
258
|
assert (
|
|
@@ -267,7 +268,7 @@ def test_clifford_trial_result_str():
|
|
|
267
268
|
)
|
|
268
269
|
|
|
269
270
|
|
|
270
|
-
def test_clifford_trial_result_repr_pretty():
|
|
271
|
+
def test_clifford_trial_result_repr_pretty() -> None:
|
|
271
272
|
q0 = cirq.LineQubit(0)
|
|
272
273
|
final_simulator_state = cirq.StabilizerChFormSimulationState(qubits=[q0])
|
|
273
274
|
result = cirq.CliffordTrialResult(
|
|
@@ -280,7 +281,7 @@ def test_clifford_trial_result_repr_pretty():
|
|
|
280
281
|
cirq.testing.assert_repr_pretty(result, "cirq.CliffordTrialResult(...)", cycle=True)
|
|
281
282
|
|
|
282
283
|
|
|
283
|
-
def test_clifford_step_result_str():
|
|
284
|
+
def test_clifford_step_result_str() -> None:
|
|
284
285
|
q0 = cirq.LineQubit(0)
|
|
285
286
|
result = next(
|
|
286
287
|
cirq.CliffordSimulator().simulate_moment_steps(cirq.Circuit(cirq.measure(q0, key='m')))
|
|
@@ -288,7 +289,7 @@ def test_clifford_step_result_str():
|
|
|
288
289
|
assert str(result) == "m=0\n|0⟩"
|
|
289
290
|
|
|
290
291
|
|
|
291
|
-
def test_clifford_step_result_repr_pretty():
|
|
292
|
+
def test_clifford_step_result_repr_pretty() -> None:
|
|
292
293
|
q0 = cirq.LineQubit(0)
|
|
293
294
|
result = next(
|
|
294
295
|
cirq.CliffordSimulator().simulate_moment_steps(cirq.Circuit(cirq.measure(q0, key='m')))
|
|
@@ -297,40 +298,40 @@ def test_clifford_step_result_repr_pretty():
|
|
|
297
298
|
cirq.testing.assert_repr_pretty(result, "cirq.CliffordSimulatorStateResult(...)", cycle=True)
|
|
298
299
|
|
|
299
300
|
|
|
300
|
-
def test_clifford_step_result_no_measurements_str():
|
|
301
|
+
def test_clifford_step_result_no_measurements_str() -> None:
|
|
301
302
|
q0 = cirq.LineQubit(0)
|
|
302
303
|
result = next(cirq.CliffordSimulator().simulate_moment_steps(cirq.Circuit(cirq.I(q0))))
|
|
303
304
|
assert str(result) == "|0⟩"
|
|
304
305
|
|
|
305
306
|
|
|
306
|
-
def test_clifford_state_str():
|
|
307
|
+
def test_clifford_state_str() -> None:
|
|
307
308
|
(q0, q1) = (cirq.LineQubit(0), cirq.LineQubit(1))
|
|
308
309
|
state = cirq.CliffordState(qubit_map={q0: 0, q1: 1})
|
|
309
310
|
|
|
310
311
|
assert str(state) == "|00⟩"
|
|
311
312
|
|
|
312
313
|
|
|
313
|
-
def test_clifford_state_state_vector():
|
|
314
|
+
def test_clifford_state_state_vector() -> None:
|
|
314
315
|
(q0, q1) = (cirq.LineQubit(0), cirq.LineQubit(1))
|
|
315
316
|
state = cirq.CliffordState(qubit_map={q0: 0, q1: 1})
|
|
316
317
|
|
|
317
318
|
np.testing.assert_equal(state.state_vector(), [1.0 + 0.0j, 0.0 + 0.0j, 0.0 + 0.0j, 0.0 + 0.0j])
|
|
318
319
|
|
|
319
320
|
|
|
320
|
-
def test_stabilizerStateChForm_H():
|
|
321
|
+
def test_stabilizerStateChForm_H() -> None:
|
|
321
322
|
(q0, q1) = (cirq.LineQubit(0), cirq.LineQubit(1))
|
|
322
323
|
state = cirq.CliffordState(qubit_map={q0: 0, q1: 1})
|
|
323
324
|
with pytest.raises(ValueError, match="|y> is equal to |z>"):
|
|
324
325
|
state.ch_form._H_decompose(0, 1, 1, 0)
|
|
325
326
|
|
|
326
327
|
|
|
327
|
-
def test_clifford_stabilizerStateChForm_repr():
|
|
328
|
+
def test_clifford_stabilizerStateChForm_repr() -> None:
|
|
328
329
|
(q0, q1) = (cirq.LineQubit(0), cirq.LineQubit(1))
|
|
329
330
|
state = cirq.CliffordState(qubit_map={q0: 0, q1: 1})
|
|
330
331
|
assert repr(state) == 'StabilizerStateChForm(num_qubits=2)'
|
|
331
332
|
|
|
332
333
|
|
|
333
|
-
def test_clifford_circuit_SHSYSHS():
|
|
334
|
+
def test_clifford_circuit_SHSYSHS() -> None:
|
|
334
335
|
q0 = cirq.LineQubit(0)
|
|
335
336
|
circuit = cirq.Circuit(
|
|
336
337
|
cirq.S(q0),
|
|
@@ -355,7 +356,7 @@ def test_clifford_circuit_SHSYSHS():
|
|
|
355
356
|
|
|
356
357
|
|
|
357
358
|
@pytest.mark.parametrize('split', [True, False])
|
|
358
|
-
def test_clifford_circuit(split):
|
|
359
|
+
def test_clifford_circuit(split) -> None:
|
|
359
360
|
(q0, q1) = (cirq.LineQubit(0), cirq.LineQubit(1))
|
|
360
361
|
circuit = cirq.Circuit()
|
|
361
362
|
|
|
@@ -363,15 +364,15 @@ def test_clifford_circuit(split):
|
|
|
363
364
|
x = np.random.randint(7)
|
|
364
365
|
|
|
365
366
|
if x == 0:
|
|
366
|
-
circuit.append(cirq.X(
|
|
367
|
+
circuit.append(cirq.X(random.choice((q0, q1))))
|
|
367
368
|
elif x == 1:
|
|
368
|
-
circuit.append(cirq.Z(
|
|
369
|
+
circuit.append(cirq.Z(random.choice((q0, q1))))
|
|
369
370
|
elif x == 2:
|
|
370
|
-
circuit.append(cirq.Y(
|
|
371
|
+
circuit.append(cirq.Y(random.choice((q0, q1))))
|
|
371
372
|
elif x == 3:
|
|
372
|
-
circuit.append(cirq.S(
|
|
373
|
+
circuit.append(cirq.S(random.choice((q0, q1))))
|
|
373
374
|
elif x == 4:
|
|
374
|
-
circuit.append(cirq.H(
|
|
375
|
+
circuit.append(cirq.H(random.choice((q0, q1))))
|
|
375
376
|
elif x == 5:
|
|
376
377
|
circuit.append(cirq.CNOT(q0, q1))
|
|
377
378
|
elif x == 6:
|
|
@@ -388,7 +389,7 @@ def test_clifford_circuit(split):
|
|
|
388
389
|
|
|
389
390
|
@pytest.mark.parametrize("qubits", [cirq.LineQubit.range(2), cirq.LineQubit.range(4)])
|
|
390
391
|
@pytest.mark.parametrize('split', [True, False])
|
|
391
|
-
def test_clifford_circuit_2(qubits, split):
|
|
392
|
+
def test_clifford_circuit_2(qubits, split) -> None:
|
|
392
393
|
circuit = cirq.Circuit()
|
|
393
394
|
|
|
394
395
|
np.random.seed(2)
|
|
@@ -419,7 +420,7 @@ def test_clifford_circuit_2(qubits, split):
|
|
|
419
420
|
|
|
420
421
|
|
|
421
422
|
@pytest.mark.parametrize('split', [True, False])
|
|
422
|
-
def test_clifford_circuit_3(split):
|
|
423
|
+
def test_clifford_circuit_3(split) -> None:
|
|
423
424
|
# This test tests the simulator on arbitrary 1-qubit Clifford gates.
|
|
424
425
|
(q0, q1) = (cirq.LineQubit(0), cirq.LineQubit(1))
|
|
425
426
|
circuit = cirq.Circuit()
|
|
@@ -435,7 +436,7 @@ def test_clifford_circuit_3(split):
|
|
|
435
436
|
if np.random.randint(5) == 0:
|
|
436
437
|
circuit.append(cirq.CNOT(q0, q1))
|
|
437
438
|
else:
|
|
438
|
-
circuit.append(random_clifford_gate()(
|
|
439
|
+
circuit.append(random_clifford_gate()(random.choice((q0, q1))))
|
|
439
440
|
|
|
440
441
|
clifford_simulator = cirq.CliffordSimulator(split_untangled_states=split)
|
|
441
442
|
state_vector_simulator = cirq.Simulator()
|
|
@@ -447,7 +448,7 @@ def test_clifford_circuit_3(split):
|
|
|
447
448
|
)
|
|
448
449
|
|
|
449
450
|
|
|
450
|
-
def test_non_clifford_circuit():
|
|
451
|
+
def test_non_clifford_circuit() -> None:
|
|
451
452
|
q0 = cirq.LineQubit(0)
|
|
452
453
|
circuit = cirq.Circuit()
|
|
453
454
|
circuit.append(cirq.T(q0))
|
|
@@ -455,7 +456,7 @@ def test_non_clifford_circuit():
|
|
|
455
456
|
cirq.CliffordSimulator().simulate(circuit)
|
|
456
457
|
|
|
457
458
|
|
|
458
|
-
def test_swap():
|
|
459
|
+
def test_swap() -> None:
|
|
459
460
|
a, b = cirq.LineQubit.range(2)
|
|
460
461
|
circuit = cirq.Circuit(
|
|
461
462
|
cirq.X(a),
|
|
@@ -472,7 +473,7 @@ def test_swap():
|
|
|
472
473
|
cirq.CliffordSimulator().simulate((cirq.Circuit(cirq.SWAP(a, b) ** 3.5)))
|
|
473
474
|
|
|
474
475
|
|
|
475
|
-
def test_sample_seed():
|
|
476
|
+
def test_sample_seed() -> None:
|
|
476
477
|
q = cirq.NamedQubit('q')
|
|
477
478
|
circuit = cirq.Circuit(cirq.H(q), cirq.measure(q))
|
|
478
479
|
simulator = cirq.CliffordSimulator(seed=1234)
|
|
@@ -482,7 +483,7 @@ def test_sample_seed():
|
|
|
482
483
|
assert result_string == '11010001111100100000'
|
|
483
484
|
|
|
484
485
|
|
|
485
|
-
def test_is_supported_operation():
|
|
486
|
+
def test_is_supported_operation() -> None:
|
|
486
487
|
class MultiQubitOp(cirq.Operation):
|
|
487
488
|
"""Multi-qubit operation with unitary.
|
|
488
489
|
|
|
@@ -514,7 +515,7 @@ def test_is_supported_operation():
|
|
|
514
515
|
assert not cirq.CliffordSimulator.is_supported_operation(MultiQubitOp())
|
|
515
516
|
|
|
516
517
|
|
|
517
|
-
def test_simulate_pauli_string():
|
|
518
|
+
def test_simulate_pauli_string() -> None:
|
|
518
519
|
q = cirq.NamedQubit('q')
|
|
519
520
|
circuit = cirq.Circuit([cirq.PauliString({q: 'X'}), cirq.PauliString({q: 'Z'})])
|
|
520
521
|
simulator = cirq.CliffordSimulator()
|
|
@@ -524,7 +525,7 @@ def test_simulate_pauli_string():
|
|
|
524
525
|
assert np.allclose(result, [0, -1])
|
|
525
526
|
|
|
526
527
|
|
|
527
|
-
def test_simulate_global_phase_operation():
|
|
528
|
+
def test_simulate_global_phase_operation() -> None:
|
|
528
529
|
q1, q2 = cirq.LineQubit.range(2)
|
|
529
530
|
circuit = cirq.Circuit([cirq.I(q1), cirq.I(q2), cirq.global_phase_operation(-1j)])
|
|
530
531
|
simulator = cirq.CliffordSimulator()
|
|
@@ -534,7 +535,7 @@ def test_simulate_global_phase_operation():
|
|
|
534
535
|
assert np.allclose(result, [-1j, 0, 0, 0])
|
|
535
536
|
|
|
536
537
|
|
|
537
|
-
def test_json_roundtrip():
|
|
538
|
+
def test_json_roundtrip() -> None:
|
|
538
539
|
(q0, q1, q2) = (cirq.LineQubit(0), cirq.LineQubit(1), cirq.LineQubit(2))
|
|
539
540
|
state = cirq.CliffordState(qubit_map={q0: 0, q1: 1, q2: 2})
|
|
540
541
|
|
|
@@ -557,19 +558,19 @@ def test_json_roundtrip():
|
|
|
557
558
|
assert np.allclose(state.ch_form.state_vector(), state_roundtrip.ch_form.state_vector())
|
|
558
559
|
|
|
559
560
|
|
|
560
|
-
def test_invalid_apply_measurement():
|
|
561
|
+
def test_invalid_apply_measurement() -> None:
|
|
561
562
|
q0 = cirq.LineQubit(0)
|
|
562
563
|
state = cirq.CliffordState(qubit_map={q0: 0})
|
|
563
|
-
measurements = {}
|
|
564
|
+
measurements: dict[str, list[int]] = {}
|
|
564
565
|
with pytest.raises(TypeError, match='only supports cirq.MeasurementGate'):
|
|
565
566
|
state.apply_measurement(cirq.H(q0), measurements, np.random.RandomState())
|
|
566
567
|
assert measurements == {}
|
|
567
568
|
|
|
568
569
|
|
|
569
|
-
def test_valid_apply_measurement():
|
|
570
|
+
def test_valid_apply_measurement() -> None:
|
|
570
571
|
q0 = cirq.LineQubit(0)
|
|
571
572
|
state = cirq.CliffordState(qubit_map={q0: 0}, initial_state=1)
|
|
572
|
-
measurements = {}
|
|
573
|
+
measurements: dict[str, list[int]] = {}
|
|
573
574
|
state.apply_measurement(
|
|
574
575
|
cirq.measure(q0), measurements, np.random.RandomState(), collapse_state_vector=False
|
|
575
576
|
)
|
|
@@ -579,7 +580,7 @@ def test_valid_apply_measurement():
|
|
|
579
580
|
|
|
580
581
|
|
|
581
582
|
@pytest.mark.parametrize('split', [True, False])
|
|
582
|
-
def test_reset(split):
|
|
583
|
+
def test_reset(split) -> None:
|
|
583
584
|
q = cirq.LineQubit(0)
|
|
584
585
|
c = cirq.Circuit(cirq.X(q), cirq.reset(q), cirq.measure(q, key="out"))
|
|
585
586
|
sim = cirq.CliffordSimulator(split_untangled_states=split)
|
|
@@ -590,7 +591,7 @@ def test_reset(split):
|
|
|
590
591
|
assert sim.sample(c)["out"][0] == 0
|
|
591
592
|
|
|
592
593
|
|
|
593
|
-
def test_state_copy():
|
|
594
|
+
def test_state_copy() -> None:
|
|
594
595
|
sim = cirq.CliffordSimulator()
|
|
595
596
|
|
|
596
597
|
q = cirq.LineQubit(0)
|
|
@@ -150,7 +150,7 @@ class StabilizerStateChForm(qis.StabilizerState):
|
|
|
150
150
|
self.F[:, r] ^= self.F[:, q]
|
|
151
151
|
self.M[:, q] ^= self.M[:, r]
|
|
152
152
|
|
|
153
|
-
def update_sum(self, t, u, delta=0, alpha=0):
|
|
153
|
+
def update_sum(self, t, u, delta=0, alpha=0) -> None:
|
|
154
154
|
"""Implements the transformation (Proposition 4 in Bravyi et al)
|
|
155
155
|
|
|
156
156
|
``i^alpha U_H (|t> + i^delta |u>) = omega W_C W_H |s'>``
|
|
@@ -255,7 +255,7 @@ class StabilizerStateChForm(qis.StabilizerState):
|
|
|
255
255
|
self.project_Z(q, x_i)
|
|
256
256
|
return x_i
|
|
257
257
|
|
|
258
|
-
def project_Z(self, q, z):
|
|
258
|
+
def project_Z(self, q, z) -> None:
|
|
259
259
|
"""Applies a Z projector on the q'th qubit.
|
|
260
260
|
|
|
261
261
|
Returns: a normalized state with Z_q |psi> = z |psi>
|
|
@@ -295,7 +295,7 @@ class StabilizerStateChForm(qis.StabilizerState):
|
|
|
295
295
|
copy.omega = self.omega
|
|
296
296
|
return copy
|
|
297
297
|
|
|
298
|
-
def apply_x(self, axis: int, exponent: float = 1, global_shift: float = 0):
|
|
298
|
+
def apply_x(self, axis: int, exponent: float = 1, global_shift: float = 0) -> None:
|
|
299
299
|
if exponent % 2 != 0:
|
|
300
300
|
if exponent % 0.5 != 0.0:
|
|
301
301
|
raise ValueError('X exponent must be half integer') # pragma: no cover
|
|
@@ -304,7 +304,7 @@ class StabilizerStateChForm(qis.StabilizerState):
|
|
|
304
304
|
self.apply_h(axis)
|
|
305
305
|
self.omega *= _phase(exponent, global_shift)
|
|
306
306
|
|
|
307
|
-
def apply_y(self, axis: int, exponent: float = 1, global_shift: float = 0):
|
|
307
|
+
def apply_y(self, axis: int, exponent: float = 1, global_shift: float = 0) -> None:
|
|
308
308
|
if exponent % 0.5 != 0.0:
|
|
309
309
|
raise ValueError('Y exponent must be half integer') # pragma: no cover
|
|
310
310
|
shift = _phase(exponent, global_shift)
|
|
@@ -325,7 +325,7 @@ class StabilizerStateChForm(qis.StabilizerState):
|
|
|
325
325
|
self.apply_z(axis)
|
|
326
326
|
self.omega *= shift * (1 - 1j) / (2**0.5)
|
|
327
327
|
|
|
328
|
-
def apply_z(self, axis: int, exponent: float = 1, global_shift: float = 0):
|
|
328
|
+
def apply_z(self, axis: int, exponent: float = 1, global_shift: float = 0) -> None:
|
|
329
329
|
if exponent % 2 != 0:
|
|
330
330
|
if exponent % 0.5 != 0.0:
|
|
331
331
|
raise ValueError('Z exponent must be half integer') # pragma: no cover
|
|
@@ -337,7 +337,7 @@ class StabilizerStateChForm(qis.StabilizerState):
|
|
|
337
337
|
self.gamma[axis] = (self.gamma[axis] - 1) % 4
|
|
338
338
|
self.omega *= _phase(exponent, global_shift)
|
|
339
339
|
|
|
340
|
-
def apply_h(self, axis: int, exponent: float = 1, global_shift: float = 0):
|
|
340
|
+
def apply_h(self, axis: int, exponent: float = 1, global_shift: float = 0) -> None:
|
|
341
341
|
if exponent % 2 != 0:
|
|
342
342
|
if exponent % 1 != 0:
|
|
343
343
|
raise ValueError('H exponent must be integer') # pragma: no cover
|
|
@@ -357,7 +357,7 @@ class StabilizerStateChForm(qis.StabilizerState):
|
|
|
357
357
|
|
|
358
358
|
def apply_cz(
|
|
359
359
|
self, control_axis: int, target_axis: int, exponent: float = 1, global_shift: float = 0
|
|
360
|
-
):
|
|
360
|
+
) -> None:
|
|
361
361
|
if exponent % 2 != 0:
|
|
362
362
|
if exponent % 1 != 0:
|
|
363
363
|
raise ValueError('CZ exponent must be integer') # pragma: no cover
|
|
@@ -369,7 +369,7 @@ class StabilizerStateChForm(qis.StabilizerState):
|
|
|
369
369
|
|
|
370
370
|
def apply_cx(
|
|
371
371
|
self, control_axis: int, target_axis: int, exponent: float = 1, global_shift: float = 0
|
|
372
|
-
):
|
|
372
|
+
) -> None:
|
|
373
373
|
if exponent % 2 != 0:
|
|
374
374
|
if exponent % 1 != 0:
|
|
375
375
|
raise ValueError('CX exponent must be integer') # pragma: no cover
|
|
@@ -385,7 +385,7 @@ class StabilizerStateChForm(qis.StabilizerState):
|
|
|
385
385
|
self.M[control_axis, :] ^= self.M[target_axis, :]
|
|
386
386
|
self.omega *= _phase(exponent, global_shift)
|
|
387
387
|
|
|
388
|
-
def apply_global_phase(self, coefficient: value.Scalar):
|
|
388
|
+
def apply_global_phase(self, coefficient: value.Scalar) -> None:
|
|
389
389
|
self.omega *= coefficient
|
|
390
390
|
|
|
391
391
|
def measure(
|
|
@@ -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
|
|
|
@@ -284,7 +284,7 @@ class DensityMatrixSimulationState(SimulationState[_BufferedDensityMatrix]):
|
|
|
284
284
|
)
|
|
285
285
|
super().__init__(state=state, prng=prng, qubits=qubits, classical_data=classical_data)
|
|
286
286
|
|
|
287
|
-
def add_qubits(self, qubits: Sequence[cirq.Qid]):
|
|
287
|
+
def add_qubits(self, qubits: Sequence[cirq.Qid]) -> Self:
|
|
288
288
|
ret = super().add_qubits(qubits)
|
|
289
289
|
return (
|
|
290
290
|
self.kronecker_product(type(self)(qubits=qubits), inplace=True)
|
|
@@ -292,7 +292,7 @@ class DensityMatrixSimulationState(SimulationState[_BufferedDensityMatrix]):
|
|
|
292
292
|
else ret
|
|
293
293
|
)
|
|
294
294
|
|
|
295
|
-
def remove_qubits(self, qubits: Sequence[cirq.Qid]):
|
|
295
|
+
def remove_qubits(self, qubits: Sequence[cirq.Qid]) -> Self:
|
|
296
296
|
ret = super().remove_qubits(qubits)
|
|
297
297
|
if ret is not NotImplemented:
|
|
298
298
|
return ret
|
|
@@ -332,15 +332,15 @@ class DensityMatrixSimulationState(SimulationState[_BufferedDensityMatrix]):
|
|
|
332
332
|
)
|
|
333
333
|
|
|
334
334
|
@property
|
|
335
|
-
def target_tensor(self):
|
|
335
|
+
def target_tensor(self) -> np.ndarray:
|
|
336
336
|
return self._state._density_matrix
|
|
337
337
|
|
|
338
338
|
@property
|
|
339
|
-
def available_buffer(self):
|
|
339
|
+
def available_buffer(self) -> list[np.ndarray]:
|
|
340
340
|
return self._state._buffer
|
|
341
341
|
|
|
342
342
|
@property
|
|
343
|
-
def qid_shape(self):
|
|
343
|
+
def qid_shape(self) -> tuple[int, ...]:
|
|
344
344
|
return self._state._qid_shape
|
|
345
345
|
|
|
346
346
|
|
|
@@ -258,7 +258,7 @@ class DensityMatrixStepResult(simulator_base.StepResultBase['cirq.DensityMatrixS
|
|
|
258
258
|
self._dtype = dtype
|
|
259
259
|
self._density_matrix: np.ndarray | None = None
|
|
260
260
|
|
|
261
|
-
def density_matrix(self, copy=True):
|
|
261
|
+
def density_matrix(self, copy=True) -> np.ndarray:
|
|
262
262
|
"""Returns the density matrix at this step in the simulation.
|
|
263
263
|
|
|
264
264
|
The density matrix that is stored in this result is returned in the
|