cirq-core 1.2.0.dev20230629203543__py3-none-any.whl → 1.2.0.dev20230704233149__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
@@ -1 +1 @@
1
- __version__ = "1.2.0.dev20230629203543"
1
+ __version__ = "1.2.0.dev20230704233149"
cirq/ops/qubit_manager.py CHANGED
@@ -39,6 +39,7 @@ class QubitManager(metaclass=abc.ABCMeta):
39
39
  class _BaseAncillaQid(raw_types.Qid):
40
40
  id: int
41
41
  dim: int = 2
42
+ prefix: str = ''
42
43
 
43
44
  def _comparison_key(self) -> int:
44
45
  return self.id
@@ -49,7 +50,8 @@ class _BaseAncillaQid(raw_types.Qid):
49
50
 
50
51
  def __repr__(self) -> str:
51
52
  dim_str = f', dim={self.dim}' if self.dim != 2 else ''
52
- return f"cirq.ops.{type(self).__name__}({self.id}{dim_str})"
53
+ prefix_str = f', prefix={self.prefix}' if self.prefix != '' else ''
54
+ return f"cirq.ops.{type(self).__name__}({self.id}{dim_str}{prefix_str})"
53
55
 
54
56
 
55
57
  class CleanQubit(_BaseAncillaQid):
@@ -57,7 +59,7 @@ class CleanQubit(_BaseAncillaQid):
57
59
 
58
60
  def __str__(self) -> str:
59
61
  dim_str = f' (d={self.dimension})' if self.dim != 2 else ''
60
- return f"_c({self.id}){dim_str}"
62
+ return f"{self.prefix}_c({self.id}){dim_str}"
61
63
 
62
64
 
63
65
  class BorrowableQubit(_BaseAncillaQid):
@@ -65,23 +67,27 @@ class BorrowableQubit(_BaseAncillaQid):
65
67
 
66
68
  def __str__(self) -> str:
67
69
  dim_str = f' (d={self.dimension})' if self.dim != 2 else ''
68
- return f"_b({self.id}){dim_str}"
70
+ return f"{self.prefix}_b({self.id}){dim_str}"
69
71
 
70
72
 
71
73
  class SimpleQubitManager(QubitManager):
72
74
  """Allocates a new `CleanQubit`/`BorrowableQubit` for every `qalloc`/`qborrow` request."""
73
75
 
74
- def __init__(self):
76
+ def __init__(self, prefix: str = ''):
75
77
  self._clean_id = 0
76
78
  self._borrow_id = 0
79
+ self._prefix = prefix
77
80
 
78
81
  def qalloc(self, n: int, dim: int = 2) -> List['cirq.Qid']:
79
82
  self._clean_id += n
80
- return [CleanQubit(i, dim) for i in range(self._clean_id - n, self._clean_id)]
83
+ return [CleanQubit(i, dim, self._prefix) for i in range(self._clean_id - n, self._clean_id)]
81
84
 
82
85
  def qborrow(self, n: int, dim: int = 2) -> List['cirq.Qid']:
83
86
  self._borrow_id = self._borrow_id + n
84
- return [BorrowableQubit(i, dim) for i in range(self._borrow_id - n, self._borrow_id)]
87
+ return [
88
+ BorrowableQubit(i, dim, self._prefix)
89
+ for i in range(self._borrow_id - n, self._borrow_id)
90
+ ]
85
91
 
86
92
  def qfree(self, qubits: Iterable['cirq.Qid']) -> None:
87
93
  for q in qubits:
@@ -11,6 +11,7 @@
11
11
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
+ import itertools
14
15
  import dataclasses
15
16
  import inspect
16
17
  from collections import defaultdict
@@ -49,6 +50,8 @@ RaiseTypeErrorIfNotProvided: Any = ([],)
49
50
 
50
51
  DecomposeResult = Union[None, NotImplementedType, 'cirq.OP_TREE']
51
52
 
53
+ _CONTEXT_COUNTER = itertools.count() # Use _reset_context_counter() to reset the counter.
54
+
52
55
 
