cirq-core 1.5.0.dev20250405043305__py3-none-any.whl → 1.5.0.dev20250405050852__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.dev20250405043305"
31
+ __version__ = "1.5.0.dev20250405050852"
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.dev20250405043305"
6
+ assert cirq.__version__ == "1.5.0.dev20250405050852"
@@ -1089,15 +1089,12 @@ class BitFlipChannel(raw_types.Gate):
1089
1089
  ValueError: if p is not a valid probability.
1090
1090
  """
1091
1091
  self._p = value.validate_probability(p, 'p')
1092
- self._delegate = AsymmetricDepolarizingChannel(p, 0.0, 0.0)
1093
1092
 
1094
1093
  def _num_qubits_(self) -> int:
1095
1094
  return 1
1096
1095
 
1097
- def _mixture_(self) -> Sequence[Tuple[float, np.ndarray]]:
1098
- mixture = self._delegate._mixture_()
1099
- # just return identity and x term
1100
- return (mixture[0], mixture[1])
1096
+ def _mixture_(self) -> Sequence[Tuple[float, Any]]:
1097
+ return ((1 - self._p, identity.I), (self._p, pauli_gates.X))
1101
1098
 
1102
1099
  def _has_mixture_(self) -> bool:
1103
1100
  return True
@@ -24,7 +24,6 @@ from cirq import linalg
24
24
  from cirq._doc import doc_private
25
25
  from cirq.protocols import qid_shape_protocol
26
26
  from cirq.protocols.apply_unitary_protocol import apply_unitary, ApplyUnitaryArgs
27
- from cirq.protocols.mixture_protocol import mixture
28
27
 
29
28
  # This is a special indicator value used by the apply_mixture method
30
29
  # to determine whether or not the caller provided a 'default' argument. It must
@@ -260,9 +259,9 @@ def apply_mixture(
260
259
  return result
261
260
 
262
261
  # Fallback to using the object's `_mixture_` matrices. (STEP C)
263
- prob_mix = mixture(val, None)
264
- if prob_mix is not None:
265
- return _mixture_strat(prob_mix, args, is_density_matrix)
262
+ result = _apply_mixture_from_mixture_strat(val, args, is_density_matrix)
263
+ if result is not None:
264
+ return result
266
265
 
267
266
  # Don't know how to apply mixture. Fallback to specified default behavior.
268
267
  # (STEP D)
@@ -359,11 +358,19 @@ def _apply_unitary_from_matrix_strat(
359
358
  return args.target_tensor
360
359
 
361
360
 
362
- def _mixture_strat(val: Any, args: 'ApplyMixtureArgs', is_density_matrix: bool) -> np.ndarray:
361
+ def _apply_mixture_from_mixture_strat(
362
+ val: Any, args: 'ApplyMixtureArgs', is_density_matrix: bool
363
+ ) -> Optional[np.ndarray]:
363
364
  """Attempt to use unitary matrices in _mixture_ and return the result."""
365
+ method = getattr(val, '_mixture_', None)
366
+ if method is None:
367
+ return None
368
+ prob_mix = method()
369
+ if prob_mix is NotImplemented or prob_mix is None:
370
+ return None
364
371
  args.out_buffer[:] = 0
365
372
  np.copyto(dst=args.auxiliary_buffer1, src=args.target_tensor)
366
- for prob, op in val:
373
+ for prob, op in prob_mix:
367
374
  np.copyto(dst=args.target_tensor, src=args.auxiliary_buffer1)
368
375
  right_result = _apply_unitary_strat(op, args, is_density_matrix)
369
376
  if right_result is None:
@@ -237,6 +237,17 @@ def test_apply_mixture_no_protocols_implemented():
237
237
  assert_apply_mixture_returns(NoProtocols(), rho, left_axes=[1], right_axes=[1])
238
238
 
239
239
 
240
+ def test_apply_mixture_mixture_returns_not_implemented():
241
+ class NoMixture:
242
+ def _mixture_(self):
243
+ return NotImplemented
244
+
245
+ rho = np.ones((2, 2, 2, 2), dtype=np.complex128)
246
+
247
+ with pytest.raises(TypeError, match='has no'):
248
+ assert_apply_mixture_returns(NoMixture(), rho, left_axes=[1], right_axes=[1])
249
+
250
+
240
251
  def test_apply_mixture_no_protocols_implemented_default():
241
252
  class NoProtocols:
242
253
  pass
@@ -24,6 +24,7 @@ from typing_extensions import Protocol
24
24
  from cirq._doc import doc_private
25
25
  from cirq.protocols.decompose_protocol import _try_decompose_into_operations_and_qubits
26
26
  from cirq.protocols.mixture_protocol import has_mixture
27
+ from cirq.protocols.unitary_protocol import unitary
27
28
 
28
29
  # This is a special indicator value used by the channel method to determine
29
30
  # whether or not the caller provided a 'default' argument. It must be of type
@@ -145,7 +146,9 @@ def kraus(
145
146
  mixture_getter = getattr(val, '_mixture_', None)
146
147
  mixture_result = NotImplemented if mixture_getter is None else mixture_getter()
147
148
  if mixture_result is not NotImplemented and mixture_result is not None:
148
- return tuple(np.sqrt(p) * u for p, u in mixture_result)
149
+ return tuple(
150
+ np.sqrt(p) * (u if isinstance(u, np.ndarray) else unitary(u)) for p, u in mixture_result
151
+ )
149
152
 
150
153
  unitary_getter = getattr(val, '_unitary_', None)
151
154
  unitary_result = NotImplemented if unitary_getter is None else unitary_getter()
@@ -23,6 +23,7 @@ from typing_extensions import Protocol
23
23
  from cirq._doc import doc_private
24
24
  from cirq.protocols.decompose_protocol import _try_decompose_into_operations_and_qubits
25
25
  from cirq.protocols.has_unitary_protocol import has_unitary
26
+ from cirq.protocols.unitary_protocol import unitary
26
27
 
27
28
  # This is a special indicator value used by the inverse method to determine
28
29
  # whether or not the caller provided a 'default' argument.
@@ -84,14 +85,14 @@ def mixture(
84
85
  with that probability in the mixture. The probabilities will sum to 1.0.
85
86
 
86
87
  Raises:
87
- TypeError: If `val` has no `_mixture_` or `_unitary_` mehod, or if it
88
+ TypeError: If `val` has no `_mixture_` or `_unitary_` method, or if it
88
89
  does and this method returned `NotImplemented`.
89
90
  """
