cirq-core 1.5.0.dev20241028183905__py3-none-any.whl → 1.5.0.dev20241030184510__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.dev20241028183905"
31
+ __version__ = "1.5.0.dev20241030184510"
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.dev20241028183905"
6
+ assert cirq.__version__ == "1.5.0.dev20241030184510"
@@ -148,3 +148,8 @@ from cirq.transformers.gauge_compiling import (
148
148
  from cirq.transformers.randomized_measurements import (
149
149
  RandomizedMeasurements as RandomizedMeasurements,
150
150
  )
151
+
152
+
153
+ from cirq.transformers.insertion_sort import (
154
+ insertion_sort_transformer as insertion_sort_transformer,
155
+ )
@@ -0,0 +1,64 @@
1
+ # Copyright 2024 The Cirq Developers
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """Transformer that sorts commuting operations in increasing order of their `.qubits` tuple."""
16
+
17
+ from typing import Dict, List, Optional, TYPE_CHECKING
18
+
19
+ from cirq import protocols, circuits
20
+ from cirq.transformers import transformer_api
21
+
22
+ if TYPE_CHECKING:
23
+ import cirq
24
+
25
+
26
+ @transformer_api.transformer(add_deep_support=True)
27
+ def insertion_sort_transformer(
28
+ circuit: 'cirq.AbstractCircuit', *, context: Optional['cirq.TransformerContext'] = None
29
+ ) -> 'cirq.Circuit':
30
+ """Sorts the operations using their sorted `.qubits` property as comparison key.
31
+
32
+ Operations are swapped only if they commute.
33
+
34
+ Args:
35
+ circuit: input circuit.
36
+ context: optional TransformerContext (not used),
37
+ """
38
+ final_operations: List['cirq.Operation'] = []
39
+ qubit_index: Dict['cirq.Qid', int] = {
40
+ q: idx for idx, q in enumerate(sorted(circuit.all_qubits()))
41
+ }
42
+ cached_qubit_indices: Dict[int, List[int]] = {}
43
+ for pos, op in enumerate(circuit.all_operations()):
44
+ # here `pos` is at the append position of final_operations
45
+ if (op_qubit_indices := cached_qubit_indices.get(id(op))) is None:
46
+ op_qubit_indices = cached_qubit_indices[id(op)] = sorted(
47
+ qubit_index[q] for q in op.qubits
48
+ )
49
+ for tail_op in reversed(final_operations):
50
+ tail_qubit_indices = cached_qubit_indices[id(tail_op)]
51
+ if op_qubit_indices < tail_qubit_indices and (
52
+ # special case for zero-qubit gates
53
+ not op_qubit_indices
54
+ # check if two sorted sequences are disjoint
55
+ or op_qubit_indices[-1] < tail_qubit_indices[0]
56
+ or set(op_qubit_indices).isdisjoint(tail_qubit_indices)
57
+ # fallback to more expensive commutation check
58
+ or protocols.commutes(op, tail_op, default=False)
59
+ ):
60
+ pos -= 1
61
+ continue
62
+ break
63
+ final_operations.insert(pos, op)
64
+ return circuits.Circuit(final_operations)
@@ -0,0 +1,34 @@
1
+ # Copyright 2024 The Cirq Developers
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import cirq
16
+ import cirq.transformers
17
+
18
+
19
+ def test_insertion_sort():
20
+ c = cirq.Circuit(
21
+ cirq.CZ(cirq.q(2), cirq.q(1)),
22
+ cirq.CZ(cirq.q(2), cirq.q(4)),
23
+ cirq.CZ(cirq.q(0), cirq.q(1)),
24
+ cirq.CZ(cirq.q(2), cirq.q(1)),
25
+ cirq.GlobalPhaseGate(1j).on(),
26
+ )
27
+ sorted_circuit = cirq.transformers.insertion_sort_transformer(c)
28
+ assert sorted_circuit == cirq.Circuit(
29
+ cirq.GlobalPhaseGate(1j).on(),
30
+ cirq.CZ(cirq.q(0), cirq.q(1)),
31
+ cirq.CZ(cirq.q(2), cirq.q(1)),
32
+ cirq.CZ(cirq.q(2), cirq.q(1)),
33
+ cirq.CZ(cirq.q(2), cirq.q(4)),
34
+ )
@@ -86,6 +86,7 @@ class CompilationTargetGateset(ops.Gateset, metaclass=abc.ABCMeta):
86
86
  name: Optional[str] = None,
87
87
  unroll_circuit_op: bool = True,
88
88
  preserve_moment_structure: bool = True,
89
+ reorder_operations: bool = False,
89
90
  ):
