cirq-core 1.5.0.dev20250226204345__py3-none-any.whl → 1.5.0.dev20250226233318__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 CHANGED
@@ -28,4 +28,4 @@ if sys.version_info < (3, 10, 0): # pragma: no cover
28
28
  'of cirq (e.g. "python -m pip install cirq==1.1.*")'
29
29
  )
30
30
 
31
- __version__ = "1.5.0.dev20250226204345"
31
+ __version__ = "1.5.0.dev20250226233318"
cirq/_version_test.py CHANGED
@@ -3,4 +3,4 @@ import cirq
3
3
 
4
4
 
5
5
  def test_version():
6
- assert cirq.__version__ == "1.5.0.dev20250226204345"
6
+ assert cirq.__version__ == "1.5.0.dev20250226233318"
@@ -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
- raw_types,
33
+ control_values as cv,
34
34
  controlled_operation as cop,
35
- op_tree,
35
+ diagonal_gate as dg,
36
+ global_phase_op as gp,
36
37
  matrix_gates,
37
- control_values as cv,
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
- return protocols.decompose_once_with_qubits(
201
- controlled_z, qubits, NotImplemented, context=context
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.num_controls() == 1 and self.control_values.is_trivial:
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}:
@@ -13,7 +13,7 @@
13
13
  # limitations under the License.
14
14
 
15
15
  from types import NotImplementedType
16
- from typing import Union, Tuple, cast
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
- cgate = cirq.ControlledGate(gate)
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():
@@ -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 (
@@ -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!r})'
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!r})'
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'])
@@ -33,7 +33,7 @@ def test_init():
33
33
 
34
34
 
35
35
  def test_protocols():
36
- for p in [1, 1j, -1]:
36
+ for p in [1, 1j, -1, sympy.Symbol('s')]:
37
37
  cirq.testing.assert_implements_consistent_protocols(cirq.global_phase_operation(p))
38
38
 
39
39
  np.testing.assert_allclose(
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: cirq-core
3
- Version: 1.5.0.dev20250226204345
3
+ Version: 1.5.0.dev20250226233318
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
@@ -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=cUoPyOhB5bj5gnlY_O3Hrw65QsxGmxH-JOL1UQC2ftQ,1206
8
- cirq/_version_test.py,sha256=Gm3PtQBJC67hG4qCMnQL_TjCiGmw56m83AnS1H3-T5E,147
7
+ cirq/_version.py,sha256=wMJgrMD0S2t64Vay6XocWM01xWLQRA8RHHpYdxR7V4s,1206
8
+ cirq/_version_test.py,sha256=mtsql_KSNl0EmQlkOYcBY9p95XWMH-DfxV5SrQqwlCw,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=8ELuov5mOUp_cNwwrwMEjB4NwDph4Cpi7ezABx86W8Y,14261
285
- cirq/ops/controlled_gate_test.py,sha256=oasofj1Qu4iD2xJffAGDSDB8FN0xzhn5t912rqkKUXI,23083
286
- cirq/ops/controlled_operation.py,sha256=7X6NgihCuV5iaK-ya9cYZHlBAgN-eFcOFZ3PhGYlkFY,13998
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=4qYUov6BnBql2_yqUHI9DE60fLiLmCsKI9ZkfMLRRyo,4826
305
- cirq/ops/global_phase_op_test.py,sha256=FjtUsohIYabTtiGytvPQw9Rzkqd6dlT7qrj4oltDbTI,9814
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.dev20250226204345.dist-info/LICENSE,sha256=tAkwu8-AdEyGxGoSvJ2gVmQdcicWw3j1ZZueVV74M-E,11357
1206
- cirq_core-1.5.0.dev20250226204345.dist-info/METADATA,sha256=M5LP0p68YIfn74tPtQH9mXOGucUhy1ghTOTEcIjLiCQ,4817
1207
- cirq_core-1.5.0.dev20250226204345.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
1208
- cirq_core-1.5.0.dev20250226204345.dist-info/top_level.txt,sha256=Sz9iOxHU0IEMLSFGwiwOCaN2e9K-jFbBbtpPN1hB73g,5
1209
- cirq_core-1.5.0.dev20250226204345.dist-info/RECORD,,
1205
+ cirq_core-1.5.0.dev20250226233318.dist-info/LICENSE,sha256=tAkwu8-AdEyGxGoSvJ2gVmQdcicWw3j1ZZueVV74M-E,11357
1206
+ cirq_core-1.5.0.dev20250226233318.dist-info/METADATA,sha256=oIbBZflbc50AQKvePmeJYCAefY0TLwId9dEbY2SQi3Q,4817
1207
+ cirq_core-1.5.0.dev20250226233318.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
1208
+ cirq_core-1.5.0.dev20250226233318.dist-info/top_level.txt,sha256=Sz9iOxHU0IEMLSFGwiwOCaN2e9K-jFbBbtpPN1hB73g,5
1209
+ cirq_core-1.5.0.dev20250226233318.dist-info/RECORD,,