90
91
 
91
92
  mixture_getter = getattr(val, '_mixture_', None)
92
93
  result = NotImplemented if mixture_getter is None else mixture_getter()
93
- if result is not NotImplemented:
94
- return result
94
+ if result is not NotImplemented and result is not None:
95
+ return tuple((p, u if isinstance(u, np.ndarray) else unitary(u)) for p, u in result)
95
96
 
96
97
  unitary_getter = getattr(val, '_unitary_', None)
97
98
  result = NotImplemented if unitary_getter is None else unitary_getter()
@@ -17,6 +17,9 @@ import pytest
17
17
 
18
18
  import cirq
19
19
 
20
+ a = np.array([1])
21
+ b = np.array([1j])
22
+
20
23
 
21
24
  class NoMethod:
22
25
  pass
@@ -32,7 +35,7 @@ class ReturnsNotImplemented:
32
35
 
33
36
  class ReturnsValidTuple(cirq.SupportsMixture):
34
37
  def _mixture_(self):
35
- return ((0.4, 'a'), (0.6, 'b'))
38
+ return ((0.4, a), (0.6, b))
36
39
 
37
40
  def _has_mixture_(self):
38
41
  return True
@@ -40,27 +43,27 @@ class ReturnsValidTuple(cirq.SupportsMixture):
40
43
 
41
44
  class ReturnsNonnormalizedTuple:
42
45
  def _mixture_(self):
43
- return ((0.4, 'a'), (0.4, 'b'))
46
+ return ((0.4, a), (0.4, b))
44
47
 
45
48
 
