cirq-core 1.7.0.dev20250818230756__py3-none-any.whl → 1.7.0.dev20250820205919__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/experiments/z_phase_calibration_test.py +8 -7
- cirq/interop/quirk/cells/arithmetic_cells.py +1 -1
- cirq/interop/quirk/cells/cell_test.py +5 -5
- cirq/interop/quirk/cells/composite_cell_test.py +5 -3
- cirq/interop/quirk/cells/control_cells.py +2 -2
- cirq/interop/quirk/cells/input_cells.py +3 -3
- cirq/interop/quirk/cells/input_rotation_cells.py +2 -2
- cirq/interop/quirk/cells/swap_cell.py +2 -2
- cirq/interop/quirk/cells/testing.py +1 -1
- cirq/interop/quirk/url_to_circuit.py +2 -2
- cirq/interop/quirk/url_to_circuit_test.py +11 -11
- cirq/linalg/decompositions.py +1 -1
- cirq/linalg/decompositions_test.py +52 -50
- cirq/linalg/diagonalize_test.py +2 -2
- cirq/linalg/predicates.py +1 -1
- cirq/linalg/predicates_test.py +24 -24
- cirq/linalg/transformations.py +6 -4
- cirq/neutral_atoms/neutral_atom_devices.py +1 -1
- cirq/ops/classically_controlled_operation.py +2 -2
- cirq/ops/clifford_gate.py +1 -1
- cirq/ops/common_channels_test.py +1 -1
- cirq/ops/common_gates.py +2 -2
- cirq/ops/controlled_operation.py +2 -2
- cirq/ops/controlled_operation_test.py +17 -16
- cirq/ops/dense_pauli_string_test.py +41 -37
- cirq/ops/eigen_gate_test.py +0 -7
- cirq/ops/gateset.py +3 -3
- cirq/ops/gateset_test.py +23 -23
- cirq/ops/linear_combinations.py +1 -1
- cirq/ops/linear_combinations_test.py +103 -89
- cirq/ops/matrix_gates_test.py +31 -31
- cirq/ops/measure_util_test.py +11 -10
- cirq/ops/measurement_gate_test.py +23 -22
- cirq/ops/pauli_gates.py +1 -1
- cirq/ops/pauli_measurement_gate_test.py +12 -12
- cirq/ops/pauli_string_phasor_test.py +45 -40
- cirq/ops/pauli_string_raw_types_test.py +5 -4
- cirq/ops/pauli_string_test.py +190 -118
- cirq/ops/permutation_gate.py +1 -1
- cirq/ops/raw_types.py +3 -4
- cirq/ops/raw_types_test.py +1 -1
- cirq/ops/three_qubit_gates.py +3 -3
- cirq/protocols/act_on_protocol.py +1 -1
- cirq/protocols/act_on_protocol_test.py +4 -4
- cirq/protocols/apply_channel_protocol_test.py +19 -18
- cirq/protocols/apply_mixture_protocol_test.py +20 -19
- cirq/protocols/apply_unitary_protocol_test.py +31 -23
- cirq/protocols/approximate_equality_protocol_test.py +22 -16
- cirq/protocols/circuit_diagram_info_protocol.py +4 -4
- cirq/protocols/commutes_protocol_test.py +4 -4
- {cirq_core-1.7.0.dev20250818230756.dist-info → cirq_core-1.7.0.dev20250820205919.dist-info}/METADATA +1 -1
- {cirq_core-1.7.0.dev20250818230756.dist-info → cirq_core-1.7.0.dev20250820205919.dist-info}/RECORD +57 -57
- {cirq_core-1.7.0.dev20250818230756.dist-info → cirq_core-1.7.0.dev20250820205919.dist-info}/WHEEL +0 -0
- {cirq_core-1.7.0.dev20250818230756.dist-info → cirq_core-1.7.0.dev20250820205919.dist-info}/licenses/LICENSE +0 -0
- {cirq_core-1.7.0.dev20250818230756.dist-info → cirq_core-1.7.0.dev20250820205919.dist-info}/top_level.txt +0 -0
cirq/ops/permutation_gate.py
CHANGED
cirq/ops/raw_types.py
CHANGED
|
@@ -33,7 +33,6 @@ from typing import (
|
|
|
33
33
|
)
|
|
34
34
|
|
|
35
35
|
import numpy as np
|
|
36
|
-
from typing_extensions import Self
|
|
37
36
|
|
|
38
37
|
from cirq import protocols, value
|
|
39
38
|
from cirq._compat import __cirq_debug__, _method_cache_name, cached_method
|
|
@@ -521,7 +520,7 @@ class Operation(metaclass=abc.ABCMeta):
|
|
|
521
520
|
return protocols.qid_shape(self.qubits)
|
|
522
521
|
|
|
523
522
|
@abc.abstractmethod
|
|
524
|
-
def with_qubits(self, *new_qubits: cirq.Qid) ->
|
|
523
|
+
def with_qubits(self, *new_qubits: cirq.Qid) -> cirq.Operation:
|
|
525
524
|
"""Returns the same operation, but applied to different qubits.
|
|
526
525
|
|
|
527
526
|
Args:
|
|
@@ -567,7 +566,7 @@ class Operation(metaclass=abc.ABCMeta):
|
|
|
567
566
|
|
|
568
567
|
def transform_qubits(
|
|
569
568
|
self, qubit_map: dict[cirq.Qid, cirq.Qid] | Callable[[cirq.Qid], cirq.Qid]
|
|
570
|
-
) ->
|
|
569
|
+
) -> cirq.Operation:
|
|
571
570
|
"""Returns the same operation, but with different qubits.
|
|
572
571
|
|
|
573
572
|
This function will return a new operation with the same gate but
|
|
@@ -769,7 +768,7 @@ class TaggedOperation(Operation):
|
|
|
769
768
|
def gate(self) -> cirq.Gate | None:
|
|
770
769
|
return self.sub_operation.gate
|
|
771
770
|
|
|
772
|
-
def with_qubits(self, *new_qubits: cirq.Qid):
|
|
771
|
+
def with_qubits(self, *new_qubits: cirq.Qid) -> TaggedOperation:
|
|
773
772
|
return TaggedOperation(self.sub_operation.with_qubits(*new_qubits), *self._tags)
|
|
774
773
|
|
|
775
774
|
def _with_measurement_key_mapping_(self, key_map: Mapping[str, str]):
|
cirq/ops/raw_types_test.py
CHANGED
cirq/ops/three_qubit_gates.py
CHANGED
|
@@ -423,8 +423,8 @@ class CCXPowGate(gate_features.InterchangeableQubitsGate, eigen_gate.EigenGate):
|
|
|
423
423
|
}
|
|
424
424
|
)
|
|
425
425
|
|
|
426
|
-
def qubit_index_to_equivalence_group_key(self, index):
|
|
427
|
-
return index < 2
|
|
426
|
+
def qubit_index_to_equivalence_group_key(self, index) -> int:
|
|
427
|
+
return 1 if index < 2 else 0
|
|
428
428
|
|
|
429
429
|
def _apply_unitary_(self, args: protocols.ApplyUnitaryArgs) -> np.ndarray:
|
|
430
430
|
if protocols.is_parameterized(self):
|
|
@@ -508,7 +508,7 @@ class CCXPowGate(gate_features.InterchangeableQubitsGate, eigen_gate.EigenGate):
|
|
|
508
508
|
class CSwapGate(gate_features.InterchangeableQubitsGate, raw_types.Gate):
|
|
509
509
|
"""A controlled swap gate. The Fredkin gate."""
|
|
510
510
|
|
|
511
|
-
def qubit_index_to_equivalence_group_key(self, index):
|
|
511
|
+
def qubit_index_to_equivalence_group_key(self, index) -> int:
|
|
512
512
|
return 0 if index == 0 else 1
|
|
513
513
|
|
|
514
514
|
def _pauli_expansion_(self) -> value.LinearDict[str]:
|
|
@@ -24,11 +24,11 @@ import cirq
|
|
|
24
24
|
|
|
25
25
|
|
|
26
26
|
class ExampleQuantumState(cirq.QuantumStateRepresentation):
|
|
27
|
-
def copy(self, deep_copy_buffers=True):
|
|
28
|
-
|
|
27
|
+
def copy(self, deep_copy_buffers=True) -> Self:
|
|
28
|
+
return self.__class__() # pragma: no cover
|
|
29
29
|
|
|
30
|
-
def measure(self, axes, seed=None):
|
|
31
|
-
|
|
30
|
+
def measure(self, axes, seed=None) -> list[int]:
|
|
31
|
+
return [] # pragma: no cover
|
|
32
32
|
|
|
33
33
|
|
|
34
34
|
class ExampleSimulationState(cirq.SimulationState):
|
|
@@ -20,7 +20,7 @@ import pytest
|
|
|
20
20
|
import cirq
|
|
21
21
|
|
|
22
22
|
|
|
23
|
-
def make_buffers(shape, dtype):
|
|
23
|
+
def make_buffers(shape, dtype) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
|
|
24
24
|
return (
|
|
25
25
|
np.empty(shape, dtype=dtype),
|
|
26
26
|
np.empty(shape, dtype=dtype),
|
|
@@ -28,7 +28,7 @@ def make_buffers(shape, dtype):
|
|
|
28
28
|
)
|
|
29
29
|
|
|
30
30
|
|
|
31
|
-
def apply_channel(val, rho, left_axes, right_axes, assert_result_is_out_buf=False):
|
|
31
|
+
def apply_channel(val, rho, left_axes, right_axes, assert_result_is_out_buf=False) -> np.ndarray:
|
|
32
32
|
out_buf, buf0, buf1 = make_buffers(rho.shape, rho.dtype)
|
|
33
33
|
result = cirq.apply_channel(
|
|
34
34
|
val,
|
|
@@ -48,7 +48,7 @@ def apply_channel(val, rho, left_axes, right_axes, assert_result_is_out_buf=Fals
|
|
|
48
48
|
return result
|
|
49
49
|
|
|
50
50
|
|
|
51
|
-
def test_apply_channel_bad_args():
|
|
51
|
+
def test_apply_channel_bad_args() -> None:
|
|
52
52
|
target = np.zeros((3,) + (1, 2, 3) + (3, 1, 2) + (3,))
|
|
53
53
|
with pytest.raises(ValueError, match='Invalid target_tensor shape'):
|
|
54
54
|
cirq.apply_channel(
|
|
@@ -77,7 +77,7 @@ def test_apply_channel_bad_args():
|
|
|
77
77
|
)
|
|
78
78
|
|
|
79
79
|
|
|
80
|
-
def test_apply_channel_simple():
|
|
80
|
+
def test_apply_channel_simple() -> None:
|
|
81
81
|
x = np.array([[0, 1], [1, 0]], dtype=np.complex128)
|
|
82
82
|
|
|
83
83
|
class HasApplyChannel:
|
|
@@ -108,7 +108,7 @@ def test_apply_channel_simple():
|
|
|
108
108
|
np.testing.assert_almost_equal(result, x)
|
|
109
109
|
|
|
110
110
|
|
|
111
|
-
def test_apply_channel_inline():
|
|
111
|
+
def test_apply_channel_inline() -> None:
|
|
112
112
|
x = np.array([[0, 1], [1, 0]], dtype=np.complex128)
|
|
113
113
|
|
|
114
114
|
class HasApplyChannel:
|
|
@@ -123,7 +123,7 @@ def test_apply_channel_inline():
|
|
|
123
123
|
np.testing.assert_almost_equal(result, x)
|
|
124
124
|
|
|
125
125
|
|
|
126
|
-
def test_apply_channel_returns_aux_buffer():
|
|
126
|
+
def test_apply_channel_returns_aux_buffer() -> None:
|
|
127
127
|
rho = np.array([[1, 0], [0, 0]], dtype=np.complex128)
|
|
128
128
|
|
|
129
129
|
class ReturnsAuxBuffer0:
|
|
@@ -141,7 +141,7 @@ def test_apply_channel_returns_aux_buffer():
|
|
|
141
141
|
_ = apply_channel(ReturnsAuxBuffer1(), rho, [0], [1])
|
|
142
142
|
|
|
143
143
|
|
|
144
|
-
def test_apply_channel_channel_fallback_simple():
|
|
144
|
+
def test_apply_channel_channel_fallback_simple() -> None:
|
|
145
145
|
x = np.array([[0, 1], [1, 0]], dtype=np.complex128)
|
|
146
146
|
|
|
147
147
|
class HasChannel:
|
|
@@ -153,7 +153,7 @@ def test_apply_channel_channel_fallback_simple():
|
|
|
153
153
|
np.testing.assert_almost_equal(result, x)
|
|
154
154
|
|
|
155
155
|
|
|
156
|
-
def test_apply_channel_channel_fallback_one_qubit_random_on_qubit():
|
|
156
|
+
def test_apply_channel_channel_fallback_one_qubit_random_on_qubit() -> None:
|
|
157
157
|
for _ in range(25):
|
|
158
158
|
state = cirq.testing.random_superposition(2)
|
|
159
159
|
rho = np.outer(np.conjugate(state), state)
|
|
@@ -171,7 +171,7 @@ def test_apply_channel_channel_fallback_one_qubit_random_on_qubit():
|
|
|
171
171
|
np.testing.assert_almost_equal(result, expected)
|
|
172
172
|
|
|
173
173
|
|
|
174
|
-
def test_apply_channel_channel_fallback_one_qubit_random_on_two_qubits():
|
|
174
|
+
def test_apply_channel_channel_fallback_one_qubit_random_on_two_qubits() -> None:
|
|
175
175
|
for _ in range(25):
|
|
176
176
|
state = cirq.testing.random_superposition(4)
|
|
177
177
|
rho = np.outer(np.conjugate(state), state)
|
|
@@ -193,7 +193,7 @@ def test_apply_channel_channel_fallback_one_qubit_random_on_two_qubits():
|
|
|
193
193
|
np.testing.assert_almost_equal(result, expected)
|
|
194
194
|
|
|
195
195
|
|
|
196
|
-
def test_apply_channel_channel_fallback_two_qubit_random():
|
|
196
|
+
def test_apply_channel_channel_fallback_two_qubit_random() -> None:
|
|
197
197
|
for _ in range(25):
|
|
198
198
|
state = cirq.testing.random_superposition(4)
|
|
199
199
|
rho = np.outer(np.conjugate(state), state)
|
|
@@ -214,7 +214,7 @@ def test_apply_channel_channel_fallback_two_qubit_random():
|
|
|
214
214
|
np.testing.assert_almost_equal(result, expected)
|
|
215
215
|
|
|
216
216
|
|
|
217
|
-
def test_apply_channel_no_protocols_implemented():
|
|
217
|
+
def test_apply_channel_no_protocols_implemented() -> None:
|
|
218
218
|
class NoProtocols:
|
|
219
219
|
pass
|
|
220
220
|
|
|
@@ -224,23 +224,24 @@ def test_apply_channel_no_protocols_implemented():
|
|
|
224
224
|
apply_channel(NoProtocols(), rho, left_axes=[1], right_axes=[1])
|
|
225
225
|
|
|
226
226
|
|
|
227
|
-
def test_apply_channel_no_protocols_implemented_default():
|
|
227
|
+
def test_apply_channel_no_protocols_implemented_default() -> None:
|
|
228
228
|
class NoProtocols:
|
|
229
229
|
pass
|
|
230
230
|
|
|
231
|
+
out_buffer, auxiliary_buffer0, auxiliary_buffer1 = make_buffers((2, 2), float)
|
|
231
232
|
args = cirq.ApplyChannelArgs(
|
|
232
233
|
target_tensor=np.eye(2),
|
|
233
234
|
left_axes=[0],
|
|
234
235
|
right_axes=[1],
|
|
235
|
-
out_buffer=
|
|
236
|
-
auxiliary_buffer0=
|
|
237
|
-
auxiliary_buffer1=
|
|
236
|
+
out_buffer=out_buffer,
|
|
237
|
+
auxiliary_buffer0=auxiliary_buffer0,
|
|
238
|
+
auxiliary_buffer1=auxiliary_buffer1,
|
|
238
239
|
)
|
|
239
240
|
result = cirq.apply_channel(NoProtocols(), args, 'cirq')
|
|
240
241
|
assert result == 'cirq'
|
|
241
242
|
|
|
242
243
|
|
|
243
|
-
def test_apply_channel_unitary():
|
|
244
|
+
def test_apply_channel_unitary() -> None:
|
|
244
245
|
m = np.diag([1, 1j])
|
|
245
246
|
|
|
246
247
|
shape = (2, 2, 2, 2)
|
|
@@ -263,7 +264,7 @@ def test_apply_channel_unitary():
|
|
|
263
264
|
)
|
|
264
265
|
|
|
265
266
|
|
|
266
|
-
def test_apply_channel_apply_unitary():
|
|
267
|
+
def test_apply_channel_apply_unitary() -> None:
|
|
267
268
|
shape = (2, 2, 2, 2)
|
|
268
269
|
rho = np.ones(shape, dtype=np.complex128)
|
|
269
270
|
|
|
@@ -291,7 +292,7 @@ def test_apply_channel_apply_unitary():
|
|
|
291
292
|
)
|
|
292
293
|
|
|
293
294
|
|
|
294
|
-
def test_apply_channel_apply_unitary_not_implemented():
|
|
295
|
+
def test_apply_channel_apply_unitary_not_implemented() -> None:
|
|
295
296
|
class ApplyUnitaryNotImplemented:
|
|
296
297
|
def _apply_unitary_(self, args: cirq.ApplyUnitaryArgs):
|
|
297
298
|
return NotImplemented
|
|
@@ -21,7 +21,7 @@ import pytest
|
|
|
21
21
|
import cirq
|
|
22
22
|
|
|
23
23
|
|
|
24
|
-
def make_buffers(shape, dtype):
|
|
24
|
+
def make_buffers(shape, dtype) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
|
|
25
25
|
return (
|
|
26
26
|
np.empty(shape, dtype=dtype),
|
|
27
27
|
np.empty(shape, dtype=dtype),
|
|
@@ -36,7 +36,7 @@ def assert_apply_mixture_returns(
|
|
|
36
36
|
right_axes: Iterable[int] | None,
|
|
37
37
|
assert_result_is_out_buf: bool = False,
|
|
38
38
|
expected_result: np.ndarray | None = None,
|
|
39
|
-
):
|
|
39
|
+
) -> None:
|
|
40
40
|
out_buf, buf0, buf1 = make_buffers(rho.shape, rho.dtype)
|
|
41
41
|
result = cirq.apply_mixture(
|
|
42
42
|
val,
|
|
@@ -58,7 +58,7 @@ def assert_apply_mixture_returns(
|
|
|
58
58
|
np.testing.assert_array_almost_equal(result, expected_result)
|
|
59
59
|
|
|
60
60
|
|
|
61
|
-
def test_apply_mixture_bad_args():
|
|
61
|
+
def test_apply_mixture_bad_args() -> None:
|
|
62
62
|
target = np.zeros((3,) + (1, 2, 3) + (3, 1, 2) + (3,))
|
|
63
63
|
with pytest.raises(ValueError, match='Invalid target_tensor shape'):
|
|
64
64
|
cirq.apply_mixture(
|
|
@@ -89,7 +89,7 @@ def test_apply_mixture_bad_args():
|
|
|
89
89
|
)
|
|
90
90
|
|
|
91
91
|
|
|
92
|
-
def test_apply_mixture_simple():
|
|
92
|
+
def test_apply_mixture_simple() -> None:
|
|
93
93
|
x = np.array([[0, 1], [1, 0]], dtype=np.complex128)
|
|
94
94
|
|
|
95
95
|
class HasApplyMixture:
|
|
@@ -121,7 +121,7 @@ def test_apply_mixture_simple():
|
|
|
121
121
|
)
|
|
122
122
|
|
|
123
123
|
|
|
124
|
-
def test_apply_mixture_inline():
|
|
124
|
+
def test_apply_mixture_inline() -> None:
|
|
125
125
|
x = np.array([[0, 1], [1, 0]], dtype=np.complex128)
|
|
126
126
|
|
|
127
127
|
class HasApplyMixture:
|
|
@@ -135,7 +135,7 @@ def test_apply_mixture_inline():
|
|
|
135
135
|
assert_apply_mixture_returns(HasApplyMixture(), rho, [0], [1], expected_result=x)
|
|
136
136
|
|
|
137
137
|
|
|
138
|
-
def test_apply_mixture_returns_aux_buffer():
|
|
138
|
+
def test_apply_mixture_returns_aux_buffer() -> None:
|
|
139
139
|
rho = np.array([[1, 0], [0, 0]], dtype=np.complex128)
|
|
140
140
|
|
|
141
141
|
class ReturnsAuxBuffer0:
|
|
@@ -153,7 +153,7 @@ def test_apply_mixture_returns_aux_buffer():
|
|
|
153
153
|
assert_apply_mixture_returns(ReturnsAuxBuffer1(), rho, [0], [1])
|
|
154
154
|
|
|
155
155
|
|
|
156
|
-
def test_apply_mixture_simple_state_vector():
|
|
156
|
+
def test_apply_mixture_simple_state_vector() -> None:
|
|
157
157
|
for _ in range(25):
|
|
158
158
|
state = cirq.testing.random_superposition(2)
|
|
159
159
|
u1 = cirq.testing.random_unitary(2)
|
|
@@ -172,7 +172,7 @@ def test_apply_mixture_simple_state_vector():
|
|
|
172
172
|
)
|
|
173
173
|
|
|
174
174
|
|
|
175
|
-
def test_apply_mixture_simple_split_fallback():
|
|
175
|
+
def test_apply_mixture_simple_split_fallback() -> None:
|
|
176
176
|
x = np.array([[0, 1], [1, 0]], dtype=np.complex128)
|
|
177
177
|
|
|
178
178
|
class HasMixture:
|
|
@@ -185,7 +185,7 @@ def test_apply_mixture_simple_split_fallback():
|
|
|
185
185
|
)
|
|
186
186
|
|
|
187
187
|
|
|
188
|
-
def test_apply_mixture_fallback_one_qubit_random_on_qubit():
|
|
188
|
+
def test_apply_mixture_fallback_one_qubit_random_on_qubit() -> None:
|
|
189
189
|
for _ in range(25):
|
|
190
190
|
state = cirq.testing.random_superposition(2)
|
|
191
191
|
rho = np.outer(np.conjugate(state), state)
|
|
@@ -203,7 +203,7 @@ def test_apply_mixture_fallback_one_qubit_random_on_qubit():
|
|
|
203
203
|
)
|
|
204
204
|
|
|
205
205
|
|
|
206
|
-
def test_apply_mixture_fallback_two_qubit_random():
|
|
206
|
+
def test_apply_mixture_fallback_two_qubit_random() -> None:
|
|
207
207
|
for _ in range(25):
|
|
208
208
|
state = cirq.testing.random_superposition(4)
|
|
209
209
|
rho = np.outer(np.conjugate(state), state)
|
|
@@ -229,7 +229,7 @@ def test_apply_mixture_fallback_two_qubit_random():
|
|
|
229
229
|
)
|
|
230
230
|
|
|
231
231
|
|
|
232
|
-
def test_apply_mixture_no_protocols_implemented():
|
|
232
|
+
def test_apply_mixture_no_protocols_implemented() -> None:
|
|
233
233
|
class NoProtocols:
|
|
234
234
|
pass
|
|
235
235
|
|
|
@@ -239,7 +239,7 @@ def test_apply_mixture_no_protocols_implemented():
|
|
|
239
239
|
assert_apply_mixture_returns(NoProtocols(), rho, left_axes=[1], right_axes=[1])
|
|
240
240
|
|
|
241
241
|
|
|
242
|
-
def test_apply_mixture_mixture_returns_not_implemented():
|
|
242
|
+
def test_apply_mixture_mixture_returns_not_implemented() -> None:
|
|
243
243
|
class NoMixture:
|
|
244
244
|
def _mixture_(self):
|
|
245
245
|
return NotImplemented
|
|
@@ -250,23 +250,24 @@ def test_apply_mixture_mixture_returns_not_implemented():
|
|
|
250
250
|
assert_apply_mixture_returns(NoMixture(), rho, left_axes=[1], right_axes=[1])
|
|
251
251
|
|
|
252
252
|
|
|
253
|
-
def test_apply_mixture_no_protocols_implemented_default():
|
|
253
|
+
def test_apply_mixture_no_protocols_implemented_default() -> None:
|
|
254
254
|
class NoProtocols:
|
|
255
255
|
pass
|
|
256
256
|
|
|
257
|
+
out_buffer, auxiliary_buffer0, auxiliary_buffer1 = make_buffers((2, 2), float)
|
|
257
258
|
args = cirq.ApplyMixtureArgs(
|
|
258
259
|
target_tensor=np.eye(2),
|
|
259
260
|
left_axes=[0],
|
|
260
261
|
right_axes=[1],
|
|
261
|
-
out_buffer=
|
|
262
|
-
auxiliary_buffer0=
|
|
263
|
-
auxiliary_buffer1=
|
|
262
|
+
out_buffer=out_buffer,
|
|
263
|
+
auxiliary_buffer0=auxiliary_buffer0,
|
|
264
|
+
auxiliary_buffer1=auxiliary_buffer1,
|
|
264
265
|
)
|
|
265
266
|
result = cirq.apply_mixture(NoProtocols(), args, default='cirq')
|
|
266
267
|
assert result == 'cirq'
|
|
267
268
|
|
|
268
269
|
|
|
269
|
-
def test_apply_mixture_unitary():
|
|
270
|
+
def test_apply_mixture_unitary() -> None:
|
|
270
271
|
m = np.diag([1, 1j])
|
|
271
272
|
|
|
272
273
|
shape = (2, 2, 2, 2)
|
|
@@ -291,7 +292,7 @@ def test_apply_mixture_unitary():
|
|
|
291
292
|
)
|
|
292
293
|
|
|
293
294
|
|
|
294
|
-
def test_apply_mixture_apply_unitary():
|
|
295
|
+
def test_apply_mixture_apply_unitary() -> None:
|
|
295
296
|
shape = (2, 2, 2, 2)
|
|
296
297
|
rho = np.ones(shape, dtype=np.complex128)
|
|
297
298
|
|
|
@@ -320,7 +321,7 @@ def test_apply_mixture_apply_unitary():
|
|
|
320
321
|
)
|
|
321
322
|
|
|
322
323
|
|
|
323
|
-
def test_apply_mixture_apply_unitary_not_implemented():
|
|
324
|
+
def test_apply_mixture_apply_unitary_not_implemented() -> None:
|
|
324
325
|
class ApplyUnitaryNotImplemented:
|
|
325
326
|
def _apply_unitary_(self, args: cirq.ApplyUnitaryArgs):
|
|
326
327
|
return NotImplemented
|
|
@@ -21,7 +21,7 @@ import cirq
|
|
|
21
21
|
from cirq.protocols.apply_unitary_protocol import _incorporate_result_into_target
|
|
22
22
|
|
|
23
23
|
|
|
24
|
-
def test_apply_unitary_presence_absence():
|
|
24
|
+
def test_apply_unitary_presence_absence() -> None:
|
|
25
25
|
m = np.diag([1, -1])
|
|
26
26
|
|
|
27
27
|
class NoUnitaryEffect:
|
|
@@ -102,7 +102,7 @@ def test_apply_unitary_presence_absence():
|
|
|
102
102
|
)
|
|
103
103
|
|
|
104
104
|
|
|
105
|
-
def test_apply_unitary_args_tensor_manipulation():
|
|
105
|
+
def test_apply_unitary_args_tensor_manipulation() -> None:
|
|
106
106
|
# All below are qubit swap operations with 1j global phase
|
|
107
107
|
|
|
108
108
|
class ModifyTargetTensor:
|
|
@@ -215,7 +215,7 @@ def test_apply_unitary_args_tensor_manipulation():
|
|
|
215
215
|
args.available_buffer[...] = 98
|
|
216
216
|
return ret
|
|
217
217
|
|
|
218
|
-
operations = [
|
|
218
|
+
operations: list[cirq.SupportsConsistentApplyUnitary] = [
|
|
219
219
|
ModifyTargetTensor(),
|
|
220
220
|
TransposeTargetTensor(),
|
|
221
221
|
ReshapeTargetTensor(),
|
|
@@ -266,7 +266,7 @@ def test_apply_unitary_args_tensor_manipulation():
|
|
|
266
266
|
assert_is_swap(op)
|
|
267
267
|
|
|
268
268
|
|
|
269
|
-
def test_big_endian_subspace_index():
|
|
269
|
+
def test_big_endian_subspace_index() -> None:
|
|
270
270
|
state = np.zeros(shape=(2, 3, 4, 5, 1, 6, 1, 1))
|
|
271
271
|
args = cirq.ApplyUnitaryArgs(state, np.empty_like(state), [1, 3])
|
|
272
272
|
s = slice(None)
|
|
@@ -274,12 +274,13 @@ def test_big_endian_subspace_index():
|
|
|
274
274
|
assert args.subspace_index(big_endian_bits_int=1) == (s, 0, s, 1, s, s, s, s)
|
|
275
275
|
|
|
276
276
|
|
|
277
|
-
def test_apply_unitaries():
|
|
277
|
+
def test_apply_unitaries() -> None:
|
|
278
278
|
a, b, c = cirq.LineQubit.range(3)
|
|
279
279
|
|
|
280
280
|
result = cirq.apply_unitaries(
|
|
281
281
|
unitary_values=[cirq.H(a), cirq.CNOT(a, b), cirq.H(c).controlled_by(b)], qubits=[a, b, c]
|
|
282
282
|
)
|
|
283
|
+
assert result is not None
|
|
283
284
|
np.testing.assert_allclose(
|
|
284
285
|
result.reshape(8), [np.sqrt(0.5), 0, 0, 0, 0, 0, 0.5, 0.5], atol=1e-8
|
|
285
286
|
)
|
|
@@ -288,6 +289,7 @@ def test_apply_unitaries():
|
|
|
288
289
|
result = cirq.apply_unitaries(
|
|
289
290
|
unitary_values=[cirq.H(a), cirq.CNOT(a, b), cirq.H(c).controlled_by(b)], qubits=[a, c, b]
|
|
290
291
|
)
|
|
292
|
+
assert result is not None
|
|
291
293
|
np.testing.assert_allclose(
|
|
292
294
|
result.reshape(8), [np.sqrt(0.5), 0, 0, 0, 0, 0.5, 0, 0.5], atol=1e-8
|
|
293
295
|
)
|
|
@@ -298,14 +300,17 @@ def test_apply_unitaries():
|
|
|
298
300
|
qubits=[a, b, c],
|
|
299
301
|
args=cirq.ApplyUnitaryArgs.default(num_qubits=3),
|
|
300
302
|
)
|
|
303
|
+
assert result is not None
|
|
301
304
|
np.testing.assert_allclose(
|
|
302
305
|
result.reshape(8), [np.sqrt(0.5), 0, 0, 0, 0, 0, 0.5, 0.5], atol=1e-8
|
|
303
306
|
)
|
|
304
307
|
|
|
305
308
|
# Empty.
|
|
306
309
|
result = cirq.apply_unitaries(unitary_values=[], qubits=[])
|
|
310
|
+
assert result is not None
|
|
307
311
|
np.testing.assert_allclose(result, [1])
|
|
308
312
|
result = cirq.apply_unitaries(unitary_values=[], qubits=[], default=None)
|
|
313
|
+
assert result is not None
|
|
309
314
|
np.testing.assert_allclose(result, [1])
|
|
310
315
|
|
|
311
316
|
# Non-unitary operation.
|
|
@@ -327,7 +332,7 @@ def test_apply_unitaries():
|
|
|
327
332
|
)
|
|
328
333
|
|
|
329
334
|
|
|
330
|
-
def test_apply_unitaries_mixed_qid_shapes():
|
|
335
|
+
def test_apply_unitaries_mixed_qid_shapes() -> None:
|
|
331
336
|
class PlusOneMod3Gate(cirq.testing.SingleQubitGate):
|
|
332
337
|
def _qid_shape_(self):
|
|
333
338
|
return (3,)
|
|
@@ -358,6 +363,7 @@ def test_apply_unitaries_mixed_qid_shapes():
|
|
|
358
363
|
],
|
|
359
364
|
qubits=[a, b],
|
|
360
365
|
)
|
|
366
|
+
assert result is not None
|
|
361
367
|
np.testing.assert_allclose(result.reshape(12), [1] + [0] * 11, atol=1e-8)
|
|
362
368
|
|
|
363
369
|
result = cirq.apply_unitaries(
|
|
@@ -377,6 +383,7 @@ def test_apply_unitaries_mixed_qid_shapes():
|
|
|
377
383
|
axes=(0, 1),
|
|
378
384
|
),
|
|
379
385
|
)
|
|
386
|
+
assert result is not None
|
|
380
387
|
np.testing.assert_allclose(result.reshape(12, 12), np.eye(12), atol=1e-8)
|
|
381
388
|
|
|
382
389
|
result = cirq.apply_unitaries(
|
|
@@ -403,6 +410,7 @@ def test_apply_unitaries_mixed_qid_shapes():
|
|
|
403
410
|
axes=(0, 1),
|
|
404
411
|
),
|
|
405
412
|
)
|
|
413
|
+
assert result is not None
|
|
406
414
|
np.testing.assert_allclose(
|
|
407
415
|
result.reshape(12, 12),
|
|
408
416
|
np.array(
|
|
@@ -426,7 +434,7 @@ def test_apply_unitaries_mixed_qid_shapes():
|
|
|
426
434
|
|
|
427
435
|
|
|
428
436
|
# fmt: off
|
|
429
|
-
def test_subspace_size_2():
|
|
437
|
+
def test_subspace_size_2() -> None:
|
|
430
438
|
result = cirq.apply_unitary(
|
|
431
439
|
unitary_value=cirq.X,
|
|
432
440
|
args=cirq.ApplyUnitaryArgs(
|
|
@@ -513,7 +521,7 @@ def test_subspace_size_2():
|
|
|
513
521
|
)
|
|
514
522
|
|
|
515
523
|
|
|
516
|
-
def test_subspaces_size_3():
|
|
524
|
+
def test_subspaces_size_3() -> None:
|
|
517
525
|
plus_one_mod_3_gate = cirq.XPowGate(dimension=3)
|
|
518
526
|
|
|
519
527
|
result = cirq.apply_unitary(
|
|
@@ -581,7 +589,7 @@ def test_subspaces_size_3():
|
|
|
581
589
|
)
|
|
582
590
|
|
|
583
591
|
|
|
584
|
-
def test_subspaces_size_1():
|
|
592
|
+
def test_subspaces_size_1() -> None:
|
|
585
593
|
phase_gate = cirq.MatrixGate(np.array([[1j]]))
|
|
586
594
|
|
|
587
595
|
result = cirq.apply_unitary(
|
|
@@ -646,7 +654,7 @@ def test_subspaces_size_1():
|
|
|
646
654
|
# fmt: on
|
|
647
655
|
|
|
648
656
|
|
|
649
|
-
def test_invalid_subspaces():
|
|
657
|
+
def test_invalid_subspaces() -> None:
|
|
650
658
|
with pytest.raises(ValueError, match='Subspace specified does not exist in axis'):
|
|
651
659
|
_ = cirq.ApplyUnitaryArgs(
|
|
652
660
|
target_tensor=cirq.eye_tensor((2,), dtype=np.complex64),
|
|
@@ -677,7 +685,7 @@ def test_invalid_subspaces():
|
|
|
677
685
|
)
|
|
678
686
|
|
|
679
687
|
|
|
680
|
-
def test_incorporate_result_not_view():
|
|
688
|
+
def test_incorporate_result_not_view() -> None:
|
|
681
689
|
tensor = np.zeros((2, 2))
|
|
682
690
|
tensor2 = np.zeros((2, 2))
|
|
683
691
|
buffer = np.empty_like(tensor)
|
|
@@ -687,12 +695,12 @@ def test_incorporate_result_not_view():
|
|
|
687
695
|
_incorporate_result_into_target(args, not_sub_args, tensor2)
|
|
688
696
|
|
|
689
697
|
|
|
690
|
-
def test_default_method_arguments():
|
|
698
|
+
def test_default_method_arguments() -> None:
|
|
691
699
|
with pytest.raises(TypeError, match='exactly one of'):
|
|
692
700
|
cirq.ApplyUnitaryArgs.default(1, qid_shape=(2,))
|
|
693
701
|
|
|
694
702
|
|
|
695
|
-
def test_apply_unitary_args_with_axes_transposed_to_start():
|
|
703
|
+
def test_apply_unitary_args_with_axes_transposed_to_start() -> None:
|
|
696
704
|
target = np.zeros((2, 3, 4, 5))
|
|
697
705
|
buffer = np.zeros((2, 3, 4, 5))
|
|
698
706
|
args = cirq.ApplyUnitaryArgs(target, buffer, [1, 3])
|
|
@@ -709,7 +717,8 @@ def test_apply_unitary_args_with_axes_transposed_to_start():
|
|
|
709
717
|
assert args.available_buffer[1, 2, 3, 4] == 2
|
|
710
718
|
|
|
711
719
|
|
|
712
|
-
def test_cast_to_complex():
|
|
720
|
+
def test_cast_to_complex() -> None:
|
|
721
|
+
y0: cirq.PauliString[cirq.LineQubit]
|
|
713
722
|
y0 = cirq.PauliString({cirq.LineQubit(0): cirq.Y})
|
|
714
723
|
state = 0.5 * np.eye(2)
|
|
715
724
|
args = cirq.ApplyUnitaryArgs(
|
|
@@ -724,7 +733,7 @@ def test_cast_to_complex():
|
|
|
724
733
|
|
|
725
734
|
|
|
726
735
|
class NotDecomposableGate(cirq.Gate):
|
|
727
|
-
def num_qubits(self):
|
|
736
|
+
def num_qubits(self) -> int:
|
|
728
737
|
return 1
|
|
729
738
|
|
|
730
739
|
|
|
@@ -734,7 +743,7 @@ class DecomposableGate(cirq.Gate):
|
|
|
734
743
|
self._sub_gate = sub_gate
|
|
735
744
|
self._allocate_ancilla = allocate_ancilla
|
|
736
745
|
|
|
737
|
-
def num_qubits(self):
|
|
746
|
+
def num_qubits(self) -> int:
|
|
738
747
|
return 1
|
|
739
748
|
|
|
740
749
|
def _decompose_(self, qubits):
|
|
@@ -743,17 +752,16 @@ class DecomposableGate(cirq.Gate):
|
|
|
743
752
|
yield self._sub_gate(qubits[0])
|
|
744
753
|
|
|
745
754
|
|
|
746
|
-
def test_strat_apply_unitary_from_decompose():
|
|
755
|
+
def test_strat_apply_unitary_from_decompose() -> None:
|
|
747
756
|
state = np.eye(2, dtype=np.complex128)
|
|
748
757
|
args = cirq.ApplyUnitaryArgs(
|
|
749
758
|
target_tensor=state, available_buffer=np.zeros_like(state), axes=(0,)
|
|
750
759
|
)
|
|
751
|
-
|
|
752
|
-
cirq.
|
|
753
|
-
[DecomposableGate(cirq.X, False)(cirq.LineQubit(0))], [cirq.LineQubit(0)], args
|
|
754
|
-
),
|
|
755
|
-
[[0, 1], [1, 0]],
|
|
760
|
+
result = cirq.apply_unitaries(
|
|
761
|
+
[DecomposableGate(cirq.X, False)(cirq.LineQubit(0))], [cirq.LineQubit(0)], args
|
|
756
762
|
)
|
|
763
|
+
assert result is not None
|
|
764
|
+
np.testing.assert_allclose(result, [[0, 1], [1, 0]])
|
|
757
765
|
|
|
758
766
|
with pytest.raises(TypeError):
|
|
759
767
|
_ = cirq.apply_unitaries(
|
|
@@ -763,7 +771,7 @@ def test_strat_apply_unitary_from_decompose():
|
|
|
763
771
|
)
|
|
764
772
|
|
|
765
773
|
|
|
766
|
-
def test_unitary_construction():
|
|
774
|
+
def test_unitary_construction() -> None:
|
|
767
775
|
with pytest.raises(TypeError):
|
|
768
776
|
_ = cirq.ApplyUnitaryArgs.for_unitary()
|
|
769
777
|
|