90
91
  """Initializes CompilationTargetGateset.
91
92
 
@@ -97,9 +98,18 @@ class CompilationTargetGateset(ops.Gateset, metaclass=abc.ABCMeta):
97
98
  validated by validating the underlying `cirq.Circuit`.
98
99
  preserve_moment_structure: Whether to preserve the moment structure of the
99
100
  circuit during compilation or not.
101
+ reorder_operations: Whether to attempt to reorder the operations in order to reduce
102
+ circuit depth or not (can be True only if preserve_moment_structure=False).
103
+ Raises:
104
+ ValueError: If both reorder_operations and preserve_moment_structure are True.
100
105
  """
106
+ if reorder_operations and preserve_moment_structure:
107
+ raise ValueError(
108
+ 'reorder_operations and preserve_moment_structure can not both be True'
109
+ )
101
110
  super().__init__(*gates, name=name, unroll_circuit_op=unroll_circuit_op)
102
111
  self._preserve_moment_structure = preserve_moment_structure
112
+ self._reorder_operations = reorder_operations
103
113
 
104
114
  @property
105
115
  @abc.abstractmethod
@@ -146,11 +156,15 @@ class CompilationTargetGateset(ops.Gateset, metaclass=abc.ABCMeta):
146
156
  @property
147
157
  def preprocess_transformers(self) -> List['cirq.TRANSFORMER']:
148
158
  """List of transformers which should be run before decomposing individual operations."""
