diff --git a/qiskit/pulse/library/parametric_pulses.py b/qiskit/pulse/library/parametric_pulses.py index 442bb6681abc..7b06af8e79d2 100644 --- a/qiskit/pulse/library/parametric_pulses.py +++ b/qiskit/pulse/library/parametric_pulses.py @@ -242,6 +242,9 @@ def __init__( limit_amplitude: If ``True``, then limit the amplitude of the waveform to 1. The default is ``True`` and the amplitude is constrained to 1. + + Raises: + PulseError: If the parameters passed are not valid. """ if not _is_parameterized(amp): amp = complex(amp) @@ -249,6 +252,13 @@ def __init__( self._sigma = sigma self._risefall_sigma_ratio = risefall_sigma_ratio self._width = width + + if self.width is not None and self.risefall_sigma_ratio is not None: + raise PulseError( + "Either the pulse width or the risefall_sigma_ratio parameter can be specified" + " but not both." + ) + super().__init__(duration=duration, name=name, limit_amplitude=limit_amplitude) @property @@ -284,11 +294,6 @@ def validate_parameters(self) -> None: ) if not _is_parameterized(self.sigma) and self.sigma <= 0: raise PulseError("Sigma must be greater than 0.") - if self.width is not None and self.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 self.width is None and self.risefall_sigma_ratio is None: raise PulseError( "Either the pulse width or the risefall_sigma_ratio parameter must be specified." diff --git a/releasenotes/notes/consistent-validation-for-gaussian-square-pulse-461087a09ff339a4.yaml b/releasenotes/notes/consistent-validation-for-gaussian-square-pulse-461087a09ff339a4.yaml new file mode 100644 index 000000000000..10e62a7c0869 --- /dev/null +++ b/releasenotes/notes/consistent-validation-for-gaussian-square-pulse-461087a09ff339a4.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - | + Parameter validation for :class:`~.pulse.library.GaussianSquare` is now + consistent before and after construction. + Refer to `#7882 `__ for more details. diff --git a/test/python/pulse/test_pulse_lib.py b/test/python/pulse/test_pulse_lib.py index eb870313d2de..1dc789ff8039 100644 --- a/test/python/pulse/test_pulse_lib.py +++ b/test/python/pulse/test_pulse_lib.py @@ -162,6 +162,13 @@ def test_gauss_square_extremes(self): gaus_square.get_waveform().samples[2:-2], const.get_waveform().samples[2:-2] ) + def test_gauss_square_passes_validation_after_construction(self): + """Test that parameter validation is consistent before and after construction. + + This previously used to raise an exception: see gh-7882.""" + pulse = GaussianSquare(duration=125, sigma=4, amp=0.5j, width=100) + pulse.validate_parameters() + def test_drag_pulse(self): """Test that the Drag sample pulse matches the pulse library.""" drag = Drag(duration=25, sigma=4, amp=0.5j, beta=1)