cirq-core 1.4.0.dev20240518010119__py3-none-any.whl → 1.4.0.dev20240521175101__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/__init__.py CHANGED
@@ -332,6 +332,7 @@ from cirq.ops import (
332
332
  ZPowGate,
333
333
  ZZ,
334
334
  ZZPowGate,
335
+ UniformSuperpositionGate,
335
336
  )
336
337
 
337
338
  from cirq.transformers import (
cirq/_version.py CHANGED
@@ -1 +1 @@
1
- __version__ = "1.4.0.dev20240518010119"
1
+ __version__ = "1.4.0.dev20240521175101"
@@ -247,6 +247,7 @@ def _class_resolver_dictionary() -> Dict[str, ObjectFactory]:
247
247
  'ZipLongest': cirq.ZipLongest,
248
248
  'ZPowGate': cirq.ZPowGate,
249
249
  'ZZPowGate': cirq.ZZPowGate,
250
+ 'UniformSuperpositionGate': cirq.UniformSuperpositionGate,
250
251
  # Old types, only supported for backwards-compatibility
251
252
  'BooleanHamiltonian': _boolean_hamiltonian_gate_op, # Removed in v0.15
252
253
  'CrossEntropyResult': _cross_entropy_result, # Removed in v0.16
cirq/ops/__init__.py CHANGED
@@ -217,3 +217,5 @@ from cirq.ops.wait_gate import wait, WaitGate
217
217
  from cirq.ops.state_preparation_channel import StatePreparationChannel
218
218
 
219
219
  from cirq.ops.control_values import AbstractControlValues, ProductOfSums, SumOfProducts
220
+
221
+ from cirq.ops.uniform_superposition_gate import UniformSuperpositionGate
@@ -0,0 +1,123 @@
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
+ from typing import Sequence, Any, Dict, TYPE_CHECKING
16
+
17
+ import numpy as np
18
+ from cirq.ops.common_gates import H, ry
19
+ from cirq.ops.pauli_gates import X
20
+ from cirq.ops import raw_types
21
+
22
+
23
+ if TYPE_CHECKING:
24
+ import cirq
25
+
26
+
27
+ class UniformSuperpositionGate(raw_types.Gate):
28
+ r"""Creates a uniform superposition state on the states $[0, M)$
29
+ The gate creates the state $\frac{1}{\sqrt{M}}\sum_{j=0}^{M-1}\ket{j}$
30
+ (where $1\leq M \leq 2^n$), using n qubits, according to the Shukla-Vedula algorithm [SV24].
31
+ References:
32
+ [SV24]
33
+ [An efficient quantum algorithm for preparation of uniform quantum superposition
34
+ states](https://arxiv.org/abs/2306.11747)
35
+ """
36
+
37
+ def __init__(self, m_value: int, num_qubits: int) -> None:
38
+ """Initializes UniformSuperpositionGate.
39
+
40
+ Args:
41
+ m_value: The number of computational basis states.
42
+ num_qubits: The number of qubits used.
43
+
44
+ Raises:
45
+ ValueError: If `m_value` is not a positive integer, or
46
+ if `num_qubits` is not an integer greater than or equal to log2(m_value).
47
+ """
48
+ if not (isinstance(m_value, int) and (m_value > 0)):
49
+ raise ValueError("m_value must be a positive integer.")
50
+ log_two_m_value = m_value.bit_length()
51
+
52
+ if (m_value & (m_value - 1)) == 0:
53
+ log_two_m_value = log_two_m_value - 1
54
+ if not (isinstance(num_qubits, int) and (num_qubits >= log_two_m_value)):
55
+ raise ValueError(
56
+ "num_qubits must be an integer greater than or equal to log2(m_value)."
57
+ )
58
+ self._m_value = m_value
59
+ self._num_qubits = num_qubits
60
+
61
+ def _decompose_(self, qubits: Sequence["cirq.Qid"]) -> "cirq.OP_TREE":
62
+ """Decomposes the gate into a sequence of standard gates.
63
+ Implements the construction from https://arxiv.org/pdf/2306.11747.
64
+ """
65
+ qreg = list(qubits)
66
+ qreg.reverse()
67
+
68
+ if self._m_value == 1: # if m_value is 1, do nothing
69
+ return
70
+ if (self._m_value & (self._m_value - 1)) == 0: # if m_value is an integer power of 2
71
+ m = self._m_value.bit_length() - 1
72
+ yield H.on_each(qreg[:m])
73
+ return
74
+ k = self._m_value.bit_length()
75
+ l_value = []
76
+ for i in range(self._m_value.bit_length()):
77
+ if (self._m_value >> i) & 1:
78
+ l_value.append(i) # Locations of '1's
79
+
80
+ yield X.on_each(qreg[q_bit] for q_bit in l_value[1:k])
81
+ m_current = 2 ** (l_value[0])
82
+ theta = -2 * np.arccos(np.sqrt(m_current / self._m_value))
83
+ if l_value[0] > 0: # if m_value is even
84
+ yield H.on_each(qreg[: l_value[0]])
85
+
86
+ yield ry(theta).on(qreg[l_value[1]])
87
+
88
+ for i in range(l_value[0], l_value[1]):
89
+ yield H(qreg[i]).controlled_by(qreg[l_value[1]], control_values=[False])
90
+
91
+ for m in range(1, len(l_value) - 1):
92
+ theta = -2 * np.arccos(np.sqrt(2 ** l_value[m] / (self._m_value - m_current)))
93
+ yield ry(theta).on(qreg[l_value[m + 1]]).controlled_by(
94
+ qreg[l_value[m]], control_values=[0]
95
+ )
96
+ for i in range(l_value[m], l_value[m + 1]):
97
+ yield H.on(qreg[i]).controlled_by(qreg[l_value[m + 1]], control_values=[0])
98
+
99
+ m_current = m_current + 2 ** (l_value[m])
100
+
101
+ def num_qubits(self) -> int:
102
+ return self._num_qubits
103
+
104
+ @property
105
+ def m_value(self) -> int:
106
+ return self._m_value
107
+
108
+ def __eq__(self, other):
109
+ if isinstance(other, UniformSuperpositionGate):
110
+ return (self._m_value == other._m_value) and (self._num_qubits == other._num_qubits)
111
+ return False
112
+
113
+ def __repr__(self) -> str:
114
+ return f'UniformSuperpositionGate(m_value={self._m_value}, num_qubits={self._num_qubits})'
115
+
116
+ def _json_dict_(self) -> Dict[str, Any]:
117
+ d = {}
118
+ d['m_value'] = self._m_value
119
+ d['num_qubits'] = self._num_qubits
120
+ return d
121
+
122
+ def __str__(self) -> str:
123
+ return f'UniformSuperpositionGate(m_value={self._m_value}, num_qubits={self._num_qubits})'
@@ -0,0 +1,94 @@
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 numpy as np
16
+ import pytest
17
+ import cirq
18
+
19
+
20
+ @pytest.mark.parametrize(
21
+ ["m", "n"],
22
+ [[int(m), n] for n in range(3, 7) for m in np.random.randint(1, 1 << n, size=3)]
23
+ + [(1, 2), (4, 2), (6, 3), (7, 3)],
24
+ )
25
+ def test_generated_unitary_is_uniform(m: int, n: int) -> None:
26
+ r"""The code checks that the unitary matrix corresponds to the generated uniform superposition
27
+ states (see uniform_superposition_gate.py). It is enough to check that the
28
+ first colum of the unitary matrix (which corresponds to the action of the gate on
29
+ $\ket{0}^n$ is $\frac{1}{\sqrt{M}} [1 1 \cdots 1 0 \cdots 0]^T$, where the first $M$
30
+ entries are all "1"s (excluding the normalization factor of $\frac{1}{\sqrt{M}}$ and the
31
+ remaining $2^n-M$ entries are all "0"s.
32
+ """
33
+ gate = cirq.UniformSuperpositionGate(m, n)
34
+ matrix = np.array(cirq.unitary(gate))
35
+ np.testing.assert_allclose(
36
+ matrix[:, 0], (1 / np.sqrt(m)) * np.array([1] * m + [0] * (2**n - m)), atol=1e-8
37
+ )
38
+
39
+
40
+ @pytest.mark.parametrize(["m", "n"], [(1, 1), (-2, 1), (-3.1, 2), (6, -4), (5, 6.1)])
41
+ def test_incompatible_m_value_and_qubit_args(m: int, n: int) -> None:
42
+ r"""The code checks that test errors are raised if the arguments m (number of
43
+ superposition states and n (number of qubits) are positive integers and are compatible
44
+ (i.e., n >= log2(m)).
45
+ """
46
+
47
+ if not (isinstance(m, int)):
48
+ with pytest.raises(ValueError, match="m_value must be a positive integer."):
49
+ cirq.UniformSuperpositionGate(m, n)
50
+ elif not (isinstance(n, int)):
51
+ with pytest.raises(
52
+ ValueError,
53
+ match="num_qubits must be an integer greater than or equal to log2\\(m_value\\).",
54
+ ):
55
+ cirq.UniformSuperpositionGate(m, n)
56
+ elif m < 1:
57
+ with pytest.raises(ValueError, match="m_value must be a positive integer."):
58
+ cirq.UniformSuperpositionGate(int(m), int(n))
59
+ elif n < np.log2(m):
60
+ with pytest.raises(
61
+ ValueError,
62
+ match="num_qubits must be an integer greater than or equal to log2\\(m_value\\).",
63
+ ):
64
+ cirq.UniformSuperpositionGate(m, n)
65
+
66
+
67
+ def test_repr():
68
+ assert (
69
+ repr(cirq.UniformSuperpositionGate(7, 3))
70
+ == 'UniformSuperpositionGate(m_value=7, num_qubits=3)'
71
+ )
72
+
73
+
74
+ def test_uniform_superposition_gate_json_dict():
75
+ assert cirq.UniformSuperpositionGate(7, 3)._json_dict_() == {'m_value': 7, 'num_qubits': 3}
76
+
77
+
78
+ def test_str():
79
+ assert (
80
+ str(cirq.UniformSuperpositionGate(7, 3))
81
+ == 'UniformSuperpositionGate(m_value=7, num_qubits=3)'
82
+ )
83
+
84
+
85
+ @pytest.mark.parametrize(["m", "n"], [(5, 3), (10, 4)])
86
+ def test_eq(m: int, n: int) -> None:
87
+ a = cirq.UniformSuperpositionGate(m, n)
88
+ b = cirq.UniformSuperpositionGate(m, n)
89
+ c = cirq.UniformSuperpositionGate(m + 1, n)
90
+ d = cirq.X
91
+ assert a.m_value == b.m_value
92
+ assert a.__eq__(b)
93
+ assert not (a.__eq__(c))
94
+ assert not (a.__eq__(d))
@@ -0,0 +1,5 @@
1
+ {
2
+ "cirq_type": "UniformSuperpositionGate",
3
+ "m_value": 7,
4
+ "num_qubits": 3
5
+ }
@@ -0,0 +1 @@
1
+ cirq.UniformSuperpositionGate(m_value=7, num_qubits=3)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: cirq-core
3
- Version: 1.4.0.dev20240518010119
3
+ Version: 1.4.0.dev20240521175101
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
@@ -1,13 +1,13 @@
1
- cirq/__init__.py,sha256=gQkxjgGyat-egE2jZlrLCVMofvpG_VA1021yh8Tv9j8,15766
1
+ cirq/__init__.py,sha256=dEwUzkNCNrL4qNyln6GUUrk7vGxY8OXNG9lk_C2gi6w,15796
2
2
  cirq/_compat.py,sha256=2tkJ50ID2PXzmMNuTfQrsZqqRSblTmfu3Y7g742gojs,29342
3
3
  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=fvYIncCrY6PRJBq5ZOweGpxrN8hohY9wqDC3_EtTFAI,40
7
+ cirq/_version.py,sha256=gjXL3lv_qbsRaFrke8tn-Un4QjDXXzaoPaCiMqKwWCg,40
8
8
  cirq/_version_test.py,sha256=yYS6xm5-nuBRQJa9R3n41WdvFtVyY7Lb5Q8bea3VgBI,133
9
9
  cirq/conftest.py,sha256=X7yLFL8GLhg2CjPw0hp5e_dGASfvHx1-QT03aUbhKJw,1168
10
- cirq/json_resolver_cache.py,sha256=9g_JQMmfBzTuV-3s2flUbXIgcLjs4K7LjAFFgngdG1U,13204
10
+ cirq/json_resolver_cache.py,sha256=ytePZtNZgKjOF2NiVpUTuotB-JKZmQNOFIFdvXqsxHw,13271
11
11
  cirq/py.typed,sha256=VFSlmh_lNwnaXzwY-ZuW-C2Ws5PkuDoVgBdNCs0jXJE,63
12
12
  cirq/type_workarounds.py,sha256=-qyat6dmVoz8z76I-JLrnK1vAXmY9G1Qa3LYjQ4rJlI,959
13
13
  cirq/circuits/__init__.py,sha256=UPBO-WG6VodQWDkYgJdBzKK8k9CsxHFGIENM95vh7ac,1117
@@ -262,7 +262,7 @@ cirq/neutral_atoms/__init__.py,sha256=D0ewdZZvXM_PC7WiyyM8V3WqBAwrpDV_GU_sCIbtw2
262
262
  cirq/neutral_atoms/convert_to_neutral_atom_gates.py,sha256=SsXFh1-NoBGqp4yX8-jIbIw-AK40baA-qh-iTL1wS6Q,1070
263
263
  cirq/neutral_atoms/convert_to_neutral_atom_gates_test.py,sha256=mIeGevxs9NoYpfTF_znHL67RrJKVQyQP-DPhn7t9SUA,1862
264
264
  cirq/neutral_atoms/neutral_atom_devices.py,sha256=s-LInrNp8k_txKbpLWfsaoiZvUScOWNxr-jiB-nFcDA,1358
265
- cirq/ops/__init__.py,sha256=x-M1ZOcdguA563aWyr65Ll-zuT191ek0BXTd-90u3U4,5381
265
+ cirq/ops/__init__.py,sha256=7xt2bPwndo2OhuLLnI5GwBpG9rZHlt1rv7cp-bOBmxM,5455
266
266
  cirq/ops/arithmetic_operation.py,sha256=PBqIwOfADRlsij11Lo1ao_OZM-O8PDlObgZBxGKsYKs,10125
267
267
  cirq/ops/arithmetic_operation_test.py,sha256=axy8xy9IvDb-ATUV-LE1HNWRqCEz06VyZWVrLNOtXXI,4942
268
268
  cirq/ops/boolean_hamiltonian.py,sha256=hjmzBvWiZnb18JfdhId-dVxt2VmbyAbqMSFAPWV7s7I,14883
@@ -370,6 +370,8 @@ cirq/ops/three_qubit_gates.py,sha256=U6GGi1aNnLbhM40ViSsRqbOIA51i1kjKMEf5ei8TmIg
370
370
  cirq/ops/three_qubit_gates_test.py,sha256=iQfQ4p4TwtyHYCqaWOEeZsqszF_Mp49VwlIKRydClMk,11778
371
371
  cirq/ops/two_qubit_diagonal_gate.py,sha256=UYz2lUN8YzQjaqBZ_rmg6TvLFSRAvSlQGhnPOL5FmNQ,5376
372
372
  cirq/ops/two_qubit_diagonal_gate_test.py,sha256=qiuREluCDKMok3ormBOdDYCFlOS9u1zFLqTsORO5JtM,4000
373
+ cirq/ops/uniform_superposition_gate.py,sha256=DBItkN48YM6GJCBITdvztEnacGB0HcEYo55ODC02WE0,4705
374
+ cirq/ops/uniform_superposition_gate_test.py,sha256=9N8woRmaFWeuaPMy2K1JlXyTG8bICIpsfmOzXc3NysU,3551
373
375
  cirq/ops/wait_gate.py,sha256=ZJ9cqRysAyUYgfswVWO5C2OkDZ9MFEQjSemLw0w3drA,5654
374
376
  cirq/ops/wait_gate_test.py,sha256=2Uw8ZjFkYGhDosoxbJr_IW2wWdxY8kXR-CLyC69DYRg,3543
375
377
  cirq/protocols/__init__.py,sha256=kGCV1tzQ1m8fjrOPhapDpnStu-rFNe4q188OQY1huHg,4006
@@ -766,6 +768,8 @@ cirq/protocols/json_test_data/TwoQubitMatrixGate.json_inward,sha256=2E6o8tzoiTnt
766
768
  cirq/protocols/json_test_data/TwoQubitMatrixGate.repr_inward,sha256=G6v-BqECV3dcPiIjNLRUQYrRVtyLt0HqD00fDXwzt8o,151
767
769
  cirq/protocols/json_test_data/UNCONSTRAINED_DEVICE.json,sha256=EK2bbhnx9EEnhJx16J7LjamlC6nUwt_DeKaXmcxcTyU,41
768
770
  cirq/protocols/json_test_data/UNCONSTRAINED_DEVICE.repr,sha256=qPILaenyLEonsD0rywxnEyICOS7ANVo6X0cb7Sg5sj8,25
771
+ cirq/protocols/json_test_data/UniformSuperpositionGate.json,sha256=iK4MquwPDvWyDaEzkz7cERIc1xcbNZ-MScdttjHb2VU,81
772
+ cirq/protocols/json_test_data/UniformSuperpositionGate.repr,sha256=lft1g9nxkx_63DFHixSQL8e8tIOoz4PXDW7RqEhGw6A,57
769
773
  cirq/protocols/json_test_data/VarianceStoppingCriteria.json,sha256=PVdbXD8P9qjLqBI1ULgEVGEHxZhl0ahduLASk2VhrDU,105
770
774
  cirq/protocols/json_test_data/VarianceStoppingCriteria.repr,sha256=8J4dzmOZuhZ8jjDISwSLwY3JqIFG8f_8HvLT7az-tqA,84
771
775
  cirq/protocols/json_test_data/VirtualTag.json,sha256=yd6Yr22V94ycuO8UoYnWkV5Y8LgX83M-6Us2mUj7wq4,31
@@ -1171,8 +1175,8 @@ cirq/work/sampler.py,sha256=JEAeQQRF3bqlO9AkOf4XbrTATDI5f5JgyM_FAUCNxao,19751
1171
1175
  cirq/work/sampler_test.py,sha256=B2ZsuqGT854gQtBIAh8k0LiG9Vj5wSzcGvkxOUoTcW4,13217
1172
1176
  cirq/work/zeros_sampler.py,sha256=x1C7cup66a43n-3tm8QjhiqJa07qcJW10FxNp9jJ59Q,2356
1173
1177
  cirq/work/zeros_sampler_test.py,sha256=JIkpBBFPJe5Ba4142vzogyWyboG1Q1ZAm0UVGgOoZn8,3279
1174
- cirq_core-1.4.0.dev20240518010119.dist-info/LICENSE,sha256=tAkwu8-AdEyGxGoSvJ2gVmQdcicWw3j1ZZueVV74M-E,11357
1175
- cirq_core-1.4.0.dev20240518010119.dist-info/METADATA,sha256=_B0kSyB6jWVIHsGmfswYX-IFIC_dGpn2itPtIdxb4aE,2000
1176
- cirq_core-1.4.0.dev20240518010119.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
1177
- cirq_core-1.4.0.dev20240518010119.dist-info/top_level.txt,sha256=Sz9iOxHU0IEMLSFGwiwOCaN2e9K-jFbBbtpPN1hB73g,5
1178
- cirq_core-1.4.0.dev20240518010119.dist-info/RECORD,,
1178
+ cirq_core-1.4.0.dev20240521175101.dist-info/LICENSE,sha256=tAkwu8-AdEyGxGoSvJ2gVmQdcicWw3j1ZZueVV74M-E,11357
1179
+ cirq_core-1.4.0.dev20240521175101.dist-info/METADATA,sha256=CwznpFgBHsmlBrBR4YPtL792EIaxWuS3RKxCjCQTgy0,2000
1180
+ cirq_core-1.4.0.dev20240521175101.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
1181
+ cirq_core-1.4.0.dev20240521175101.dist-info/top_level.txt,sha256=Sz9iOxHU0IEMLSFGwiwOCaN2e9K-jFbBbtpPN1hB73g,5
1182
+ cirq_core-1.4.0.dev20240521175101.dist-info/RECORD,,