46
49
  class ReturnsNegativeProbability:
47
50
  def _mixture_(self):
48
- return ((0.4, 'a'), (-0.4, 'b'))
51
+ return ((0.4, a), (-0.4, b))
49
52
 
50
53
 
51
54
  class ReturnsGreaterThanUnityProbability:
52
55
  def _mixture_(self):
53
- return ((1.2, 'a'), (0.4, 'b'))
56
+ return ((1.2, a), (0.4, b))
54
57
 
55
58
 
56
59
  class ReturnsMixtureButNoHasMixture:
57
60
  def _mixture_(self):
58
- return ((0.4, 'a'), (0.6, 'b'))
61
+ return ((0.4, a), (0.6, b))
59
62
 
60
63
 
61
64
  class ReturnsUnitary:
62
65
  def _unitary_(self):
63
- return np.ones((2, 2))
66
+ return np.eye(2)
64
67
 
65
68
  def _has_unitary_(self):
66
69
  return True
@@ -74,12 +77,18 @@ class ReturnsNotImplementedUnitary:
74
77
  return NotImplemented
75
78
 
76
79
 
80
+ class ReturnsMixtureOfReturnsUnitary:
81
+ def _mixture_(self):
82
+ return ((0.4, ReturnsUnitary()), (0.6, ReturnsUnitary()))
83
+
84
+
77
85
  @pytest.mark.parametrize(
78
86
  'val,mixture',
79
87
  (
80
- (ReturnsValidTuple(), ((0.4, 'a'), (0.6, 'b'))),
81
- (ReturnsNonnormalizedTuple(), ((0.4, 'a'), (0.4, 'b'))),
82
- (ReturnsUnitary(), ((1.0, np.ones((2, 2))),)),
88
+ (ReturnsValidTuple(), ((0.4, a), (0.6, b))),
89
+ (ReturnsNonnormalizedTuple(), ((0.4, a), (0.4, b))),
90
+ (ReturnsUnitary(), ((1.0, np.eye(2)),)),
91
+ (ReturnsMixtureOfReturnsUnitary(), ((0.4, np.eye(2)), (0.6, np.eye(2)))),
83
92
  ),
84
93
  )
85
94
  def test_objects_with_mixture(val, mixture):
@@ -88,7 +97,7 @@ def test_objects_with_mixture(val, mixture):
88
97
  np.testing.assert_almost_equal(keys, expected_keys)
89
98
  np.testing.assert_equal(values, expected_values)
90
99
 
91
- keys, values = zip(*cirq.mixture(val, ((0.3, 'a'), (0.7, 'b'))))
100
+ keys, values = zip(*cirq.mixture(val, ((0.3, a), (0.7, b))))
92
101
  np.testing.assert_almost_equal(keys, expected_keys)
93
102
  np.testing.assert_equal(values, expected_values)
94
103
 
@@ -101,7 +110,7 @@ def test_objects_with_no_mixture(val):
101
110
  _ = cirq.mixture(val)
102
111
  assert cirq.mixture(val, None) is None
103
112
  assert cirq.mixture(val, NotImplemented) is NotImplemented
104
- default = ((0.4, 'a'), (0.6, 'b'))
113
+ default = ((0.4, a), (0.6, b))
105
114
  assert cirq.mixture(val, default) == default
106
115
 
107
116
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: cirq-core
3
- Version: 1.5.0.dev20250405043305
3
+ Version: 1.5.0.dev20250405050852
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=lcCzHo7o7aBVgMbzjtSwrvnpFHV1Hut93nbUgxA0ibU,1206
8
- cirq/_version_test.py,sha256=6ZYv5DT6HSuJAbOiQc4EeFJuUzypuwLQTA2AHZusBCI,147
7
+ cirq/_version.py,sha256=rrHncQSD1uha8V-ZVfxjGi97KobvlRJIdilCH7DsjIw,1206
8
+ cirq/_version_test.py,sha256=ZAHy9pdX7FT9NoLH9SJy53S2EU42iikkMzR--NkmMMg,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
@@ -276,7 +276,7 @@ cirq/ops/classically_controlled_operation.py,sha256=ePhBPrHymodrsztJFk_g2IGI3QSb
276
276
  cirq/ops/classically_controlled_operation_test.py,sha256=nIYyXfNH4E2IibZSLk6QDVHpfJQbuI_iWwirCH8rhi8,50209
