/
symbolic_pulses.py
1962 lines (1578 loc) · 75.7 KB
/
symbolic_pulses.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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This code is part of Qiskit.
#
# (C) Copyright IBM 2020.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
# pylint: disable=invalid-name
"""Symbolic waveform module.
These are pulses which are described by symbolic equations for their envelopes and for their
parameter constraints.
"""
from __future__ import annotations
import functools
import warnings
from collections.abc import Mapping, Callable
from copy import deepcopy
from typing import Any
import numpy as np
import symengine as sym
from qiskit.circuit.parameterexpression import ParameterExpression, ParameterValueType
from qiskit.pulse.exceptions import PulseError
from qiskit.pulse.library.pulse import Pulse
from qiskit.pulse.library.waveform import Waveform
def _lifted_gaussian(
t: sym.Symbol,
center: sym.Symbol | sym.Expr | complex,
t_zero: sym.Symbol | sym.Expr | complex,
sigma: sym.Symbol | sym.Expr | complex,
) -> sym.Expr:
r"""Helper function that returns a lifted Gaussian symbolic equation.
For :math:`\sigma=` ``sigma`` the symbolic equation will be
.. math::
f(x) = \exp\left(-\frac12 \left(\frac{x - \mu}{\sigma}\right)^2 \right),
with the center :math:`\mu=` ``duration/2``.
Then, each output sample :math:`y` is modified according to:
.. math::
y \mapsto \frac{y-y^*}{1.0-y^*},
where :math:`y^*` is the value of the un-normalized Gaussian at the endpoints of the pulse.
This sets the endpoints to :math:`0` while preserving the amplitude at the center,
i.e. :math:`y` is set to :math:`1.0`.
Args:
t: Symbol object representing time.
center: Symbol or expression representing the middle point of the samples.
t_zero: The value of t at which the pulse is lowered to 0.
sigma: Symbol or expression representing Gaussian sigma.
Returns:
Symbolic equation.
"""
# Sympy automatically does expand.
# This causes expression inconsistency after qpy round-trip serializing through sympy.
# See issue for details: https://github.com/symengine/symengine.py/issues/409
t_shifted = (t - center).expand()
t_offset = (t_zero - center).expand()
gauss = sym.exp(-((t_shifted / sigma) ** 2) / 2)
offset = sym.exp(-((t_offset / sigma) ** 2) / 2)
return (gauss - offset) / (1 - offset)
@functools.lru_cache(maxsize=None)
def _is_amplitude_valid(
envelope_lam: Callable, time: tuple[float, ...], *fargs: float
) -> bool | np.bool_:
"""A helper function to validate maximum amplitude limit.
Result is cached for better performance.
Args:
envelope_lam: The SymbolicPulse's lambdified envelope_lam expression.
time: The SymbolicPulse's time array, given as a tuple for hashability.
fargs: The arguments for the lambdified envelope_lam, as given by `_get_expression_args`,
except for the time array.
Returns:
Return True if no sample point exceeds 1.0 in absolute value.
"""
time = np.asarray(time, dtype=float)
samples_norm = np.abs(envelope_lam(time, *fargs))
epsilon = 1e-7 # The value of epsilon mimics that of Waveform._clip()
return np.all(samples_norm < 1.0 + epsilon)
def _get_expression_args(expr: sym.Expr, params: dict[str, float]) -> list[np.ndarray | float]:
"""A helper function to get argument to evaluate expression.
Args:
expr: Symbolic expression to evaluate.
params: Dictionary of parameter, which is a superset of expression arguments.
Returns:
Arguments passed to the lambdified expression.
Raises:
PulseError: When a free symbol value is not defined in the pulse instance parameters.
"""
args: list[np.ndarray | float] = []
for symbol in sorted(expr.free_symbols, key=lambda s: s.name):
if symbol.name == "t":
# 't' is a special parameter to represent time vector.
# This should be place at first to broadcast other parameters
# in symengine lambdify function.
times = np.arange(0, params["duration"]) + 1 / 2
args.insert(0, times)
continue
try:
args.append(params[symbol.name])
except KeyError as ex:
raise PulseError(
f"Pulse parameter '{symbol.name}' is not defined for this instance. "
"Please check your waveform expression is correct."
) from ex
return args
class LambdifiedExpression:
"""Descriptor to lambdify symbolic expression with cache.
When a new symbolic expression is assigned for the first time, :class:`.LambdifiedExpression`
will internally lambdify the expressions and store the resulting callbacks in its cache.
The next time it encounters the same expression it will return the cached callbacks
thereby increasing the code's speed.
Note that this class is a python `Descriptor`_, and thus not intended to be
directly called by end-users. This class is designed to be attached to the
:class:`.SymbolicPulse` as attributes for symbolic expressions.
_`Descriptor`: https://docs.python.org/3/reference/datamodel.html#descriptors
"""
def __init__(self, attribute: str):
"""Create new descriptor.
Args:
attribute: Name of attribute of :class:`.SymbolicPulse` that returns
the target expression to evaluate.
"""
self.attribute = attribute
self.lambda_funcs: dict[int, Callable] = {}
def __get__(self, instance, owner) -> Callable:
expr = getattr(instance, self.attribute, None)
if expr is None:
raise PulseError(f"'{self.attribute}' of '{instance.pulse_type}' is not assigned.")
key = hash(expr)
if key not in self.lambda_funcs:
self.__set__(instance, expr)
return self.lambda_funcs[key]
def __set__(self, instance, value):
key = hash(value)
if key not in self.lambda_funcs:
params: list[Any] = []
for p in sorted(value.free_symbols, key=lambda s: s.name):
if p.name == "t":
# Argument "t" must be placed at first. This is a vector.
params.insert(0, p)
continue
params.append(p)
try:
lamb = sym.lambdify(params, [value], real=False)
def _wrapped_lamb(*args):
if isinstance(args[0], np.ndarray):
# When the args[0] is a vector ("t"), tile other arguments args[1:]
# to prevent evaluation from looping over each element in t.
t = args[0]
args = np.hstack(
(
t.reshape(t.size, 1),
np.tile(args[1:], t.size).reshape(t.size, len(args) - 1),
)
)
return lamb(args)
func = _wrapped_lamb
except RuntimeError:
# Currently symengine doesn't support complex_double version for
# several functions such as comparison operator and piecewise.
# If expression contains these function, it fall back to sympy lambdify.
# See https://github.com/symengine/symengine.py/issues/406 for details.
import sympy
func = sympy.lambdify(params, value)
self.lambda_funcs[key] = func
class SymbolicPulse(Pulse):
r"""The pulse representation model with parameters and symbolic expressions.
A symbolic pulse instance can be defined with an envelope and parameter constraints.
Envelope and parameter constraints should be provided as symbolic expressions.
Rather than creating a subclass, different pulse shapes can be distinguished by
the instance attributes :attr:`SymbolicPulse.envelope` and :attr:`SymbolicPulse.pulse_type`.
The symbolic expressions must be defined either with SymPy_ or Symengine_.
Usually Symengine-based expression is much more performant for instantiation
of the :class:`SymbolicPulse`, however, it doesn't support every functions available in SymPy.
You may need to choose proper library depending on how you define your pulses.
Symengine works in the most envelopes and constraints, and thus it is recommended to use
this library especially when your program contains a lot of pulses.
Also note that Symengine has the limited platform support and may not be available
for your local system. Symengine is a required dependency for Qiskit on platforms
that support it will always be installed along with Qiskit on macOS ``x86_64`` and ``arm64``,
and Linux ``x86_64``, ``aarch64``, and ``ppc64le``.
For 64-bit Windows users they will need to manual install it.
For 32-bit platforms such as ``i686`` and ``armv7`` Linux, and on Linux ``s390x``
there are no pre-compiled packages available and to use symengine you'll need to
compile it from source. If Symengine is not available in your environment SymPy will be used.
.. _SymPy: https://www.sympy.org/en/index.html
.. _Symengine: https://symengine.org
.. _symbolic_pulse_envelope:
.. rubric:: Envelope function
The waveform at time :math:`t` is generated by the :meth:`get_waveform` according to
.. math::
F(t, \Theta) = \times F(t, {\rm duration}, \overline{\rm params})
where :math:`\Theta` is the set of full pulse parameters in the :attr:`SymbolicPulse.parameters`
dictionary which must include the :math:`\rm duration`.
Note that the :math:`F` is an envelope of the waveform, and a programmer must provide this
as a symbolic expression. :math:`\overline{\rm params}` can be arbitrary complex values
as long as they pass :meth:`.validate_parameters` and your quantum backend can accept.
The time :math:`t` and :math:`\rm duration` are in units of dt, i.e. sample time resolution,
and this function is sampled with a discrete time vector in :math:`[0, {\rm duration}]`
sampling the pulse envelope at every 0.5 dt (middle sampling strategy) when
the :meth:`SymbolicPulse.get_waveform` method is called.
The sample data is not generated until this method is called
thus a symbolic pulse instance only stores parameter values and waveform shape,
which greatly reduces memory footprint during the program generation.
.. _symbolic_pulse_validation:
.. rubric:: Pulse validation
When a symbolic pulse is instantiated, the method :meth:`.validate_parameters` is called,
and performs validation of the pulse. The validation process involves testing the constraint
functions and the maximal amplitude of the pulse (see below). While the validation process
will improve code stability, it will reduce performance and might create
compatibility issues (particularly with JAX). Therefore, it is possible to disable the
validation by setting the class attribute :attr:`.disable_validation` to ``True``.
.. _symbolic_pulse_constraints:
.. rubric:: Constraint functions
Constraints on the parameters are defined with an instance attribute
:attr:`SymbolicPulse.constraints` which can be provided through the constructor.
The constraints value must be a symbolic expression, which is a
function of parameters to be validated and must return a boolean value
being ``True`` when parameters are valid.
If there are multiple conditions to be evaluated, these conditions can be
concatenated with logical expressions such as ``And`` and ``Or`` in SymPy or Symengine.
The symbolic pulse instance can be played only when the constraint function returns ``True``.
The constraint is evaluated when :meth:`.validate_parameters` is called.
.. _symbolic_pulse_eval_condition:
.. rubric:: Maximum amplitude validation
When you play a pulse in a quantum backend, you might face the restriction on the power
that your waveform generator can handle. Usually, the pulse amplitude is normalized
by this maximum power, namely :math:`\max |F| \leq 1`. This condition is
evaluated along with above constraints when you set ``limit_amplitude = True`` in the constructor.
To evaluate maximum amplitude of the waveform, we need to call :meth:`get_waveform`.
However, this introduces a significant overhead in the validation, and this cannot be ignored
when you repeatedly instantiate symbolic pulse instances.
:attr:`SymbolicPulse.valid_amp_conditions` provides a condition to skip this waveform validation,
and the waveform is not generated as long as this condition returns ``True``,
so that `healthy` symbolic pulses are created very quick.
For example, for a simple pulse shape like ``amp * cos(f * t)``, we know that
pulse amplitude is valid as long as ``amp`` remains less than magnitude 1.0.
So ``abs(amp) <= 1`` could be passed as :attr:`SymbolicPulse.valid_amp_conditions` to skip
doing a full waveform evaluation for amplitude validation.
This expression is provided through the constructor. If this is not provided,
the waveform is generated everytime when :meth:`.validate_parameters` is called.
.. rubric:: Examples
This is how a user can instantiate a symbolic pulse instance.
In this example, we instantiate a custom `Sawtooth` envelope.
.. code-block::
from qiskit.pulse.library import SymbolicPulse
my_pulse = SymbolicPulse(
pulse_type="Sawtooth",
duration=100,
parameters={"amp": 0.1, "freq": 0.05},
name="pulse1",
)
Note that :class:`SymbolicPulse` can be instantiated without providing
the envelope and constraints. However, this instance cannot generate waveforms
without knowing the envelope definition. Now you need to provide the envelope.
.. plot::
:include-source:
import sympy
from qiskit.pulse.library import SymbolicPulse
t, amp, freq = sympy.symbols("t, amp, freq")
envelope = 2 * amp * (freq * t - sympy.floor(1 / 2 + freq * t))
my_pulse = SymbolicPulse(
pulse_type="Sawtooth",
duration=100,
parameters={"amp": 0.1, "freq": 0.05},
envelope=envelope,
name="pulse1",
)
my_pulse.draw()
Likewise, you can define :attr:`SymbolicPulse.constraints` for ``my_pulse``.
After providing the envelope definition, you can generate the waveform data.
Note that it would be convenient to define a factory function that automatically
accomplishes this procedure.
.. code-block:: python
def Sawtooth(duration, amp, freq, name):
t, amp, freq = sympy.symbols("t, amp, freq")
instance = SymbolicPulse(
pulse_type="Sawtooth",
duration=duration,
parameters={"amp": amp, "freq": freq},
envelope=2 * amp * (freq * t - sympy.floor(1 / 2 + freq * t)),
name=name,
)
return instance
You can also provide a :class:`Parameter` object in the ``parameters`` dictionary,
or define ``duration`` with a :class:`Parameter` object when you instantiate
the symbolic pulse instance.
A waveform cannot be generated until you assign all unbounded parameters.
Note that parameters will be assigned through the schedule playing the pulse.
.. _symbolic_pulse_serialize:
.. rubric:: Serialization
The :class:`~SymbolicPulse` subclass can be serialized along with the
symbolic expressions through :mod:`qiskit.qpy`.
A user can therefore create a custom pulse subclass with a novel envelope and constraints,
and then one can instantiate the class with certain parameters to run on a backend.
This pulse instance can be saved in the QPY binary, which can be loaded afterwards
even within the environment not having original class definition loaded.
This mechanism also allows us to easily share a pulse program including
custom pulse instructions with collaborators.
"""
__slots__ = (
"_pulse_type",
"_params",
"_envelope",
"_constraints",
"_valid_amp_conditions",
)
disable_validation = False
# Lambdify caches keyed on sympy expressions. Returns the corresponding callable.
_envelope_lam = LambdifiedExpression("_envelope")
_constraints_lam = LambdifiedExpression("_constraints")
_valid_amp_conditions_lam = LambdifiedExpression("_valid_amp_conditions")
def __init__(
self,
pulse_type: str,
duration: ParameterExpression | int,
parameters: Mapping[str, ParameterExpression | complex] | None = None,
name: str | None = None,
limit_amplitude: bool | None = None,
envelope: sym.Expr | None = None,
constraints: sym.Expr | None = None,
valid_amp_conditions: sym.Expr | None = None,
):
"""Create a parametric pulse.
Args:
pulse_type: Display name of this pulse shape.
duration: Duration of pulse.
parameters: Dictionary of pulse parameters that defines the pulse envelope.
name: Display name for this particular pulse envelope.
limit_amplitude: If ``True``, then limit the absolute value of the amplitude of the
waveform to 1. The default is ``True`` and the amplitude is constrained to 1.
envelope: Pulse envelope expression.
constraints: Pulse parameter constraint expression.
valid_amp_conditions: Extra conditions to skip a full-waveform check for the
amplitude limit. If this condition is not met, then the validation routine
will investigate the full-waveform and raise an error when the amplitude norm
of any data point exceeds 1.0. If not provided, the validation always
creates a full-waveform.
Raises:
PulseError: When not all parameters are listed in the attribute :attr:`PARAM_DEF`.
"""
super().__init__(
duration=duration,
name=name,
limit_amplitude=limit_amplitude,
)
if parameters is None:
parameters = {}
self._pulse_type = pulse_type
self._params = parameters
self._envelope = envelope
self._constraints = constraints
self._valid_amp_conditions = valid_amp_conditions
if not self.__class__.disable_validation:
self.validate_parameters()
def __getattr__(self, item):
# Get pulse parameters with attribute-like access.
params = object.__getattribute__(self, "_params")
if item not in params:
raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{item}'")
return params[item]
@property
def pulse_type(self) -> str:
"""Return display name of the pulse shape."""
return self._pulse_type
@property
def envelope(self) -> sym.Expr:
"""Return symbolic expression for the pulse envelope."""
return self._envelope
@property
def constraints(self) -> sym.Expr:
"""Return symbolic expression for the pulse parameter constraints."""
return self._constraints
@property
def valid_amp_conditions(self) -> sym.Expr:
"""Return symbolic expression for the pulse amplitude constraints."""
return self._valid_amp_conditions
def get_waveform(self) -> Waveform:
r"""Return a Waveform with samples filled according to the formula that the pulse
represents and the parameter values it contains.
Since the returned array is a discretized time series of the continuous function,
this method uses a midpoint sampler. For ``duration``, return:
.. math::
\{f(t+0.5) \in \mathbb{C} | t \in \mathbb{Z} \wedge 0<=t<\texttt{duration}\}
Returns:
A waveform representation of this pulse.
Raises:
PulseError: When parameters are not assigned.
PulseError: When expression for pulse envelope is not assigned.
"""
if self.is_parameterized():
raise PulseError("Unassigned parameter exists. All parameters must be assigned.")
if self._envelope is None:
raise PulseError("Pulse envelope expression is not assigned.")
fargs = _get_expression_args(self._envelope, self.parameters)
return Waveform(samples=self._envelope_lam(*fargs), name=self.name)
def validate_parameters(self) -> None:
"""Validate parameters.
Raises:
PulseError: If the parameters passed are not valid.
"""
if self.is_parameterized():
return
if self._constraints is not None:
fargs = _get_expression_args(self._constraints, self.parameters)
if not bool(self._constraints_lam(*fargs)):
param_repr = ", ".join(f"{p}={v}" for p, v in self.parameters.items())
const_repr = str(self._constraints)
raise PulseError(
f"Assigned parameters {param_repr} violate following constraint: {const_repr}."
)
if self._limit_amplitude:
if self._valid_amp_conditions is not None:
fargs = _get_expression_args(self._valid_amp_conditions, self.parameters)
check_full_waveform = not bool(self._valid_amp_conditions_lam(*fargs))
else:
check_full_waveform = True
if check_full_waveform:
# Check full waveform only when the condition is satisified or
# evaluation condition is not provided.
# This operation is slower due to overhead of 'get_waveform'.
fargs = _get_expression_args(self._envelope, self.parameters)
if not _is_amplitude_valid(self._envelope_lam, tuple(fargs.pop(0)), *fargs):
param_repr = ", ".join(f"{p}={v}" for p, v in self.parameters.items())
raise PulseError(
f"Maximum pulse amplitude norm exceeds 1.0 with parameters {param_repr}."
"This can be overruled by setting Pulse.limit_amplitude."
)
def is_parameterized(self) -> bool:
"""Return True iff the instruction is parameterized."""
return any(isinstance(val, ParameterExpression) for val in self.parameters.values())
@property
def parameters(self) -> dict[str, Any]:
params: dict[str, ParameterExpression | complex | int] = {"duration": self.duration}
params.update(self._params)
return params
def __eq__(self, other: object) -> bool:
if not isinstance(other, SymbolicPulse):
return NotImplemented
if self._pulse_type != other._pulse_type:
return False
if self._envelope != other._envelope:
return False
if self.parameters != other.parameters:
return False
return True
def __repr__(self) -> str:
param_repr = ", ".join(f"{p}={v}" for p, v in self.parameters.items())
return "{}({}{})".format(
self._pulse_type,
param_repr,
f", name='{self.name}'" if self.name is not None else "",
)
__hash__ = None
class ScalableSymbolicPulse(SymbolicPulse):
r"""Subclass of :class:`SymbolicPulse` for pulses with scalable envelope.
Instance of :class:`ScalableSymbolicPulse` behaves the same as an instance of
:class:`SymbolicPulse`, but its envelope is assumed to have a scalable form
:math:`\text{amp}\times\exp\left(i\times\text{angle}\right)\times\text{F}
\left(t,\text{parameters}\right)`,
where :math:`\text{F}` is some function describing the rest of the envelope,
and both `amp` and `angle` are real (float). Note that both `amp` and `angle` are
stored in the :attr:`parameters` dictionary of the :class:`ScalableSymbolicPulse`
instance.
When two :class:`ScalableSymbolicPulse` objects are equated, instead of comparing
`amp` and `angle` individually, only the complex amplitude
:math:'\text{amp}\times\exp\left(i\times\text{angle}\right)' is compared.
"""
def __init__(
self,
pulse_type: str,
duration: ParameterExpression | int,
amp: ParameterValueType,
angle: ParameterValueType,
parameters: dict[str, ParameterExpression | complex] | None = None,
name: str | None = None,
limit_amplitude: bool | None = None,
envelope: sym.Expr | None = None,
constraints: sym.Expr | None = None,
valid_amp_conditions: sym.Expr | None = None,
):
"""Create a scalable symbolic pulse.
Args:
pulse_type: Display name of this pulse shape.
duration: Duration of pulse.
amp: The magnitude of the complex amplitude of the pulse.
angle: The phase of the complex amplitude of the pulse.
parameters: Dictionary of pulse parameters that defines the pulse envelope.
name: Display name for this particular pulse envelope.
limit_amplitude: If ``True``, then limit the absolute value of the amplitude of the
waveform to 1. The default is ``True`` and the amplitude is constrained to 1.
envelope: Pulse envelope expression.
constraints: Pulse parameter constraint expression.
valid_amp_conditions: Extra conditions to skip a full-waveform check for the
amplitude limit. If this condition is not met, then the validation routine
will investigate the full-waveform and raise an error when the amplitude norm
of any data point exceeds 1.0. If not provided, the validation always
creates a full-waveform.
Raises:
PulseError: If ``amp`` is complex.
"""
if isinstance(amp, complex):
raise PulseError(
"amp represents the magnitude of the complex amplitude and can't be complex"
)
if not isinstance(parameters, dict):
parameters = {"amp": amp, "angle": angle}
else:
parameters = deepcopy(parameters)
parameters["amp"] = amp
parameters["angle"] = angle
super().__init__(
pulse_type=pulse_type,
duration=duration,
parameters=parameters,
name=name,
limit_amplitude=limit_amplitude,
envelope=envelope,
constraints=constraints,
valid_amp_conditions=valid_amp_conditions,
)
# pylint: disable=too-many-return-statements
def __eq__(self, other: object) -> bool:
if not isinstance(other, ScalableSymbolicPulse):
return NotImplemented
if self._pulse_type != other._pulse_type:
return False
if self._envelope != other._envelope:
return False
complex_amp1 = self.amp * np.exp(1j * self.angle)
complex_amp2 = other.amp * np.exp(1j * other.angle)
if isinstance(complex_amp1, ParameterExpression) or isinstance(
complex_amp2, ParameterExpression
):
if complex_amp1 != complex_amp2:
return False
else:
if not np.isclose(complex_amp1, complex_amp2):
return False
for key in self.parameters:
if key not in ["amp", "angle"] and self.parameters[key] != other.parameters[key]:
return False
return True
class _PulseType(type):
"""Metaclass to warn at isinstance check."""
def __instancecheck__(cls, instance):
cls_alias = getattr(cls, "alias", None)
# TODO promote this to Deprecation warning in future.
# Once type information usage is removed from user code,
# we will convert pulse classes into functions.
warnings.warn(
"Typechecking with the symbolic pulse subclass will be deprecated. "
f"'{cls_alias}' subclass instance is turned into SymbolicPulse instance. "
f"Use self.pulse_type == '{cls_alias}' instead.",
PendingDeprecationWarning,
)
if not isinstance(instance, SymbolicPulse):
return False
return instance.pulse_type == cls_alias
def __getattr__(cls, item):
# For pylint. A SymbolicPulse subclass must implement several methods
# such as .get_waveform and .validate_parameters.
# In addition, they conventionally offer attribute-like access to the pulse parameters,
# for example, instance.amp returns instance._params["amp"].
# If pulse classes are directly instantiated, pylint yells no-member
# since the pulse class itself implements nothing. These classes just
# behave like a factory by internally instantiating the SymbolicPulse and return it.
# It is not realistic to write disable=no-member across qiskit packages.
return NotImplemented
class Gaussian(metaclass=_PulseType):
r"""A lifted and truncated pulse envelope shaped according to the Gaussian function whose
mean is centered at the center of the pulse (duration / 2):
.. math::
\begin{aligned}
f'(x) &= \exp\Bigl( -\frac12 \frac{{(x - \text{duration}/2)}^2}{\text{sigma}^2} \Bigr)\\
f(x) &= \text{A} \times \frac{f'(x) - f'(-1)}{1-f'(-1)}, \quad 0 \le x < \text{duration}
\end{aligned}
where :math:`f'(x)` is the gaussian waveform without lifting or amplitude scaling, and
:math:`\text{A} = \text{amp} \times \exp\left(i\times\text{angle}\right)`.
"""
alias = "Gaussian"
def __new__(
cls,
duration: int | ParameterValueType,
amp: ParameterValueType,
sigma: ParameterValueType,
angle: ParameterValueType = 0.0,
name: str | None = None,
limit_amplitude: bool | None = None,
) -> ScalableSymbolicPulse:
"""Create new pulse instance.
Args:
duration: Pulse length in terms of the sampling period `dt`.
amp: The magnitude of the amplitude of the Gaussian envelope.
sigma: A measure of how wide or narrow the Gaussian peak is; described mathematically
in the class docstring.
angle: The angle of the complex amplitude of the Gaussian envelope. Default value 0.
name: Display name for this pulse envelope.
limit_amplitude: If ``True``, then limit the amplitude of the
waveform to 1. The default is ``True`` and the amplitude is constrained to 1.
Returns:
ScalableSymbolicPulse instance.
"""
parameters = {"sigma": sigma}
# Prepare symbolic expressions
_t, _duration, _amp, _sigma, _angle = sym.symbols("t, duration, amp, sigma, angle")
_center = _duration / 2
envelope_expr = (
_amp * sym.exp(sym.I * _angle) * _lifted_gaussian(_t, _center, _duration + 1, _sigma)
)
consts_expr = _sigma > 0
valid_amp_conditions_expr = sym.Abs(_amp) <= 1.0
return ScalableSymbolicPulse(
pulse_type=cls.alias,
duration=duration,
amp=amp,
angle=angle,
parameters=parameters,
name=name,
limit_amplitude=limit_amplitude,
envelope=envelope_expr,
constraints=consts_expr,
valid_amp_conditions=valid_amp_conditions_expr,
)
class GaussianSquare(metaclass=_PulseType):
"""A square pulse with a Gaussian shaped risefall on both sides lifted such that
its first sample is zero.
Exactly one of the ``risefall_sigma_ratio`` and ``width`` parameters has to be specified.
If ``risefall_sigma_ratio`` is not None and ``width`` is None:
.. math::
\\begin{aligned}
\\text{risefall} &= \\text{risefall\\_sigma\\_ratio} \\times \\text{sigma}\\\\
\\text{width} &= \\text{duration} - 2 \\times \\text{risefall}
\\end{aligned}
If ``width`` is not None and ``risefall_sigma_ratio`` is None:
.. math:: \\text{risefall} = \\frac{\\text{duration} - \\text{width}}{2}
In both cases, the lifted gaussian square pulse :math:`f'(x)` is defined as:
.. math::
\\begin{aligned}
f'(x) &= \\begin{cases}\
\\exp\\biggl(-\\frac12 \\frac{(x - \\text{risefall})^2}{\\text{sigma}^2}\\biggr)\
& x < \\text{risefall}\\\\
1\
& \\text{risefall} \\le x < \\text{risefall} + \\text{width}\\\\
\\exp\\biggl(-\\frac12\
\\frac{{\\bigl(x - (\\text{risefall} + \\text{width})\\bigr)}^2}\
{\\text{sigma}^2}\
\\biggr)\
& \\text{risefall} + \\text{width} \\le x\
\\end{cases}\\\\
f(x) &= \\text{A} \\times \\frac{f'(x) - f'(-1)}{1-f'(-1)},\
\\quad 0 \\le x < \\text{duration}
\\end{aligned}
where :math:`f'(x)` is the gaussian square waveform without lifting or amplitude scaling, and
:math:`\\text{A} = \\text{amp} \\times \\exp\\left(i\\times\\text{angle}\\right)`.
"""
alias = "GaussianSquare"
def __new__(
cls,
duration: int | ParameterValueType,
amp: ParameterValueType,
sigma: ParameterValueType,
width: ParameterValueType | None = None,
angle: ParameterValueType = 0.0,
risefall_sigma_ratio: ParameterValueType | None = None,
name: str | None = None,
limit_amplitude: bool | None = None,
) -> ScalableSymbolicPulse:
"""Create new pulse instance.
Args:
duration: Pulse length in terms of the sampling period `dt`.
amp: The magnitude of the amplitude of the Gaussian and square pulse.
sigma: A measure of how wide or narrow the Gaussian risefall is; see the class
docstring for more details.
width: The duration of the embedded square pulse.
angle: The angle of the complex amplitude of the pulse. Default value 0.
risefall_sigma_ratio: The ratio of each risefall duration to sigma.
name: Display name for this pulse envelope.
limit_amplitude: If ``True``, then limit the amplitude of the
waveform to 1. The default is ``True`` and the amplitude is constrained to 1.
Returns:
ScalableSymbolicPulse instance.
Raises:
PulseError: When width and risefall_sigma_ratio are both empty or both non-empty.
"""
# Convert risefall_sigma_ratio into width which is defined in OpenPulse spec
if width is None and risefall_sigma_ratio is None:
raise PulseError(
"Either the pulse width or the risefall_sigma_ratio parameter must be specified."
)
if width is not None and risefall_sigma_ratio is not None:
raise PulseError(
"Either the pulse width or the risefall_sigma_ratio parameter can be specified"
" but not both."
)
if width is None and risefall_sigma_ratio is not None:
width = duration - 2.0 * risefall_sigma_ratio * sigma
parameters = {"sigma": sigma, "width": width}
# Prepare symbolic expressions
_t, _duration, _amp, _sigma, _width, _angle = sym.symbols(
"t, duration, amp, sigma, width, angle"
)
_center = _duration / 2
_sq_t0 = _center - _width / 2
_sq_t1 = _center + _width / 2
_gaussian_ledge = _lifted_gaussian(_t, _sq_t0, -1, _sigma)
_gaussian_redge = _lifted_gaussian(_t, _sq_t1, _duration + 1, _sigma)
envelope_expr = (
_amp
* sym.exp(sym.I * _angle)
* sym.Piecewise(
(_gaussian_ledge, _t <= _sq_t0), (_gaussian_redge, _t >= _sq_t1), (1, True)
)
)
consts_expr = sym.And(_sigma > 0, _width >= 0, _duration >= _width)
valid_amp_conditions_expr = sym.Abs(_amp) <= 1.0
return ScalableSymbolicPulse(
pulse_type=cls.alias,
duration=duration,
amp=amp,
angle=angle,
parameters=parameters,
name=name,
limit_amplitude=limit_amplitude,
envelope=envelope_expr,
constraints=consts_expr,
valid_amp_conditions=valid_amp_conditions_expr,
)
def GaussianSquareDrag(
duration: int | ParameterExpression,
amp: float | ParameterExpression,
sigma: float | ParameterExpression,
beta: float | ParameterExpression,
width: float | ParameterExpression | None = None,
angle: float | ParameterExpression | None = 0.0,
risefall_sigma_ratio: float | ParameterExpression | None = None,
name: str | None = None,
limit_amplitude: bool | None = None,
) -> ScalableSymbolicPulse:
"""A square pulse with a Drag shaped rise and fall
This pulse shape is similar to :class:`~.GaussianSquare` but uses
:class:`~.Drag` for its rise and fall instead of :class:`~.Gaussian`. The
addition of the DRAG component of the rise and fall is sometimes helpful in
suppressing the spectral content of the pulse at frequencies near to, but
slightly offset from, the fundamental frequency of the drive. When there is
a spectator qubit close in frequency to the fundamental frequency,
suppressing the drive at the spectator's frequency can help avoid unwanted
excitation of the spectator.
Exactly one of the ``risefall_sigma_ratio`` and ``width`` parameters has to be specified.
If ``risefall_sigma_ratio`` is not ``None`` and ``width`` is ``None``:
.. math::
\\begin{aligned}
\\text{risefall} &= \\text{risefall\\_sigma\\_ratio} \\times \\text{sigma}\\\\
\\text{width} &= \\text{duration} - 2 \\times \\text{risefall}
\\end{aligned}
If ``width`` is not None and ``risefall_sigma_ratio`` is None:
.. math:: \\text{risefall} = \\frac{\\text{duration} - \\text{width}}{2}
Gaussian :math:`g(x, c, σ)` and lifted gaussian :math:`g'(x, c, σ)` curves
can be written as:
.. math::
\\begin{aligned}
g(x, c, σ) &= \\exp\\Bigl(-\\frac12 \\frac{(x - c)^2}{σ^2}\\Bigr)\\\\
g'(x, c, σ) &= \\frac{g(x, c, σ)-g(-1, c, σ)}{1-g(-1, c, σ)}
\\end{aligned}
From these, the lifted DRAG curve :math:`d'(x, c, σ, β)` can be written as
.. math::
d'(x, c, σ, β) = g'(x, c, σ) \\times \\Bigl(1 + 1j \\times β \\times\
\\Bigl(-\\frac{x - c}{σ^2}\\Bigr)\\Bigr)
The lifted gaussian square drag pulse :math:`f'(x)` is defined as:
.. math::
\\begin{aligned}
f'(x) &= \\begin{cases}\
\\text{A} \\times d'(x, \\text{risefall}, \\text{sigma}, \\text{beta})\
& x < \\text{risefall}\\\\
\\text{A}\
& \\text{risefall} \\le x < \\text{risefall} + \\text{width}\\\\
\\text{A} \\times \\times d'(\
x - (\\text{risefall} + \\text{width}),\
\\text{risefall},\
\\text{sigma},\
\\text{beta}\
)\
& \\text{risefall} + \\text{width} \\le x\
\\end{cases}\\\\
\\end{aligned}
where :math:`\\text{A} = \\text{amp} \\times
\\exp\\left(i\\times\\text{angle}\\right)`.
Args:
duration: Pulse length in terms of the sampling period `dt`.
amp: The amplitude of the DRAG rise and fall and of the square pulse.
sigma: A measure of how wide or narrow the DRAG risefall is; see the class
docstring for more details.
beta: The DRAG correction amplitude.
width: The duration of the embedded square pulse.
angle: The angle in radians of the complex phase factor uniformly
scaling the pulse. Default value 0.