cirq-core 1.5.0.dev20250404212936__py3-none-any.whl → 1.5.0.dev20250404235631__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.dev20250404212936"
31
+ __version__ = "1.5.0.dev20250404235631"
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.dev20250404212936"
6
+ assert cirq.__version__ == "1.5.0.dev20250404235631"
cirq/linalg/__init__.py CHANGED
@@ -82,9 +82,11 @@ from cirq.linalg.tolerance import (
82
82
  from cirq.linalg.transformations import (
83
83
  apply_matrix_to_slices as apply_matrix_to_slices,
84
84
  density_matrix_kronecker_product as density_matrix_kronecker_product,
85
+ can_numpy_support_shape as can_numpy_support_shape,
85
86
  match_global_phase as match_global_phase,
86
87
  partial_trace as partial_trace,
87
88
  partial_trace_of_state_vector_as_mixture as partial_trace_of_state_vector_as_mixture,
89
+ phase_delta as phase_delta,
88
90
  reflection_matrix_pow as reflection_matrix_pow,
89
91
  state_vector_kronecker_product as state_vector_kronecker_product,
90
92
  sub_state_vector as sub_state_vector,
@@ -92,5 +94,4 @@ from cirq.linalg.transformations import (
92
94
  targeted_left_multiply as targeted_left_multiply,
93
95
  to_special as to_special,
94
96
  transpose_flattened_array as transpose_flattened_array,
95
- can_numpy_support_shape as can_numpy_support_shape,
96
97
  )
@@ -814,3 +814,17 @@ def _can_numpy_support_dims(num_dims: int) -> bool:
814
814
  def can_numpy_support_shape(shape: Sequence[int]) -> bool:
815
815
  """Returns whether numpy supports the given shape or not numpy/numpy#5744."""
816
816
  return min(shape, default=0) >= 0 and _can_numpy_support_dims(len(shape))
817
+
818
+
819
+ def phase_delta(u1: np.ndarray, u2: np.ndarray) -> complex:
820
+ """Calculates the phase delta of two unitaries.
821
+
822
+ The delta is from u1 to u2. i.e. u1 * phase_delta(u1, u2) == u2.
823
+
824
+ Assumes but does not verify that inputs are valid unitaries and differ only
825
+ by phase.
826
+ """
827
+ # All cells will have the same phase difference. Just choose the cell with the largest
828
+ # absolute value, to minimize rounding error.
829
+ max_index = np.unravel_index(np.abs(u1).argmax(), u1.shape)
830
+ return u2[max_index] / u1[max_index]
@@ -653,3 +653,11 @@ def test_transpose_flattened_array(num_dimensions):
653
653
  @pytest.mark.parametrize('shape, result', [((), True), (30 * (1,), True), ((-3, 1, -1), False)])
654
654
  def test_can_numpy_support_shape(shape: tuple[int, ...], result: bool) -> None:
655
655
  assert linalg.can_numpy_support_shape(shape) is result
656
+
657
+
658
+ @pytest.mark.parametrize('coeff', [1, 1j, -1, -1j, 1j**0.5, 1j**0.3])
659
+ def test_phase_delta(coeff):
660
+ u1 = cirq.testing.random_unitary(4)
661
+ u2 = u1 * coeff
662
+ np.testing.assert_almost_equal(linalg.phase_delta(u1, u2), coeff)
663
+ np.testing.assert_almost_equal(u1 * linalg.phase_delta(u1, u2), u2)
@@ -34,7 +34,6 @@ from cirq.ops import (
34
34
  controlled_operation as cop,
35
35
  diagonal_gate as dg,
36
36
  global_phase_op as gp,
37
- matrix_gates,
38
37
  op_tree,
39
38
  raw_types,
40
39
  )
@@ -220,12 +219,6 @@ class ControlledGate(raw_types.Gate):
220
219
  control_qid_shape=self.control_qid_shape,
221
220
  ).on(*control_qubits)
222
221
  return [result, controlled_phase_op]
223
-
224
- if isinstance(self.sub_gate, matrix_gates.MatrixGate):
225
- # Default decompositions of 2/3 qubit `cirq.MatrixGate` ignores global phase, which is
226
- # local phase in the controlled variant and hence cannot be ignored.
227
- return NotImplemented
228
-
229
222
  result = protocols.decompose_once_with_qubits(
230
223
  self.sub_gate,
231
224
  qubits[self.num_controls() :],
@@ -431,10 +431,23 @@ def test_controlled_gate_is_consistent(gate: cirq.Gate, should_decompose_to_targ
431
431
  @pytest.mark.parametrize(
432
432
  'gate',
433
433
  [
434
+ cirq.I,
435
+ cirq.GlobalPhaseGate(1),
436
+ cirq.GlobalPhaseGate(-1),
437
+ cirq.GlobalPhaseGate(1j),
434
438
  cirq.GlobalPhaseGate(1j**0.7),
439
+ cirq.Z,
435
440
  cirq.ZPowGate(exponent=1.2, global_shift=0.3),
441
+ cirq.CZ,
436
442
  cirq.CZPowGate(exponent=1.2, global_shift=0.3),
443
+ cirq.CCZ,
437
444
  cirq.CCZPowGate(exponent=1.2, global_shift=0.3),
445
+ cirq.X,
446
+ cirq.XPowGate(exponent=1.2, global_shift=0.3),
447
+ cirq.CX,
448
+ cirq.CXPowGate(exponent=1.2, global_shift=0.3),
449
+ cirq.CCX,
450
+ cirq.CCXPowGate(exponent=1.2, global_shift=0.3),
438
451
  ],
439
452
  )
440
453
  @pytest.mark.parametrize(
@@ -476,10 +489,9 @@ def _test_controlled_gate_is_consistent(
476
489
  shape = cirq.qid_shape(cgate)
477
490
  qids = cirq.LineQid.for_qid_shape(shape)
478
491
  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)
492
+ first_op = cirq.IdentityGate(qid_shape=shape).on(*qids) # To ensure same qid order
493
+ circuit = cirq.Circuit(first_op, *decomposed)
494
+ np.testing.assert_allclose(cirq.unitary(cgate), cirq.unitary(circuit), atol=1e-13)
483
495
 
484
496
 
485
497
  def test_pow_inverse():
cirq/ops/matrix_gates.py CHANGED
@@ -14,13 +14,13 @@
14
14
 
15
15
  """Quantum gates defined by a matrix."""
16
16
 
17
- from typing import Any, Dict, Iterable, Optional, Tuple, TYPE_CHECKING
17
+ from typing import Any, Dict, Iterable, List, Optional, Tuple, TYPE_CHECKING
18
18
 
19
19
  import numpy as np
20
20
 
21
21
  from cirq import _import, linalg, protocols
22
22
  from cirq._compat import proper_repr
23
- from cirq.ops import phased_x_z_gate, raw_types
23
+ from cirq.ops import global_phase_op, identity, phased_x_z_gate, raw_types
24
24
 
25
25
  if TYPE_CHECKING:
26
26
  import cirq
@@ -148,18 +148,34 @@ class MatrixGate(raw_types.Gate):
148
148
  return MatrixGate(matrix=result.reshape(self._matrix.shape), qid_shape=self._qid_shape)
149
149
 
150
150
  def _decompose_(self, qubits: Tuple['cirq.Qid', ...]) -> 'cirq.OP_TREE':
151
+ from cirq.circuits import Circuit
152
+
153
+ decomposed: List['cirq.Operation'] = NotImplemented
151
154
  if self._qid_shape == (2,):
152
- return [
155
+ decomposed = [
153
156
  g.on(qubits[0])
154
157
  for g in single_qubit_decompositions.single_qubit_matrix_to_gates(self._matrix)
155
158
  ]
156
159
  if self._qid_shape == (2,) * 2:
157
- return two_qubit_to_cz.two_qubit_matrix_to_cz_operations(
160
+ decomposed = two_qubit_to_cz.two_qubit_matrix_to_cz_operations(
158
161
  *qubits, self._matrix, allow_partial_czs=True
159
162
  )
160
163
  if self._qid_shape == (2,) * 3:
161
- return three_qubit_decomposition.three_qubit_matrix_to_operations(*qubits, self._matrix)
162
- return NotImplemented
164
+ decomposed = three_qubit_decomposition.three_qubit_matrix_to_operations(
165
+ *qubits, self._matrix
166
+ )
167
+ if decomposed is NotImplemented:
168
+ return NotImplemented
169
+ # The above algorithms ignore phase, but phase is important to maintain if the gate is
170
+ # controlled. Here, we add it back in with a global phase op.
171
+ ident = identity.IdentityGate(qid_shape=self._qid_shape).on(*qubits) # Preserve qid order
172
+ u = protocols.unitary(Circuit(ident, *decomposed)).reshape(self._matrix.shape)
173
+ phase_delta = linalg.phase_delta(u, self._matrix)
174
+ # Phase delta is on the complex unit circle, so if real(phase_delta) >= 1, that means
175
+ # no phase delta. (>1 is rounding error).
176
+ if phase_delta.real < 1:
177
+ decomposed.append(global_phase_op.global_phase_operation(phase_delta))
178
+ return decomposed
163
179
 
164
180
  def _has_unitary_(self) -> bool:
165
181
  return True
@@ -24,7 +24,7 @@ from cirq import ops, transformers as opt
24
24
 
25
25
  def three_qubit_matrix_to_operations(
26
26
  q0: ops.Qid, q1: ops.Qid, q2: ops.Qid, u: np.ndarray, atol: float = 1e-8
27
- ) -> Sequence[ops.Operation]:
27
+ ) -> List[ops.Operation]:
28
28
  """Returns operations for a 3 qubit unitary.
29
29
 
30
30
  The algorithm is described in Shende et al.:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: cirq-core
3
- Version: 1.5.0.dev20250404212936
3
+ Version: 1.5.0.dev20250404235631
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=0m3sYIyxRNv9jvAo6rzJ-cnbpny3KGnAByrbU7bApgQ,34720
4
4
  cirq/_doc.py,sha256=yDyWUD_2JDS0gShfGRb-rdqRt9-WeL7DhkqX7np0Nko,2879
5
5
  cirq/_import.py,sha256=cfocxtT1BJ4HkfZ-VO8YyIhPP-xfqHDkLrzz6eeO5U0,8421
6
6
  cirq/_import_test.py,sha256=6K_v0riZJXOXUphHNkGA8MY-JcmGlezFaGmvrNhm3OQ,1015
7
- cirq/_version.py,sha256=8jkmcBM_XFQvrblAos1u_xMK-SfW0h5cnN-V3TqnVLs,1206
8
- cirq/_version_test.py,sha256=l0Ucd3Bf6XezY7GuMOsP7cBsseeHmaRjSOicoSEVSXU,147
7
+ cirq/_version.py,sha256=KinPtb06uZbX8CW--RiF5vnOluHAD1sq7bzElrrToKI,1206
8
+ cirq/_version_test.py,sha256=PwTQFs7hoDk7E-ZsPnJZ1KoVIt3qxLszOF88pLVLN_o,147
9
9
  cirq/conftest.py,sha256=X7yLFL8GLhg2CjPw0hp5e_dGASfvHx1-QT03aUbhKJw,1168
10
10
  cirq/json_resolver_cache.py,sha256=YVamU72nCUT5dG0bhAvRKVX5lXcZMNTwP3H36v-cYag,13615
11
11
  cirq/py.typed,sha256=VFSlmh_lNwnaXzwY-ZuW-C2Ws5PkuDoVgBdNCs0jXJE,63
@@ -248,7 +248,7 @@ cirq/interop/quirk/cells/testing_test.py,sha256=Qb__MOXjUrna7Wy6mlRWFyyy78KfzqlK
248
248
  cirq/interop/quirk/cells/unsupported_cells.py,sha256=xTE4aKpuVocey_lvWwb8Q1fla6oMqI1S4rc7a2UtDgs,2841
249
249
  cirq/interop/quirk/cells/unsupported_cells_test.py,sha256=5bl-maazy7Dr8u6kwK1AhGT4vtHqzIMRKxoMKYC-JWs,2178
250
250
  cirq/ion/__init__.py,sha256=F6tf4JZOGpDdxX0FxT42qgq8rF96ZTFHMJ0OV09Yj1c,787
251
- cirq/linalg/__init__.py,sha256=9WLnBqLQ02FzCIUcchHBMYpOGVcENAjzv7GyDNgh89I,4013
251
+ cirq/linalg/__init__.py,sha256=0dSlIBy-TVzf7b_-rLLlrS8ZFkgCfYg0pJjWs72xYOk,4045
252
252
  cirq/linalg/combinators.py,sha256=0tts29gbwQg9lpZvCSX8QKMIMf38NGGdBJqI911m7jA,5336
253
253
  cirq/linalg/combinators_test.py,sha256=eRy1FrGujE8UC3pP1X5MfWmKlpjimHTxdiixr-G4nJM,4829
254
254
  cirq/linalg/decompositions.py,sha256=9dOJSJuYG8_dk3zdBO5rXDONKkw7SKZd5ZpWK4FnypQ,38652
@@ -261,8 +261,8 @@ cirq/linalg/predicates.py,sha256=vLTRaAYqVf0mk6Qgm53ROhLDtXxxoEhHbYxQN74aGRk,120
261
261
  cirq/linalg/predicates_test.py,sha256=syNiyS-clEGeZnbKT7zyR8_ClDnXFYtDnLKozLbitzw,21504
262
262
  cirq/linalg/tolerance.py,sha256=a68RNOmCw0ybFwhXOq6DERK5gHAOlPJIRdPuMzV6xuU,1870
263
263
  cirq/linalg/tolerance_test.py,sha256=wnmuXIGEn_mugGoNm3AigSgjV2DMFdj8xpgRTMBbO7A,2355
264
- cirq/linalg/transformations.py,sha256=cUZWPZJq6DISQ4rS02KSObmNwqLHYxIkNCfYijgJLoA,31986
265
- cirq/linalg/transformations_test.py,sha256=GsqD8_X0N88ECKr44t0O9wAJ6-uJJi0wExivn0Okf7M,25333
264
+ cirq/linalg/transformations.py,sha256=zo9Gwo4VX2uQfN_7iOzQrxI-27uDj7s8eTUe2eJQetU,32519
265
+ cirq/linalg/transformations_test.py,sha256=4GnfQhB1lERteVxvTHXMXxEt4vwQLZBXNJOvnFY3AKY,25636
266
266
  cirq/neutral_atoms/__init__.py,sha256=VoQBkmZ5m4TPxjxShRixjqJbnc-IAnAWkGOPu8MBS5o,813
267
267
  cirq/neutral_atoms/convert_to_neutral_atom_gates.py,sha256=SsXFh1-NoBGqp4yX8-jIbIw-AK40baA-qh-iTL1wS6Q,1070
268
268
  cirq/neutral_atoms/convert_to_neutral_atom_gates_test.py,sha256=svleNo8P_cQLCMfcGLkU896cpLjZKPynDKnJGM2pyJ0,1861
@@ -284,8 +284,8 @@ cirq/ops/common_gates.py,sha256=nTW9PAOdkbDei01ZUx6N-D2V1zslRJnk8WS_-NJSdlg,5833
284
284
  cirq/ops/common_gates_test.py,sha256=6y6uPS29Lnby3YesV4YG8fmI1FWqj7BfPQEGbvJl928,46882
285
285
  cirq/ops/control_values.py,sha256=Loi_voLNOzPJpjD6AnQz8JrqJLOAUe0jvV3XB_0tTGE,13430
286
286
  cirq/ops/control_values_test.py,sha256=K8tbKM6b6PqMEL_lHLFzdrnWF1SkLN0Scft6YMT7FlE,12907
287
- cirq/ops/controlled_gate.py,sha256=Is4s8-yRZye3gkhg-dlte5zfdAiXYtioC0oP-BXQ37g,15531
288
- cirq/ops/controlled_gate_test.py,sha256=6Z_HaIbXXKOJOhwb2l_Dmtfx9tcggl_vYf8pkB_UDms,25205
287
+ cirq/ops/controlled_gate.py,sha256=uzQL7scPsKmnu3Kdiw-zF7b4FxdyqvaD6oPkczs1skQ,15234
288
+ cirq/ops/controlled_gate_test.py,sha256=8PprvTdGMfBQOOQ-BpW_decRQU39P2gM_xJfX-fawMo,25512
289
289
  cirq/ops/controlled_operation.py,sha256=kfZUj5XISlHJGvm2mzn7-UzNmIFimLMNvn9QhD68Lro,14077
290
290
  cirq/ops/controlled_operation_test.py,sha256=kHdHGR6Q6ROgUJqG4DSKOvyrLJhi-u4uMh-rM3QRJ8o,16525
291
291
  cirq/ops/dense_pauli_string.py,sha256=nQs4lfm9zSGPGE9p9KAJbEhkldpg9krqTwvIkLePs90,24477
@@ -314,7 +314,7 @@ cirq/ops/kraus_channel.py,sha256=qX828mvPSFgEaG0I3Jhj04y5rD7dUjeEl2HYDn7_7j4,508
314
314
  cirq/ops/kraus_channel_test.py,sha256=-E6ExIO3P0y2T74Gx-RMu0kLpy1RWP9wH6UGDI21oFU,4820
315
315
  cirq/ops/linear_combinations.py,sha256=PE1o_mvpdnlg3o95iSn5ID18kuxwS2q3DGDuFnlNC90,39916
316
316
  cirq/ops/linear_combinations_test.py,sha256=ip-wN8T8nUQviD3ql42eet7k_MQ6DVUfIK8aX-_ygOs,67217
317
- cirq/ops/matrix_gates.py,sha256=q-lyxDVg5K-eokmZWPN1woUmJkR90IpbNhnFqQgjw4Q,9297
317
+ cirq/ops/matrix_gates.py,sha256=PoaJuONeh7wFQ0iJUtMsN-OdvYW9wm_q8kFBLWu6I1k,10221
318
318
  cirq/ops/matrix_gates_test.py,sha256=m5rMwq_sqVvsmkc5opVr3Ikd1ERuULmSRNAvGZUg7ds,14224
319
319
  cirq/ops/measure_util.py,sha256=ZaH1Lze4Yk3nYrojn7SILUeIN96p_khIoa25ixXLu6A,7390
320
320
  cirq/ops/measure_util_test.py,sha256=Yzlced4nb4DHO-0cM_a-QZGO_3R8oqExkpIALN_pG4A,5307
@@ -1097,7 +1097,7 @@ cirq/transformers/analytical_decompositions/single_qubit_decompositions.py,sha25
1097
1097
  cirq/transformers/analytical_decompositions/single_qubit_decompositions_test.py,sha256=4GfU6wctBoG-OVFqFOE08xymd5dXzY-doE8ZNpsKohI,12308
1098
1098
  cirq/transformers/analytical_decompositions/single_to_two_qubit_isometry.py,sha256=Qmxv4YJgiBSbZUoonwSSBGGmzuPsUQLQbNTk2z59xRg,2439
1099
1099
  cirq/transformers/analytical_decompositions/single_to_two_qubit_isometry_test.py,sha256=qBoSS8OoM2HdOIM_46pSdbWFsQBu0aeQxUqiJ9znlnk,2517
1100
- cirq/transformers/analytical_decompositions/three_qubit_decomposition.py,sha256=3nCZItd7ZZ2IU0ysNuiM0OYn6RQGBCe9kF-H7kWlEBU,9746
1100
+ cirq/transformers/analytical_decompositions/three_qubit_decomposition.py,sha256=yrVbl6TKffGltCuDTxSs2vMoe5kMuv7R0yJKtOZjvm4,9742
1101
1101
  cirq/transformers/analytical_decompositions/three_qubit_decomposition_test.py,sha256=p9H-XuoQ3jdR5NTcAcbW6Pak69qGfzvBN4N1SDw5eg8,7847
1102
1102
  cirq/transformers/analytical_decompositions/two_qubit_state_preparation.py,sha256=OOjIy4Nw_28CdcmK5OE8uQEzy0wO5ITEF_tSZKaFSMM,6178
1103
1103
  cirq/transformers/analytical_decompositions/two_qubit_state_preparation_test.py,sha256=486hVPQp620r8SPKlhyaFIK8TC5jDngX8WqHY1Hb0Hk,4458
@@ -1209,8 +1209,8 @@ cirq/work/sampler.py,sha256=sW0RhIelGABAKbqTM58shwyyCPgf86JIv9IGdJe__js,19186
1209
1209
  cirq/work/sampler_test.py,sha256=mdk1J-WrvbPUYhY41VhWf9_te4DnXr_XMPcugWwc4-I,13281
1210
1210
  cirq/work/zeros_sampler.py,sha256=8_Ne6dBkDANtTZuql7Eb0Qg_E_P3-_gu-ybFzxTbKAQ,2356
1211
1211
  cirq/work/zeros_sampler_test.py,sha256=JIkpBBFPJe5Ba4142vzogyWyboG1Q1ZAm0UVGgOoZn8,3279
1212
- cirq_core-1.5.0.dev20250404212936.dist-info/LICENSE,sha256=tAkwu8-AdEyGxGoSvJ2gVmQdcicWw3j1ZZueVV74M-E,11357
1213
- cirq_core-1.5.0.dev20250404212936.dist-info/METADATA,sha256=9L-Fv4MoT-eMxynxNHCPLlpReljxs7aDgUb_etLiID8,4584
1214
- cirq_core-1.5.0.dev20250404212936.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
1215
- cirq_core-1.5.0.dev20250404212936.dist-info/top_level.txt,sha256=Sz9iOxHU0IEMLSFGwiwOCaN2e9K-jFbBbtpPN1hB73g,5
1216
- cirq_core-1.5.0.dev20250404212936.dist-info/RECORD,,
1212
+ cirq_core-1.5.0.dev20250404235631.dist-info/LICENSE,sha256=tAkwu8-AdEyGxGoSvJ2gVmQdcicWw3j1ZZueVV74M-E,11357
1213
+ cirq_core-1.5.0.dev20250404235631.dist-info/METADATA,sha256=h1oY0Qt8QuSr4lKNpxi8bG2GYLU8amhYZz7moJH89_U,4584
1214
+ cirq_core-1.5.0.dev20250404235631.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
1215
+ cirq_core-1.5.0.dev20250404235631.dist-info/top_level.txt,sha256=Sz9iOxHU0IEMLSFGwiwOCaN2e9K-jFbBbtpPN1hB73g,5
1216
+ cirq_core-1.5.0.dev20250404235631.dist-info/RECORD,,