cirq-core 1.5.0.dev20250226204345__py3-none-any.whl → 1.5.0.dev20250226222923__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/ops/controlled_gate.py +32 -12
- cirq/ops/controlled_gate_test.py +57 -2
- cirq/ops/controlled_operation.py +1 -0
- cirq/ops/global_phase_op.py +3 -2
- cirq/ops/global_phase_op_test.py +1 -1
- {cirq_core-1.5.0.dev20250226204345.dist-info → cirq_core-1.5.0.dev20250226222923.dist-info}/METADATA +1 -1
- {cirq_core-1.5.0.dev20250226204345.dist-info → cirq_core-1.5.0.dev20250226222923.dist-info}/RECORD +12 -12
- {cirq_core-1.5.0.dev20250226204345.dist-info → cirq_core-1.5.0.dev20250226222923.dist-info}/LICENSE +0 -0
- {cirq_core-1.5.0.dev20250226204345.dist-info → cirq_core-1.5.0.dev20250226222923.dist-info}/WHEEL +0 -0
- {cirq_core-1.5.0.dev20250226204345.dist-info → cirq_core-1.5.0.dev20250226222923.dist-info}/top_level.txt +0 -0
cirq/_version.py
CHANGED
cirq/_version_test.py
CHANGED
cirq/ops/controlled_gate.py
CHANGED
|
@@ -30,11 +30,13 @@ import numpy as np
|
|
|
30
30
|
|
|
31
31
|
from cirq import protocols, value, _import
|
|
32
32
|
from cirq.ops import (
|
|
33
|
-
|
|
33
|
+
control_values as cv,
|
|
34
34
|
controlled_operation as cop,
|
|
35
|
-
|
|
35
|
+
diagonal_gate as dg,
|
|
36
|
+
global_phase_op as gp,
|
|
36
37
|
matrix_gates,
|
|
37
|
-
|
|
38
|
+
op_tree,
|
|
39
|
+
raw_types,
|
|
38
40
|
)
|
|
39
41
|
|
|
40
42
|
if TYPE_CHECKING:
|
|
@@ -157,13 +159,13 @@ class ControlledGate(raw_types.Gate):
|
|
|
157
159
|
def _decompose_with_context_(
|
|
158
160
|
self, qubits: Tuple['cirq.Qid', ...], context: Optional['cirq.DecompositionContext'] = None
|
|
159
161
|
) -> Union[None, NotImplementedType, 'cirq.OP_TREE']:
|
|
162
|
+
control_qubits = list(qubits[: self.num_controls()])
|
|
160
163
|
if (
|
|
161
164
|
protocols.has_unitary(self.sub_gate)
|
|
162
165
|
and protocols.num_qubits(self.sub_gate) == 1
|
|
163
166
|
and self._qid_shape_() == (2,) * len(self._qid_shape_())
|
|
164
167
|
and isinstance(self.control_values, cv.ProductOfSums)
|
|
165
168
|
):
|
|
166
|
-
control_qubits = list(qubits[: self.num_controls()])
|
|
167
169
|
invert_ops: List['cirq.Operation'] = []
|
|
168
170
|
for cvals, cqbit in zip(self.control_values, qubits[: self.num_controls()]):
|
|
169
171
|
if set(cvals) == {0}:
|
|
@@ -174,11 +176,20 @@ class ControlledGate(raw_types.Gate):
|
|
|
174
176
|
protocols.unitary(self.sub_gate), control_qubits, qubits[-1]
|
|
175
177
|
)
|
|
176
178
|
return invert_ops + decomposed_ops + invert_ops
|
|
177
|
-
|
|
179
|
+
if isinstance(self.sub_gate, gp.GlobalPhaseGate):
|
|
180
|
+
# A controlled global phase is a diagonal gate, where each active control value index
|
|
181
|
+
# is set equal to the phase angle.
|
|
182
|
+
shape = self.control_qid_shape
|
|
183
|
+
if protocols.is_parameterized(self.sub_gate) or set(shape) != {2}:
|
|
184
|
+
# Could work in theory, but DiagonalGate decompose does not support them.
|
|
185
|
+
return NotImplemented
|
|
186
|
+
angle = np.angle(complex(self.sub_gate.coefficient))
|
|
187
|
+
rads = np.zeros(shape=shape)
|
|
188
|
+
for hot in self.control_values.expand():
|
|
189
|
+
rads[hot] = angle
|
|
190
|
+
return dg.DiagonalGate(diag_angles_radians=[*rads.flatten()]).on(*qubits)
|
|
178
191
|
if isinstance(self.sub_gate, common_gates.CZPowGate):
|
|
179
|
-
z_sub_gate = common_gates.ZPowGate(
|
|
180
|
-
exponent=self.sub_gate.exponent, global_shift=self.sub_gate.global_shift
|
|
181
|
-
)
|
|
192
|
+
z_sub_gate = common_gates.ZPowGate(exponent=self.sub_gate.exponent)
|
|
182
193
|
num_controls = self.num_controls() + 1
|
|
183
194
|
control_values = self.control_values & cv.ProductOfSums(((1,),))
|
|
184
195
|
control_qid_shape = self.control_qid_shape + (2,)
|
|
@@ -197,9 +208,18 @@ class ControlledGate(raw_types.Gate):
|
|
|
197
208
|
)
|
|
198
209
|
)
|
|
199
210
|
if self != controlled_z:
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
211
|
+
result = controlled_z.on(*qubits)
|
|
212
|
+
if self.sub_gate.global_shift == 0:
|
|
213
|
+
return result
|
|
214
|
+
# Reconstruct the controlled global shift of the subgate.
|
|
215
|
+
total_shift = self.sub_gate.exponent * self.sub_gate.global_shift
|
|
216
|
+
phase_gate = gp.GlobalPhaseGate(1j ** (2 * total_shift))
|
|
217
|
+
controlled_phase_op = phase_gate.controlled(
|
|
218
|
+
num_controls=self.num_controls(),
|
|
219
|
+
control_values=self.control_values,
|
|
220
|
+
control_qid_shape=self.control_qid_shape,
|
|
221
|
+
).on(*control_qubits)
|
|
222
|
+
return [result, controlled_phase_op]
|
|
203
223
|
|
|
204
224
|
if isinstance(self.sub_gate, matrix_gates.MatrixGate):
|
|
205
225
|
# Default decompositions of 2/3 qubit `cirq.MatrixGate` ignores global phase, which is
|
|
@@ -328,7 +348,7 @@ class ControlledGate(raw_types.Gate):
|
|
|
328
348
|
return str(self.control_values) + str(self.sub_gate)
|
|
329
349
|
|
|
330
350
|
def __repr__(self) -> str:
|
|
331
|
-
if self.
|
|
351
|
+
if self.control_qid_shape == (2,) and self.control_values.is_trivial:
|
|
332
352
|
return f'cirq.ControlledGate(sub_gate={self.sub_gate!r})'
|
|
333
353
|
|
|
334
354
|
if self.control_values.is_trivial and set(self.control_qid_shape) == {2}:
|
cirq/ops/controlled_gate_test.py
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
|
|
15
15
|
from types import NotImplementedType
|
|
16
|
-
from typing import
|
|
16
|
+
from typing import Any, cast, Optional, Sequence, Tuple, Union
|
|
17
17
|
|
|
18
18
|
import numpy as np
|
|
19
19
|
import pytest
|
|
@@ -408,6 +408,11 @@ def test_unitary():
|
|
|
408
408
|
),
|
|
409
409
|
True,
|
|
410
410
|
),
|
|
411
|
+
(cirq.GlobalPhaseGate(-1), True),
|
|
412
|
+
(cirq.GlobalPhaseGate(1j**0.7), True),
|
|
413
|
+
(cirq.GlobalPhaseGate(sympy.Symbol("s")), False),
|
|
414
|
+
(cirq.CZPowGate(exponent=1.2, global_shift=0.3), True),
|
|
415
|
+
(cirq.CZPowGate(exponent=sympy.Symbol("s"), global_shift=0.3), False),
|
|
411
416
|
# Single qudit gate with dimension 4.
|
|
412
417
|
(cirq.MatrixGate(np.kron(*(cirq.unitary(cirq.H),) * 2), qid_shape=(4,)), False),
|
|
413
418
|
(cirq.MatrixGate(cirq.testing.random_unitary(4, random_state=1234)), False),
|
|
@@ -420,11 +425,61 @@ def test_unitary():
|
|
|
420
425
|
],
|
|
421
426
|
)
|
|
422
427
|
def test_controlled_gate_is_consistent(gate: cirq.Gate, should_decompose_to_target):
|
|
423
|
-
|
|
428
|
+
_test_controlled_gate_is_consistent(gate, should_decompose_to_target)
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
@pytest.mark.parametrize(
|
|
432
|
+
'gate',
|
|
433
|
+
[
|
|
434
|
+
cirq.GlobalPhaseGate(1j**0.7),
|
|
435
|
+
cirq.ZPowGate(exponent=1.2, global_shift=0.3),
|
|
436
|
+
cirq.CZPowGate(exponent=1.2, global_shift=0.3),
|
|
437
|
+
cirq.CCZPowGate(exponent=1.2, global_shift=0.3),
|
|
438
|
+
],
|
|
439
|
+
)
|
|
440
|
+
@pytest.mark.parametrize(
|
|
441
|
+
'control_qid_shape, control_values, should_decompose_to_target',
|
|
442
|
+
[
|
|
443
|
+
([2, 2], None, True),
|
|
444
|
+
([2, 2], xor_control_values, False),
|
|
445
|
+
([3], None, False),
|
|
446
|
+
([3, 4], xor_control_values, False),
|
|
447
|
+
],
|
|
448
|
+
)
|
|
449
|
+
def test_nontrivial_controlled_gate_is_consistent(
|
|
450
|
+
gate: cirq.Gate,
|
|
451
|
+
control_qid_shape: Sequence[int],
|
|
452
|
+
control_values: Any,
|
|
453
|
+
should_decompose_to_target: bool,
|
|
454
|
+
):
|
|
455
|
+
_test_controlled_gate_is_consistent(
|
|
456
|
+
gate, should_decompose_to_target, control_qid_shape, control_values
|
|
457
|
+
)
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
def _test_controlled_gate_is_consistent(
|
|
461
|
+
gate: cirq.Gate,
|
|
462
|
+
should_decompose_to_target: bool,
|
|
463
|
+
control_qid_shape: Optional[Sequence[int]] = None,
|
|
464
|
+
control_values: Any = None,
|
|
465
|
+
):
|
|
466
|
+
cgate = cirq.ControlledGate(
|
|
467
|
+
gate, control_qid_shape=control_qid_shape, control_values=control_values
|
|
468
|
+
)
|
|
424
469
|
cirq.testing.assert_implements_consistent_protocols(cgate)
|
|
425
470
|
cirq.testing.assert_decompose_ends_at_default_gateset(
|
|
426
471
|
cgate, ignore_known_gates=not should_decompose_to_target
|
|
427
472
|
)
|
|
473
|
+
# The above only decompose once, which doesn't check that the sub-gate's phase is handled.
|
|
474
|
+
# We need to check full decomposition here.
|
|
475
|
+
if not cirq.is_parameterized(gate):
|
|
476
|
+
shape = cirq.qid_shape(cgate)
|
|
477
|
+
qids = cirq.LineQid.for_qid_shape(shape)
|
|
478
|
+
decomposed = cirq.decompose(cgate.on(*qids))
|
|
479
|
+
if len(decomposed) < 1000: # CCCCCZ rounding error explodes
|
|
480
|
+
first_op = cirq.IdentityGate(qid_shape=shape).on(*qids) # To ensure same qid order
|
|
481
|
+
circuit = cirq.Circuit(first_op, *decomposed)
|
|
482
|
+
np.testing.assert_allclose(cirq.unitary(cgate), cirq.unitary(circuit), atol=1e-1)
|
|
428
483
|
|
|
429
484
|
|
|
430
485
|
def test_pow_inverse():
|
cirq/ops/controlled_operation.py
CHANGED
|
@@ -212,6 +212,7 @@ class ControlledOperation(raw_types.Operation):
|
|
|
212
212
|
hasattr(self._sub_operation, "gate")
|
|
213
213
|
and len(self._controls) == 1
|
|
214
214
|
and self.control_values == cv.ProductOfSums(((1,),))
|
|
215
|
+
and all(q.dimension == 2 for q in self.qubits)
|
|
215
216
|
):
|
|
216
217
|
gate = self.sub_operation.gate
|
|
217
218
|
if (
|
cirq/ops/global_phase_op.py
CHANGED
|
@@ -21,6 +21,7 @@ import sympy
|
|
|
21
21
|
|
|
22
22
|
import cirq
|
|
23
23
|
from cirq import value, protocols
|
|
24
|
+
from cirq._compat import proper_repr
|
|
24
25
|
from cirq.ops import raw_types, controlled_gate, control_values as cv
|
|
25
26
|
|
|
26
27
|
|
|
@@ -68,10 +69,10 @@ class GlobalPhaseGate(raw_types.Gate):
|
|
|
68
69
|
return str(self.coefficient)
|
|
69
70
|
|
|
70
71
|
def __repr__(self) -> str:
|
|
71
|
-
return f'cirq.GlobalPhaseGate({self.coefficient
|
|
72
|
+
return f'cirq.GlobalPhaseGate({proper_repr(self.coefficient)})'
|
|
72
73
|
|
|
73
74
|
def _op_repr_(self, qubits: Sequence['cirq.Qid']) -> str:
|
|
74
|
-
return f'cirq.global_phase_operation({self.coefficient
|
|
75
|
+
return f'cirq.global_phase_operation({proper_repr(self.coefficient)})'
|
|
75
76
|
|
|
76
77
|
def _json_dict_(self) -> Dict[str, Any]:
|
|
77
78
|
return protocols.obj_to_dict_helper(self, ['coefficient'])
|
cirq/ops/global_phase_op_test.py
CHANGED
{cirq_core-1.5.0.dev20250226204345.dist-info → cirq_core-1.5.0.dev20250226222923.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.dev20250226222923
|
|
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
|
{cirq_core-1.5.0.dev20250226204345.dist-info → cirq_core-1.5.0.dev20250226222923.dist-info}/RECORD
RENAMED
|
@@ -4,8 +4,8 @@ cirq/_compat_test.py,sha256=Qq3ZcfgD-Nb81cEppQdJqhAyrVqXKtfXZYGXT0p-Wh0,34718
|
|
|
4
4
|
cirq/_doc.py,sha256=yDyWUD_2JDS0gShfGRb-rdqRt9-WeL7DhkqX7np0Nko,2879
|
|
5
5
|
cirq/_import.py,sha256=p9gMHJscbtDDkfHOaulvd3Aer0pwUF5AXpL89XR8dNw,8402
|
|
6
6
|
cirq/_import_test.py,sha256=6K_v0riZJXOXUphHNkGA8MY-JcmGlezFaGmvrNhm3OQ,1015
|
|
7
|
-
cirq/_version.py,sha256=
|
|
8
|
-
cirq/_version_test.py,sha256=
|
|
7
|
+
cirq/_version.py,sha256=CKWJ2BvRhdloQnAyu-09wRIGEKC0k3vjuJbO3i4TT7o,1206
|
|
8
|
+
cirq/_version_test.py,sha256=xdKx0vf8ykrjruFtuUY2DFmW7SjrLE0SnuoYRZYJX0o,147
|
|
9
9
|
cirq/conftest.py,sha256=X7yLFL8GLhg2CjPw0hp5e_dGASfvHx1-QT03aUbhKJw,1168
|
|
10
10
|
cirq/json_resolver_cache.py,sha256=p-vEOa-8GQ2cFIAdze-kd6C1un1uRvtujVPljVKaHBg,13557
|
|
11
11
|
cirq/py.typed,sha256=VFSlmh_lNwnaXzwY-ZuW-C2Ws5PkuDoVgBdNCs0jXJE,63
|
|
@@ -281,9 +281,9 @@ cirq/ops/common_gates.py,sha256=izKH25lp0i_1Vrx9gwGzOIk1PxzOmMnq4MlFu4da4Vk,5817
|
|
|
281
281
|
cirq/ops/common_gates_test.py,sha256=tNymwCOXCgVe6JnrHSq7kwzCO-ucQZYHdBF9cvdZRMo,46731
|
|
282
282
|
cirq/ops/control_values.py,sha256=nNDN6pgz_aWkUfrpOZ9zHHD42AGFaplWhVQj9rmJwbQ,13410
|
|
283
283
|
cirq/ops/control_values_test.py,sha256=iDtdQjL39u80MaR16XLp00LRZqWgJqC54cIeADWf0IY,12906
|
|
284
|
-
cirq/ops/controlled_gate.py,sha256=
|
|
285
|
-
cirq/ops/controlled_gate_test.py,sha256=
|
|
286
|
-
cirq/ops/controlled_operation.py,sha256=
|
|
284
|
+
cirq/ops/controlled_gate.py,sha256=g5pCZMcCbQFhAUeNUndMtnUVnTt8YoOaPql07Bc2Ogg,15511
|
|
285
|
+
cirq/ops/controlled_gate_test.py,sha256=6Z_HaIbXXKOJOhwb2l_Dmtfx9tcggl_vYf8pkB_UDms,25205
|
|
286
|
+
cirq/ops/controlled_operation.py,sha256=DBTv3zvynztbSE1PSTU3JXeurz7zb8In5gmeg6LLxyk,14057
|
|
287
287
|
cirq/ops/controlled_operation_test.py,sha256=iRRkzxMmsChYiWYMuz5-q3xq2mWV1wJY8Y8QQ1ieP5c,16443
|
|
288
288
|
cirq/ops/dense_pauli_string.py,sha256=cDEWmS_r0mD4djxgUdxfE9zCsefB5P6pbkGP89stbIE,24457
|
|
289
289
|
cirq/ops/dense_pauli_string_test.py,sha256=duvgzhgTV9wuem4kDSwtL62SEUCThkz1tdP984-C4_s,21504
|
|
@@ -301,8 +301,8 @@ cirq/ops/gate_operation.py,sha256=VHyA2T-G7afqEjYhEA7yRT72tsQOPGxg8ULsGrSDBJc,13
|
|
|
301
301
|
cirq/ops/gate_operation_test.py,sha256=3sXTtOR3tkrF6C0jCcDexC0qrHCiqcjRClsvc9NXPq8,17457
|
|
302
302
|
cirq/ops/gateset.py,sha256=Tx1CIlve0RhnX9jHZsCVw7EHtfC6b3drbBncmCEtcsQ,21634
|
|
303
303
|
cirq/ops/gateset_test.py,sha256=BY5UJ1k3NC0jz0d4yocyO1qcQU6e4UbN9mapWE7z7AM,16361
|
|
304
|
-
cirq/ops/global_phase_op.py,sha256=
|
|
305
|
-
cirq/ops/global_phase_op_test.py,sha256=
|
|
304
|
+
cirq/ops/global_phase_op.py,sha256=Un7IBSkKP_cpgViUOQIsIpuySfQK2qs9WXWvdUZiTEs,4885
|
|
305
|
+
cirq/ops/global_phase_op_test.py,sha256=C-YMN5ja9IKKgmwC5w2sPDBpFr8p0pJrE5WFikCVZOg,9833
|
|
306
306
|
cirq/ops/greedy_qubit_manager.py,sha256=O9qY8GB1KGxm3B7JEjq50sGVD51bNwTSupJpi3WUeAc,4039
|
|
307
307
|
cirq/ops/greedy_qubit_manager_test.py,sha256=iVZDKes-r08raTiJHpYNmP-Dp89ok3hIU-QboL2BHdw,3300
|
|
308
308
|
cirq/ops/identity.py,sha256=mlmyTQknjlomA8RdqS1SZOmyB4SXPSly1PDW75yfTJo,5892
|
|
@@ -1202,8 +1202,8 @@ cirq/work/sampler.py,sha256=bE5tmVkcR6cZZMLETxDfHehdsYUMbx2RvBeIBetehI4,19187
|
|
|
1202
1202
|
cirq/work/sampler_test.py,sha256=hL2UWx3dz2ukZVNxWftiKVvJcQoLplLZdQm-k1QcA40,13282
|
|
1203
1203
|
cirq/work/zeros_sampler.py,sha256=x1C7cup66a43n-3tm8QjhiqJa07qcJW10FxNp9jJ59Q,2356
|
|
1204
1204
|
cirq/work/zeros_sampler_test.py,sha256=JIkpBBFPJe5Ba4142vzogyWyboG1Q1ZAm0UVGgOoZn8,3279
|
|
1205
|
-
cirq_core-1.5.0.
|
|
1206
|
-
cirq_core-1.5.0.
|
|
1207
|
-
cirq_core-1.5.0.
|
|
1208
|
-
cirq_core-1.5.0.
|
|
1209
|
-
cirq_core-1.5.0.
|
|
1205
|
+
cirq_core-1.5.0.dev20250226222923.dist-info/LICENSE,sha256=tAkwu8-AdEyGxGoSvJ2gVmQdcicWw3j1ZZueVV74M-E,11357
|
|
1206
|
+
cirq_core-1.5.0.dev20250226222923.dist-info/METADATA,sha256=44Cbr9yUHFf9p1lHch-GG7R9DfOwETmOc7cqp-s3hRk,4817
|
|
1207
|
+
cirq_core-1.5.0.dev20250226222923.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
|
|
1208
|
+
cirq_core-1.5.0.dev20250226222923.dist-info/top_level.txt,sha256=Sz9iOxHU0IEMLSFGwiwOCaN2e9K-jFbBbtpPN1hB73g,5
|
|
1209
|
+
cirq_core-1.5.0.dev20250226222923.dist-info/RECORD,,
|
{cirq_core-1.5.0.dev20250226204345.dist-info → cirq_core-1.5.0.dev20250226222923.dist-info}/LICENSE
RENAMED
|
File without changes
|
{cirq_core-1.5.0.dev20250226204345.dist-info → cirq_core-1.5.0.dev20250226222923.dist-info}/WHEEL
RENAMED
|
File without changes
|
|
File without changes
|