277
277
  cirq/ops/clifford_gate.py,sha256=5mE97WQG6kW9ntRcn20hoFip71TFofnNKMx01RHETO4,39488
278
278
  cirq/ops/clifford_gate_test.py,sha256=dqghYb7_afYxCLceBarX56Tn9y_dSWCKF75W-Qrzvcw,40341
279
- cirq/ops/common_channels.py,sha256=LfIjy4WBRF_K81kc12Y4nLjdVZnJUhIKHL7sxiNNINU,37259
279
+ cirq/ops/common_channels.py,sha256=lGDOSdstoK57DingL-lo2n49-a51MshhJOl4LOcpQfg,37126
280
280
  cirq/ops/common_channels_test.py,sha256=nQsSSxu7vtedb3ZUuw4hNKIX7MYI4x8lxvLyWMZNt10,30079
281
281
  cirq/ops/common_gate_families.py,sha256=2E31Qr_Yv1zI-r_MNWmr1xJYrEHHU45274iDrt_oKPE,8611
282
282
  cirq/ops/common_gate_families_test.py,sha256=bEF6Q6GtEOTc9kHM5WC1UIULPGnMPXdtm8gzLT_aNBI,5276
@@ -384,8 +384,8 @@ cirq/protocols/act_on_protocol.py,sha256=pRjl2lHqxuYWlJopkWCSPiwrHPggHOdRMb0nu-k
384
384
  cirq/protocols/act_on_protocol_test.py,sha256=q4vOBfG9-O8CIzmwU8HaB2HZR_U1fk2Vm-q1QhyeEpY,3147
385
385
  cirq/protocols/apply_channel_protocol.py,sha256=s5gkjAcAMe_qdPJGPuZnt4r6zZhszpddnK8Ed-wEO90,15678
386
386
  cirq/protocols/apply_channel_protocol_test.py,sha256=ETAWrBTVkPuUPUgHsLZo1HUsJnZsO0hD2fQeXlXtvjE,10582
387
- cirq/protocols/apply_mixture_protocol.py,sha256=t_nSa5nISHqDM-wUzMRRz50RhxZ2d1nHq2a4RL2NTqU,16306
388
- cirq/protocols/apply_mixture_protocol_test.py,sha256=6sNZhauFDec2MvHmUsvmkVF-qjH1WLDnZO35RT0OtRY,10793
387
+ cirq/protocols/apply_mixture_protocol.py,sha256=pYxlpOXxPjMIddyYtEw82_18ioNNvEEcmsImaQ1YiBc,16479
388
+ cirq/protocols/apply_mixture_protocol_test.py,sha256=aWyzK9h9QNmpZJBu0ASaQ8BGYJt5T5KugN4OPMO5pCU,11129
389
389
  cirq/protocols/apply_unitary_protocol.py,sha256=giJwec5XCEt5s0_uyNEuhGBlcDeJymPvuoIVx08hszY,29772
390
390
  cirq/protocols/apply_unitary_protocol_test.py,sha256=ajjHvcBBv5n8Qh_hMPZkdsOvy1xJ774q4kuC25DJnKM,26136
391
391
  cirq/protocols/approximate_equality_protocol.py,sha256=ZqnkoltD8vS1eQjV7Lw3RS49cdjZjGTF0LbdRUlBOCw,6257
@@ -409,12 +409,12 @@ cirq/protocols/inverse_protocol.py,sha256=aicyqdJVDbd-ZO-wKHA8S_5CcPl3HDhRklSSdo
409
409
  cirq/protocols/inverse_protocol_test.py,sha256=pqqIU4_G4Npc9Z-SeoM9eCB2T5JRTeI02NCXhP0UtaI,2017
