SoftPulse
A SoftPulse is a time-dependent pulse with a configurable envelope shape. Unlike
rotation pulses which apply instantaneous rotations, soft pulses apply a smooth
time-varying Hamiltonian that evolves the system over a finite duration.
Note: SoftPulse cannot be created using the from_string method. It must be
instantiated directly or added to a sequence via the soft_pulse builder method.
Physics
The soft pulse applies a time-dependent Hamiltonian of the form:
H_total(t) = -i(H + f(t) * O(phi))
where:
His the molecular Hamiltonianf(t)is the envelope functionO(phi) = cos(phi) * Sx + sin(phi) * Syis the pulse operator modulated by the phasephi
The time evolution is computed using a piecewise 2nd order Magnus expansion, dividing
the total pulse time into n_steps = total_pulse_time / dt intervals.
Creating a SoftPulse
from hqs_nmr.pulse_sequence.pulses.soft_pulse import SoftPulse, PulseShape, IntegrationMethod
pulse = SoftPulse(
shape=PulseShape.GAUSSIAN,
shape_params={"t_0": 0.5, "sigma": 0.1, "amplitude": np.pi/2},
phi=0.0,
total_pulse_time=1.0,
integration_method=IntegrationMethod.MIDPOINT,
)
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
shape | PulseShape | Yes | — | Envelope shape (see below) |
shape_params | dict | Yes | — | Parameters for the chosen envelope shape (see below) |
phi | float | Yes | — | Carrier phase in radians |
total_pulse_time | float | Yes | — | Total pulse duration in seconds |
dt | float | None | No | None |
integration_method | IntegrationMethod | No | IntegrationMethod.MIDPOINT | Magnus expansion method |
points_per_feature | int | No | 10 | Number of integration steps per characteristic feature. Only used when dt is None |
Pulse Shapes
All shapes are available via the PulseShape enum.
Pulse Normalization
All pulse shapes use sigma-based normalization to ensure consistent behavior. The envelope functions are scaled so that the pulse applies its specified amplitude over each sigma amount of time.
Normalization Principle
For all pulse shapes, the normalization is designed such that:
- The envelope function includes a division by
sigma - This ensures that the time integral of the envelope over a period of
sigmaequals the specifiedamplitude
Example
For a constant pulse with sigma = τ and amplitude = π/2:
f(t) = (π/2) / τ
Applied over a period of 4τ, the total rotation is:
Total rotation = (π/2 / τ) × 4τ = 2π
This normalization applies to all pulse shapes:
- Constant and Rectangular: Direct division by sigma
- Gaussian: Normalized with factor
1 / (sigma * sqrt(2π)), Half-Gaussian: Normalized with factor2 / (sigma * sqrt(2π))to ensure proper area under the curve - EBURP-2, IBURP-2, REBURP, Q3, Q5: Shape function multiplied by
amplitude / sigma
The factor of 2 in the Half-Gaussian normalization ensures that a half-Gaussian applies the same total pulse as a full Gaussian when used appropriately.
Constant (PulseShape.CONSTANT)
A flat envelope of constant amplitude.
f(t) = amplitude / sigma
Parameters in shape_params:
| Parameter | Required | Default | Description |
|---|---|---|---|
sigma | Yes | — | Scale parameter that controls pulse duration |
amplitude | No | π/2 | Pulse amplitude |
shape_params = {"sigma": 1.0, "amplitude": np.pi/2}
See Pulse Normalization for details on how sigma affects the pulse.
Rectangular (PulseShape.RECTANGULAR)
A rectangular window that is non-zero only between t_start and t_end.
f(t) = amplitude / sigma if t_start <= t <= t_end
f(t) = 0 otherwise
Parameters in shape_params:
| Parameter | Required | Default | Description |
|---|---|---|---|
t_start | Yes | — | Start time of the active window |
t_end | Yes | — | End time of the active window |
sigma | Yes | — | Scale parameter for pulse amplitude |
amplitude | No | π/2 | Amplitude inside the window |
shape_params = {"t_start": 0.25, "t_end": 0.75, "sigma": 1.0, "amplitude": np.pi/2}
See Pulse Normalization for details on how sigma affects the pulse.
Gaussian (PulseShape.GAUSSIAN)
A normalised Gaussian envelope centred at t_0 with width sigma.
f(t) = amplitude * exp(-(t - t_0)^2 / (2 * sigma^2)) * 1 / (sigma * sqrt(2*π))
Parameters in shape_params:
| Parameter | Required | Default | Description |
|---|---|---|---|
t_0 | Yes | — | Centre time of the Gaussian |
sigma | Yes | — | Standard deviation (width) |
amplitude | No | π/2 | Amplitude scaling factor |
shape_params = {"t_0": 0.5, "sigma": 0.1, "amplitude": np.pi/2}
Half-Gaussian (PulseShape.HALF_GAUSSIAN)
A half-Gaussian envelope that rises towards T (rising) or falls from 0 (falling).
f(t) = amplitude * exp(-(t - t_0)^2 / (2 * sigma^2)) * 2 / (sigma * sqrt(2*π))
where t_0 = T if rising=True, else t_0 = 0.
Parameters in shape_params:
| Parameter | Required | Default | Description |
|---|---|---|---|
sigma | Yes | — | Standard deviation (width) in seconds |
rising | No | True | If True, envelope rises towards end of pulse |
amplitude | No | π/2 | Amplitude scaling factor |
# Rising half-Gaussian
shape_params = {"sigma": 0.3, "rising": True, "amplitude": np.pi/2}
# Falling half-Gaussian
shape_params = {"sigma": 0.3, "rising": False, "amplitude": np.pi/2}
The factor of 2 normalization ensures the half-Gaussian applies the same total pulse as a full Gaussian when used appropriately. See Pulse Normalization for details.
EBURP-2 (PulseShape.EBURP2)
An excitation pulse shape defined by a Fourier series with the EBURP-2 coefficients. Suitable for selective excitation.
Parameters in shape_params:
| Parameter | Required | Default | Description |
|---|---|---|---|
sigma | Yes | — | Scale parameter for pulse amplitude |
amplitude | No | π/2 | Amplitude scaling factor |
shape_params = {"sigma": 1.0, "amplitude": np.pi/2}
IBURP-2 (PulseShape.IBURP2)
An inversion pulse shape defined by a Fourier series with the IBURP-2 coefficients. Suitable for selective inversion.
Parameters in shape_params:
| Parameter | Required | Default | Description |
|---|---|---|---|
sigma | Yes | — | Scale parameter for pulse amplitude |
amplitude | No | π/2 | Amplitude scaling factor |
shape_params = {"sigma": 1.0, "amplitude": np.pi/2}
REBURP (PulseShape.REBURP)
A refocusing pulse shape defined by a Fourier series with the REBURP coefficients. Suitable for selective refocusing.
Parameters in shape_params:
| Parameter | Required | Default | Description |
|---|---|---|---|
sigma | Yes | — | Scale parameter for pulse amplitude |
amplitude | No | π/2 | Amplitude scaling factor |
shape_params = {"sigma": 1.0, "amplitude": np.pi/2}
Q3 Gaussian Cascade (PulseShape.Q3)
A 90° excitation pulse defined as a cascade of 3 Gaussian functions with optimised positions, weights and widths.
Parameters in shape_params:
| Parameter | Required | Default | Description |
|---|---|---|---|
sigma | Yes | — | Scale parameter for pulse amplitude |
amplitude | No | π/2 | Amplitude scaling factor |
shape_params = {"sigma": 1.0, "amplitude": np.pi/2}
Q5 Gaussian Cascade (PulseShape.Q5)
A 90° excitation pulse defined as a cascade of 5 Gaussian functions with optimised positions, weights and widths. Higher selectivity than Q3.
Parameters in shape_params:
| Parameter | Required | Default | Description |
|---|---|---|---|
sigma | Yes | — | Scale parameter for pulse amplitude |
amplitude | No | π/2 | Amplitude scaling factor |
shape_params = {"sigma": 1.0, "amplitude": np.pi/2}
Integration Methods
Two integration methods are available via the IntegrationMethod enum.
MIDPOINT (IntegrationMethod.MIDPOINT)
Uses the midpoint rule for the first Magnus term and a commutator correction for the second:
Omega_1 = -i * dt * (H + f(t_mid) * O(phi))
Omega_2 = (dt^2 / 12) * (f(t_k) - f(t_{k+1})) * [O(phi), H]
Good balance of speed and accuracy for smooth envelopes.
GL (IntegrationMethod.GL)
Uses 2-point Gauss-Legendre quadrature for improved accuracy:
Omega_1 = -i * dt * (H + 0.5 * (f(t_1) + f(t_2)) * O(phi))
Omega_2 = (sqrt(3) * dt^2 / 12) * (f(t_1) - f(t_2)) * [O(phi), H]
where t_1 and t_2 are the two Gauss-Legendre quadrature points within the interval.
Choosing the Timestep
When dt is not provided, it is derived automatically from the pulse shape and the
points_per_feature parameter:
dt = characteristic_timescale / points_per_feature
The characteristic timescale is chosen to capture the finest feature of each envelope:
| Shape | Characteristic timescale |
|---|---|
CONSTANT | total_pulse_time |
RECTANGULAR | t_end - t_start |
GAUSSIAN, HALF_GAUSSIAN | sigma * total_pulse_time |
EBURP2, IBURP2, REBURP | total_pulse_time / n_max (highest Fourier harmonic) |
Q3, Q5 | min(fwhm) * total_pulse_time (narrowest Gaussian component) |
The timestep is additionally capped at total_pulse_time / points_per_feature to
guarantee a minimum number of integration steps regardless of the shape.
Since the midpoint rule converges as 1 / points_per_feature^2, the expected
relative integration accuracy is:
points_per_feature | Relative accuracy |
|---|---|
10 (default) | ~1e-4 |
100 | ~1e-6 |
1000 | ~1e-8 |
Note: Composite shapes such as
Q3andQ5may be up to two orders of magnitude less accurate than single-feature shapes for the samepoints_per_feature, due to error accumulation across Gaussian components. If high accuracy is required for these shapes, consider increasingpoints_per_featureor providing an explicitdt.
Adding to a Pulse Sequence
The recommended way to add a soft pulse to a sequence is via the soft_pulse builder
method on NMRPulseSequence:
from hqs_nmr.pulse_sequence import NMRPulseSequence
sequence = NMRPulseSequence()
sequence.soft_pulse(
total_time=1.0,
phi=0.0,
shape_params={"t_0": 0.5, "sigma": 0.1, "amplitude": np.pi/2},
timestep=1e-4,
shape="gaussian",
integration_method="midpoint",
)
Soft pulses can be freely combined with other pulse types and support method chaining:
sequence = (
NMRPulseSequence()
.rotation_pulse(90, "x")
.soft_pulse(
total_time=1.0,
phi=0.0,
shape_params={"t_0": 0.5, "sigma": 0.1, "amplitude": np.pi/2},
shape="gaussian",
)
.rotation_pulse(90, "y")
)
Applying the Pulse Directly
When using SoftPulse directly, the Hamiltonian must always be provided:
import numpy as np
hamiltonian = np.array([[1, 0], [0, -1]], dtype=complex)
density_mat = np.array([[0.5, 0.5], [0.5, 0.5]], dtype=complex)
rho_evolved = pulse.apply_pulse_operator(
density_mat=density_mat,
num_spins=1,
hamiltonian=hamiltonian,
)
Visualising Envelope Shapes
The pulse_shapes.ipynb notebook in the repository root helps to visualise all available
envelope shapes. Run it to compare the different pulse profiles before choosing one
for your experiment.
Methods
The SoftPulse class provides several methods for applying the pulse to density matrices and state vectors. All methods compute the time evolution operator using Magnus expansion.
apply_pulse_operator
def apply_pulse_operator(
self,
density_mat: np.ndarray,
**kwargs: Any,
) -> np.ndarray:
"""Apply the soft pulse to the density matrix.
Computes the evolved density matrix: rho' = U @ rho @ U† where U is the
time evolution operator computed via Magnus expansion.
Args:
density_mat (np.ndarray): The initial density matrix as a (2^N × 2^N) complex array.
**kwargs: Additional arguments (all required):
num_spins (int): Number of spins N in the system.
hamiltonian (np.ndarray): The molecular Hamiltonian as a (2^N × 2^N) complex array.
Returns:
np.ndarray: The evolved density matrix rho' = U @ rho @ U†.
Raises:
TypeError: If num_spins is not provided.
TypeError: If hamiltonian is not provided.
"""
Example:
from hqs_nmr.pulse_sequence.pulses import SoftPulse
from hqs_nmr.pulse_sequence.pulses.soft_pulse import PulseShape
import numpy as np
pulse = SoftPulse(
shape=PulseShape.GAUSSIAN,
shape_params={"t_0": 0.5, "sigma": 0.1, "amplitude": np.pi/2},
phi=0.0,
total_pulse_time=1.0,
)
hamiltonian = np.array([[1, 0.1], [0.1, -1]], dtype=complex)
density_mat = np.eye(2, dtype=complex) / 2
rho_evolved = pulse.apply_pulse_operator(
density_mat=density_mat,
num_spins=1,
hamiltonian=hamiltonian
)
apply_time_evolution
def apply_time_evolution(
self,
density_mat: np.ndarray,
measurement_operator: Union[np.ndarray, List[np.ndarray]],
t_0: float = 0,
correlation_operator: Optional[np.ndarray] = None,
**kwargs,
) -> Tuple[Union[List[complex], list[List[complex]]], np.ndarray, List[float]]:
"""Applies the time evolution and collects expectation values at each timestep.
This method evolves the density matrix under the soft pulse Hamiltonian for the
total pulse time, recording the expectation value of the measurement operator at
each integration step. The timesteps are determined by the `dt` attribute of
the pulse.
Args:
density_mat (np.ndarray): The initial density matrix.
measurement_operator (Union[np.ndarray, List[np.ndarray]]): The operator(s) whose
expectation value ⟨M⟩ = Tr(M @ rho(t)) is recorded at each timestep.
t_0: Time at the start of the pulse
correlation_operator: Operator whose expectation value is recorded
kwargs: Additional arguments:
num_spins (int): Number of spins N in the system.
hamiltonian (np.ndarray): The molecular Hamiltonian as a (2^N × 2^N) complex array.
If a Hamiltonian was set on the pulse, it takes precedence.
Returns:
Tuple[Union[List[complex], list[List[complex]]], np.ndarray, List[float]]: A tuple containing:
- Union[List[complex], list[List[complex]]]: Expectation values at each timestep
from t=t_0 to t=t_0+total_pulse_time.
- np.ndarray: The final density matrix after full time evolution.
- List[float]: The times of each timestep
"""
Example:
from hqs_nmr.pulse_sequence.pulses import SoftPulse
from hqs_nmr.pulse_sequence.pulses.soft_pulse import PulseShape
import numpy as np
pulse = SoftPulse(
shape=PulseShape.GAUSSIAN,
shape_params={"t_0": 0.5, "sigma": 0.1, "amplitude": np.pi/2},
phi=0.0,
total_pulse_time=1.0,
dt=0.01,
)
hamiltonian = np.array([[1, 0.1], [0.1, -1]], dtype=complex)
density_mat = np.eye(2, dtype=complex) / 2
measurement_op = np.array([[1, 0], [0, -1]], dtype=complex) # Sz
expectation_values, final_density, times = pulse.apply_time_evolution(
density_mat=density_mat,
measurement_operator=measurement_op,
num_spins=1,
hamiltonian=hamiltonian,
)
# expectation_values contains values at each timestep
apply_pulse_operator_vec
def apply_pulse_operator_vec(
self,
state_vector: np.ndarray,
num_spins: int,
hamiltonian: Optional[np.ndarray] = None,
) -> np.ndarray:
"""Apply the soft pulse to a state vector.
Computes the evolved state vector: v' = U @ v where U is the time evolution
operator computed via Magnus expansion.
Args:
state_vector (np.ndarray): The initial state vector as a (2^N,) complex array.
num_spins (int): Number of spins N in the system.
hamiltonian (np.ndarray): The molecular Hamiltonian. Must be provided for soft pulses.
Returns:
np.ndarray: The evolved state vector v' = U @ v.
Raises:
TypeError: If hamiltonian is None.
"""
Example:
from hqs_nmr.pulse_sequence.pulses import SoftPulse
from hqs_nmr.pulse_sequence.pulses.soft_pulse import PulseShape
import numpy as np
pulse = SoftPulse(
shape=PulseShape.HALF_GAUSSIAN,
shape_params={"sigma": 0.2, "rising": True, "amplitude": np.pi/2},
phi=0.0,
total_pulse_time=1.0,
)
hamiltonian = np.array([[1, 0.1], [0.1, -1]], dtype=complex)
state = np.array([1, 0], dtype=complex) # Ground state
evolved_state = pulse.apply_pulse_operator_vec(
state_vector=state,
num_spins=1,
hamiltonian=hamiltonian
)
apply_time_evolution_vec
def apply_time_evolution_vec(
self,
state_vector: np.ndarray,
measurement_operator: Union[np.ndarray, List[np.ndarray]],
t_0: float = 0,
correlation_operator: Optional[np.ndarray] = None,
**kwargs,
) -> Tuple[Union[List[complex], list[List[complex]]], np.ndarray, List[float]]:
"""Apply the soft pulse to a state vector and collect expectation values.
Evolves the state vector under the soft pulse Hamiltonian and records
the expectation value of measurement_operator at each integration step.
Args:
state_vector (np.ndarray): The initial state vector as a (2^N,) complex array.
measurement_operator (Union[np.ndarray, List[np.ndarray]]): Operator(s) whose
expectation value ⟨M⟩ = ⟨v|U†@M@U|v⟩ is recorded at each timestep.
t_0: Time at the start of the pulse
correlation_operator (Optional[np.ndarray]): Optional operator applied to the state
before computing expectation values. If provided, the state is first transformed
as v → correlation_operator @ v before evolution.
kwargs: Additional arguments:
num_spins (int): Number of spins N in the system.
hamiltonian (np.ndarray): The molecular Hamiltonian as a (2^N × 2^N) complex array.
If a Hamiltonian was set on the pulse, it takes precedence.
Returns:
Tuple[Union[List[complex], list[List[complex]]], np.ndarray, List[float]]: A tuple containing:
- Union[List[complex], list[List[complex]]]: Expectation values at each
integration step (n_steps + 1 values).
- np.ndarray: The evolved state vector v' = U @ v.
- List[float]: The times of each timestep.
"""
Example:
from hqs_nmr.pulse_sequence.pulses import SoftPulse
from hqs_nmr.pulse_sequence.pulses.soft_pulse import PulseShape
import numpy as np
pulse = SoftPulse(
shape=PulseShape.Q3,
shape_params={"sigma": 1.0, "amplitude": np.pi/2},
phi=0.0,
total_pulse_time=2.0,
dt=0.01,
)
hamiltonian = np.array([[1, 0.1], [0.1, -1]], dtype=complex)
state = np.array([1, 0], dtype=complex)
measurement_op = np.array([[1, 0], [0, -1]], dtype=complex)
correlation_op = np.array([[0, 1], [1, 0]], dtype=complex) # Sx
expectation_values, final_state, times = pulse.apply_time_evolution_vec(
state_vector=state,
measurement_operator=measurement_op,
num_spins=1,
hamiltonian=hamiltonian,
correlation_operator=correlation_op
)
get_pulse_operator
def get_pulse_operator(
self,
**kwargs: Any,
) -> np.ndarray:
"""Returns the time evolution operator (propagator) for the soft pulse.
Computes the propagator U using piecewise 2nd order Magnus expansion.
Args:
**kwargs: Additional arguments (all required):
num_spins (int): The number of spins.
hamiltonian (np.ndarray): The molecular Hamiltonian as a (2^N × 2^N) complex array.
Returns:
np.ndarray: The propagator U as a (2^num_spins × 2^num_spins) complex matrix.
Raises:
TypeError: If num_spins is not provided.
TypeError: If hamiltonian is not provided.
"""
Example:
from hqs_nmr.pulse_sequence.pulses import SoftPulse
from hqs_nmr.pulse_sequence.pulses.soft_pulse import PulseShape
import numpy as np
pulse = SoftPulse(
shape=PulseShape.EBURP2,
shape_params={"sigma": 1.0, "amplitude": np.pi/2},
phi=np.pi/4,
total_pulse_time=1.0,
)
hamiltonian = np.array([[1, 0.1], [0.1, -1]], dtype=complex)
# Get the propagator
U = pulse.get_pulse_operator(num_spins=1, hamiltonian=hamiltonian)
# Use it to evolve any state or density matrix
state = np.array([1, 0], dtype=complex)
evolved_state = U @ state
density = np.outer(state, state.conj())
evolved_density = U @ density @ U.conj().T