cirq-core 1.5.0.dev20241120181130__py3-none-any.whl → 1.5.0.dev20241122145042__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 +1 -1
- cirq/_version_test.py +1 -1
- cirq/circuits/qasm_output.py +32 -16
- cirq/circuits/qasm_output_test.py +55 -0
- cirq/protocols/qasm.py +10 -1
- cirq/protocols/qasm_test.py +12 -0
- {cirq_core-1.5.0.dev20241120181130.dist-info → cirq_core-1.5.0.dev20241122145042.dist-info}/METADATA +1 -1
- {cirq_core-1.5.0.dev20241120181130.dist-info → cirq_core-1.5.0.dev20241122145042.dist-info}/RECORD +11 -11
- {cirq_core-1.5.0.dev20241120181130.dist-info → cirq_core-1.5.0.dev20241122145042.dist-info}/LICENSE +0 -0
- {cirq_core-1.5.0.dev20241120181130.dist-info → cirq_core-1.5.0.dev20241122145042.dist-info}/WHEEL +0 -0
- {cirq_core-1.5.0.dev20241120181130.dist-info → cirq_core-1.5.0.dev20241122145042.dist-info}/top_level.txt +0 -0
cirq/_version.py
CHANGED
cirq/_version_test.py
CHANGED
cirq/circuits/qasm_output.py
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
|
|
15
15
|
"""Utility classes for representing QASM."""
|
|
16
16
|
|
|
17
|
-
from typing import Callable, Dict, Iterator, Optional, Sequence,
|
|
17
|
+
from typing import Callable, Dict, Iterator, Optional, Sequence, Tuple, Union, TYPE_CHECKING
|
|
18
18
|
|
|
19
19
|
import re
|
|
20
20
|
import numpy as np
|
|
@@ -203,6 +203,7 @@ class QasmOutput:
|
|
|
203
203
|
qubit_id_map=qubit_id_map,
|
|
204
204
|
meas_key_id_map=meas_key_id_map,
|
|
205
205
|
)
|
|
206
|
+
self.cregs = self._generate_cregs()
|
|
206
207
|
|
|
207
208
|
def _generate_measurement_ids(self) -> Tuple[Dict[str, str], Dict[str, Optional[str]]]:
|
|
208
209
|
# Pick an id for the creg that will store each measurement
|
|
@@ -226,6 +227,30 @@ class QasmOutput:
|
|
|
226
227
|
def _generate_qubit_ids(self) -> Dict['cirq.Qid', str]:
|
|
227
228
|
return {qubit: f'q[{i}]' for i, qubit in enumerate(self.qubits)}
|
|
228
229
|
|
|
230
|
+
def _generate_cregs(self) -> Dict[str, tuple[int, str]]:
|
|
231
|
+
"""Pick an id for the creg that will store each measurement
|
|
232
|
+
|
|
233
|
+
This function finds the largest measurement using each key.
|
|
234
|
+
That is, if multiple measurements are made with the same key,
|
|
235
|
+
it will use the key with the most number of qubits.
|
|
236
|
+
|
|
237
|
+
Returns: dictionary with key of measurement id and value of (#qubits, comment).
|
|
238
|
+
"""
|
|
239
|
+
cregs: Dict[str, tuple[int, str]] = {}
|
|
240
|
+
for meas in self.measurements:
|
|
241
|
+
key = protocols.measurement_key_name(meas)
|
|
242
|
+
meas_id = self.args.meas_key_id_map[key]
|
|
243
|
+
|
|
244
|
+
if self.meas_comments[key] is not None:
|
|
245
|
+
comment = f' // Measurement: {self.meas_comments[key]}'
|
|
246
|
+
else:
|
|
247
|
+
comment = ''
|
|
248
|
+
|
|
249
|
+
if meas_id not in cregs or cregs[meas_id][0] < len(meas.qubits):
|
|
250
|
+
cregs[meas_id] = (len(meas.qubits), comment)
|
|
251
|
+
|
|
252
|
+
return cregs
|
|
253
|
+
|
|
229
254
|
def is_valid_qasm_id(self, id_str: str) -> bool:
|
|
230
255
|
"""Test if id_str is a valid id in QASM grammar."""
|
|
231
256
|
return self.valid_id_re.match(id_str) is not None
|
|
@@ -287,24 +312,15 @@ class QasmOutput:
|
|
|
287
312
|
output(f'qreg q[{len(self.qubits)}];\n')
|
|
288
313
|
else:
|
|
289
314
|
output(f'qubit[{len(self.qubits)}] q;\n')
|
|
290
|
-
# Classical registers
|
|
291
|
-
# Pick an id for the creg that will store each measurement
|
|
292
|
-
already_output_keys: Set[str] = set()
|
|
293
|
-
for meas in self.measurements:
|
|
294
|
-
key = protocols.measurement_key_name(meas)
|
|
295
|
-
if key in already_output_keys:
|
|
296
|
-
continue
|
|
297
|
-
already_output_keys.add(key)
|
|
298
|
-
meas_id = self.args.meas_key_id_map[key]
|
|
299
|
-
if self.meas_comments[key] is not None:
|
|
300
|
-
comment = f' // Measurement: {self.meas_comments[key]}'
|
|
301
|
-
else:
|
|
302
|
-
comment = ''
|
|
303
315
|
|
|
316
|
+
# Classical registers
|
|
317
|
+
for meas_id in self.cregs:
|
|
318
|
+
length, comment = self.cregs[meas_id]
|
|
304
319
|
if self.args.version == '2.0':
|
|
305
|
-
output(f'creg {meas_id}[{
|
|
320
|
+
output(f'creg {meas_id}[{length}];{comment}\n')
|
|
306
321
|
else:
|
|
307
|
-
output(f'bit[{
|
|
322
|
+
output(f'bit[{length}] {meas_id};{comment}\n')
|
|
323
|
+
|
|
308
324
|
# In OpenQASM 2.0, the transformation of global phase gates is ignored.
|
|
309
325
|
# Therefore, no newline is created when the operations contained in
|
|
310
326
|
# a circuit consist only of global phase gates.
|
|
@@ -590,3 +590,58 @@ reset q[0];
|
|
|
590
590
|
reset q[1];
|
|
591
591
|
""".strip()
|
|
592
592
|
)
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
def test_different_sized_registers():
|
|
596
|
+
qubits = cirq.LineQubit.range(2)
|
|
597
|
+
c = cirq.Circuit(cirq.measure(qubits[0], key='c'), cirq.measure(qubits, key='c'))
|
|
598
|
+
output = cirq.QasmOutput(
|
|
599
|
+
c.all_operations(), tuple(sorted(c.all_qubits())), header='Generated from Cirq!'
|
|
600
|
+
)
|
|
601
|
+
assert (
|
|
602
|
+
str(output)
|
|
603
|
+
== """// Generated from Cirq!
|
|
604
|
+
|
|
605
|
+
OPENQASM 2.0;
|
|
606
|
+
include "qelib1.inc";
|
|
607
|
+
|
|
608
|
+
|
|
609
|
+
// Qubits: [q(0), q(1)]
|
|
610
|
+
qreg q[2];
|
|
611
|
+
creg m_c[2];
|
|
612
|
+
|
|
613
|
+
|
|
614
|
+
measure q[0] -> m_c[0];
|
|
615
|
+
|
|
616
|
+
// Gate: cirq.MeasurementGate(2, cirq.MeasurementKey(name='c'), ())
|
|
617
|
+
measure q[0] -> m_c[0];
|
|
618
|
+
measure q[1] -> m_c[1];
|
|
619
|
+
"""
|
|
620
|
+
)
|
|
621
|
+
# OPENQASM 3.0
|
|
622
|
+
output3 = cirq.QasmOutput(
|
|
623
|
+
c.all_operations(),
|
|
624
|
+
tuple(sorted(c.all_qubits())),
|
|
625
|
+
header='Generated from Cirq!',
|
|
626
|
+
version='3.0',
|
|
627
|
+
)
|
|
628
|
+
assert (
|
|
629
|
+
str(output3)
|
|
630
|
+
== """// Generated from Cirq!
|
|
631
|
+
|
|
632
|
+
OPENQASM 3.0;
|
|
633
|
+
include "stdgates.inc";
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
// Qubits: [q(0), q(1)]
|
|
637
|
+
qubit[2] q;
|
|
638
|
+
bit[2] m_c;
|
|
639
|
+
|
|
640
|
+
|
|
641
|
+
m_c[0] = measure q[0];
|
|
642
|
+
|
|
643
|
+
// Gate: cirq.MeasurementGate(2, cirq.MeasurementKey(name='c'), ())
|
|
644
|
+
m_c[0] = measure q[0];
|
|
645
|
+
m_c[1] = measure q[1];
|
|
646
|
+
"""
|
|
647
|
+
)
|
cirq/protocols/qasm.py
CHANGED
|
@@ -55,14 +55,23 @@ class QasmArgs(string.Formatter):
|
|
|
55
55
|
self.qubit_id_map = {} if qubit_id_map is None else qubit_id_map
|
|
56
56
|
self.meas_key_id_map = {} if meas_key_id_map is None else meas_key_id_map
|
|
57
57
|
|
|
58
|
+
def _format_number(self, value) -> str:
|
|
59
|
+
"""OpenQASM 2.0 does not support '1e-5' and wants '1.0e-5'"""
|
|
60
|
+
s = f'{value}'
|
|
61
|
+
if 'e' in s and not '.' in s:
|
|
62
|
+
return s.replace('e', '.0e')
|
|
63
|
+
return s
|
|
64
|
+
|
|
58
65
|
def format_field(self, value: Any, spec: str) -> str:
|
|
59
66
|
"""Method of string.Formatter that specifies the output of format()."""
|
|
60
67
|
if isinstance(value, (float, int)):
|
|
61
68
|
if isinstance(value, float):
|
|
62
69
|
value = round(value, self.precision)
|
|
63
70
|
if spec == 'half_turns':
|
|
64
|
-
value = f'pi*{value}' if value != 0 else '0'
|
|
71
|
+
value = f'pi*{self._format_number(value)}' if value != 0 else '0'
|
|
65
72
|
spec = ''
|
|
73
|
+
else:
|
|
74
|
+
value = self._format_number(value)
|
|
66
75
|
elif isinstance(value, ops.Qid):
|
|
67
76
|
value = self.qubit_id_map[value]
|
|
68
77
|
elif isinstance(value, str) and spec == 'meas':
|
cirq/protocols/qasm_test.py
CHANGED
|
@@ -52,3 +52,15 @@ def test_qasm():
|
|
|
52
52
|
|
|
53
53
|
assert cirq.qasm(ExpectsArgs(), args=cirq.QasmArgs()) == 'text'
|
|
54
54
|
assert cirq.qasm(ExpectsArgsQubits(), args=cirq.QasmArgs(), qubits=()) == 'text'
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def test_qasm_args_formatting():
|
|
58
|
+
args = cirq.QasmArgs()
|
|
59
|
+
assert args.format_field(0.01, '') == '0.01'
|
|
60
|
+
assert args.format_field(0.01, 'half_turns') == 'pi*0.01'
|
|
61
|
+
assert args.format_field(0.00001, '') == '1.0e-05'
|
|
62
|
+
assert args.format_field(0.00001, 'half_turns') == 'pi*1.0e-05'
|
|
63
|
+
assert args.format_field(1e-10, 'half_turns') == 'pi*1.0e-10'
|
|
64
|
+
args = cirq.QasmArgs(precision=6)
|
|
65
|
+
assert args.format_field(0.00001234, '') == '1.2e-05'
|
|
66
|
+
assert args.format_field(0.00001234, 'half_turns') == 'pi*1.2e-05'
|
{cirq_core-1.5.0.dev20241120181130.dist-info → cirq_core-1.5.0.dev20241122145042.dist-info}/METADATA
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: cirq-core
|
|
3
|
-
Version: 1.5.0.
|
|
3
|
+
Version: 1.5.0.dev20241122145042
|
|
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
|
{cirq_core-1.5.0.dev20241120181130.dist-info → cirq_core-1.5.0.dev20241122145042.dist-info}/RECORD
RENAMED
|
@@ -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=
|
|
8
|
-
cirq/_version_test.py,sha256=
|
|
7
|
+
cirq/_version.py,sha256=XjIeSY8WKIWhGLgodeSyCtawAjZPlguzDgvoeD0yolU,1206
|
|
8
|
+
cirq/_version_test.py,sha256=AILj5qsds81og90a0QuY362o3WNzZLY_Ou91IJeQKTM,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
|
|
@@ -28,8 +28,8 @@ cirq/circuits/moment.py,sha256=MskIVl5jbGnZdKKLNWbrQhWI5FHwjsuPOEm0hze8GDE,26299
|
|
|
28
28
|
cirq/circuits/moment_test.py,sha256=oNHNXhiPEoCKbzeh16tRwiW1qZWlg4X2O_uiVDP1D58,27375
|
|
29
29
|
cirq/circuits/optimization_pass.py,sha256=uw3ne0-ebZo6GNjwfQMuQ3b5u9RCgyaXRfhpbljlxao,6468
|
|
30
30
|
cirq/circuits/optimization_pass_test.py,sha256=eQB0NBJ9EvqjgSFGQMgaHIh5niQhksdnvqSXhsj3nOg,5947
|
|
31
|
-
cirq/circuits/qasm_output.py,sha256=
|
|
32
|
-
cirq/circuits/qasm_output_test.py,sha256=
|
|
31
|
+
cirq/circuits/qasm_output.py,sha256=atCdZwEx3T8NgiFuA7X4vx0jEo9REh1MyjiROutx4cg,12963
|
|
32
|
+
cirq/circuits/qasm_output_test.py,sha256=PawmzjqGpwauWgESqoi_U_iaYVZAMg_eIhZ_2VrPkPw,13634
|
|
33
33
|
cirq/circuits/text_diagram_drawer.py,sha256=ctZUG5fk2pf4XswHTJG4kteQYzzH0TefL9JWUagLJvc,17232
|
|
34
34
|
cirq/circuits/text_diagram_drawer_test.py,sha256=2bSoBIeQajRi0aQxqYDpbMlT2eqpx_f-Cmg9XO6A9Jk,10750
|
|
35
35
|
cirq/contrib/__init__.py,sha256=Mha0eF2ci88OVQX3laQiXgdEVo0yGwM7R5a13ryQ8jM,1065
|
|
@@ -419,8 +419,8 @@ cirq/protocols/phase_protocol.py,sha256=dXZRJb7aT8ZvtliItZOvoiaev-QvjBT4SknwZ5p-
|
|
|
419
419
|
cirq/protocols/phase_protocol_test.py,sha256=eU4g6VDqfWZCpklVlY1t5msc5kAZsP-UlPrROLll_ms,1939
|
|
420
420
|
cirq/protocols/pow_protocol.py,sha256=0wgb4LLAkOWjmP_u-_6IweXovDViocbdSgtsMOcXdkM,3290
|
|
421
421
|
cirq/protocols/pow_protocol_test.py,sha256=8eBcGUqRWSSYkEkvSHXL6tcv1o-ilnl6nzgNl9vfctg,1626
|
|
422
|
-
cirq/protocols/qasm.py,sha256=
|
|
423
|
-
cirq/protocols/qasm_test.py,sha256=
|
|
422
|
+
cirq/protocols/qasm.py,sha256=ekPlvfHIz9PUPdLeO7P_KMRuAh0rngM7r5grmW3kdWM,6869
|
|
423
|
+
cirq/protocols/qasm_test.py,sha256=MFkqSHByCbB46XOlAJgKQgEpANVwnmfO-tD-XofqrQ4,2013
|
|
424
424
|
cirq/protocols/qid_shape_protocol.py,sha256=jCAdpBBkEyK6JGmZwVBp__lHW3JhLmYt0Qzf8OgZtNU,7690
|
|
425
425
|
cirq/protocols/qid_shape_protocol_test.py,sha256=Qjo-wemjkP__1jQnVkkB91mUs8EpfXyKI9GzeZQVb1E,2303
|
|
426
426
|
cirq/protocols/resolve_parameters.py,sha256=NVFS5PSq18Hcvjv_P6vaZIa2D4OCgZY1u5j6QgPMlTA,7374
|
|
@@ -1187,8 +1187,8 @@ cirq/work/sampler.py,sha256=y6qtCpAwO8SqZ_JKU8PwlbMLHpJskNPqGWD_pNbyZew,19779
|
|
|
1187
1187
|
cirq/work/sampler_test.py,sha256=hL2UWx3dz2ukZVNxWftiKVvJcQoLplLZdQm-k1QcA40,13282
|
|
1188
1188
|
cirq/work/zeros_sampler.py,sha256=x1C7cup66a43n-3tm8QjhiqJa07qcJW10FxNp9jJ59Q,2356
|
|
1189
1189
|
cirq/work/zeros_sampler_test.py,sha256=JIkpBBFPJe5Ba4142vzogyWyboG1Q1ZAm0UVGgOoZn8,3279
|
|
1190
|
-
cirq_core-1.5.0.
|
|
1191
|
-
cirq_core-1.5.0.
|
|
1192
|
-
cirq_core-1.5.0.
|
|
1193
|
-
cirq_core-1.5.0.
|
|
1194
|
-
cirq_core-1.5.0.
|
|
1190
|
+
cirq_core-1.5.0.dev20241122145042.dist-info/LICENSE,sha256=tAkwu8-AdEyGxGoSvJ2gVmQdcicWw3j1ZZueVV74M-E,11357
|
|
1191
|
+
cirq_core-1.5.0.dev20241122145042.dist-info/METADATA,sha256=lB-DsJBzFZpD3HAkSm3ia7EoK-Y9ddZaFKEk62F8BVE,1992
|
|
1192
|
+
cirq_core-1.5.0.dev20241122145042.dist-info/WHEEL,sha256=bFJAMchF8aTQGUgMZzHJyDDMPTO3ToJ7x23SLJa1SVo,92
|
|
1193
|
+
cirq_core-1.5.0.dev20241122145042.dist-info/top_level.txt,sha256=Sz9iOxHU0IEMLSFGwiwOCaN2e9K-jFbBbtpPN1hB73g,5
|
|
1194
|
+
cirq_core-1.5.0.dev20241122145042.dist-info/RECORD,,
|
{cirq_core-1.5.0.dev20241120181130.dist-info → cirq_core-1.5.0.dev20241122145042.dist-info}/LICENSE
RENAMED
|
File without changes
|
{cirq_core-1.5.0.dev20241120181130.dist-info → cirq_core-1.5.0.dev20241122145042.dist-info}/WHEEL
RENAMED
|
File without changes
|
|
File without changes
|