cirq-core 1.5.0.dev20241015235131__py3-none-any.whl → 1.5.0.dev20241028183905__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.dev20241015235131"
31
+ __version__ = "1.5.0.dev20241028183905"
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.dev20241015235131"
6
+ assert cirq.__version__ == "1.5.0.dev20241028183905"
cirq/work/sampler.py CHANGED
@@ -14,11 +14,24 @@
14
14
  """Abstract base class for things sampling quantum circuits."""
15
15
 
16
16
  import collections
17
- from typing import Dict, FrozenSet, List, Optional, Sequence, Tuple, TYPE_CHECKING, Union
17
+ from itertools import islice
18
+ from typing import (
19
+ Dict,
20
+ FrozenSet,
21
+ Iterator,
22
+ List,
23
+ Optional,
24
+ Sequence,
25
+ Tuple,
26
+ TypeVar,
27
+ TYPE_CHECKING,
28
+ Union,
29
+ )
18
30
 
19
31
  import duet
20
32
  import pandas as pd
21
33
 
34
+
22
35
  from cirq import ops, protocols, study, value
23
36
  from cirq.work.observable_measurement import (
24
37
  measure_observables,
@@ -30,10 +43,17 @@ from cirq.work.observable_settings import _hashable_param
30
43
  if TYPE_CHECKING:
31
44
  import cirq
32
45
 
46
+ T = TypeVar('T')
47
+
33
48
 
34
49
  class Sampler(metaclass=value.ABCMetaImplementAnyOneOf):
35
50
  """Something capable of sampling quantum circuits. Simulator or hardware."""
36
51
 
52
+ # Users have a rate limit of 1000 QPM for read/write requests to
53
+ # the Quantum Engine. 1000/60 ~= 16 QPS. So requests are sent
54
+ # in chunks of size 16 per second.
55
+ CHUNK_SIZE: int = 16
56
+
37
57
  def run(
38
58
  self,
39
59
  program: 'cirq.AbstractCircuit',
@@ -294,9 +314,26 @@ class Sampler(metaclass=value.ABCMetaImplementAnyOneOf):
294
314
  See docs for `cirq.Sampler.run_batch`.
295
315
  """
296
316
  params_list, repetitions = self._normalize_batch_args(programs, params_list, repetitions)
297
- return await duet.pstarmap_async(
298
- self.run_sweep_async, zip(programs, params_list, repetitions)
299
- )
317
+ if len(programs) <= self.CHUNK_SIZE:
318
+ return await duet.pstarmap_async(
319
+ self.run_sweep_async, zip(programs, params_list, repetitions)
320
+ )
321
+
322
+ results = []
323
+ for program_chunk, params_chunk, reps_chunk in zip(
324
+ _chunked(programs, self.CHUNK_SIZE),
325
+ _chunked(params_list, self.CHUNK_SIZE),
326
+ _chunked(repetitions, self.CHUNK_SIZE),
327
+ ):
328
+ # Run_sweep_async for the current chunk
329
+ await duet.sleep(1) # Delay for 1 second between chunk
330
+ results.extend(
331
+ await duet.pstarmap_async(
332
+ self.run_sweep_async, zip(program_chunk, params_chunk, reps_chunk)
333
+ )
334
+ )
335
+
336
+ return results
300
337
 
301
338
  def _normalize_batch_args(
302
339
  self,
@@ -449,3 +486,8 @@ class Sampler(metaclass=value.ABCMetaImplementAnyOneOf):
449
486
  )
450
487
  num_instances[key] += 1
451
488
  return {k: (num_instances[k], qid_shape) for k, qid_shape in qid_shapes.items()}
489
+
490
+
491
+ def _chunked(iterable: Sequence[T], n: int) -> Iterator[tuple[T, ...]]:
492
+ it = iter(iterable)
493
+ return iter(lambda: tuple(islice(it, n)), ())
cirq/work/sampler_test.py CHANGED
@@ -13,6 +13,7 @@
13
13
  # limitations under the License.
14
14
  """Tests for cirq.Sampler."""
15
15
  from typing import Sequence
16
+ from unittest import mock
16
17
 
17
18
  import pytest
18
19
 
@@ -266,6 +267,55 @@ def test_sampler_run_batch_bad_input_lengths():
266
267
  )
267
268
 
268
269
 
270
+ @mock.patch('duet.pstarmap_async')
271
+ @pytest.mark.parametrize('call_count', [1, 2, 3])
272
+ @duet.sync
273
+ async def test_run_batch_async_sends_circuits_in_chunks(spy, call_count):
274
+ class AsyncSampler(cirq.Sampler):
275
+ CHUNK_SIZE = 3
276
+
277
+ async def run_sweep_async(self, _, params, __: int = 1):
278
+ pass # pragma: no cover
279
+
280
+ sampler = AsyncSampler()
281
+ a = cirq.LineQubit(0)
282
+ circuit_list = [cirq.Circuit(cirq.X(a) ** sympy.Symbol('t'), cirq.measure(a, key='m'))] * (
283
+ sampler.CHUNK_SIZE * call_count
284
+ )
285
+ param_list = [cirq.Points('t', [0.3, 0.7])] * (sampler.CHUNK_SIZE * call_count)
286
+
287
+ await sampler.run_batch_async(circuit_list, params_list=param_list)
288
+
289
+ assert spy.call_count == call_count
290
+
291
+
292
+ @pytest.mark.parametrize('call_count', [1, 2, 3])
293
+ @duet.sync
294
+ async def test_run_batch_async_runs_runs_sequentially(call_count):
295
+ a = cirq.LineQubit(0)
296
+ finished = []
297
+ circuit1 = cirq.Circuit(cirq.X(a) ** sympy.Symbol('t'), cirq.measure(a, key='m'))
298
+ circuit2 = cirq.Circuit(cirq.Y(a) ** sympy.Symbol('t'), cirq.measure(a, key='m'))
299
+ params1 = cirq.Points('t', [0.3, 0.7])
300
+ params2 = cirq.Points('t', [0.4, 0.6])
301
+
302
+ class AsyncSampler(cirq.Sampler):
303
+ CHUNK_SIZE = 1
304
+
305
+ async def run_sweep_async(self, _, params, __: int = 1):
306
+ if params == params1:
307
+ await duet.sleep(0.001)
308
+
309
+ finished.append(params)
310
+
311
+ sampler = AsyncSampler()
312
+ circuit_list = [circuit1, circuit2] * call_count
313
+ param_list = [params1, params2] * call_count
314
+ await sampler.run_batch_async(circuit_list, params_list=param_list)
315
+
316
+ assert finished == param_list
317
+
318
+
269
319
  def test_sampler_simple_sample_expectation_values():
270
320
  a = cirq.LineQubit(0)
271
321
  sampler = cirq.Simulator()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: cirq-core
3
- Version: 1.5.0.dev20241015235131
3
+ Version: 1.5.0.dev20241028183905
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=f9WvHus92SnBuwqN-aXjPDdduouMkkQ9hUMenxZ4ruM,1206
8
- cirq/_version_test.py,sha256=Z4gyUPwyCOEH6sZtdOhEsKwxPIRFmXkHKFhC3fSu3_4,147
7
+ cirq/_version.py,sha256=3IiIATzwlV1upJzvXw0UZy6fu4n_YeB_8p-bCzR00QM,1206
8
+ cirq/_version_test.py,sha256=94XwthEQxZNTDejp24Nne93gtOLGxV1KNift0jGwsjs,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
@@ -1180,12 +1180,12 @@ cirq/work/observable_settings.py,sha256=OsZS8XvHT2LCFGkzu-wh1WShOx93I1nZ2K7uce-i
1180
1180
  cirq/work/observable_settings_test.py,sha256=hV3g5ld5OZchTjVMVL_8cLXsbSBMz5DAsE4Vui2xTuk,4233
1181
1181
  cirq/work/pauli_sum_collector.py,sha256=N1IsIwudBi84XWP1x7LNE2uQ6DGR2LFIWPhGbdHxaA4,4230
1182
1182
  cirq/work/pauli_sum_collector_test.py,sha256=aeo06iLIYZjWjN3C4loVHRYWpV35lSSlcX2cOVdt2Ss,2437
1183
- cirq/work/sampler.py,sha256=JEAeQQRF3bqlO9AkOf4XbrTATDI5f5JgyM_FAUCNxao,19751
1184
- cirq/work/sampler_test.py,sha256=B2ZsuqGT854gQtBIAh8k0LiG9Vj5wSzcGvkxOUoTcW4,13217
1183
+ cirq/work/sampler.py,sha256=rzimb1Zmqrdb0coOJhGf459KTUaFpAnIR2Vm0mt1_i8,20848
1184
+ cirq/work/sampler_test.py,sha256=0HoptI2r-7yR5hmlvu6ykqkZ8TYO-lkIVvlv8yyDnDk,14885
1185
1185
  cirq/work/zeros_sampler.py,sha256=x1C7cup66a43n-3tm8QjhiqJa07qcJW10FxNp9jJ59Q,2356
1186
1186
  cirq/work/zeros_sampler_test.py,sha256=JIkpBBFPJe5Ba4142vzogyWyboG1Q1ZAm0UVGgOoZn8,3279
1187
- cirq_core-1.5.0.dev20241015235131.dist-info/LICENSE,sha256=tAkwu8-AdEyGxGoSvJ2gVmQdcicWw3j1ZZueVV74M-E,11357
1188
- cirq_core-1.5.0.dev20241015235131.dist-info/METADATA,sha256=NVuPhfje4RXdPn-aUsOQfUjNHE5nNqTFYkKSqqLARjE,1992
1189
- cirq_core-1.5.0.dev20241015235131.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
1190
- cirq_core-1.5.0.dev20241015235131.dist-info/top_level.txt,sha256=Sz9iOxHU0IEMLSFGwiwOCaN2e9K-jFbBbtpPN1hB73g,5
1191
- cirq_core-1.5.0.dev20241015235131.dist-info/RECORD,,
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,,