410
410
  cirq/protocols/json_serialization.py,sha256=VJCEXB9fkIZh3_d6dkh_QDxwjC3iJAlELGHsCmAMvE4,24779
411
411
  cirq/protocols/json_serialization_test.py,sha256=qrh6XTy6k-FCVAd3QvSV_ENHVf9UUgHOt_pzsniI6UM,28144
412
- cirq/protocols/kraus_protocol.py,sha256=Icazcmk9J1GSC0FL_dYLkrnpE_FH7G7xtawYuYbx3_g,9179
412
+ cirq/protocols/kraus_protocol.py,sha256=NVzMsIyqxytGAui-avLdq3K0C7UqjckrTDvr0ENmDtQ,9300
413
413
  cirq/protocols/kraus_protocol_test.py,sha256=NYVayiCaEpfvelsoR7bP57lUKn2pjFKYOiVOFHeZn9Q,5400
414
414
  cirq/protocols/measurement_key_protocol.py,sha256=vqoxjmthtmQ1nWbi7Xd4fdxFNe5y3WL7DZkQ3M_VPnc,13409
415
415
  cirq/protocols/measurement_key_protocol_test.py,sha256=W7yWKFVo6fMr7JjTECe8bMh1-NMDOpGMh3S-vmNl_3s,8654
416
- cirq/protocols/mixture_protocol.py,sha256=Hva80ZD09ZEfGa_jCGVNYygk_yyI02zzOEDykHejxPo,6301
417
- cirq/protocols/mixture_protocol_test.py,sha256=N09cs9WoXj3CBvs1p95yDpLK-h6WTManz3-xvHcFGO4,3615
416
+ cirq/protocols/mixture_protocol.py,sha256=L6FDIJmYqirVGsNwoUz5sulEDFoRUbhwz4yFL0ML2Po,6448
417
+ cirq/protocols/mixture_protocol_test.py,sha256=5mCh6JjlumMiSwDM818Nvo72AGxttuIYuawyESIQFtY,3818
418
418
  cirq/protocols/mul_protocol.py,sha256=ZhkowiHCulggWanjoz6HpMGf0ODqDNIjM32knFKPuJ0,2770
419
419
  cirq/protocols/mul_protocol_test.py,sha256=Qv7y17r-HjLXC7e-6t_Y6ht8lD_iiXE6CRyRFb7noGE,2136
420
420
  cirq/protocols/pauli_expansion_protocol.py,sha256=vtpVoBlU1pXm4RQf3DDGervXwcW8jqNxs6ETHPSsp1I,3750
@@ -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.dev20250405043305.dist-info/LICENSE,sha256=tAkwu8-AdEyGxGoSvJ2gVmQdcicWw3j1ZZueVV74M-E,11357
1213
- cirq_core-1.5.0.dev20250405043305.dist-info/METADATA,sha256=mP0FqihGxdqzrn2lCHUtmxPFrW_4FeXXFBMSk45NBrI,4584
1214
- cirq_core-1.5.0.dev20250405043305.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
1215
- cirq_core-1.5.0.dev20250405043305.dist-info/top_level.txt,sha256=Sz9iOxHU0IEMLSFGwiwOCaN2e9K-jFbBbtpPN1hB73g,5
1216
- cirq_core-1.5.0.dev20250405043305.dist-info/RECORD,,
1212
+ cirq_core-1.5.0.dev20250405050852.dist-info/LICENSE,sha256=tAkwu8-AdEyGxGoSvJ2gVmQdcicWw3j1ZZueVV74M-E,11357
1213
+ cirq_core-1.5.0.dev20250405050852.dist-info/METADATA,sha256=JbcLAzlUu5jxiad1Wd8Gb9vhve4q05YQnFhfyMBMgLI,4584
1214
+ cirq_core-1.5.0.dev20250405050852.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
1215
+ cirq_core-1.5.0.dev20250405050852.dist-info/top_level.txt,sha256=Sz9iOxHU0IEMLSFGwiwOCaN2e9K-jFbBbtpPN1hB73g,5
1216
+ cirq_core-1.5.0.dev20250405050852.dist-info/RECORD,,