Skip to content

Commit 683286b

Browse files
dstrain115maffoo
andauthored
Add metadata to sweeps (#6099)
* Add metadata to sweeps - Adds optional metadata attribute to Linspace and Points. - This will be used to adjust device parameters via sweep. See https://tinyurl.com/cirq-sweep-metadata-public for RFC. Co-authored-by: Matthew Neeley <mneeley@gmail.com>
1 parent 0066a53 commit 683286b

File tree

6 files changed

+98
-23
lines changed

6 files changed

+98
-23
lines changed
Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1-
{
2-
"cirq_type": "Linspace",
3-
"key": "a",
4-
"start": 0,
5-
"stop": 1,
6-
"length": 4
7-
}
1+
[
2+
{
3+
"cirq_type": "Linspace",
4+
"key": "a",
5+
"start": 0,
6+
"stop": 1,
7+
"length": 4
8+
},
9+
{
10+
"cirq_type": "Linspace",
11+
"key": "b",
12+
"start": 1,
13+
"stop": 2,
14+
"length": 3,
15+
"metadata": {
16+
"cirq_type": "NamedQubit",
17+
"name": "b"
18+
}
19+
}
20+
]
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
cirq.Linspace("a", start=0, stop=1, length=4)
1+
[
2+
cirq.Linspace("a", start=0, stop=1, length=4),
3+
cirq.Linspace("b", start=1, stop=2, length=3, metadata=cirq.NamedQubit("b"))
4+
]
Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1-
{
2-
"cirq_type": "Points",
3-
"key": "a",
4-
"points": [
5-
0,
6-
0.4
7-
]
8-
}
1+
[
2+
{
3+
"cirq_type": "Points",
4+
"key": "a",
5+
"points": [
6+
0,
7+
0.4
8+
]
9+
},
10+
{
11+
"cirq_type": "Points",
12+
"key": "amp",
13+
"points": [
14+
0,
15+
1
16+
],
17+
"metadata": "used to turn amp on or off"
18+
}
19+
]
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
cirq.Points('a', [0, 0.4])
1+
[
2+
cirq.Points('a', [0, 0.4]),
3+
cirq.Points('amp', [0, 1], metadata='used to turn amp on or off'),
4+
]

cirq-core/cirq/study/sweeps.py

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
Iterable,
1919
Iterator,
2020
List,
21+
Optional,
2122
overload,
2223
Sequence,
2324
TYPE_CHECKING,
@@ -421,9 +422,23 @@ def _values(self) -> Iterator[float]:
421422
class Points(SingleSweep):
422423
"""A simple sweep with explicitly supplied values."""
423424

424-
def __init__(self, key: 'cirq.TParamKey', points: Sequence[float]) -> None:
425-
super(Points, self).__init__(key)
425+
def __init__(
426+
self, key: 'cirq.TParamKey', points: Sequence[float], metadata: Optional[Any] = None
427+
) -> None:
428+
"""Creates a sweep on a variable with supplied values.
429+
430+
Args:
431+
key: sympy.Symbol or equivalent to sweep across.
432+
points: sequence of floating point values that represent
433+
the values to sweep across. The length of the sweep
434+
will be equivalent to the length of this sequence.
435+
metadata: Optional metadata to attach to the sweep to
436+
annotate the sweep or its variable.
437+
438+
"""
439+
super().__init__(key)
426440
self.points = points
441+
self.metadata = metadata
427442

428443
def _tuple(self) -> Tuple[Union[str, sympy.Expr], Sequence[float]]:
429444
return self.key, tuple(self.points)
@@ -435,25 +450,44 @@ def _values(self) -> Iterator[float]:
435450
return iter(self.points)
436451

437452
def __repr__(self) -> str:
438-
return f'cirq.Points({self.key!r}, {self.points!r})'
453+
metadata_repr = f', metadata={self.metadata!r}' if self.metadata is not None else ""
454+
return f'cirq.Points({self.key!r}, {self.points!r}{metadata_repr})'
439455

440456
def _json_dict_(self) -> Dict[str, Any]:
457+
if self.metadata is not None:
458+
return protocols.obj_to_dict_helper(self, ["key", "points", "metadata"])
441459
return protocols.obj_to_dict_helper(self, ["key", "points"])
442460

443461

444462
class Linspace(SingleSweep):
445463
"""A simple sweep over linearly-spaced values."""
446464

447-
def __init__(self, key: 'cirq.TParamKey', start: float, stop: float, length: int) -> None:
465+
def __init__(
466+
self,
467+
key: 'cirq.TParamKey',
468+
start: float,
469+
stop: float,
470+
length: int,
471+
metadata: Optional[Any] = None,
472+
) -> None:
448473
"""Creates a linear-spaced sweep for a given key.
449474
450475
For the given args, assigns to the list of values
451476
start, start + (stop - start) / (length - 1), ..., stop
477+
478+
Args:
479+
key: sympy.Symbol or equivalent to sweep across.
480+
start: minimum value of linear sweep.
481+
stop: maximum value of linear sweep.
482+
length: number of points in the sweep.
483+
metadata: Optional metadata to attach to the sweep to
484+
annotate the sweep or its variable.
452485
"""
453-
super(Linspace, self).__init__(key)
486+
super().__init__(key)
454487
self.start = start
455488
self.stop = stop
456489
self.length = length
490+
self.metadata = metadata
457491

458492
def _tuple(self) -> Tuple[Union[str, sympy.Expr], float, float, int]:
459493
return (self.key, self.start, self.stop, self.length)
@@ -470,12 +504,17 @@ def _values(self) -> Iterator[float]:
470504
yield self.start * (1 - p) + self.stop * p
471505

472506
def __repr__(self) -> str:
507+
metadata_repr = f', metadata={self.metadata!r}' if self.metadata is not None else ""
473508
return (
474509
f'cirq.Linspace({self.key!r}, start={self.start!r}, '
475-
f'stop={self.stop!r}, length={self.length!r})'
510+
f'stop={self.stop!r}, length={self.length!r}{metadata_repr})'
476511
)
477512

478513
def _json_dict_(self) -> Dict[str, Any]:
514+
if self.metadata is not None:
515+
return protocols.obj_to_dict_helper(
516+
self, ["key", "start", "stop", "length", "metadata"]
517+
)
479518
return protocols.obj_to_dict_helper(self, ["key", "start", "stop", "length"])
480519

481520

cirq-core/cirq/study/sweeps_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,12 @@ def test_repr():
273273
)
274274
cirq.testing.assert_equivalent_repr(cirq.Points('zero&pi', [0, 3.14159]))
275275
cirq.testing.assert_equivalent_repr(cirq.Linspace('I/10', 0, 1, 10))
276+
cirq.testing.assert_equivalent_repr(
277+
cirq.Points('zero&pi', [0, 3.14159], metadata='example str')
278+
)
279+
cirq.testing.assert_equivalent_repr(
280+
cirq.Linspace('for_q0', 0, 1, 10, metadata=cirq.LineQubit(0))
281+
)
276282

277283

278284
def test_zip_product_str():

0 commit comments

Comments
 (0)