53
56
  @runtime_checkable
54
57
  class OpDecomposerWithContext(Protocol):
@@ -299,6 +302,8 @@ def decompose(
299
302
  "acceptable to keep."
300
303
  )
301
304
 
305
+ if context is None:
306
+ context = DecompositionContext(ops.SimpleQubitManager(prefix='_decompose_protocol'))
302
307
  args = _DecomposeArgs(
303
308
  context=context,
304
309
  intercepting_decomposer=intercepting_decomposer,
@@ -364,6 +369,11 @@ def decompose_once(
364
369
  TypeError: `val` didn't have a `_decompose_` method (or that method returned
365
370
  `NotImplemented` or `None`) and `default` wasn't set.
366
371
  """
372
+ if context is None:
373
+ context = DecompositionContext(
374
+ ops.SimpleQubitManager(prefix=f'_decompose_protocol_{next(_CONTEXT_COUNTER)}')
375
+ )
376
+
367
377
  method = getattr(val, '_decompose_with_context_', None)
368
378
  decomposed = NotImplemented if method is None else method(*args, **kwargs, context=context)
369
379
  if decomposed is NotImplemented or None:
@@ -11,6 +11,7 @@
11
11
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
+ import itertools
14
15
  from typing import Optional
15
16
  from unittest import mock
16
17
  import pytest
@@ -387,7 +388,7 @@ def test_decompose_recursive_dfs(with_context: bool):
387
388
  circuit = cirq.Circuit(moment)
388
389
  for val in [gate_op, tagged_op, controlled_op, classically_controlled_op, moment, circuit]:
389
390
  mock_qm.reset_mock()
390
- _ = cirq.decompose(val)
391
+ _ = cirq.decompose(val, context=cirq.DecompositionContext(qubit_manager=mock_qm))
391
392
  assert mock_qm.method_calls == expected_calls
392
393
 
393
394
  mock_qm.reset_mock()
@@ -398,3 +399,42 @@ def test_decompose_recursive_dfs(with_context: bool):
398
399
  if with_context
399
400
  else mock_qm.method_calls == expected_calls
400
401
  )
402
+
403
+
404
+ class G1(cirq.Gate):
405
+ def _num_qubits_(self) -> int:
406
+ return 1
407
+
408
+ def _decompose_with_context_(self, qubits, context):
409
+ yield cirq.CNOT(qubits[0], context.qubit_manager.qalloc(1)[0])
410
+
411
+
412
+ class G2(cirq.Gate):
413
+ def _num_qubits_(self) -> int:
414
+ return 1
415
+
416
+ def _decompose_with_context_(self, qubits, context):
417
+ yield G1()(*context.qubit_manager.qalloc(1))
418
+
419
+
420
+ @mock.patch('cirq.protocols.decompose_protocol._CONTEXT_COUNTER', itertools.count())
421
+ def test_successive_decompose_once_succeed():
422
+ op = G2()(cirq.NamedQubit('q'))
423
+ d1 = cirq.decompose_once(op)
424
+ d2 = cirq.decompose_once(d1[0])
425
+ assert d2 == [
426
+ cirq.CNOT(
427
+ cirq.ops.CleanQubit(0, prefix='_decompose_protocol_0'),
428
+ cirq.ops.CleanQubit(0, prefix='_decompose_protocol_1'),
429
+ )
430
+ ]
431
+
432
+
433
+ def test_decompose_without_context_succeed():
434
+ op = G2()(cirq.NamedQubit('q'))
435
+ assert cirq.decompose(op, keep=lambda op: op.gate is cirq.CNOT) == [
436
+ cirq.CNOT(
437
+ cirq.ops.CleanQubit(0, prefix='_decompose_protocol'),
438
+ cirq.ops.CleanQubit(1, prefix='_decompose_protocol'),
439
+ )
440
+ ]
@@ -20,6 +20,7 @@ import numpy as np
20
20
 
21
21
  from cirq import _compat, ops, value, qis
22
22
  from cirq.sim import simulator, state_vector, simulator_base
23
+ from cirq.protocols import qid_shape
23
24
 
24
25
  if TYPE_CHECKING:
25
26
  import cirq
@@ -31,7 +32,7 @@ TStateVectorStepResult = TypeVar('TStateVectorStepResult', bound='StateVectorSte
31
32
  class SimulatesIntermediateStateVector(
32
33
  Generic[TStateVectorStepResult],
33
34
  simulator_base.SimulatorBase[
34
- TStateVectorStepResult, 'cirq.StateVectorTrialResult', 'cirq.StateVectorSimulationState',
35
+ TStateVectorStepResult, 'cirq.StateVectorTrialResult', 'cirq.StateVectorSimulationState'
35
36
  ],
36
37
  simulator.SimulatesAmplitudes,
37
38
  metaclass=abc.ABCMeta,
@@ -172,7 +173,7 @@ class StateVectorTrialResult(
172
173
  size = np.prod(shape, dtype=np.int64)
173
174
  final = final.reshape(size)
174
175
  if len([1 for e in final if abs(e) > 0.001]) < 16:
175
- state_vector = qis.dirac_notation(final, 3)
176
+ state_vector = qis.dirac_notation(final, 3, qid_shape(substate.qubits))
176
177
  else:
177
178
  state_vector = str(final)
178
179
  label = f'qubits: {substate.qubits}' if substate.qubits else 'phase:'
@@ -159,6 +159,28 @@ def test_str_big():
159
159
  assert 'output vector: [0.03125+0.j 0.03125+0.j 0.03125+0.j ..' in str(result)
160
160
 
161
161
 
162
+ def test_str_qudit():
163
+ qutrit = cirq.LineQid(0, dimension=3)
164
+ final_simulator_state = cirq.StateVectorSimulationState(
165
+ prng=np.random.RandomState(0),
166
+ qubits=[qutrit],
167
+ initial_state=np.array([0, 0, 1]),
168
+ dtype=np.complex64,
169
+ )
170
+ result = cirq.StateVectorTrialResult(cirq.ParamResolver(), {}, final_simulator_state)
171
+ assert "|2⟩" in str(result)
172
+
173
+ ququart = cirq.LineQid(0, dimension=4)
174
+ final_simulator_state = cirq.StateVectorSimulationState(
175
+ prng=np.random.RandomState(0),
176
+ qubits=[ququart],
177
+ initial_state=np.array([0, 1, 0, 0]),
178
+ dtype=np.complex64,
179
+ )
180
+ result = cirq.StateVectorTrialResult(cirq.ParamResolver(), {}, final_simulator_state)
181
+ assert "|1⟩" in str(result)
182
+
183
+
162
184
  def test_pretty_print():
163
185
  final_simulator_state = cirq.StateVectorSimulationState(
164
186
  available_buffer=np.array([1]),
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: cirq-core
3
- Version: 1.2.0.dev20230629203543
3
+ Version: 1.2.0.dev20230704233149
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,7 +4,7 @@ cirq/_compat_test.py,sha256=yaTzjrXRFRHRZ5mom2NFrGP-1SeV7AlnH4fMmL5Jeps,33945
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=Ym8NriI8C54dsW7BzIJTCN2wM7BOVxl4Emzrckb6W5E,40
7
+ cirq/_version.py,sha256=k1a4_IMZAPnferSuFo0iDuGq31Fkzm0K8qiXKQmIdBk,40
8
8
  cirq/_version_test.py,sha256=ZM9GLAiU02rzyJQ_HOT2o_tZixJ0lMXs4tCkOareqTA,133
9
9
  cirq/conftest.py,sha256=mHCDs5--u17oLFDAfIlkTS4TRGSc35eLnZ2CXuIuB7I,1175
10
10
  cirq/json_resolver_cache.py,sha256=JJiO1dhHsEqYClUv68eg-hiOzbb_C1QiQ-BCcvoe4Ck,13067
@@ -347,7 +347,7 @@ cirq/ops/projector.py,sha256=isDlNLR8YS5CxsVCihtRAgfKZXFFQxk7kuFXY14t8ys,5690
347
347
  cirq/ops/projector_test.py,sha256=Wq7ddj-PV30yUXJxJoT3XIw2sIUC1AilnZ9m9N5Ejr8,9063
348
348
  cirq/ops/qid_util.py,sha256=2x9BCtJzTpfDAXvs5teROPsU1lA8gyr2JEaLWi1Q9y8,2148
349
349
  cirq/ops/qid_util_test.py,sha256=JdViBgFfH4bZJyPKTjUf9MuPxMQe08JV_Tl6tusekfk,1061
350
- cirq/ops/qubit_manager.py,sha256=GV-_U0ivGbQpoaC0Ebeno1UxLmbFanGCGukZCufCI9Y,3136
350
+ cirq/ops/qubit_manager.py,sha256=EbA8nC00lmi3YMkovSCJjCXYvS_AKIPW-GBzKbUaeyc,3381
351
351
  cirq/ops/qubit_manager_test.py,sha256=adogGKdWVulCwpByXC_KMP6YNst94R0wuW-5VTEozSk,2351
352
352
  cirq/ops/qubit_order.py,sha256=nYkcMg-y5QtR7M3W1HXq1IWJyMKjrTZXVK1GEbY6gng,5616
353
353
  cirq/ops/qubit_order_or_list.py,sha256=WVnhQcOYCgAhiB4t47Kji-pN1tnvs--X5deCQwwGVno,1165
@@ -385,8 +385,8 @@ cirq/protocols/commutes_protocol.py,sha256=ZSwtnkqB7as1y28alLfZWnMvvP1vFhel0-EML
385
385
  cirq/protocols/commutes_protocol_test.py,sha256=h0Lky4jrs7Hxrh4MeHxmxNciuofKGGZ2eC-ceWP8wKU,5849
386
386
  cirq/protocols/control_key_protocol.py,sha256=S3b4XOvTIj6TxNbj0W33fjdvXiWSKOtflvfEGeruzWw,2630
387
387
  cirq/protocols/control_key_protocol_test.py,sha256=190gp4QBu5QpP2doMmzx9RkAkp6VZOOWGOXp0RIFgqc,970
388
- cirq/protocols/decompose_protocol.py,sha256=p053AJFyo7TjbPKQRz6CyudNEed1cDmbEEPNz2Xzws4,18772
389
- cirq/protocols/decompose_protocol_test.py,sha256=2lH00efYg0ZXGIZ5dxO6NH8XKRcpQleAbbYYk-9w-78,14752
388
+ cirq/protocols/decompose_protocol.py,sha256=b4NCMzdI_RuHzkGhRBNL_vW1YiX8gmkMvCLEwqDMuK4,19164
389
+ cirq/protocols/decompose_protocol_test.py,sha256=zV-eAOYYqeXd73MuoqsRMe3FKj7Q1vxjJAizNKpZaHs,15969
390
390
  cirq/protocols/equal_up_to_global_phase_protocol.py,sha256=0a93790FDjID46cX94PVAlA9i9Fu7uN8cj6qT94NH9w,4101
391
391
  cirq/protocols/equal_up_to_global_phase_protocol_test.py,sha256=qpiPXPCncim2SEsnpJ-Xx5Bv_nRKBED9N-iRkJ6aWIc,5718
392
392
  cirq/protocols/has_stabilizer_effect_protocol.py,sha256=q7x5AlDtqgmbGWJPXKdPR3jYacwPoBHGsylHxqCvoMY,2685
@@ -897,8 +897,8 @@ cirq/sim/sparse_simulator_test.py,sha256=3EAeCHUQeKllAAtdw14X592zBsGQY_vwfIYK-gE
897
897
  cirq/sim/state_vector.py,sha256=N6N9EELlW66UaLTBaq62ms0XkfIK7CzN9SBM7t52dXo,13428
898
898
  cirq/sim/state_vector_simulation_state.py,sha256=ZTmRdf9J--L3hISroWmW9vgqPvj13s99I2axIgV5x7o,17631
899
899
  cirq/sim/state_vector_simulation_state_test.py,sha256=UtGMIurlV6N74nX7qoVnGoRhwF35-ghDEIP7Mj5AXmI,9841
900
- cirq/sim/state_vector_simulator.py,sha256=d_6s5cBgUVCSCgx6yxzKYlbRp-e9BBfFmlZetN8MIS8,7465
901
- cirq/sim/state_vector_simulator_test.py,sha256=Xl6v_WghJGZtEEK7EHw_1QGoXCVbnvKhVu1K8OJ5D24,7098
900
+ cirq/sim/state_vector_simulator.py,sha256=L2S0xyUGqRcVgYdzYef0gVREVZVIDcCXPclVbIJEpPE,7529
901
+ cirq/sim/state_vector_simulator_test.py,sha256=wJq1OZRzKokeM9cJyaJXi6wHH2qi97h0HmJlYOEBDzU,7864
902
902
  cirq/sim/state_vector_test.py,sha256=OjhAL2tWqJWstHV8RvJYQVqg95zm0PcS9nQKrLOhMmQ,16934
903
903
  cirq/sim/clifford/__init__.py,sha256=lD7l6JuE5n0xwvOYNYH-giCH3qAEVH1SUwDrZM1jKKY,636
904
904
  cirq/sim/clifford/clifford_simulator.py,sha256=6M69sv6IbtJTOk5ZE8CGWiILf6Ee2sCEALiZaw9NmvQ,9735
@@ -1136,8 +1136,8 @@ cirq/work/sampler.py,sha256=JVv1vvfa6EgFiR3UeDk44U186dCrioH2NZXueCgsb9w,19828
1136
1136
  cirq/work/sampler_test.py,sha256=zo1Hj6sn6fLs_WZMxYRApBqgBsldmptn74NL0jhNukc,12325
1137
1137
  cirq/work/zeros_sampler.py,sha256=D3hbNZC-jXKuNAWg2OUiUuT8pmDV_WFnEfMank6In4o,2357
1138
1138
  cirq/work/zeros_sampler_test.py,sha256=JIkpBBFPJe5Ba4142vzogyWyboG1Q1ZAm0UVGgOoZn8,3279
1139
- cirq_core-1.2.0.dev20230629203543.dist-info/LICENSE,sha256=tAkwu8-AdEyGxGoSvJ2gVmQdcicWw3j1ZZueVV74M-E,11357
1140
- cirq_core-1.2.0.dev20230629203543.dist-info/METADATA,sha256=6P2IeFXstBhyYNva39vgvhLU08mHF-V7wa4Jc-WcLKY,2095
1141
- cirq_core-1.2.0.dev20230629203543.dist-info/WHEEL,sha256=pkctZYzUS4AYVn6dJ-7367OJZivF2e8RA9b_ZBjif18,92
1142
- cirq_core-1.2.0.dev20230629203543.dist-info/top_level.txt,sha256=Sz9iOxHU0IEMLSFGwiwOCaN2e9K-jFbBbtpPN1hB73g,5
1143
- cirq_core-1.2.0.dev20230629203543.dist-info/RECORD,,
1139
+ cirq_core-1.2.0.dev20230704233149.dist-info/LICENSE,sha256=tAkwu8-AdEyGxGoSvJ2gVmQdcicWw3j1ZZueVV74M-E,11357
1140
+ cirq_core-1.2.0.dev20230704233149.dist-info/METADATA,sha256=59UAg0yYVU9wwJDLEW7dnwd6O3pslgVl4P1V8T-siHY,2095
1141
+ cirq_core-1.2.0.dev20230704233149.dist-info/WHEEL,sha256=pkctZYzUS4AYVn6dJ-7367OJZivF2e8RA9b_ZBjif18,92
1142
+ cirq_core-1.2.0.dev20230704233149.dist-info/top_level.txt,sha256=Sz9iOxHU0IEMLSFGwiwOCaN2e9K-jFbBbtpPN1hB73g,5
1143
+ cirq_core-1.2.0.dev20230704233149.dist-info/RECORD,,