-
Notifications
You must be signed in to change notification settings - Fork 982
/
sweeps.py
576 lines (448 loc) · 17.8 KB
/
sweeps.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
# Copyright 2018 The Cirq Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import (
Any,
cast,
Dict,
Iterable,
Iterator,
List,
overload,
Sequence,
TYPE_CHECKING,
Tuple,
Union,
)
import abc
import collections
import itertools
import sympy
from cirq import protocols
from cirq._doc import document
from cirq.study import resolver
if TYPE_CHECKING:
import cirq
Params = Iterable[Tuple['cirq.TParamKey', 'cirq.TParamVal']]
ProductOrZipSweepLike = Dict['cirq.TParamKey', Union['cirq.TParamVal', Sequence['cirq.TParamVal']]]
def _check_duplicate_keys(sweeps):
keys = set()
for sweep in sweeps:
if any(key in keys for key in sweep.keys):
raise ValueError('duplicate keys')
keys.update(sweep.keys)
class Sweep(metaclass=abc.ABCMeta):
"""A sweep is an iterator over ParamResolvers.
A ParamResolver assigns values to Symbols. For sweeps, each ParamResolver
must specify the same Symbols that are assigned. So a sweep is a way to
iterate over a set of different values for a fixed set of Symbols. This is
useful for a circuit, where there are a fixed set of Symbols, and you want
to iterate over an assignment of all values to all symbols.
For example, a sweep can explicitly assign a set of equally spaced points
between two endpoints using a Linspace,
sweep = Linspace("angle", start=0.0, end=2.0, length=10)
This can then be used with a circuit that has an 'angle' sympy.Symbol to
run simulations multiple simulations, one for each of the values in the
sweep
result = simulator.run_sweep(program=circuit, params=sweep)
Sweeps support Cartesian and Zip products using the '*' and '+' operators,
see the Product and Zip documentation.
"""
def __mul__(self, other: 'Sweep') -> 'Sweep':
factors: List[Sweep] = []
if isinstance(self, Product):
factors.extend(self.factors)
else:
factors.append(self)
if isinstance(other, Product):
factors.extend(other.factors)
elif isinstance(other, Sweep):
factors.append(other)
else:
raise TypeError(f'cannot multiply sweep and {type(other)}')
return Product(*factors)
def __add__(self, other: 'Sweep') -> 'Sweep':
sweeps: List[Sweep] = []
if isinstance(self, Zip):
sweeps.extend(self.sweeps)
else:
sweeps.append(self)
if isinstance(other, Zip):
sweeps.extend(other.sweeps)
elif isinstance(other, Sweep):
sweeps.append(other)
else:
raise TypeError(f'cannot add sweep and {type(other)}')
return Zip(*sweeps)
@abc.abstractmethod
def __eq__(self, other) -> bool:
pass
def __ne__(self, other) -> bool:
return not self == other
@property
@abc.abstractmethod
def keys(self) -> List['cirq.TParamKey']:
"""The keys for the all of the sympy.Symbols that are resolved."""
@abc.abstractmethod
def __len__(self) -> int:
pass
def __iter__(self) -> Iterator[resolver.ParamResolver]:
for params in self.param_tuples():
yield resolver.ParamResolver(collections.OrderedDict(params))
# pylint: disable=function-redefined
@overload
def __getitem__(self, val: int) -> resolver.ParamResolver:
pass
@overload
def __getitem__(self, val: slice) -> 'Sweep':
pass
def __getitem__(self, val: Union[int, slice]) -> Union[resolver.ParamResolver, 'Sweep']:
n = len(self)
if isinstance(val, int):
if val < -n or val >= n:
raise IndexError(f'sweep index out of range: {val}')
if val < 0:
val += n
return next(itertools.islice(self, val, val + 1))
if not isinstance(val, slice):
raise TypeError(f'Sweep indices must be either int or slices, not {type(val)}')
inds_map: Dict[int, int] = {
sweep_i: slice_i for slice_i, sweep_i in enumerate(range(n)[val])
}
results = [resolver.ParamResolver()] * len(inds_map)
for i, item in enumerate(self):
if i in inds_map:
results[inds_map[i]] = item
return ListSweep(results)
# pylint: enable=function-redefined
@abc.abstractmethod
def param_tuples(self) -> Iterator[Params]:
"""An iterator over (key, value) pairs assigning Symbol key to value."""
def __str__(self) -> str:
length = len(self)
max_show = 10
# Show a maximum of max_show entries with an ellipsis in the middle
if length > max_show:
beginning_len = max_show - max_show // 2
else:
beginning_len = max_show
end_len = max_show - beginning_len
lines = ['Sweep:']
lines.extend(str(dict(r.param_dict)) for r in itertools.islice(self, beginning_len))
if end_len > 0:
lines.append('...')
lines.extend(
str(dict(r.param_dict)) for r in itertools.islice(self, length - end_len, length)
)
return '\n'.join(lines)
class _Unit(Sweep):
"""A sweep with a single element that assigns no parameter values.
This is useful as a base sweep, instead of special casing None.
"""
def __eq__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return True
@property
def keys(self) -> List['cirq.TParamKey']:
return []
def __len__(self) -> int:
return 1
def param_tuples(self) -> Iterator[Params]:
yield ()
def __repr__(self) -> str:
return 'cirq.UnitSweep'
def _json_dict_(self) -> Dict[str, Any]:
return {}
UnitSweep = _Unit()
document(UnitSweep, """The singleton sweep with no parameters.""")
class Product(Sweep):
"""Cartesian product of one or more sweeps.
If one sweep assigns 'a' to the values 0, 1, 2, and the second sweep
assigns 'b' to the values 2, 3, then the product is a sweep that
assigns the tuple ('a','b') to all possible combinations of these
assignments: (0, 2), (1, 2), (2, 2), (0, 3), (1, 3), (2, 3).
"""
def __init__(self, *factors: Sweep) -> None:
_check_duplicate_keys(factors)
self.factors = factors
def __eq__(self, other):
if not isinstance(other, Product):
return NotImplemented
return self.factors == other.factors
def __hash__(self):
return hash(tuple(self.factors))
@property
def keys(self) -> List['cirq.TParamKey']:
return sum((factor.keys for factor in self.factors), [])
def __len__(self) -> int:
if not self.factors:
return 0
length = 1
for factor in self.factors:
length *= len(factor)
return length
def param_tuples(self) -> Iterator[Params]:
def _gen(factors):
if not factors:
yield ()
else:
first, rest = factors[0], factors[1:]
for first_values in first.param_tuples():
for rest_values in _gen(rest):
yield first_values + rest_values
return _gen(self.factors)
def __repr__(self) -> str:
factors_repr = ', '.join(repr(f) for f in self.factors)
return f'cirq.Product({factors_repr})'
def __str__(self) -> str:
if not self.factors:
return 'Product()'
factor_strs = []
for factor in self.factors:
factor_str = repr(factor)
if isinstance(factor, Zip):
factor_str = '(' + str(factor) + ')'
factor_strs.append(factor_str)
return ' * '.join(factor_strs)
def _json_dict_(self) -> Dict[str, Any]:
return protocols.obj_to_dict_helper(self, ['factors'])
@classmethod
def _from_json_dict_(cls, factors, **kwargs):
return Product(*factors)
class Zip(Sweep):
"""Zip product (direct sum) of one or more sweeps.
If one sweep assigns 'a' to values 0, 1, 2, and the second sweep assigns 'b'
to the values 3, 4, 5, then the zip is a sweep that assigns to the
tuple ('a', 'b') the pair-wise matched values (0, 3), (1, 4), (2, 5).
When iterating over a Zip, we iterate the individual sweeps in parallel,
stopping when the first component sweep stops. For example if one sweep
assigns 'a' to values 0, 1 and the second sweep assigns 'b' to the values
3, 4, 5, then the zip is a sweep that assigns to the tuple ('a', 'b') the
values (0, 3), (1, 4).
"""
def __init__(self, *sweeps: Sweep) -> None:
_check_duplicate_keys(sweeps)
self.sweeps = sweeps
def __eq__(self, other):
if not isinstance(other, Zip):
return NotImplemented
return self.sweeps == other.sweeps
def __hash__(self) -> int:
return hash(tuple(self.sweeps))
@property
def keys(self) -> List['cirq.TParamKey']:
return sum((sweep.keys for sweep in self.sweeps), [])
def __len__(self) -> int:
if not self.sweeps:
return 0
return min(len(sweep) for sweep in self.sweeps)
def param_tuples(self) -> Iterator[Params]:
iters = [sweep.param_tuples() for sweep in self.sweeps]
for values in zip(*iters):
yield sum(values, ())
def __repr__(self) -> str:
sweeps_repr = ', '.join(repr(s) for s in self.sweeps)
return f'cirq.Zip({sweeps_repr})'
def __str__(self) -> str:
if not self.sweeps:
return 'Zip()'
return ' + '.join(str(s) if isinstance(s, Product) else repr(s) for s in self.sweeps)
def _json_dict_(self) -> Dict[str, Any]:
return protocols.obj_to_dict_helper(self, ['sweeps'])
@classmethod
def _from_json_dict_(cls, sweeps, **kwargs):
return cls(*sweeps)
class ZipLongest(Zip):
"""Iterate over constituent sweeps in parallel
Analogous to itertools.zip_longest.
Note that we iterate until all sweeps terminate,
so if the sweeps are different lengths, the
shorter sweeps will be filled by repeating their last value
until all sweeps have equal length.
Note that this is different from itertools.zip_longest,
which uses a fixed fill value.
Raises:
ValueError if an input sweep if completely empty.
"""
def __init__(self, *sweeps: Sweep) -> None:
super().__init__(*sweeps)
if any(len(sweep) == 0 for sweep in self.sweeps):
raise ValueError('All sweeps must be non-empty for ZipLongest')
def __eq__(self, other):
if not isinstance(other, ZipLongest):
return NotImplemented
return self.sweeps == other.sweeps
def __len__(self) -> int:
if not self.sweeps:
return 0
return max(len(sweep) for sweep in self.sweeps)
def __hash__(self) -> int:
return hash(tuple(self.sweeps))
def __repr__(self) -> str:
sweeps_repr = ', '.join(repr(s) for s in self.sweeps)
return f'cirq_google.ZipLongest({sweeps_repr})'
def __str__(self) -> str:
sweeps_repr = ', '.join(repr(s) for s in self.sweeps)
return f'ZipLongest({sweeps_repr})'
def param_tuples(self) -> Iterator[Params]:
def _iter_and_repeat_last(one_iter: Iterator[Params]):
last = None
for last in one_iter:
yield last
while True:
yield last
iters = [_iter_and_repeat_last(sweep.param_tuples()) for sweep in self.sweeps]
for values in itertools.islice(zip(*iters), len(self)):
yield tuple(item for value in values for item in value)
class SingleSweep(Sweep):
"""A simple sweep over one parameter with values from an iterator."""
def __init__(self, key: 'cirq.TParamKey') -> None:
if isinstance(key, sympy.Symbol):
key = str(key)
self.key = key
def __eq__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return self._tuple() == other._tuple()
def __hash__(self) -> int:
return hash((self.__class__, self._tuple()))
@abc.abstractmethod
def _tuple(self) -> Tuple[Any, ...]:
pass
@property
def keys(self) -> List['cirq.TParamKey']:
return [self.key]
def param_tuples(self) -> Iterator[Params]:
for value in self._values():
yield ((self.key, value),)
@abc.abstractmethod
def _values(self) -> Iterator[float]:
pass
class Points(SingleSweep):
"""A simple sweep with explicitly supplied values."""
def __init__(self, key: 'cirq.TParamKey', points: Sequence[float]) -> None:
super(Points, self).__init__(key)
self.points = points
def _tuple(self) -> Tuple[Union[str, sympy.Expr], Sequence[float]]:
return self.key, tuple(self.points)
def __len__(self) -> int:
return len(self.points)
def _values(self) -> Iterator[float]:
return iter(self.points)
def __repr__(self) -> str:
return f'cirq.Points({self.key!r}, {self.points!r})'
def _json_dict_(self) -> Dict[str, Any]:
return protocols.obj_to_dict_helper(self, ["key", "points"])
class Linspace(SingleSweep):
"""A simple sweep over linearly-spaced values."""
def __init__(self, key: 'cirq.TParamKey', start: float, stop: float, length: int) -> None:
"""Creates a linear-spaced sweep for a given key.
For the given args, assigns to the list of values
start, start + (stop - start) / (length - 1), ..., stop
"""
super(Linspace, self).__init__(key)
self.start = start
self.stop = stop
self.length = length
def _tuple(self) -> Tuple[Union[str, sympy.Expr], float, float, int]:
return (self.key, self.start, self.stop, self.length)
def __len__(self) -> int:
return self.length
def _values(self) -> Iterator[float]:
if self.length == 1:
yield self.start
else:
for i in range(self.length):
p = i / (self.length - 1)
yield self.start * (1 - p) + self.stop * p
def __repr__(self) -> str:
return (
f'cirq.Linspace({self.key!r}, start={self.start!r}, '
f'stop={self.stop!r}, length={self.length!r})'
)
def _json_dict_(self) -> Dict[str, Any]:
return protocols.obj_to_dict_helper(self, ["key", "start", "stop", "length"])
class ListSweep(Sweep):
"""A wrapper around a list of `ParamResolver`s."""
def __init__(self, resolver_list: Iterable[resolver.ParamResolverOrSimilarType]):
"""Creates a `Sweep` over a list of `ParamResolver`s.
Args:
resolver_list: The list of parameter resolvers to use in the sweep.
All resolvers must resolve the same set of parameters.
Raises:
TypeError: If `resolver_list` is not a `cirq.ParamResolver` or a
dict.
"""
self.resolver_list: List[resolver.ParamResolver] = []
for r in resolver_list:
if not isinstance(r, (dict, resolver.ParamResolver)):
raise TypeError(f'Not a ParamResolver or dict: <{r!r}>')
self.resolver_list.append(resolver.ParamResolver(r))
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
return self.resolver_list == other.resolver_list
def __ne__(self, other):
return not self == other
@property
def keys(self) -> List['cirq.TParamKey']:
if not self.resolver_list:
return []
return list(map(str, self.resolver_list[0].param_dict))
def __len__(self) -> int:
return len(self.resolver_list)
def param_tuples(self) -> Iterator[Params]:
for r in self.resolver_list:
yield tuple(_params_without_symbols(r))
def __repr__(self) -> str:
return f'cirq.ListSweep({self.resolver_list!r})'
def _json_dict_(self) -> Dict[str, Any]:
return protocols.obj_to_dict_helper(self, ["resolver_list"])
def _params_without_symbols(resolver: resolver.ParamResolver) -> Params:
for sym, val in resolver.param_dict.items():
if isinstance(sym, sympy.Symbol):
sym = sym.name
yield cast(str, sym), cast(float, val)
def dict_to_product_sweep(factor_dict: ProductOrZipSweepLike) -> Product:
"""Cartesian product of sweeps from a dictionary.
Each entry in the dictionary specifies a sweep as a mapping from the
parameter to a value or sequence of values. The Cartesian product of these
sweeps is returned.
Args:
factor_dict: The dictionary containing the sweeps.
Returns:
Cartesian product of the sweeps.
"""
return Product(
*(
Points(k, v if isinstance(v, Sequence) else [v]) # type: ignore
for k, v in factor_dict.items()
)
)
def dict_to_zip_sweep(factor_dict: ProductOrZipSweepLike) -> Zip:
"""Zip product of sweeps from a dictionary.
Each entry in the dictionary specifies a sweep as a mapping from the
parameter to a value or sequence of values. The zip product of these
sweeps is returned.
Args:
factor_dict: The dictionary containing the sweeps.
Returns:
Zip product of the sweeps.
"""
return Zip(
*(
Points(k, cast(float, v) if isinstance(v, Sequence) else [v]) # type: ignore
for k, v in factor_dict.items()
)
)