NMRPulse
The NMRPulse class is a Pydantic BaseModel that describes a single pulse and is inherited by the classes of the other pulses.
For now there are four types of pulses:
- Rotation pulse
- Time evolution pulse
- Selective frequency pulse
- Soft pulse
A pulse has to implement the following abstract methods:
@abstractmethod
def apply_pulse_operator(
self,
density_mat: np.ndarray,
**kwargs,
) -> np.ndarray:
"""Applies the current pulse on the density matrix.
Args:
density_mat (Operator): The density matrix rho.
kwargs: Additional arguments needed for the pulse application, such as:
num_spins (int): The number of spins.
hamiltonian (np.ndarray): The Hamiltonian.
nmr_parameters (NMRParameters): The NMR parameters.
calculation_params (NMRCalculationParameters): The calculation parameters.
eigen_hamiltonian (Tuple[np.ndarray, np.ndarray]): The eigenvalues and eigenvectors
Returns:
Operator: P rho P^dagger.
"""
@abstractmethod
def get_pulse_operator(
self,
**kwargs,
) -> np.ndarray:
"""Returns the current pulse operator.
Args:
kwargs: Additional arguments needed for the pulse application, such as:
num_spins (int): The number of spins.
hamiltonian (np.ndarray): The Hamiltonian.
nmr_parameters (NMRParameters): The NMR parameters.
calculation_params (NMRCalculationParameters): The calculation parameters.
eigen_hamiltonian (Tuple[np.ndarray, np.ndarray]): The eigenvalues and eigenvectors
Returns:
np.ndarray: The pulse operator P.
"""
All pulse classes inherit from Pydantic’s BaseModel, which provides automatic validation and serialization capabilities.
Pydantic Features
Serialization
Individual pulses can be serialized to dictionaries or JSON:
from hqs_nmr.pulse_sequence.pulses import RotationPulse
pulse = RotationPulse(angle=90, axis="x")
# Serialize to dictionary
pulse_dict = pulse.model_dump()
# Serialize to JSON
pulse_json = pulse.model_dump_json()
Deserialization
Pulses can be reconstructed from dictionaries or JSON:
# From dictionary
pulse_dict = {"angle": 90, "axis": "x", "active_sites": None}
pulse = RotationPulse.model_validate(pulse_dict)
# From JSON
pulse_json = '{"angle": 90, "axis": "x", "active_sites": null}'
pulse = RotationPulse.model_validate_json(pulse_json)
Validation
Pydantic automatically validates input data:
# This will raise a validation error
try:
pulse = RotationPulse(angle="invalid", axis="x")
except ValidationError as e:
print(e)
String Parsing
The NMRPulse.from_string() class method provides a convenient way to create pulses from string representations:
pulse = NMRPulse.from_string("90x") # Creates a RotationPulse
pulse = NMRPulse.from_string("t5") # Creates a TimeEvolutionPulse
pulse = NMRPulse.from_string("pi/2x|1099.82f") # Creates a SelectiveFrequencyPulse
# Format: {angle}{axis}|{frequency_Hz}f{fwhm_Hz}w
# The fwhm_Hz (w parameter) is optional,
# defaulting to 10 Hz
Note: SoftPulse cannot be created from a string. Use the soft_pulse builder method on NMRPulseSequence or instantiate it directly.