1818 Iterable ,
1919 Iterator ,
2020 List ,
21+ Optional ,
2122 overload ,
2223 Sequence ,
2324 TYPE_CHECKING ,
@@ -421,9 +422,23 @@ def _values(self) -> Iterator[float]:
421422class 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
444462class 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
0 commit comments