159
+ reorder_transfomers = (
160
+ [transformers.insertion_sort_transformer] if self._reorder_operations else []
161
+ )
149
162
  return [
150
163
  create_transformer_with_kwargs(
151
164
  transformers.expand_composite,
152
165
  no_decomp=lambda op: protocols.num_qubits(op) <= self.num_qubits,
153
166
  ),
167
+ *reorder_transfomers,
154
168
  create_transformer_with_kwargs(
155
169
  merge_k_qubit_gates.merge_k_qubit_unitaries,
156
170
  k=self.num_qubits,
@@ -49,6 +49,7 @@ class CZTargetGateset(compilation_target_gateset.TwoQubitCompilationTargetGatese
49
49
  allow_partial_czs: bool = False,
50
50
  additional_gates: Sequence[Union[Type['cirq.Gate'], 'cirq.Gate', 'cirq.GateFamily']] = (),
51
51
  preserve_moment_structure: bool = True,
52
+ reorder_operations: bool = False,
52
53
  ) -> None:
53
54
  """Initializes CZTargetGateset
54
55
 
@@ -60,6 +61,8 @@ class CZTargetGateset(compilation_target_gateset.TwoQubitCompilationTargetGatese
60
61
  be "accepted" by this gateset. This is empty by default.
61
62
  preserve_moment_structure: Whether to preserve the moment structure of the
62
63
  circuit during compilation or not.
64
+ reorder_operations: Whether to attempt to reorder the operations in order to reduce
65
+ circuit depth or not (can be True only if preserve_moment_structure=False).
63
66
  """
64
67
  super().__init__(
65
68
  ops.CZPowGate if allow_partial_czs else ops.CZ,
@@ -69,6 +72,7 @@ class CZTargetGateset(compilation_target_gateset.TwoQubitCompilationTargetGatese
69
72
  *additional_gates,
70
73
  name='CZPowTargetGateset' if allow_partial_czs else 'CZTargetGateset',
71
74
  preserve_moment_structure=preserve_moment_structure,
75
+ reorder_operations=reorder_operations,
72
76
  )
73
77
  self.additional_gates = tuple(
74
78
  g if isinstance(g, ops.GateFamily) else ops.GateFamily(gate=g) for g in additional_gates
@@ -319,3 +319,22 @@ def test_unsupported_gate():
319
319
  )
320
320
  def test_repr(gateset):
321
321
  cirq.testing.assert_equivalent_repr(gateset)
322
+
323
+
324
+ def test_with_commutation():
325
+ c = cirq.Circuit(
326
+ cirq.CZ(cirq.q(0), cirq.q(1)), cirq.CZ(cirq.q(1), cirq.q(2)), cirq.CZ(cirq.q(0), cirq.q(1))
327
+ )
328
+ got = cirq.optimize_for_target_gateset(
329
+ c,
330
+ gateset=cirq.CZTargetGateset(preserve_moment_structure=False, reorder_operations=True),
331
+ max_num_passes=1,
332
+ )
333
+ assert got == cirq.Circuit(cirq.CZ(cirq.q(1), cirq.q(2)))
334
+
335
+
336
+ def test_reorder_operations_and_preserve_moment_structure_raises():
337
+ with pytest.raises(
338
+ ValueError, match='reorder_operations and preserve_moment_structure can not both be True'
339
+ ):
340
+ _ = cirq.CZTargetGateset(preserve_moment_structure=True, reorder_operations=True)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: cirq-core
3
- Version: 1.5.0.dev20241028183905
3
+ Version: 1.5.0.dev20241030184510
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=3IiIATzwlV1upJzvXw0UZy6fu4n_YeB_8p-bCzR00QM,1206
8
- cirq/_version_test.py,sha256=94XwthEQxZNTDejp24Nne93gtOLGxV1KNift0jGwsjs,147
7
+ cirq/_version.py,sha256=cGjYAFK1kb_f_bComEY-4kxVfz8DTK4e4tLZSeN7Dqg,1206
8
+ cirq/_version_test.py,sha256=AvbtaK4AQENKcY0yl9MG11qtXwI3xDTsRJxXm7S5I5c,147
9
9
  cirq/conftest.py,sha256=X7yLFL8GLhg2CjPw0hp5e_dGASfvHx1-QT03aUbhKJw,1168
10
10
  cirq/json_resolver_cache.py,sha256=ytePZtNZgKjOF2NiVpUTuotB-JKZmQNOFIFdvXqsxHw,13271
11
11
  cirq/py.typed,sha256=VFSlmh_lNwnaXzwY-ZuW-C2Ws5PkuDoVgBdNCs0jXJE,63
@@ -1022,7 +1022,7 @@ cirq/testing/test_data/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3
1022
1022
  cirq/testing/test_data/test_module_missing_json_test_data/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
1023
1023
  cirq/testing/test_data/test_module_missing_testspec/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
1024
1024
  cirq/testing/test_data/test_module_missing_testspec/json_test_data/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
1025
- cirq/transformers/__init__.py,sha256=b_RQ5kTwwMeHFGSdBMNLDjjtaOJ0Oqb526XZN8WTgM4,6792
1025
+ cirq/transformers/__init__.py,sha256=QjYovkmQHRi7D6nPHkWrszVBM_1__thvOXG4wcYvX_E,6905
1026
1026
  cirq/transformers/align.py,sha256=B4DuX84DWd4tLfMH21JviMZSOFYu7KCOMSEMALYsQpw,2727
1027
1027
  cirq/transformers/align_test.py,sha256=M4etT2cgESv1RdkKASguiGxEuqY7kmI1IswjSi-1jjY,7174
1028
1028
  cirq/transformers/drop_empty_moments.py,sha256=Rtn_BrpwkLXyZBdLzwdnsnEGWTdYuf1xOPakzbpv7-w,1517
@@ -1037,6 +1037,8 @@ cirq/transformers/eject_z.py,sha256=d53z1siUVCPrtNbPls6_RSujz6d2gF77_AQAWhnJmVM,
1037
1037
  cirq/transformers/eject_z_test.py,sha256=U0BMdW6nW1cI18I5tE__1YpCvtzDwYGECgqUph5Fc8I,13302
1038
1038
  cirq/transformers/expand_composite.py,sha256=nASRoP4qfjsnX_t2a2hBw8BE7B_JD-0XLGIIXxbIdbc,2387
1039
1039
  cirq/transformers/expand_composite_test.py,sha256=4Gn6LVqr0DeuUumde80O4esOLGIoo86_S_Mk-HwnMfk,8640
1040
+ cirq/transformers/insertion_sort.py,sha256=cAwseOcAdBFoPlP8F45B_XP8ySGmkPHn0EftTVVsF9Q,2612
1041
+ cirq/transformers/insertion_sort_test.py,sha256=q81tMHSmlxTRqznkfgIGxanF-1fX8GclopAHTaF6yHs,1187
1040
1042
  cirq/transformers/measurement_transformers.py,sha256=m2v4FAEaIZtYXEh-rFzxa_sx-syqdWZmCbf6yar0GSM,19035
1041
1043
  cirq/transformers/measurement_transformers_test.py,sha256=tkuevHQWnFWmxmvfJZ2T42uCIw4oaYyZ4XvjRGFGdug,27504
1042
1044
  cirq/transformers/merge_k_qubit_gates.py,sha256=dUsswQOIHfbb6Lu37fecgrpT6_45zmDE7eqCUbW1KOI,4390
@@ -1118,10 +1120,10 @@ cirq/transformers/routing/route_circuit_cqc_test.py,sha256=gKGIBwftHR6CkUGdZ5z8t
1118
1120
  cirq/transformers/routing/visualize_routed_circuit.py,sha256=cKYvswXnNIQJ0Okketzkpy9Tw0Z_Y2DaG4cJmK9ouHY,2917
1119
1121
  cirq/transformers/routing/visualize_routed_circuit_test.py,sha256=L3IJ0UdyVg8cBIULjG5kcjWoTmKee9Sr_MHkyJwcopA,4140
1120
1122
  cirq/transformers/target_gatesets/__init__.py,sha256=q7v9KTCXZ8OyWUy89xBM4hp5b-M2hbXMyS3SYFJ0wGQ,1143
1121
- cirq/transformers/target_gatesets/compilation_target_gateset.py,sha256=-dqUX5nRU70ceM5jfDmadVYd8bAhqN3bxM7ZHMRRmnM,13537
1123
+ cirq/transformers/target_gatesets/compilation_target_gateset.py,sha256=8gh_-CRvfe_h9-p9YNcZ9mEuVdqZ9BLEALeU05wcp9c,14287
1122
1124
  cirq/transformers/target_gatesets/compilation_target_gateset_test.py,sha256=_M8R9n8vklqlCHesokcNINDGTTLx22TIa4tVLQNswoE,9482
1123
- cirq/transformers/target_gatesets/cz_gateset.py,sha256=5YJVD0uyb6w848_x8Qg5nLYMKyylyWmgDBNtm725oAU,4750
1124
- cirq/transformers/target_gatesets/cz_gateset_test.py,sha256=M5rhQPt2XGrX26cvyknTIFGLBqa2jPgg4D0z8rhFogU,10584
1125
+ cirq/transformers/target_gatesets/cz_gateset.py,sha256=qN-TvRzQmTziafHwAQm6P8nqSO_b-b4nMpK8qqjhWt8,5031
1126
+ cirq/transformers/target_gatesets/cz_gateset_test.py,sha256=PvdIYSFtEA8FXYm4ZwdNk1RaQIa0rtTImjjH-6YbmDI,11277
1125
1127
  cirq/transformers/target_gatesets/sqrt_iswap_gateset.py,sha256=Rtoi2xmhlk_fobf5Hwc1nDQlLtmKPcJm2FJTR8JgpN0,6278
1126
1128
  cirq/transformers/target_gatesets/sqrt_iswap_gateset_test.py,sha256=-Oi3b9oX-0_3U9SKAkijVO-uBy4ut4PrFnsx-yFJ9mU,14391
1127
1129
  cirq/value/__init__.py,sha256=gk5m8gFTJiNtiOxO3SjsidqKI5L8ZLhRxQyZCyjITqU,2899
@@ -1184,8 +1186,8 @@ cirq/work/sampler.py,sha256=rzimb1Zmqrdb0coOJhGf459KTUaFpAnIR2Vm0mt1_i8,20848
1184
1186
  cirq/work/sampler_test.py,sha256=0HoptI2r-7yR5hmlvu6ykqkZ8TYO-lkIVvlv8yyDnDk,14885
1185
1187
  cirq/work/zeros_sampler.py,sha256=x1C7cup66a43n-3tm8QjhiqJa07qcJW10FxNp9jJ59Q,2356
1186
1188
  cirq/work/zeros_sampler_test.py,sha256=JIkpBBFPJe5Ba4142vzogyWyboG1Q1ZAm0UVGgOoZn8,3279
1187
- cirq_core-1.5.0.dev20241028183905.dist-info/LICENSE,sha256=tAkwu8-AdEyGxGoSvJ2gVmQdcicWw3j1ZZueVV74M-E,11357
1188
- cirq_core-1.5.0.dev20241028183905.dist-info/METADATA,sha256=q8NRE1EH66sAQsVIeGnFzO6MhwEyIzJLArPfNqWT29Q,1992
1189
- cirq_core-1.5.0.dev20241028183905.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
1190
- cirq_core-1.5.0.dev20241028183905.dist-info/top_level.txt,sha256=Sz9iOxHU0IEMLSFGwiwOCaN2e9K-jFbBbtpPN1hB73g,5
1191
- cirq_core-1.5.0.dev20241028183905.dist-info/RECORD,,
1189
+ cirq_core-1.5.0.dev20241030184510.dist-info/LICENSE,sha256=tAkwu8-AdEyGxGoSvJ2gVmQdcicWw3j1ZZueVV74M-E,11357
1190
+ cirq_core-1.5.0.dev20241030184510.dist-info/METADATA,sha256=8FnTTAd3YmYT07Z_zSjfsL5_Pnqz-YjQ_wnUp0m6QoM,1992
1191
+ cirq_core-1.5.0.dev20241030184510.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
1192
+ cirq_core-1.5.0.dev20241030184510.dist-info/top_level.txt,sha256=Sz9iOxHU0IEMLSFGwiwOCaN2e9K-jFbBbtpPN1hB73g,5
1193
+ cirq_core-1.5.0.dev20241030184510.dist-info/RECORD,,