Introduction
HQS Spectrum Tools is a software toolkit to simulate NMR spectra. It consists of hqs-nmr, containing simulation tools and hqs-nmr-parameters, providing NMR parameters for a variety of molecules. With HQS Spectrum Tools you can calculate and analyze NMR spectra for molecules or other spin systems.
Given the NMR parameters of a molecule, a simple python API can be used to calculate the associated NMR spectrum.
RUST and C++ components are used internally to achieve remarkable speed and accuracy, even for complex, large molecules.
“One of the fastest Hilbert-space NMR simulation tools I have ever seen, with a remarkably efficient frequency-domain implementation.”
- Ilya Kuprov, Professor of Physics, University of Southampton
HQS Spectrum Tools is also an integral part of HQSpectrum, our end-to-end solution for NMR spectra analysis, which is available to you, if you have an existing HQStage account.
NMR spectra prediction is also a very promising use case for the quantum computer. To explore this possibility, we offer to extract the relevant spin Hamiltonians in struqture format for all molecules in the hqs-nmr-parameters database.
If you are interested in the theory behind NMR or the math of how to calculate NMR spectra, take a look at the background chapter.
Applications
Nuclear magnetic resonance (NMR) spectroscopy is a key analytical tool in chemistry and related disciplines. Its broad application not only facilitates the identification of molecules, but also provides intricate details about their structure, dynamics, and chemical environment. The ability to compare an experimental NMR spectrum with theoretical predictions for various compounds is crucial for the accurate identification and characterization of chemical entities.
Our NMR spectrum solvers are designed for both speed and precision. They are based on a very efficient implementation in the frequency domain. With a variety of solver strategies at your disposal, you can leverage symmetries and clustering techniques either fully automated or customized according to your specifications. This flexibility allows you to tailor your approach based on the specific characteristics of your system, optimizing performance without sacrificing accuracy.
Getting started
To use HQS Spectrum Tools you need a running version of HQStage. You can check here how to set it up. Once you have HQStage correctly configured you can install HQS Spectrum Tools using the command:
hqstage install hqs-nmr
Please note that HQS Spectrum Tools is currently only supported on Linux.
For some of the functionalities of HQS Spectrum Tools (especially the provided example NMR parameters), the RDKit cheminformatics package is required. While packages are available on pypi.org, at present they are not officially supported by the RDKit development team. Therefore, it is strongly recommended to install it via the conda-forge channel to ensure the access to its latest version. The easiest way to install the library is running the following command in an active conda environment:
conda install rdkit
Note
Alternative ways to install the required libraries
RDKit will be installed automatically from pypi.org if it is not already present in the Python environment, for instance, if you do not have conda available.
The following code snippet can be used to create your first program using HQS Spectrum Tools. For an expanded collection of examples please see the examples section.
from hqs_nmr.calculate import calculate_spectrum
from hqs_nmr.datatypes import NMRCalculationParameters
from hqs_nmr.visualization import plot_spectrum
from hqs_nmr_parameters.examples import molecules
from matplotlib import pyplot as plt
# Obtain example molecule of datatype NMRParameters.
molecule_parameters = molecules["C3H8"].spin_system()
# Define the calculation parameters. The only required field is the magnetic
# field in Tesla
calculation_parameters = NMRCalculationParameters(field_T=11.7433)
# Calculate the individual spin contributions of the spectrum.
nmr_result = calculate_spectrum(
molecule_parameters,
calculation_parameters
)
# Plot the spectrum.
plot_spectrum(nmr_result.spectrum)
plt.show()
Executing the code snippet using python should result in the following plot:
Features
- Frequency domain-based fast and precise NMR spectra calculation.
- Automatic implementation to compute accurate 1D NMR spectra with minimal truncation errors for large numbers of spins, featuring linear scaling with system size.
- NMR example parameters (chemical shifts and J-couplings) for molecules of different sizes, accessible via data classes containing structural information.
- Custom NMR parameters can be specified conveniently for NMR spectrum simulation.
- Easy construction of an NMR Spin Hamiltonian from provided or custom parameters using the
struqture-pypackage. - Extensive customization options of the NMR solver for expert users.
- Postprocessing methods to analyze simulated spectra and compare them to experiment.
You can get an overview of the HQS Spectrum Tools Python library in the components chapter or take a look at the API-documentation.
Getting started
The best way to learn about HQS Spectrum Tools is to go through our example notebooks. You can find information on how to download and run the examples in the local usage section of our HQStage documentation. Alternatively you can take a look at the executed notebooks in the hqstage-examples public github repository.
The following notebooks provide brief introductions to the software if you are new to it.
-
1_getting_started: Introduces the main routines of HQS Spectrum Tools. These are mostly automated and include all you need to simulate NMR spectra.
-
2_hqs_nmr_parameters_introduction: Introduces the
hqs-nmr-parametersdatabase and shows how to retrieve the spin Hamiltonian instruqtureformat for the available molecules.
Further examples
For a more detailed introduction into HQS Spectrum tools, the following advanced example notebooks are available:
-
3_customization: Explains customization options, e.g., to decrease runtime or improve resolution by discussing the solver strategies employed. This includes the clustering approach and the frequency-based implementation.
-
4_hqs_nmr_parameters_in_detail: A detailed overview of the functionalities of
hqs-nmr-parametersas well as the available datasets. -
5_plot_spectra_hqsnmr_exp: Demonstrates how to compare the calculated spectra to experimental results.
-
6_high_symmetry_molecules: Details the special case of a highly symmetric, strongly coupled molecule where the clustering approach fails and how to obtain the correct result.
Using struqture and the solver backend
In case you want to try out your own approximation schemes or customize the solver backend, check out the following examples:
-
7_spectrum_from_struqture_hamiltonian: Elaborates on how to interface with the solver backend given an NMR spin Hamiltonian in
struqtureformat and how to use the spin-dependent clustering approach. -
8_spin_lattice_models: Showcases how to extend the provided functionality to spin lattice models.
-
9_user_defined_solver: Allows to add a custom solver to the framework provided by HQS Spectrum Tools.
-
10_nmr_using_struqture_and_qiskit: Example on how to calculate an NMR spectrum on a quantum computer using
struqtureandqiskit.
Background
This chapter provides an overview of the fundamentals behind HQS Spectrum Tools
In section NMR, we describe the basics of nuclear magnetic resonance (NMR) from an experimental and theoretical perspective. We discuss, for example, the different relevant parameters entering an NMR calculation, such as the chemical shifts or couplings between nuclear spins, and introduce the relevant Hamiltonian of a molecule for NMR.
In Mathematical Background, we describe in greater detail the math behind calculation of spectra in HQS Spectrum Tools.
Finally, in the section Calculating NMR Spectra, we discuss approaches for evaluating NMR spectra of large molecules.
NMR
Nuclear magnetic resonance (NMR) spectroscopy is one of the most important analytical techniques in chemistry and related fields. It is widely used to identify molecules, but also to obtain information about their structure, dynamics, and chemical environment.
For a detailed overview to the field there are many suitable textbooks like High Resolution NMR Techniques in Organic Chemistry by T. Claridge or Understanding NMR Spectroscopy by J. Keeler. However, in the following we will only summarize some fundamental aspects of NMR which should be sufficient to get you started to use HQS Spectrum Tools.
NMR spectrometers place the sample into a strong, but constant magnetic field and use a weak electromagnetic pulse to perturb the nuclei. At or near resonance, when the oscillation frequency matches the intrinsic frequency of a nucleus, the system responds by producing an electromagnetic signal with a frequency characteristic of the magnetic field at the respective nucleus.
The NMR Hamiltonian used to simulate NMR spectra as well as the effects leading to the intrinsic frequency of a nucleus are discussed below, while the origin of the electromagnetic signal as well as a possible experimental setup are discussed in detail in section Measurement of a 1D NMR spectrum.
Zeeman interaction
For the simulation of NMR spectra for molecules, the most important part is the Zeeman effect describing the interaction of the nuclear spin with the external magnetic field . The corresponding Hamiltonian can be written as
where is the gyromagnetic ratio, the ratio of a system’s magnetic moment to its angular momentum, and is the total spin operator. Assuming that the strong (and constant) magnetic field is in -direction only, meaning , the Hamiltonian simplifies to
where is the so-called Larmor frequency, the angular frequency corresponding to the precession of the spin magnetization around the magnetic field at the position of the nucleus.
As an example, consider the simplest nucleus, 1H, consisting of only one proton, for which the gyromagnetic ratio is MHz T−1, meaning that a 500 MHz NMR spectrometer has a static magnetic field of about 11.7 Tesla. The energy of radiation of the Larmor frequency MHz ( J) is several orders of magnitude smaller than the average thermal energy of a molecule at a temperature of K ( J). Therefore, the occupations of the spin states are almost equal at room temperature, only a small surplus is responsible for the sample magnetization.
Chemical shift
Perhaps the most important aspect for NMR spectroscopy in chemistry is that the nuclei in molecules are shielded against the external magnetic field by the electrons surrounding them. This can be expressed by adding a correction term to the Hamiltonian as
where is referred to as the shielding tensor quantifying the change in the local magnetic field experienced by the nucleus in the molecule relative to a bare nucleus in vacuum. A significant simplification occurs if the molecules of interest are in solution, or in liquid phase in general, as they can rotate freely and only the isotropic shielding is of interest,
In practice, chemical shifts are normally used instead of chemical shieldings: instead of invoking the Larmor frequency of a nucleus in vacuum, shifts are defined with respect to the resonance frequency of a reference compound:
The standard reference for 1H NMR is the Larmor frequency of the protons in TMS [tetramethylsilane, Si(CH3)4]. Chemical shifts are normally reported on a scale of ppm (parts per million): most 1H chemical shifts are observed in the range between 0 and 10 ppm, and most 13C chemical shifts between 0 and 200 ppm. Since a constant shift of the form leaves the spectrum unchanged up to a scaling factor and the scale of chemical shieldings is so small in absolute terms , for practical intents and purposes the chemical shift can be substituted directly into the Hamiltonian:
Spin-spin coupling
Up to this point, the nuclear spins have been regarded to be isolated from each other. However, their magnetic moments have an effect on neighboring spins. The interaction of the nuclear spins can happen through two different mechanisms. The first one is the direct (or through space) spin-spin coupling, where the interaction strength depends on the distance of the two nuclei and the angle of their distance vector relative to the external field. As it comes from the direct interaction of two magnetic dipoles, it is also referred to as dipolar coupling. However, the effect is generally not observable in liquid phase since the free rotation of the molecules averages over all orientations and thus results in a vanishing average coupling.
An effect observable in the NMR spectrum is indirect spin-spin coupling, which is mediated by the electrons of a chemical bond. Due to the Pauli principle, the electrons of a covalent bond always have an anti-parallel spin orientation, and one electron will be closer to one nucleus than to the other, preferring an anti-parallel orientation with the nearby nucleus. Depending on the number of electrons involved in the transmission of the interaction, either a parallel or an anti-parallel orientation of two nuclei may result in a lower energy. Importantly, this interaction does not average out in solution since it mainly depends on the electron density at the position of the nucleus and not on the orientation of the distance vector relative to the field, which is why it is also referred to as scalar coupling. Since only s-orbitals have a finite electron density at the nucleus, the coupling depends on the electron density in those orbitals alone.
The interaction Hamiltonian in the case of homonuclear coupling is given by
where .
It should be noted that the -coupling tensor is a real matrix that depends on the molecular orientation, but in liquid phase only its isotropic part is observed due to motional averaging. Typical -coupling strengths between protons in 1H NMR amount to a few Hz.
NMR spin Hamiltonian for molecules in liquid phase
The spin Hamiltonian in a static magnetic field in frequency units (rad s−1) is given by
where the sum runs over all nuclear spins of interest.
There are several interactions that have not been taken into account here. As already mentioned, the direct dipolar spin-spin interaction vanishes in liquids due to motional averaging. Beyond dipolar coupling, such as quadrupolar interactions, for instance, are relevant only for nuclei with spin quantum number . Furthermore, interactions with unpaired electrons need a special treatment as well. While most organic compounds are diamagnetic (closed-shell), paramagnetic NMR also exists.
Mathematical Background
The Hamiltonian we are using for the NMR systems is of the form
where are the gyromagnetic factors and the chemical shifts of nuclear spin . denotes the coupling between spins and and where the are the usual spin operators with .
Note in the following, whenever we denote a spin operator as without an additional site index, we mean the sum over the individual spin operators for each nuclear spin .
In NMR spectroscopy, there is a strong magnetic field in the -direction, and electromagnetic pulses / oscillating fields are applied to flip the spins into the -plane. is typically of the order of 500 MHz, the bandwidth of the pulses is about 10 kHz, and the required resolution is less than 1 Hz.
The spectrum measured in an NMR experiment corresponds to the spectral function calculated in HQS Spectrum Tools.
Measurement of a 1D NMR spectrum
A 1D NMR spectrum can be obtained using a pulse-acquire experiment. To discuss how to calculate it, we will first elaborate on how the experiment is performed. For this, after a probe has been placed inside an NMR spectrometer, a strong magnetic field is applied along the -axis, which leads to a net magnetization along the same axis. Note that at room temperature this magnetization is typically quite small. One then applies a -pulse along the -axis which flips the magnetization to the minus -direction. The spins then start to precess in the -plane, which creates a signal that can be recorded by measuring the magnetic field along the - or -axis. The induced magnetic field is directly linked to the magnetization of the sample as
which in turn can be linked to the magnetic moment
where the integral goes over the volume of the sample and we are assuming a uniform magnetization within the sample to evaluate the integral. At the same time, the magnetic moment of an individual molecule is given as
Therefore, to calculate the spectrum, we simply have to calculate the time-dependent expectation value of the nuclear spin operators of each spin along the - or -direction and scale them with the corresponding gyromagnetic ratio. Note that in the actual implementation we perform the calculation using the dimensionless nuclear spin operator, so we explicitly calculate
The sum of these contributions is directly proportional to the measured magnetic field. To obtain the absolute measured magnetic field, an additional scaling would be required based on information on the sample volume and concentration of the molecule. However, this scaling is typically unnecessary as one is not interested in absolute values. So in HQS Spectrum Tools the spectrum is by default normalized to integrate to the number of relevant spins, i.e., to those of the same isotope type as the reference isotope.
Calculation of the spectrum
Time domain
Let us now discuss how to evaluate these expectation values. For this, we will only consider the field along the -direction. The time-dependent expectation value can be calculated using the density operator
Where denotes the trace. This implies that we need to find an expression for the time evolution of the density operator, which is described by the Liouville-von-Neumann equation
In this formulation the equation assumes a Hamiltonian in units of Joule, however the Hamiltonian as defined above is in units of radians per second, which allows us to drop the factor. Therefore the time evolution can be written as
which can easily be verified by reinserting this expression into the Liouville-von-Neumann equation. Note that is the Hamiltonian of the process taking place within the time interval . Therefore we have to perform two time evolutions: we start with the density operator at some time and perform a time evolution using the Hamiltonian associated with the pulse. Then we perform a second time evolution using the NMR Hamiltonian, describing the precession of the spins in the -plane.
Due to the setup of the experiment we can assume a hard (or instantaneous) pulse, meaning we will not be interested in the explicit time evolution during the pulse and can therefore write the time evolution operator of the pulse as
This leads to the following density operator after the pulse
where is the time duration of the pulse. Performing the second time evolution under the NMR Hamiltonian we arrive at
We now just have to define the density operator at time . Due to the experimental setup it is simply given via the Boltzmann distribution
with the inverse temperature , where is the Boltzmann constant and the temperature, as well as the partition function . Note that the factor is necessary as the Hamiltonian is given in units of radians per second.
While HQS Spectrum Tools provides a solver that evaluates a spectrum using the exact expression of the density operator, one should note that for typical NMR experiments it is sufficient to approximate it using a series expansion of the exponential
In the second approximation applied here, the second term is replaced by the magnetic field term of the Hamiltonian. This is valid as the contribution from the J-coupling term to the distribution is insignificant in comparison. Furthermore, the first term with the identity can be dropped in practical calculations as it does not contribute to the overall expectation value.
Note that in the NMR literature this step is often skipped and sometimes even is called a density operator. This is technically incorrect as is not positive semi-definite, a key requirement for a density operator. Furthermore, the prefactor is often omitted, as it only serves as normalization.
However, with this approximation one can now further simplify the calculation using the identity
which in our case of a -pulse reduces to
This leads us to the final expression for the expectation value
Where, as mentioned above, in the last step we dropped the term arising from the identity in the parentheses, since it does not contribute to the overall expectation value.
Frequency domain
Up to now we have only discussed how to calculate the time signal, however what we are actually interested in is the spectrum given by its Fourier transform. Most implementations therefore first calculate a time evolution according to the derivation performed above and then do a Fourier transform of the result. However, as will be explained in the next section, it is much more efficient to do the Fourier transform analytically first and then perform all calculations in the frequency domain. To be able to do this, we have to rewrite the above formulation into a so called Lehmann representation
where and are the eigenvectors of the NMR Hamiltonian constructed in a many-body spin basis with the respective eigenvalues and . These can be obtained from numerical diagonalization using standard dense linear algebra routines. By introducing a small convergence aiding factor we can now do the Fourier transform
This represents the final form we are evaluating, although for the spectrum we are measuring in NMR, typically just the real part is required. However for some postprocessing routines also the imaginary part is required, in which case we will refer to this as a Green’s function.
Additionally, note that the factor leads to a broadening of the peaks, giving them generally the shape of a Lorentzian. This broadening has to be chosen to fit the expected resolution, as discussed in the next section.
Energy rediscretization
One problem that arises when simulating NMR spectra is that the peaks are usually very sharp, which means that resolving them numerically poses a challenge in itself. Therefore, performing calculations in time domain typically requires a lot of time steps that have to be evaluated. Also in the frequency domain, discretizing the frequency axis with a very fine grid would be computationally ineffective. Therefore, we perform several rounds of computing the spectral function and use the result of each round to rediscretize the frequency grid for the next. We first calculate the spectral function with a linear grid in frequency space and a rather large artificial broadening . Based on this result we rediscretize the frequency space in equal weight partitions of the total spectral function. This gives us a new non-linear frequency grid with grid points accumulated around the spectral function peaks. We then repeat this process with a smaller broadening , which allows us to resolve sharper peaks. This way, we can iteratively reach the desired resolution.
Calculating NMR Spectra
While using the Lehmann representation for the expectation value
is useful to resolve sharp features in an NMR spectrum, it does not address the problem of the exponential scaling of the Hamiltonian dimension. Evaluating this expression in a brute force approach would imply diagonalizing a Hamiltonian of dimension where is the number of spins in the molecule. This would restrict one to around 10 spins on a modern laptop. Therefore, different strategies need to be employed to evaluate NMR spectra for larger molecules.
Symmetry
First one can use the fact that the NMR Hamiltonian always conserves the quantum number, which means that we can identify a block diagonal structure in the Hamiltonian, where each block can be diagonalized individually. While this is always possible for the standard NMR Hamiltonian, it only restricts the dimension of the largest block to which still grows rather quickly.
For some molecules one can also identify magnetically equivalent groups. Such a symmetry group is defined as a group of spins, where each individual spin has the same chemical shift and couples in the exact same way to the rest of the system. Identifying these groups is advantageous, as one can combine them into higher order spin representations, which allows to exploit the local symmetry of these groups. As an example consider a propane molecule, which has eight hydrogen atoms. By identifying all symmetrically coupled groups in this molecule, the number of spins can be reduced to two. One being the CH2 group which has a combined spin representation of one and the second being the two methyl groups each representing a spin 3/2 and adding up to a total spin representation of spin 3. As the corresponding operators of these two combined spins commute with the Hamiltonian, we can construct a basis in which the Hamiltonian has again a block diagonal structure, however the individual blocks are even smaller, as when exploiting just the conservation.
While these symmetry considerations are exact and can lead to a reasonable reduction in computational effort, they eventually break down when going to larger and larger molecules. Therefore, also approximate methods have to be used.
Clustering methods
The main approximation method used in HQS Spectrum Tools is based on the observation that we do not just evaluate the full sum over spin contributions to the spectrum at once, but rather determine the contribution of each individual spin separately. This allows us to identify an effective Hamiltonian for each spin and evaluate the spectral function for it independently from the other spin contributions. It turns out that a good approximation for the Hamiltonian for a specific spin contribution is typically given by simply identifying the cluster of spins most strongly coupled to the spin of interest. A good measure for this coupling is the following weight matrix, motivated from perturbation theory
Here, are the entries in the J-coupling matrix connecting spins with index and , the gyromagnetic ratios, the chemical shift values, and the magnetic field strength in -direction.
Especially at high field, this method is extremely accurate allowing to choose cluster sizes around 8–12 spins for basically all molecules.
Database
In this section we will give an overview of the hqs-nmr-parameters database. In molecular data, we will introduce the molecular data structure, which is used as input in HQS Spectrum Tools and involves:
- Chemical name and molecular formula
- Chemical structures
- Conditions of the experiment/simulation
- NMR parameters
Next, it is shown how to extract the relevant spin Hamiltonian from this input.
Then, the various datasets available within hqs-nmr-parameters are discussed.
Finally, we will demonstrate how to provide external data as text input to use the functionalities of HQS Spectrum Tools for your own molecules, where we are using custom Pydantic data classes to ensure consistency.
Representation in Python of molecular data structures
Molecular data
Inside the hqs-nmr-parameters package, the MolecularData class, which is derived from Pydantic’s BaseModel, has been implemented to
describe a molecule. It contains the following attributes:
name: The name of the molecule (just a simple string).isotopes: List containing pairs of an atom index and the associated isotope. Atom indices are associated with the order in which atoms appear in the chemical structure representations, starting by index 0.shifts: List containing pairs of an atom index and the associated chemical shift in ppm.j_couplings: List containing pairs of atomic indices and the associated J-coupling values in Hz. Note that atom index pairs are unique: if a value is provided for an atom pair (k, l), then no value is provided for pair (l, k).structures: Contains a dictionary with chemical structure representations. It accepts entries for a SMILES string (“SMILES”), a Molfile (“Molfile”), and an XYZ file (“XYZ”). Each value will correspond to aChemicalStructureobject. See below for a full explanation of the chemical structure representations and the atom numeration.formula: The molecular formula of the corresponding molecule.temperature: An optional temperature in K for which the parameters have been calculated or at which the experiment has been performed.solvent: Name of the solvent. An empty string represents an unknown or undefined solvent, or the absence of a solvent.description: Optional further information.method_json: Stores a JSON serialization of computational method settings. An empty string indicates that the field is not applicable. Creating and interpreting the content is the responsibility of the user of the model.
This is best illustrated by an example from one of our available datasets:
# Import a MolecularDataSet
from hqs_nmr_parameters.examples import molecules
# Choose ethanol as an example from the available molecules
parameters = molecules["C2H5OH"]
print(type(parameters))
print("Chemical name:", parameters.name)
print("Molecular Formula:", parameters.formula)
print("Type(s) of representations:", parameters.structures.keys())
print("Information about solvent:", parameters.solvent)
print("Shifts:", parameters.shifts)
print("###")
print(parameters.description)
<class 'hqs_nmr_parameters.code.data_classes.MolecularData'>
Chemical name: Ethanol
Molecular Formula: C2H6O
Type(s) of representations: dict_keys(['SMILES'])
Information about solvent: chloroform
Shifts: [(3, 1.25), (4, 1.25), (5, 1.25), (6, 3.72), (7, 3.72), (8, 1.32)]
###
1H parameters for ethanol in CDCl3.
Shifts from: https://doi.org/10.1021/om100106e
J-couplings estimated.
In this case, we are getting information for the ethanol molecule, for which we have stored a SMILES representation (check below for details) and according to the description experimental 1H NMR parameters in chloroform.
Note that the number of H atoms in a molecule is not necessarily the same as the number of spins in its 1H NMR spectrum simulation. For instance, H atoms attached to electronegative elements such as N, O, or S tend to exchange rapidly with the deuterium atoms in protic solvents such as deuterated water (D2O), which results in an extreme line broadening in the NMR spectrum or even makes the signal disappear entirely. That means, if the ethanol entry shown above was recorded in water instead of chloroform, the chemical shift value for the hydroxyl proton (OH, index 8) would probably be missing.
NMR parameters subset
The easiest way to check the NMR data is getting only the information necessary to perform an NMR calculation, i.e., a subset of the parameters object containing only the attributes isotopes, shifts, and j_couplings. Those can be obtained using the spin_system method of MolecularData, which returns an object of type NMRParameters:
from pprint import pprint
from hqs_nmr_parameters.examples import molecules
parameters = molecules["C2H5OH"]
nmr_parameters = parameters.spin_system()
print(type(nmr_parameters))
pprint(nmr_parameters.model_dump())
<class 'hqs_nmr_parameters.code.data_classes.NMRParameters'>
{'isotopes': [(1, 'H'), (1, 'H'), (1, 'H'), (1, 'H'), (1, 'H'), (1, 'H')],
'j_couplings': [((0, 3), 7.0),
((0, 4), 7.0),
((1, 3), 7.0),
((1, 4), 7.0),
((2, 3), 7.0),
((2, 4), 7.0)],
'shifts': [1.25, 1.25, 1.25, 3.72, 3.72, 1.32]}
Here, nmr_parameters is an instance of the Pydantic NMRParameters class, where the atomic indices always start from 0.
The total number of spins, determined from the number of chemical shifts, can be accessed with the nspins property. The NMRParameters class can be used as input for several functions to calculate an NMR spectrum, which is explained here in detail. The following code block shows how to perform a 1H NMR simulation (default case), providing the molecular data either as NMRParameters or directly as MolecularData object:
from hqs_nmr_parameters.examples import molecules
from hqs_nmr import calculate_spectrum, NMRCalculationParameters
parameters = molecules["C2H5OH"]
nmr_parameters = parameters.spin_system()
print(type(nmr_parameters))
print(nmr_parameters.nspins)
calculation_parameters = NMRCalculationParameters(field_T=9.395)
# Using parameters instead of nmr_parameters would also be valid
spectrum = calculate_spectrum(nmr_parameters, calculation_parameters)
print(type(spectrum))
<class 'hqs_nmr_parameters.code.data_classes.NMRParameters'>
6
<class 'hqs_nmr.datatypes.NMRResultSpectrum1D'>
For information on the output class NMRResultSpectrum1D have a look here.
Isotopes/atoms selection
MolecularData objects can contain data for several isotopes. However, we are often interested in creating a spin system for a given nucleus only. For example, if we want to simulate a 1H NMR spectrum but we also have 13C NMR parameters in our data, it is possible either to select only 1H using keep_nuclei(isotopes=["1H"]) or to drop 13C using drop_nuclei(isotopes=["13C"]). Let us take a look at this in more detail:
from pprint import pprint
from hqs_nmr_parameters.examples import molecules
parameters = molecules["CH3Cl_13C"]
pprint(parameters.isotopes)
drop_C = parameters.drop_nuclei(isotopes=["13C"])
keep_H = parameters.keep_nuclei(isotopes=["1H"])
print(drop_C == keep_H)
nmr_parameters_1H = drop_C.spin_system()
pprint(nmr_parameters_1H.model_dump())
[(0, Isotope(mass_number=13, symbol='C')),
(2, Isotope(mass_number=1, symbol='H')),
(3, Isotope(mass_number=1, symbol='H')),
(4, Isotope(mass_number=1, symbol='H'))]
True
{'isotopes': [(1, 'H'), (1, 'H'), (1, 'H')],
'j_couplings': [((0, 1), -10.8), ((0, 2), -10.8), ((1, 2), -10.8)],
'shifts': [3.05, 3.05, 3.05]}
We should take into account that the dropping/keeping of isotopes needs to be done carefully. The 13C nucleus has a nuclear spin of 1/2 (like 1H) and is NMR-activate but has a very low natural abundance, and the 13C–1H coupling pattern is only barely visible in 1H NMR spectra. Therefore, it is common to simulate only 13 C-decoupled 1H NMR spectra. However, the coupling of 1H with other isotopes such as 19F or 31P should not be ignored in order to get the correct peak pattern.
keep_nuclei can also take an atoms argument, which accepts a list of atom indices for which the NMR parameters are to be kept.
In the case of drop_nuclei, specified nuclei can be dropped via atoms, but a keep_atoms argument prevents certain atoms from being dropped (useful in combination with isotopes).
Note that many datasets provided by hqs-nmr-parameters already only contain the data that is needed to simulate 1H NMR spectra. More information on each dataset can be found here.
Molecular structure representations
As shown above a MolecularData object has the attribute structures, which is a dictionary that can accept three entries
(keys): “SMILES”, “Molfile” and “XYZ”. The value of each of these entries is a ChemicalStructure object that stores chemical structure representations of the type defined in the key:
- SMILES (Simplified Molecular-Input Line-Entry System): Strings representing the connectivity of all non-hydrogen atoms in a molecule. They become laborious to understand for complex molecules.
- Molfiles: Text files with a two-dimensional (2D) structure of a molecule (skeletal representation). It may omit hydrogen atoms. The number of hydrogen atoms is inferred from the atomic valencies of the heavy atoms.
- XYZ files: Text files containing an integer with the total number of atoms in the first line, followed by a comment line, and then the chemical element symbols and three-dimensional atomic positions for all the atoms explicitly.
Connectivity and charge information can be extracted from SMILES strings or Molfiles, but not from XYZ files. More information about Molfiles and SMILES can be found here.
The attributes of the ChemicalStructure class are:
representation: Type of chemical structure representation present in thecontentattribute. It can be “SMILES”, “Molfile” or “XYZ”.content: The raw structure representation (as a string), i.e., a SMILES string or the content of an XYZ file or a Molfile.charge: Total net electric charge of the molecule.symbols: List of chemical element symbols for the full set of atoms.atom_map: As we have seen, 2D representations can omit hydrogen atoms, this attribute contains information about how the full representation would map into this reduced representation. It is a list of the length of the molecule (full set of atoms), where the atom counting starts from zero and takes into account:- Each non-hydrogen atom maps onto the respective index in the reduced representation.
- Each explicit hydrogen maps onto the index of its explicitly represented counterpart.
- Each implicit hydrogen maps onto the reduced index of the respective backbone atom.
In the case of ethanol, a full structures representation may look like the following. It might look a bit crowded as the content attribute contains the full content of the corresponding files (or SMILES string):
from hqs_nmr_parameters import ChemicalStructure
ethanol_representation = {
"XYZ": ChemicalStructure(representation="XYZ", content="9\n\nC -0.88708900 0.17506400 -0.01253500\nC 0.46048900 -0.51551600 -0.04653500\nO 1.44296500 0.30726700 0.56557200\nH -0.84747800 1.12776800 -0.55081700\nH -1.65878200 -0.45332700 -0.46584200\nH -1.17694400 0.40367600 1.01830600\nH 0.76871200 -0.72432800 -1.07546000\nH 0.41948600 -1.46207300 0.50017700\nH 1.47864000 1.14146800 0.06713500\n", charge=0, symbols=["C", "C", "O", "H", "H", "H", "H", "H", "H"], atom_map=[0, 1, 2, 3, 4, 5, 6, 7, 8]),
"Molfile": ChemicalStructure(representation="Molfile", content="\n RDKit 2D\n\n 0 0 0 0 0 0 0 0 0 0999 V3000\nM V30 BEGIN CTAB\nM V30 COUNTS 3 2 0 0 0\nM V30 BEGIN ATOM\nM V30 1 C -1.299038 -0.250000 0.000000 0\nM V30 2 C 0.000000 0.500000 0.000000 0\nM V30 3 O 1.299038 -0.250000 0.000000 0\nM V30 END ATOM\nM V30 BEGIN BOND\nM V30 1 1 1 2 CFG=3\nM V30 2 1 2 3 CFG=3\nM V30 END BOND\nM V30 END CTAB\nM END\n", charge=0, symbols=["C", "C", "O", "H", "H", "H", "H", "H", "H"], atom_map=[0, 1, 2, 0, 0, 0, 1, 1, 2]),
"SMILES": ChemicalStructure(representation="SMILES", content="CCO", charge=0, symbols=["C", "C", "O", "H", "H", "H", "H", "H", "H"], atom_map=[0, 1, 2, 0, 0, 0, 1, 1, 2])
}
Each of the entries corresponds to a ChemicalStructure object:
from pprint import pprint
smiles_representation = ethanol_representation["SMILES"]
print(type(smiles_representation))
pprint(smiles_representation.model_dump())
<class 'hqs_nmr_parameters.code.data_classes.ChemicalStructure'>
{'atom_map': [0, 1, 2, 0, 0, 0, 1, 1, 2],
'charge': 0,
'content': 'CCO',
'representation': 'SMILES',
'symbols': ['C', 'C', 'O', 'H', 'H', 'H', 'H', 'H', 'H']}
ethanol_representation["Molfile"].atom_map and ethanol_representation["SMILES"].atom_map will return:
[0, 1, 2, 0, 0, 0, 1, 1, 2]
In both cases, only the carbon and the oxygen atoms have been indicated explicitly. Therefore, the first three atoms are listed expressly (carbon atoms 0 and 1, and oxygen atom 2, i.e., the atom counting starts from zero) and the six following hydrogens are assigned to one of those backbone atoms. The full atomic indices associated with each reduced index can be obtained using the inverted_map method:
from pprint import pprint
smiles_representation = ethanol_representation["SMILES"]
print(smiles_representation.atom_map)
print(smiles_representation.inverted_map)
[0, 1, 2, 0, 0, 0, 1, 1, 2]
[{0, 3, 4, 5}, {1, 6, 7}, {8, 2}]
Hence, C (index = 0) is connected to H atoms 3, 4, 5, while C (index = 1) is connected to H atoms 6, 7, and the O (index = 2), to H 8.
However, ethanol_representation["XYZ"].atom_map will return:
[0, 1, 2, 3, 4, 5, 6, 7, 8]
Since all the atoms are explicitly provided in an XYZ format.
The ChemicalStructure class can also yield the chemical formula in Hill notation using the formula method. smiles_representation.formula will return:
'C2H6O'
Serialization (saving in a JSON file) of a MolecularData object and deserialization
We can save a MolecularData instance in a JSON file for future uses. To do so, employ the write_file method of the
MolecularData class:
from hqs_nmr_parameters.examples import molecules
parameters = molecules["C2H5OH"]
parameters.write_file("etoh_molecular_data.json")
This JSON file can easily be loaded and validated against the MolecularData model using the read_file method:
from hqs_nmr_parameters import MolecularData
loaded_parameters = MolecularData.read_file("etoh_molecular_data.json")
print(type(loaded_parameters))
<class 'hqs_nmr_parameters.code.data_classes.MolecularData'>
Spin Hamiltonian
With the molecular data or just the NMR parameters mentioned in the previous section, it is possible to set up the spin Hamiltonian of the spin system relevant for an NMR simulation. This can be achieved by calling the hqs_nmr_parameters.nmr_hamiltonian function that returns the spin Hamiltonian defined in a struqture format. It contains the terms normally needed to represent the spin Hamiltonian of a closed-shell organic molecule in solution:
- Zeeman interaction of nuclei with the static magnetic field
- isotropic chemical shifts
- indirect spin-spin scalar couplings
Note that the units of the spin Hamiltonian conform to the conventions in the field of NMR. In consequence, the Hamiltonian is in angular frequency units of rad s−1. To convert to Hz, the Hamiltonian needs to be divided by 2π. To convert to energy units (Joule), the Hamiltonian needs to be multiplied with the reduced Planck constant ℏ.
The parameters argument in nmr_hamiltonian can accept either a MolecularData or an NMRParameters object. For example, an NMR spin Hamiltonian with the example parameters for nitrobenzene at a field of 2.5 T can be set up as shown below (for information on how to retrieve the molecular data from a dataset, see the next section):
from hqs_nmr_parameters import examples, nmr_hamiltonian
parameters = examples.molecules["C6H5NO2"]
hamiltonian = nmr_hamiltonian(parameters=parameters, field=2.5)
Furthermore, the following optional arguments can be set:
reference_isotope: Used to set up a relative spin Hamiltonian for use within the rotating frame approximation. The Larmor frequency of the reference isotope is subtracted from the Zeeman terms of all nuclei. The default value isNone, i.e., the full Hamiltonian will be set up instead.reference_shift: Shift in ppm for the reference frequency in the rotating frame approximation. It can help to evaluate the Hamiltonian in the correct range of frequencies. Default: 0.0.gyromagnetic_ratios: Gyromagnetic ratios in rad s−1 T−1 of relevant isotopes. Default: pre-saved values.
As mentioned above, the reference_isotope option should be set to construct the spin Hamiltonian in the rotating frame approximation. For example, using the Larmor frequency of 1H with a shift of 0 ppm as the reference frequency (default), a spin Hamiltonian is obtained via:
from hqs_nmr_parameters import examples, nmr_hamiltonian
parameters = examples.molecules["C6H5NO2"]
hamiltonian_rf = nmr_hamiltonian(
parameters=parameters,
field=2.5,
reference_isotope=(1, "H"),
)
As discussed in the Molecular data section, it is possible to drop terms related to, e.g., 13C isotopes. Here this is not necessary, as the examples.molecules dataset used here only contains 1H NMR parameters for the selected molecule C6H5NO2.
For a more detailed introduction on how to set up and work with a spin Hamiltonian, please have a look at the example notebooks.
Datasets
This chapter gives an introduction to all datasets provided by hqs-nmr-parameters and the data structure that is used to retrieve them.
Data types
MolecularDataSet
Molecular data for a group of molecules can be collected using the MolecularDataSet Pydantic class, which contains instances of type MolecularData. It is composed of two attributes:
description: Summary of the content of the dataset.dataset: Dictionary where the keys are identifiers for the molecules (e.g., molecule names) and the values correspond to aMolecularDataobject per molecule.
A list with the keys of the dataset can be obtained directly from the MolecularDataSet.keys property. This allows us to conveniently access the molecular data of each molecule using its key as string.
In oder to have a brief summary of the molecules belonging to a dataset, we can use the get_names method. It retrieves a dictionary where the keys correspond to the keys of the dataset and the values are the chemical names. An equivalent dictionary providing the chemical formulas can be accessed via the get_formulas method.
Similar to keep_nuclei/drop_nuclei in MolecularData, keep_isotopes, drop_isotopes keeps/drops selected isotopes for all molecules in a dataset. An extra description can be added with updated information about the set (whitespace for separation from the original description content must be included).
As in MolecularData, it is possible to save or load datasets thanks to the read_file and write_file methods that
(de)serialize JSON files.
MolecularDataTable
It is possible to store multiple molecular datasets in one MolecularDataTable object. This is particularly useful if the datasets contain the same or partially the same molecular structures, but different NMR parameters, e.g., from theoretical calculations using different DFT approaches. A MolecularDataTable object has two attributes:
description: Summary of the content of the data table.content: A dictionary with keys being row labels (e.g., molecular identifiers as in aMolecularDataSet) and the values being objects of typeMolecularDataTableRowthat store different NMR parameters for a single molecular structure.
The most important properties of this class are row_labels and column_labels providing the respective labels, where the rows correspond to molecular structures and the columns to a specific data source. With the get_column and get_dataset methods, a dictionary or a MolecularDataSet can be obtained from a table column by specifying its label. Other functions that are known from the MolecularDataSet also exist for the MolecularDataTable. These are, for instance, get_names, get_formulas, read_file, and write_file.
General remarks
The hqs-nmr-parameters package contains several datasets organized in modules with data from different origins and for different purposes. In general, the MolecularDataSet object of a specific set can be imported with:
from hqs_nmr_parameters.<module> import <dataset>
With the exception of assignments, all modules have a dataset called molecules, which is an alias for the dataset that is presumably the most interesting one for the majority of users and is therefore recommended for general use.
To import the different modules in a single MolecularDataSet object, one can do:
from hqs_nmr_parameters import molecules
which is equivalent to:
from hqs_nmr_parameters.merged import molecules
Including the one imported above, there are four datasets that integrate the data available in hqs-nmr-parameters in some way:
molecules: Includes all available molecules with any data, using experimental shifts if possible.calculated: Includes all available molecules that contain purely calculated parameters (shifts and J-couplings).combined: Includes all available molecules for which a combination of experimental shifts and calculated J-couplings is available.free_trial: Includes data available in the free trial version of HQSpectrum (calculated data from CHESHIRE and examples).
The datasets used here are described in more detail below along with the most important methods to access their contents. These methods (and properties) are accessible for each dataset, including the merged ones presented here.
Examples module
The first dataset that is worth mentioning is the examples module which has a set of molecule definitions
encapsulated in the MolecularDataSet object molecules. This set can be accessed via:
from pprint import pprint
from hqs_nmr_parameters.examples import molecules
print(type(molecules)) # MolecularDataSet
# Keys of the dataset:
print(molecules.keys)
<class 'hqs_nmr_parameters.code.data_classes.MolecularDataSet'>
['CH3Cl', 'limonene_DFT', '1,2,4-trichlorobenzene', 'Anethole', 'Artemisinin_exp', 'endo-dicyclopentadiene_DFT', 'CH3Cl_13C', 'C2H3CN', 'Artemisinin', 'camphor_DFT', 'C6H6', 'C10H8', 'Triphenylphosphine_oxide', 'H2CCF2', 'C2H5Cl', 'Androstenedione', 'Cinnamaldehyde', 'CHCl3_13C', 'C6H5NO2', 'C2H5OH', 'C2H6', 'C10H7Br', 'CHCl3', 'camphor_exp', 'exo-dicyclopentadiene_DFT', '1,2-di-tert-butyl-diphosphane', 'C2H3NC', 'cyclopentadiene_DFT', 'cis-3-chloroacrylic_acid_exp', 'C3H8']
Note that the content of this list of keys is just an example and might appear in a different order or with different entries depending on the installed version of hqs-nmr-parameters. The same holds for the dictionary of molecule names which can be obtained as follows:
pprint(molecules.get_names())
{'1,2,4-trichlorobenzene': '1,2,4-Trichlorobenzene',
'1,2-di-tert-butyl-diphosphane': 'tert-Butyl(tert-butylphosphanyl)phosphane',
'Androstenedione': 'Androstenedione',
'Anethole': 'Anethole',
'Artemisinin': 'Artemisinin',
'Artemisinin_exp': 'Artemisinin',
'C10H7Br': '2-Bromonaphthalene',
'C10H8': 'Naphthalene',
'C2H3CN': 'Acrylonitrile',
'C2H3NC': 'Vinyl isocyanide',
'C2H5Cl': 'Chloroethane',
'C2H5OH': 'Ethanol',
'C2H6': 'Ethane',
'C3H8': 'Propane',
'C6H5NO2': 'Nitrobenzene',
'C6H6': 'Benzene',
'CH3Cl': 'Chloromethane',
'CH3Cl_13C': 'Chloromethane',
'CHCl3': 'Chloroform',
'CHCl3_13C': 'Chloroform',
'Cinnamaldehyde': 'Cinnamaldehyde',
'H2CCF2': '1,1-Difluoroethene',
'Triphenylphosphine_oxide': 'Triphenylphosphine oxide',
'camphor_DFT': 'Camphor',
'camphor_exp': 'Camphor',
'cis-3-chloroacrylic_acid_exp': 'cis-3-Chloroacrylic acid',
'cyclopentadiene_DFT': 'Cyclopentadiene',
'endo-dicyclopentadiene_DFT': 'endo-Dicyclopentadiene',
'exo-dicyclopentadiene_DFT': 'exo-Dicyclopentadiene',
'limonene_DFT': 'Limonene'}
In addition, if we want to have a feeling of the size of the molecules in the set, we can print their formulas using the
get_formulas method.
The full molecular definition for a given molecule can be loaded using its string key. Each entry of this dataset includes a 2D representation (Molfile or SMILES string) of the molecule. Let us consider an example:
from pprint import pprint
from hqs_nmr_parameters.examples import molecules
# Obtain the MolecularData object for acrylonitrile
parameters = molecules["C2H3CN"]
# Print parameters
pprint(parameters.model_dump())
{'description': '1H parameters for acrylonitrile.\n'
"Values were obtained from Hans Reich's Collection, NMR "
'Spectroscopy.\n'
'https://organicchemistrydata.org\n',
'formula': 'C3H3N',
'isotopes': [(3, (1, 'H')), (4, (1, 'H')), (5, (1, 'H'))],
'j_couplings': [((3, 4), 0.9), ((3, 5), 11.8), ((4, 5), 17.9)],
'method_json': '',
'name': 'Acrylonitrile',
'shifts': [(3, 5.79), (4, 5.97), (5, 5.48)],
'solvent': '',
'structures': {'Molfile': {'atom_map': [0, 1, 2, 3, 4, 5, 6],
'charge': 0,
'content': '\n'
'JME 2022-02-26 Wed Sep 07 15:54:28 '
'GMT+200 2022\n'
'\n'
' 0 0 0 0 0 0 0 0 0 0999 '
'V3000\n'
'M V30 BEGIN CTAB\n'
'M V30 COUNTS 7 6 0 0 0\n'
'M V30 BEGIN ATOM\n'
'M V30 1 C 2.4249 2.1000 0.0000 0\n'
'M V30 2 C 3.6373 1.4000 0.0000 0\n'
'M V30 3 C 1.2124 1.4000 0.0000 0\n'
'M V30 4 H 0.0000 2.1000 0.0000 0\n'
'M V30 5 H 1.2124 0.0000 0.0000 0\n'
'M V30 6 H 2.4249 3.5000 0.0000 0\n'
'M V30 7 N 4.8497 0.7000 0.0000 0\n'
'M V30 END ATOM\n'
'M V30 BEGIN BOND\n'
'M V30 1 1 1 2\n'
'M V30 2 2 1 3\n'
'M V30 3 1 3 4\n'
'M V30 4 1 3 5\n'
'M V30 5 1 1 6\n'
'M V30 6 3 2 7\n'
'M V30 END BOND\n'
'M V30 END CTAB\n'
'M END\n',
'representation': 'Molfile',
'symbols': ['C', 'C', 'C', 'H', 'H', 'H', 'N']}},
'temperature': None}
In this MolecularData object, data for setting up a 1H NMR spectrum of the acrylonitrile molecule has been stored together with its Molfile.
To set up an NMR calculation, only a part of the previous data is needed. To retrieve only the essential values, the spin_system method can be used as follows (for more information, see the section on the NMRParameters class):
nmr_parameters = parameters.spin_system()
pprint(nmr_parameters.model_dump())
{'isotopes': [(1, 'H'), (1, 'H'), (1, 'H')],
'j_couplings': [((0, 1), 0.9), ((0, 2), 11.8), ((1, 2), 17.9)],
'shifts': [5.79, 5.97, 5.48]}
CHESHIRE module
In the cheshire module, one can find molecular data for molecules belonging to the CHESHIRE database. Five datasets (MolecularDataSet objects) have been created from this database depending on the collected NMR data:
experimental_shifts_only: It includes the experimental shifts (for 13C and 1H) of all 105 molecules, but no J-coupling values.calculated_full: It has theoretical NMR data for all molecules. Details of the calculations can be found under thedescriptionattribute of each item (see below).combined_full: It contains experimental shifts and theoretical J-couplings for all molecules in the set.- The
calculatedandcombineddatasets are the reduced versions of the aforementioned sets and contain only the NMR data required for simulating 1H NMR spectra.
In addition, for non-expert users, we have included the alias molecules, which returns the combined set, i.e., 1H NMR data with experimental shifts and calculated J-couplings.
These datasets can be imported as follows (we will focus on the molecules set that will be imported as
cheshire_molecules to avoid confusion with the examples module):
from hqs_nmr_parameters.cheshire import molecules as cheshire_molecules
As a brief illustration of the type of data included in the sets, the most important information can be retrieved from the dataset’s description attribute:
print(cheshire_molecules.description)
The keys of the molecules give access to the molecular data. Since chemical structure names are not practical for a large amount of compounds, the keys correspond to string representations of numbers from 1 to 106 (a list of all keys can be accessed with the cheshire_molecules.keys property, 63 is missing as it contains duplicate data). Note that some molecule entries may be missing in CHESHIRE datasets. For information on that, please refer to the dataset’s description.
As before, the molecule names can be obtained using the get_names function. But here, we will focus on a single entry:
print(cheshire_molecules["1"].name)
'Dichloromethane'
In the description of each entry we find important information about how the NMR parameters were obtained:
print(cheshire_molecules["1"].description)
Each entry in the dataset includes three structure representations of the molecule: a SMILES string, a two-dimensional Molfile, and a three-dimensional XYZ representation.
print(cheshire_molecules["1"].structures.keys())
dict_keys(['XYZ', 'Molfile', 'SMILES'])
To access only the NMR data, use the spin_system method:
pprint(cheshire_molecules["1"].spin_system().model_dump())
{'isotopes': [(1, 'H'), (1, 'H')],
'j_couplings': [((0, 1), -5.171)],
'shifts': [5.28, 5.28]}
Benchmark
The CHESHIRE module contains a submodule called benchmark with additional datasets, which can be imported as from hqs_nmr_parameters.cheshire.benchmark import .... These contain the results of different computational predictions of NMR parameters for all or parts of the molecules in the CHESHIRE set. Since the content of this folder may grow and/or change, we refer to the respective dataset and molecule descriptions. Currently, the following additional datasets are available:
experimental_shifts_only: Same dataset as in thecheshiremodule directly.calculated_full: Calculated parameters. Geometries optimized at B97-3c, chemical shifts at PBE0/pcSseg-2, and J-coupling constants at PBE/pcJ-2 level of theory. Same dataset as in thecheshiremodule directly.calculated: Reduced version ofcalculated_fullwith only the NMR data required for simulating 1H NMR spectra.predicted: 1H chemical shifts and J-coupling constants obtained from HQS’s empirical prediction method (method still under development).
Furthermore, the benchmark_1h molecular data table can be imported and combines the datasets experimental_shifts_only (without 13C NMR data), calculated, and predicted.
GISSMO module
Similar to CHESHIRE, the GISSMO module contains molecules from an external database called GISSMO, which contains experimental 1H NMR chemical shifts and J-coupling constants for 1H–1H, 1H–19F, and 1H–31P couplings of more than 1200 organic molecules.
As there is no chemical shift data for any other isotopes than 1H, it is possible to import the following slightly different datasets:
experimental_dummy_hetero_shifts: Contains all available experimental shift and J-coupling data. The chemical shift values of 19F and 31P are set to dummy values of 0.0 ppm.experimental_no_hetero_nuclei: Contains only the available 1H NMR shifts and 1H–1H J-coupling constants.
Here, the molecules dataset is an alias for experimental_dummy_hetero_shifts. It can be imported as follows.
from hqs_nmr_parameters.gissmo import molecules as gissmo_molecules
The molecule keys in the GISSMO dataset are either of the form bmseXXXXXX or Maybridge_XX_YXX (where X are numbers and Y are letters). When searching for a specific molecule entry, we recommend accessing the names in the dictionary returned by gissmo_molecules.get_names() or taking a look at the online library, where the same tags are used. For entries with a bmseXXXXXX key, experimental NMR spectra are available at the BMRB website.
Like the other datasets, the description of the GISSMO dataset and the individual molecule descriptions provide the most important information on the stored data. Additional notes in the descriptions point to special features of the molecular data, e.g., the presence of 1H–19F J-coupling constant data in the following example entry (p-fluorobenzoic acid). In the output of this code snippet, there will be a 19F isotope in the isotopes list, which has a shift value of 0.0 ppm but reasonable J-coupling values to other 1H atoms. The molecule description contains a note on the included values.
example_gissmo = gissmo_molecules["bmse000739"]
print(example_gissmo.isotopes)
print(example_gissmo.shifts)
print(example_gissmo.j_couplings)
print(example_gissmo.description)
Phytolab module
The phytolab module contains some selected molecules from a catalogue by Phytolab. It includes three datasets:
calculated_full: All NMR parameters in this set are computed. Details can be obtained from the description of each item in the set. Where 13C data has also been calculated, shifts and couplings are included for each nucleus.calculated: This set of computed parameters is intended for the calculation of one-dimensional 1H NMR spectra, as the parameters for 13C are omitted.combined: Where possible, the chemical shifts have been adjusted manually to achieve a better match with experimental 1H NMR spectra. Therefore, this set contains a combination of adjusted or computed shifts, and computed J-couplings. This set is recommended to simulate 1H NMR spectra to obtain the closest agreement with experiment.
In addition, the module includes the set molecules, which is an alias for combined as described above.
To access these datasets, import them analogously to the other modules:
from hqs_nmr_parameters.phytolab import molecules as phytolab_molecules
print("Dataset content:")
print(phytolab_molecules.description + "\n")
print(f"Entries of the set: {phytolab_molecules.keys}\n")
print("Details on the NMR parameters for Psoralen:")
print(phytolab_molecules["psoralen"].description)
NMR parameters for selected natural products from a catalogue by Phytolab.
Where possible, chemical shifts have been adjusted to match experimental spectra. The remaining parameters are computed.
For further details, please refer to descriptions of the individual items in the set.
Shifts and couplings only for nuclei ['1H'].
Entries of the set: ['angelicin', 'psoralen', 'friedelin']
Details on the NMR parameters for Psoralen:
Geometry in chloroform at B97-3c.
Shifts manually adjusted to match 1H-NMR spectrum at 80 MHz in CDCl3 provided by Phytolab.
J-couplings (gas-phase) at PBE/pcJ-3.
Like for all other modules, the content of the sets and especially their descriptions will depend on the installed version of hqs-nmr-parameters.
Assignments module
The assignments module contains example data of other complex molecules.
Patchoulol
The patchoulol dataset contains two molecules, the originally proposed structure for patchouli alcohol
and the correct structure. We can access it as:
from hqs_nmr_parameters.assignments import patchoulol
To get an overview of the set, we can access a brief summary with print(patchoulol.description).
Only the two mentioned molecules are present in the set, we can access them via their keys:
for key in patchoulol.keys:
print(f"{key}: {patchoulol[key].name}")
correct: Patchouli alcohol
erroneous: 4,10,11,11-Tetramethyltricyclo[5.3.1.01,5]undecan-10-ol
With this data, we can now use HQS Spectrum Tools to simulate both spectra and see the differences between these two similar molecules as well as compare with the experimental spectrum.
Menthol isomers
The menthol_isomers dataset is a collection of the four possible diastereomers of menthol
(5-methyl-2-(propan-2-yl)cyclohexan-1-ol). With three chiral centers at positions 1, 2, and 5 (in IUPAC convention), there are the following eight possible structures:
-
Menthol:
- (+)-enantiomer, with stereocenters (1S,2R,5S).
- (−)-enantiomer, with stereocenters (1R,2S,5R.)
-
Neomenthol:
- (+)-enantiomer, with stereocenters (1S,2S,5R).
- (−)-enantiomer, with stereocenters (1R,2R,5S).
-
Isomenthol:
- (+)-enantiomer, with stereocenters (1S,2R,5R).
- (−)-enantiomer, with stereocenters (1R,2S,5S).
-
Neoisomenthol:
- (+)-enantiomer, with stereocenters (1R,2R,5R).
- (−)-enantiomer, with stereocenters (1S,2S,5S).
Since enantiomers are not distinguishable by conventional NMR spectroscopy, there are four different possible NMR spectra. The given dataset contains NMR parameters calculated with density functional theory (DFT) for one enantiomer of each pair and can be imported from the assignments module as menthol_isomers_full for 1H and 13C NMR parameters or as menthol_isomers for only 1H NMR data.
For an overview of the dataset, just print its description:
from hqs_nmr_parameters.assignments import menthol_isomers
print(menthol_isomers.description)
The molecular keys and names of the structures in the dataset can be listed as:
for key in menthol_isomers.keys:
print(f"{key}: {menthol_isomers[key].name}")
SSR: (+)-Neomenthol (SSR)
RSR: (-)-Menthol (RSR)
SRR: (+)-Isomenthol (SRR)
SSS: (-)-Neoisomenthol (SSS)
For more information on the applied computational level of theory, please inspect the individual descriptions with the description attribute.
This data can be used to simulate the NMR spectra of all diastereomers as explained earlier and compare them to experimental ones, e.g., to that of neomenthol available here. Due to the limited accuracy of DFT calculations, it is not always straightforward to identify the correct isomer if the exact structure of the experimental measurement is unknown, but the comparison with all four possibilities will provide valuable insights for structure elucidation. Furthermore, the postprocessing module of HQS Spectrum Tools allows the user to modify the simulated spectrum to better match an experimental reference which will help to reduce the number of reasonable candidate structures.
Statistical evaluations
If, for the same set of molecular structures, two datasets with probe (e.g., calculations) and reference (e.g., experiments) NMR parameter data are available, the probe data can be evaluated quickly using the evaluate_shifts and evaluate_couplings functions to obtain the errors with respect to the reference data and resulting statistical measures. The functions take two MolecularDataSet objects, the first one being the probe dataset to be evaluated and the second one being the reference dataset to be evaluated against. The following basic example shows how to evaluate the calculated chemical shifts from CHESHIRE against the experimental data.
from hqs_nmr_parameters import evaluate_shifts
from hqs_nmr_parameters.cheshire import calculated, experimental_shifts_only
evaluation = evaluate_shifts(calculated, experimental_shifts_only)
The returned object (here evaluation) is an instance of type BenchmarkEvaluation and contains an attribute errors with a list of errors between the values from the probe and the reference dataset along with various properties providing statistical measures. For convenience, all relevant data from the evaluation can be printed with the print_all function.
evaluation.print_all()
The evaluate_couplings function can be used analogously for evaluating J-coupling constants. By default, all data points from both datasets are considered in the evaluation. However, for instance, if chemical shift values are available for more than one isotope, it makes sense to perform the evaluation only for data of a specific isotope. Therefore, the following isotope selection arguments are available:
evaluate_shiftsisotope: Only data for this specified isotope (string orIsotopeobject) is considered in the evaluation.
evaluate_couplingsisotopes: Only data for couplings between atoms of these specified isotopes (list of strings/Isotopes) is considered in the evaluation.isotope_pairs: Only data for couplings between these specified isotope pairs (list of tuples of strings/Isotopes) is considered in the evaluation.- Both arguments can be combined, for example, the call
performs a J-coupling constant evaluation of the data inevaluate_couplings(probe_dataset, ref_dataset, isotopes=["1H", "19F"], isotope_pairs=[("1H", "31P")])probe_datasetagainst that inref_datasetfor all of the following isotope pairs: 1H–1H, 1H–19F, 19F–19F, and 1H–31P.
These statistical evaluations can also directly be performed on a MolecularDataTable object. In this case, the functions evaluate_table_shifts and evaluate_table_couplings can be used by specifying the table and the label of the column that shall be used as reference dataset. They return a dictionary with the column labels of all other columns as keys and the corresponding BenchmarkEvaluation objects as values. The isotope selection options are the same as those shown above. An example for evaluating the calcualted and predicted chemical shifts in the CHESHIRE benchmark submodule is shown in the following:
from hqs_nmr_parameters import evaluate_table_shifts
from hqs_nmr_parameters.cheshire.benchmark import benchmark_1h
# The column labels are: "experimental shifts", "DFT", and "empirical prediction".
print("Column labels:", benchmark_1h.column_labels)
evaluations = evaluate_table_shifts(benchmark_1h, "experimental shifts", isotope="1H")
print("\nStatistics for calculated 1H NMR shifts:")
evaluations["DFT"].print_all()
print("\nStatistics for empirically predicted 1H NMR shifts:")
evaluations["empirical prediction"].print_all()
Input of molecular NMR parameters via a YAML file
NMR parameters for molecules can be provided in a YAML file. A brief summary of relevant YAML features is provided before proceeding to more detailed explanations and examples.
- Dictionaries in YAML are defined as
key: valuepairs. Most commonly, a dictionary contains one key/value pair per line:key 1: value 1 key 2: value 2 value 1is interpreted as a string,1is interpreted as an integer and1.0is interpreted as a floating-point number. To avoid problems with special characters (e.g., square brackets), strings may be enclosed in single or double quotes (they have different meanings, and single quotes should be preferred for a literal interpretation of the string).- Lists can be defined over multiple lines as:
Lists can also be enclosed in square brackets:- item 1 - item 2[item 1, item 2, item 3]. The nested list[[1, 2, 3], [4, 5, 6]]is equivalent to:- [1, 2, 3] - [4, 5, 6] - Indentation is part of the syntax: key/value pairs or list entries over multiple lines need to have the same number of leading spaces (no tabs).
- Comments start with a hash,
#.
Definition of the molecular structure
Definition using SMILES
A molecular structure needs to be provided along with its NMR parameters in order to get a complete molecular data input. The YAML input accepts one 2D structural representation, i.e., a SMILES string or a Molfile.
The simplest way to define a structure in the input file is through its SMILES representation. This is done using the key smiles, followed by a representation of the molecule. For acetic acid:
# Acetic acid defined using SMILES.
smiles: CC(=O)O
SMILES strings often contain square brackets [...]. In such cases, the string should be enclosed within quotes, '...', to avoid problems with the YAML parser.
Definition using a Molfile
Manual definition of increasingly large molecules using SMILES can be cumbersome. For example, the string representation of penicillin V would be:
smiles: 'CC1(C)S[C@@H]2[C@H](NC(=O)COc3ccccc3)C(=O)N2[C@H]1C(=O)O'
Instead, it is easier to draw a graphical representation such as the one below using one of many available proprietary or open-source packages.
Such structural 2D representations are commonly stored in Molfiles. A molecule can be read from a Molfile by specifying the file name after the key molfile:
molfile: penicillin_v.mol
Note that the YAML input needs to contain either a Molfile or SMILES, but it is not possible to specify both at the same time. Both the V2000 and V3000 variants of the Molfile specification are supported in the input.
Hydrogens in the molecular structure
2D representations of molecular structures, whether as skeletal formulas or as SMILES, tend to omit hydrogens. Instead, the number of hydrogen atoms is inferred from the atomic valencies, especially those of the carbons. Any of the following three structures can be provided as a Molfile for the acrylamide molecule:

Where hydrogens are suppressed (not drawn out as separate atoms with a bond), their NMR parameters are specified through assignment to the respective skeletal atom. In the leftmost of the three structures shown above, it would not be possible to assign different parameters to the two protons in the CH2 group. Instead, one of the two other structures shown above could be used to specify different parameters for those protons.
The only restriction with regard to hydrogens is that any skeletal atom can be connected either to suppressed or to stand-alone hydrogens, but not to a mixture of both. Thus, the following two structures would be rejected during input parsing:

The structure to the left mixes a non-suppressed hydrogen with a suppressed “implicit” hydrogen (CH) on the carbon atom; the structure to the right mixes a non-suppressed hydrogen with a suppressed “explicit” hydrogen (NH) on the nitrogen atom.
Numbering of atoms
Atoms in the structural representation of the molecule are labelled with integers for the assignment of parameters. Indices can be counted starting from zero or from one. To avoid errors or misunderstandings, it is mandatory to specify a count from key in the input file, followed by either 0 or 1. The choice between those two options is entirely arbitrary.
An example for acetamide with atom counting starting from zero:
# Atom indices are 0, 1, 2, 3 in their order of appearance in the SMILES string.
smiles: CC(=O)N
count from: 0
An example for atom counting starting from one:
# Atom indices are 1, 2, 3, 4 in their order of appearance in the SMILES string.
smiles: CC(=O)N
count from: 1
The atoms in a molecule are indexed by their order of appearance in the Molfile. Acetamide may be represented by a Molfile with the following content (note that the file header must contain three lines):
RDKit 2D
4 3 0 0 0 0 0 0 0 0999 V2000
0.0000 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1.2990 0.7500 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1.2990 2.2500 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
2.5981 -0.0000 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0
1 2 1 0
2 3 2 0
2 4 1 0
M END
Counting the atoms starting from 1 (count from: 1), the appropriate indices are represented in the image below.
Chemical shifts
Providing chemical shifts will be illustrated using the nitrobenzene molecule as an example. Its molecular structure is contained in the file PhNO2.mol and shown in the picture below, including a numbering of its atoms.
All chemical shifts are specified under the key shifts. Additionally, the values need to be nested under keys representing isotopes, which contain the atomic mass number and the element symbol (e.g., 1H or 13C). For each isotope, the chemical shifts are provided in pairs of an atomic index and the associated value in ppm (parts per million):
# Structure with suppressed protons.
molfile: PhNO2.mol
count from: 0
shifts:
# chemical shifts for protons in ppm
1H:
1: 8.23
2: 7.56
3: 7.71
4: 7.56
5: 8.23
# chemical shifts for carbon-13 nuclei in ppm
13C:
0: 148.5
1: 123.7
2: 129.4
3: 134.3
4: 129.4
5: 123.7
To assign chemical shifts for suppressed protons (that are not provided explicitly in the skeletal structure), the indices of the respective non-hydrogen atoms are used instead. All suppressed protons connected to the same atom are assigned an identical shift value.
If a Molfile contains hydrogens as standalone atoms, the chemical shifts are assigned to those protons using their respective atom indices. This is illustrated using the file PhNO2_allH.mol. Its structure is shown below.
In this example, the 1H shifts need to be assigned to atoms 9–13. Assigning them to atoms 1–5, as in the previous example, would produce an error.
# Structure with explicit protons.
molfile: PhNO2_allH.mol
count from: 0
shifts:
# chemical shifts for protons in ppm
1H:
9: 8.23
10: 7.56
11: 7.71
12: 7.56
13: 8.23
# chemical shifts for carbon-13 nuclei in ppm
13C:
0: 148.5
1: 123.7
2: 129.4
3: 134.3
4: 129.4
5: 123.7
Indirect spin-spin coupling constants
Indirect spin-spin coupling constants are provided under the key J-couplings in the YAML file. Additionally, the coupling constant values need to be grouped together by isotopes. Keys for each combination of isotopes are combined as isotope1-isotope2: e.g., 1H-1H for coupling constants between two protons or 1H-13C for the associated heteronuclear coupling.
J-coupling constants in units of Hz for each combination of nuclei are provided as a list of lists with the following structure:
J-couplings:
isotope1-isotope2:
- [atom index 1, atom index 2, coupling constant in Hz]
- [atom index 1, atom index 2, coupling constant in Hz]
- [...]
isotope1-isotope2:
- [atom index 1, atom index 2, coupling constant in Hz]
- [...]
The first atom index refers to the first isotope and the second atom index refers to the second isotope. As with shifts, values for suppressed hydrogens are assigned via the associated skeletal carbon or heteroatom. If multiple protons are connected to the same skeletal atom, they are assigned the same coupling constant. Inequivalent protons attached to the same skeletal atom, need to be specified explicitly as standalone atoms in the molecule definition, so that they can be referred to via their respective atom indices.
Examples
1H parameters for propane with SMILES input
smiles: CCC
count from: 0
shifts:
1H:
0: 0.9
1: 1.3
2: 0.9
J-couplings:
1H-1H:
- [0, 1, 7.26]
- [1, 2, 7.26]
The protons at the terminal CH3 groups are assigned chemical shifts of 0.9 ppm each, and the protons of the central CH2 group are given a value of 1.3 ppm. J-couplings between all protons of the neighboring CH3 and CH2 groups are assigned as 7.26 Hz. While an indirect spin-spin coupling interaction exists between equivalent protons within the CH3 and CH2 groups, it is not observed in the spectrum and the associated values are left out.
1H parameters for acrylonitrile
The structure of acrylonitrile is provided as a Molfile in acrylonitrile.mol. Its depiction is shown below.
Since protons 4 and 5 are inequivalent, they are specified as standalone atoms with different parameters. In addition, hydrogen 6 is represented as a standalone atom, though suppressing it would also be an equally valid choice.
molfile: acrylonitrile.mol
count from: 1
shifts:
1H:
4: 5.79 # H(trans)
5: 5.97 # H(cis)
6: 5.48 # H(gem)
J-couplings:
1H-1H:
- [4, 5, 0.9]
- [4, 6, 11.8]
- [5, 6, 17.9]
Combined 1H and 13C parameters for chloromethane
To illustrate the definition of heteronuclear coupling constants, the following example shows parameters for 13C-enriched chloromethane. The parameters include the shifts of the three protons and the 13C nucleus, as well as the coupling constants between these nuclei.
# CH3Cl with 13C, the hydrogens inside square brackets are implicit.
smiles: '[13CH3]Cl'
# C will have index 1 and Cl will have index 2
count from: 1
shifts:
# Shifts of the three protons.
1H:
1: 3.05
# Shift of carbon-13 in the molecule.
13C:
1: 25.6
J-couplings:
# Coupling between the three protons (would normally not be observed).
1H-1H:
- [1, 1, -10.8]
# Coupling between the three protons and the 13C atom.
1H-13C:
- [1, 1, 150.0]
Stored YAML files
For the molecules included in the examples module of the hqs-nmr-parameters package, YAML input files (and Molfiles when indicated as molecular structure) are available in the file system. To access them, use the following command, taking into account that each YAML file takes the name of its key in the dataset. The following shows the stored file and its data for acrylonitrile (with key “C2H3CN”):
from pathlib import Path
from hqs_nmr_parameters import examples
identifier = "C2H3CN"
acrylonitrile_yaml = Path(examples.__file__).parent.joinpath("parameters", identifier + ".yaml")
print(acrylonitrile_yaml.read_text())
name: Acrylonitrile
molfile: C2H3CN.mol
count from: 1
shifts:
1H:
4: 5.79 # H(trans)
5: 5.97 # H(cis)
6: 5.48 # H(gem)
J-couplings:
1H-1H:
- [4, 5, 0.9]
- [4, 6, 11.8]
- [5, 6, 17.9]
description: |
1H parameters for acrylonitrile.
Values were obtained from Hans Reich's Collection, NMR Spectroscopy.
https://organicchemistrydata.org
As mentioned earlier, this data is already included as MolecularData instance:
from hqs_nmr_parameters.examples import molecules
identifier = "C2H3CN"
parameters = molecules[identifier]
print(parameters.shifts)
print(parameters.j_couplings)
[(3, 5.79), (4, 5.97), (5, 5.48)]
[((3, 4), 0.9), ((3, 5), 11.8), ((4, 5), 17.9)]
Therefore, even if the atom counting in the YAML file starts at 1, the numbering in MolecularData always starts at 0.
Reading and processing YAML input files
In HQS Spectrum Tools, a YAML file containing NMR parameters is parsed using the read_parameters_yaml function
from the hqs-nmr-parameters package:
from hqs_nmr_parameters import read_parameters_yaml
parameters = read_parameters_yaml("input_file.yaml")
The read_parameters_yaml function can read the following keywords from a YAML file:
name: An optional name of the molecule.shifts: Chemical shifts in format{isotope: {index: value}}.j_couplings/J-couplings: J-coupling values in format{isotope1-isotope2: [[index1, index2, value], ...]}.count from/count_from: Specifies whether to count atomic indices starting from zero or from one.smiles: SMILES string of the molecule (better enclosed in quotation marks).molfile: Path to a Molfile with the molecular structure. As mentioned above, only one representation entry can be set, i.e., eithersmilesormolfile.molblock: Compressed Molfile content (not in clear text).temperature: Temperature in K.solvent: Name of the solvent.description: Additional further description.
parameters is then an instance of the Pydantic MolecularData class.
Program components
The following components will enable you to quickly and conveniently run NMR spectrum calculations using HQS Spectrum Tools:
- A convenience function for quick and easy spectrum calculations.
- Different data types to serialize the input and output of NMR calculations.
While these functionalities are typically all you need, you may also browse through the API-documentation to check out all available features. However, we recommend to first go through the example notebooks to get an idea of how the package is typically used.
NMR spectra calculations
calculate_spectrum
For most use cases the calculate_spectrum function should be sufficient to perform NMR spectra calculations. By default, it uses the clustering approach discussed in the solver chapter. This approach is exact for molecules smaller than the specified maximum cluster size (set by default to 12) and for larger systems still accurate and efficient.
For a quick introduction to this method you can just read on, however, for a proper walkthrough of all the customization options check out the example notebooks.
The calculate_spectrum function takes as input an object with the molecular data of the system of interest which can be of type MolecularData or NMRParameters. The latter is derived from the former using the spin_system method and only stores the isotopes, chemical shifts and J-coupling values. The second input argument of calculate_spectrum is of type NMRCalculationParameters and stores all parameters necessary to specify a NMR spectrum calculation. For more details on these objects check out the corresponding sections molecular data and data types.
As part of HQS Spectrum Tools the hqs-nmr-parameters package containing example molecules is provided. You can calculate the spectrum for one of the example molecules simply as follows:
from hqs_nmr.calculate import calculate_spectrum
from hqs_nmr.datatypes import NMRCalculationParameters
from hqs_nmr_parameters.examples import molecules
# Obtain example molecule of datatype NMRParameters.
molecule_parameters = molecules["C3H8"].spin_system()
# Define the calculation parameters. The only required field is the magnetic
# field strength in Tesla.
calculation_parameters = NMRCalculationParameters(field_T=11.7433)
# Calculate the individual spin contributions of the spectrum.
nmr_result = calculate_spectrum(
molecule_parameters,
calculation_parameters
)
nmr_result is now an object of datatype NMRResultSpectrum1D and stores the calculated spectrum as well as the input to the calculation. The spectrum can be plotted as follows:
from hqs_nmr.visualization import plot_spectrum
from matplotlib import pyplot as plt
plot_spectrum(nmr_result.spectrum)
plt.show()
Data types
Molecular NMR parameters, spectra and calculation results are stored in Python objects in HQS Spectrum Tools. These data structures are all Pydantic data classes and the data can be accessed as attributes of the class. See NMRResultSpectrum1D for an example. Here we will elaborate on the datatypes defined within hqs-nmr, while in section Molecular data those defined in hqs-nmr-parameters are discussed.
Note that for the full signature of each datatype you can check out the API-documentation. Here we will just point out the most important attributes of each class.
NMRResultSpectrum1D
The NMRResultSpectrum1D is used to store all of the input and output of a NMR spectrum calculation when using the calculate_spectrum routine. It comprises:
-
The molecular data used for the calculation, which can include the full data in a
MolecularDataobject or only the NMR parameters with data typeNMRParameters(molecule_parameters). -
All the parameters handed to
calculate_spectrumin form of aNMRCalculationParametersobject (calculation_parameters). -
The calculated spectrum of data type
NMRSpectrum1D(spectrum).
Assuming we have obtained an object called result_spectrum of data type NMRResultSpectrum1D the attributes can be accessed as follows:
molecule_parameters = result.molecule_parameters
calculation_parameters = result.calculation_parameters
spectrum = result.spectrum
In the following we will elaborate on the data type of each of these objects in more detail.
NMRResultGreensFunction1D
The NMRResultGreensFunction1D is the analog data type to NMRResultSpectrum1D when using calculate_greens_function instead of calculate_spectrum. The only difference is that instead of the attribute spectrum, it has the attribute greens_function, which stores a NMRGreensFunction1D data type. For information on the difference between a spectrum and a Green’s function check out the Math section.
Assuming we have an object called result_greens_function of data type NMRResultGreensFunction1D the greens_function attribute can be accessed as follows:
greens_function = result_greens_function.greens_function
NMRCalculationParameters
The NMRCalculationParameters data type stores all possible customization options when performing spectra calculations with calculate_spectrum or calculate_greens_function.
The only parameter the user always has to specify is the magnetic field strength in Tesla, so the most simple and often already sufficient instantiation of this data type may look as follows:
from hqs_nmr.datatypes import NMRCalculationParameters
calculation_parameters = NMRCalculationParameters(field_T=11.7433)
Another important option the user can set is the reference_isotope, which is the Isotope specified as Isotope(mass, symbol) to define the frequency of the rotating frame. Furthermore, the object contains multiple attributes allowing to improve the resolution. For a detailed explanation of the available customization options check out the tutorial notebooks or take a look at the API-documentation.
NMRSolverSettings
The NMRSolverSettings data type stores customization options specific for the solver backend. It is stored as an attribute of a NMRCalculationParameters object and typically does not need to be altered.
It is however important to know that by default a spin-dependent clustering of the molecule into overlapping clusters is performed (for details check here). This is an extremely accurate approximation especially at high field, however in some cases this might not be wanted. In these cases you can set the attribute perform_clustering=False, such that the spectrum of the entire molecule is calculated at once and symmetries will be exploited where applicable, but no approximations are made. Note that this can lead to a significant increase in runtime, so typically it is more advisable to increase the size of the clusters instead. To do so the attribute max_cluster_size has to be specified:
from hqs_nmr.datatypes import NMRSolverSettings
solver_settings = NMRSolverSettings(max_cluster_size=16)
To see all customization options check the API-documentation.
NMRSpectrum1D
The NMRSpectrum1D data type holds an NMR spectrum comprising the frequencies in ppm (omegas_ppm), an array with the individual spin contributions to the spectrum (spin_contributions) and the full-width-half-maximum in ppm (fwhm_ppm).
Assuming we have performed a NMR spectrum calculation using calculate_spectrum and obtained as output an NMRResultSpectrum1D object we called result_spectrum, we can obtain an object of data type NMRSpectrum1D as an attribute of this object:
spectrum = result_spectrum.spectrum
The spin_contributions are a NumPy array, where each row holds the contribution of the corresponding spin, i.e.:
spectrum.spin_contributions[0,:]
holds the contribution of the first spin (index 0). The total spectrum that would be measured experimentally can be calculated using
import numpy as np
np.sum(spectrum.spin_contributions, axis=0)
NMRGreensFunction1D
The NMRGreensFunction1D data type basically has the same attributes as NMRSpectrum1D, however spin_contributions is a complex array, as it stores the full NMR Green’s function and not just the spectral function. This data type is typically only used if one wants to perform post-processing steps that require also the real part of the Green’s function.
Assuming we have performed a NMR Green’s function calculation using calculate_greens_function and obtained as output an NMRResultGreensFunction1D object called result_greens_function, we can obtain an object of data type NMRGreensFunction1D as an attribute of this object:
greens_function = result_greens_function.greens_function
The total spectrum can then be obtained as follows:
import numpy as np
np.sum(- np.imag(greens_function.spin_contributions), axis=0)
Serialization (Saving and Loading)
The data types described above have a common interface for data serialization. There exists a function called to_json common to all classes that can take a class object and a file name (with or without the full path) to store the data as a JSON file. For example, if we have an object called spectrum of the data class NMRSpectrum1D and want to save it as spectrum_data.json:
from hqs_nmr.datatypes import to_json
to_json(spectrum, "spectrum_data.json")
There is also the inverse method from_json to load the data again. It needs as additional information the type of class the JSON file has stored, e.g.:
from hqs_nmr.datatypes import from_json, NMRSpectrum1D
spectrum = from_json(NMRSpectrum1D, "spectrum_data.json")
Pulse sequence
The following components will enable you to quickly and conveniently define NMR pulse sequences using HQS Spectrum Tools:
- A pulse sequence class for quick and easy pulse sequence definition.
- Different pulse types are available to cover a variety of experiments.
While these functionalities are typically all you need, you may also browse through the API-documentation to check out all available features. However, we recommend to first go through the example notebooks to get an idea of how the package is typically used.
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.
RotationPulse
A RotationPulse is defined by an angle and an axis. To create a rotation pulse the easiest way is to use the from_string method of NMRPulse class.
The str format for a rotation pulse is the angle followed by the axis.
The axis can be x, y or z. The angle can be any number and will be interpreted as an angle in degrees.
This pulse applies an instantaneous rotation.
from hqs_nmr.pulse_sequence import NMRPulse
rotation_pulse = NMRPulse.from_string("90y")
rotation_pulse2 = NMRPulse.from_string("-26.8x")
Pi expressions are not supported except for a few special cases: ±π, ±π/2 and ±π/4 which will be interpreted as radians.
from hqs_nmr.pulse_sequence import NMRPulse
# These two are equivalent
rotation_pulse = NMRPulse.from_string("90y")
rotation_pulse2 = NMRPulse.from_string("pi/2y")
Another feature is for a pulse to only act on specific spins. This can be done by adding a | after the angle and list the spins it should act on separated by commas.
from hqs_nmr.pulse_sequence import NMRPulse
# Apply pulse only to spins 0, 1, 3, and 5
rotation_pulse = NMRPulse.from_string("90y|0,1,3,5")
Methods
apply_pulse_operator
def apply_pulse_operator(
self,
density_mat: np.ndarray,
**kwargs: Any,
) -> np.ndarray:
"""Apply the pulse P on the density matrix.
Args:
density_mat (np.ndarray): The density matrix rho_0.
**kwargs: Additional arguments:
num_spins (int): Required. The number of spins in the system.
Returns:
np.ndarray: The transformed density matrix P @ rho_0 @ P†.
Raises:
ValueError: If num_spins is not provided.
ValueError: If active_sites contains indices >= num_spins.
"""
Example:
from hqs_nmr.pulse_sequence.pulses import RotationPulse
import numpy as np
pulse = RotationPulse(angle=90, axis="x")
density_mat = np.eye(4, dtype=complex) # Example density matrix for 2 spins
result = pulse.apply_pulse_operator(density_mat, num_spins=2)
get_pulse_operator
def get_pulse_operator(
self,
**kwargs: Any,
) -> np.ndarray:
"""Returns the total pulse operator for this rotation pulse.
Args:
**kwargs: Additional arguments:
num_spins (int): Required. The number of spins in the system.
Returns:
np.ndarray: The total pulse operator P as a (2^num_spins × 2^num_spins) matrix.
For pulses with active_sites specified, this is the product of all individual
site operators: P = P_site_n @ ... @ P_site_1 @ P_site_0
Raises:
ValueError: If num_spins is not provided.
ValueError: If active_sites contains indices >= num_spins.
"""
Example:
from hqs_nmr.pulse_sequence.pulses import RotationPulse
pulse = RotationPulse(angle=90, axis="y", active_sites=[0, 2])
P = pulse.get_pulse_operator(num_spins=4)
# P is the combined rotation operator for spins 0 and 2
SelectiveFrequencyPulse
A SelectiveFrequencyPulse is defined by an angle an axis and a frequency. To create a selective frequency pulse the easiest way is to use the from_string method of NMRPulse class.
The str format for a selective frequency pulse is the angle followed by the axis then a |, and the frequency followed by the letter f.
The axis can be x, y or z. The angle can be any number and will be interpreted as an angle in degrees or pi expression just like rotation pulses. The frequency can be any float.
This pulse applies an instantaneous rotation to each spin. The angle of this rotation differs for each spin and is calculated based on how close the shift is to the targetted frequency using a gaussian centered on this frequency. So the targeted frequency shouldn’t be the actual frequency of the targeted spin but the difference between its frequency and the larmor frequency of the reference isotope.
The NMRCalculationParameters aswell as the NMRParameters needs to be provided to the apply_pulse_operator for this pulse.
from hqs_nmr.pulse_sequence import NMRPulse
selective_frequency_pulse = NMRPulse.from_string("90y|389f")
selective_frequency_pulse2 = NMRPulse.from_string("-pi/4x|2860.05f")
The user can also control the width of the gaussian by adding {width}w at the end of the string, where {width} is the full width at half maximum (FWHM) of the Gaussian in Hz.
The default value is 10 Hz: smaller values produce a narrower, more selective peak, while larger values produce a broader one.
from hqs_nmr.pulse_sequence import NMRPulse
selective_frequency_pulse = NMRPulse.from_string("90y|389f0.005w") # very narrow, will only acts on spins with a shift very close to 389Hz
selective_frequency_pulse = NMRPulse.from_string("90y|389f1w") # wide, will also acts on spins with shifts close to 389Hz
How It Works
The selective frequency pulse applies a rotation to each spin in the system, but with the rotation angle modified based on how close each spin’s frequency is to the target frequency. The angle for each spin is calculated using a Gaussian function:
angle_per_spin[i] = theta * exp(-((shift_Hz[i] - target_Hz)^2) / (2 * sigma^2))
where:
thetais the specified rotation angleshift_Hz[i]is the frequency shift of spin i in Hz (converted from ppm)target_Hzis the target frequencysigmais calculated from thefwhm_Hzparameter
The FWHM (Full Width at Half Maximum) in Hz is used directly to calculate sigma:
sigma = fwhm_Hz / (2 * sqrt(2 * ln(2)))
Methods
apply_pulse_operator
def apply_pulse_operator(
self,
density_mat: np.ndarray,
**kwargs: Any,
) -> np.ndarray:
"""Apply the selective frequency pulse P on the density matrix.
Args:
density_mat (np.ndarray): The initial density matrix rho_0.
**kwargs: Additional arguments (all required):
num_spins (int): The number of spins in the system.
nmr_parameters (NMRParameters): The NMR parameters containing the chemical
shifts (in ppm) for each spin. These are converted to Hz using the
calculation parameters.
calculation_parameters (NMRCalculationParameters): The calculation parameters
containing the reference frequency and other settings needed to convert
chemical shifts from ppm to Hz.
Returns:
np.ndarray: The transformed density matrix P @ rho_0 @ P†, where P is the
product of individual rotation operators for each spin with frequency-dependent
rotation angles.
Raises:
ValueError: If num_spins is not provided.
ValueError: If nmr_parameters or calculation_parameters is not provided.
"""
Example:
from hqs_nmr.pulse_sequence.pulses import SelectiveFrequencyPulse
from hqs_nmr import NMRParameters, NMRCalculationParameters
import numpy as np
# Create a selective frequency pulse targeting 1000 Hz
pulse = SelectiveFrequencyPulse(
spin_frequency_target_Hz=1000.0,
theta=90.0,
axis="x",
fwhm_Hz=0.1
)
# Set up NMR parameters
nmr_params = NMRParameters(
shifts=[5.0, 5.5, 6.0], # Chemical shifts in ppm
# ... other parameters
)
calc_params = NMRCalculationParameters(
# ... calculation parameters including reference frequency
)
density_mat = np.eye(8, dtype=complex) / 8 # Example for 3 spins
# Apply the selective pulse
result = pulse.apply_pulse_operator(
density_mat,
num_spins=3,
nmr_parameters=nmr_params,
calculation_parameters=calc_params
)
get_pulse_operator
def get_pulse_operator(
self,
**kwargs: Any,
) -> np.ndarray:
"""Returns the total selective frequency pulse operator.
Args:
**kwargs: Additional arguments (all required):
num_spins (int): The number of spins in the system.
nmr_parameters (NMRParameters): The NMR parameters containing the chemical
shifts (in ppm) for each spin.
calculation_parameters (NMRCalculationParameters): The calculation parameters
for converting chemical shifts from ppm to Hz.
Returns:
np.ndarray: The total pulse operator P as a (2^num_spins × 2^num_spins) matrix.
This is the product of all individual spin operators with frequency-dependent
rotation angles: P = P_spin_n @ ... @ P_spin_1 @ P_spin_0
Raises:
ValueError: If num_spins is not provided.
ValueError: If nmr_parameters or calculation_parameters is not provided.
"""
Example:
from hqs_nmr.pulse_sequence.pulses import SelectiveFrequencyPulse
from hqs_nmr import NMRParameters, NMRCalculationParameters
pulse = SelectiveFrequencyPulse(
spin_frequency_target_Hz=1500.0,
theta=180.0,
axis="y",
fwhm_Hz=0.05 # Narrow selectivity (FWHM in Hz)
)
# Get the pulse operator
P = pulse.get_pulse_operator(
num_spins=4,
nmr_parameters=nmr_params,
calculation_parameters=calc_params
)
# Use it to transform states or density matrices
state = np.array([1, 0, 0, 0, ...], dtype=complex) # 2^4 = 16 elements
evolved_state = P @ state
TimeEvolutionPulse
A TimeEvolutionPulse is defined by a time. To create a time evolution pulse the easiest way is to use the from_string method of NMRPulse class.
The str format for a time evolution pulse is the time preceeded by the letter t.
The time can be any number.
The hamiltonian used for the time evolution should be provided when calling the apply_pulse_operator method.
This pulse applies the time evolution of the provided hamiltonian for a period of time t.
from hqs_nmr.pulse_sequence import NMRPulse
rotation_pulse = NMRPulse.from_string("t2")
rotation_pulse2 = NMRPulse.from_string("t0.0001")
rotation_pulse2 = NMRPulse.from_string("t-0.01")
One can also use this pulse to gather the expectation values during the time evolution. The
default timestep is 0.01 and can be selected with the following pattern.
from hqs_nmr.pulse_sequence import NMRPulse
rotation_pulse = NMRPulse.from_string("t2s0.001")
Methods
apply_pulse_operator
def apply_pulse_operator(
self,
density_mat: np.ndarray,
**kwargs: Any,
) -> np.ndarray:
"""Applies the time evolution pulse P = exp(-i*H*t) on the density matrix.
Args:
density_mat (np.ndarray): The initial density matrix rho_0.
**kwargs: Additional arguments (one of the following is required):
hamiltonian (np.ndarray): The Hamiltonian matrix H. The time evolution
operator is computed as P = exp(-i * t * H) using matrix exponential.
If a Hamiltonian was provided when creating the pulse, it takes
precedence.
eigen_hamiltonian (Tuple[np.ndarray, np.ndarray]): Alternative to hamiltonian.
A tuple of (eigenvalues, eigenvectors) of the Hamiltonian. This provides
more efficient time evolution using: P = V @ diag(exp(-i * t * λ)) @ V†
where λ are the eigenvalues and V are the eigenvectors.
Returns:
np.ndarray: The evolved density matrix P @ rho_0 @ P†.
Raises:
ValueError: If neither hamiltonian nor eigen_hamiltonian is provided.
"""
Example:
from hqs_nmr.pulse_sequence.pulses import TimeEvolutionPulse
import numpy as np
pulse = TimeEvolutionPulse(t=5.0, timestep=0.01)
hamiltonian = np.array([[1.0, 0.1], [0.1, -1.0]], dtype=complex)
density_mat = np.eye(2, dtype=complex) / 2
# Apply time evolution
result = pulse.apply_pulse_operator(density_mat, hamiltonian=hamiltonian)
# Or use eigendecomposition for efficiency
eigenvalues, eigenvectors = np.linalg.eigh(hamiltonian)
result = pulse.apply_pulse_operator(
density_mat,
eigen_hamiltonian=(eigenvalues, eigenvectors)
)
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 Hamiltonian for the specified time,
recording the expectation value of the measurement operator at each timestep interval.
The timesteps are determined by the `timestep` 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 (one of the following is required):
hamiltonian (np.ndarray): The Hamiltonian matrix. If a Hamiltonian was
provided when creating the pulse, it takes precedence.
eigen_hamiltonian (Tuple[np.ndarray, np.ndarray]): Alternative to hamiltonian.
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+self.t in intervals of self.timestep.
- 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 TimeEvolutionPulse
import numpy as np
pulse = TimeEvolutionPulse(t=10.0, timestep=0.1) # 10 seconds, 0.1s steps
hamiltonian = np.array([[1.0, 0.1], [0.1, -1.0]], dtype=complex)
density_mat = np.eye(2, dtype=complex) / 2
measurement_op = np.array([[1, 0], [0, -1]], dtype=complex) # Sz
# Collect expectation values during evolution
expectation_values, final_density, times = pulse.apply_time_evolution(
density_mat, measurement_op, hamiltonian=hamiltonian
)
# expectation_values contains 101 values (t=0, 0.1, 0.2, ..., 10.0)
get_pulse_operator
def get_pulse_operator(
self,
**kwargs: Any,
) -> np.ndarray:
"""Returns the time evolution operator P = exp(-i*H*t).
Args:
**kwargs: Additional arguments (one of the following is required):
hamiltonian (np.ndarray): The Hamiltonian matrix H. The time evolution
operator is computed as P = exp(-i * t * H) using matrix exponential.
If a Hamiltonian was provided when creating the pulse, it takes
precedence.
eigen_hamiltonian (Tuple[np.ndarray, np.ndarray]): Alternative to hamiltonian.
A tuple of (eigenvalues, eigenvectors) of the Hamiltonian. This provides
more efficient computation using: P = V @ diag(exp(-i * t * λ)) @ V†
Returns:
np.ndarray: The time evolution operator P.
Raises:
ValueError: If neither hamiltonian nor eigen_hamiltonian is provided.
"""
Example:
from hqs_nmr.pulse_sequence.pulses import TimeEvolutionPulse
import numpy as np
pulse = TimeEvolutionPulse(t=5.0)
hamiltonian = np.array([[1.0, 0.1], [0.1, -1.0]], dtype=complex)
# Get the propagator
P = pulse.get_pulse_operator(hamiltonian=hamiltonian)
# Apply to any state or density matrix
state = np.array([1, 0], dtype=complex)
evolved_state = P @ state
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
NMRPulseSequence
The NMRPulseSequence class is a Pydantic BaseModel representing a sequence of NMRPulse objects.
Creating a Pulse Sequence
There are multiple ways to create a pulse sequence:
String-based initialization
The traditional way is to use a string where pulses are separated by ::
from hqs_nmr.pulse_sequence import NMRPulseSequence
pulse_sequence = NMRPulseSequence("90x:t5")
Method chaining
All convenience methods return self, allowing for method chaining:
pulse_sequence = (NMRPulseSequence()
.rotation_pulse(90, "x")
.time_evolution(5.0)
.rotation_pulse(-90, "y"))
Adding Pulses
Using string representation
You can add more pulses to an existing sequence using the add_pulses method with a string:
pulse_sequence.add_pulses("t-5:-pi/2y:t0.1")
Using convenience methods
The class provides convenient methods for adding specific types of pulses:
Rotation Pulse
pulse_sequence.rotation_pulse(angle=90, axis="x", active_sites=None)
Creates and adds a rotation pulse with the specified angle (in degrees) and axis. Optionally, you can specify which spins the pulse should act on using active_sites (a list of spin indices).
Parameters:
angle(float): The rotation angle in degreesaxis(str): The rotation axis (“x”, “y”, or “z”)active_sites(Optional[List[int]]): List of spin indices to apply the pulse to. If None, applies to all spins.
Example:
# Apply 90° rotation around x-axis to all spins
pulse_sequence.rotation_pulse(90, "x")
# Apply 180° rotation around y-axis only to spins 0 and 2
pulse_sequence.rotation_pulse(180, "y", active_sites=[0, 2])
Time Evolution
pulse_sequence.time_evolution(t=5.0, timestep=0.01)
Creates and adds a time evolution pulse for time t (in seconds) with the specified timestep.
Parameters:
t(float): The evolution time in secondstimestep(Optional[float]): The time step for the simulation (default: 0.01)
Example:
# Evolve for 5 seconds with default timestep
pulse_sequence.time_evolution(5.0)
# Evolve for 2 seconds with custom timestep
pulse_sequence.time_evolution(2.0, timestep=0.001)
Selective Frequency Pulse
pulse_sequence.selective_frequency_pulse(
spin_frequency_target_Hz=1000.0,
theta=90,
axis="x",
fwhm_Hz=0.1
)
Creates and adds a selective frequency pulse that targets a specific spin frequency. The pulse acts on spins based on how close their shift is to the target frequency, with a Gaussian profile controlled by fwhm_Hz (the full width at half maximum in Hz).
Parameters:
spin_frequency_target_Hz(float): The target spin frequency in Hztheta(float): The rotation angle in degreesaxis(str): The rotation axis (“x”, “y”, or “z”)fwhm_Hz(Optional[float]): The full width at half maximum of the Gaussian profile in Hz (default: 0.1). Note that theSelectiveFrequencyPulseclass itself defaults to 10 Hz when constructed directly.
Example:
# Apply selective pulse at 1000 Hz
pulse_sequence.selective_frequency_pulse(1000.0, 90, "x")
# Apply selective pulse with narrower frequency selectivity
pulse_sequence.selective_frequency_pulse(1000.0, 90, "x", fwhm_Hz=0.05)
Soft Pulse
pulse_sequence.soft_pulse(
total_time=1.0,
phi=0.0,
shape_params={"t_0": 0.5, "sigma": 0.1, "amplitude": np.pi/2},
timestep=None,
shape="gaussian",
integration_method="midpoint"
)
Creates and adds a soft pulse with a time-dependent envelope to the sequence. Soft pulses apply a smooth time-varying Hamiltonian over a finite duration, unlike rotation pulses which are instantaneous.
Parameters:
total_time(float): The duration of the pulse in secondsphi(float): The phase of the pulse in radiansshape_params(Dict[str, Any]): Parameters for the pulse shape (see Soft Pulse for details on each shape)timestep(Optional[float]): The time step for simulation (default: None, auto-calculated based on shape)shape(str): The shape of the pulse (default: “gaussian”). Options include: “gaussian”, “constant”, “rectangular”, “half_gaussian”, “eburp2”, “iburp2”, “reburp”, “q3”, “q5”integration_method(str): The integration method for Magnus expansion (default: “midpoint”). Options: “midpoint”, “gl”
Example:
import numpy as np
# Add a Gaussian soft pulse
pulse_sequence.soft_pulse(
total_time=1.0,
phi=0.0,
shape_params={"t_0": 0.5, "sigma": 0.1, "amplitude": np.pi/2},
shape="gaussian"
)
For detailed information about pulse shapes, parameters, integration methods, and more examples, see the Soft Pulse documentation.
Pydantic Serialization
Since NMRPulseSequence is a Pydantic BaseModel, it provides built-in serialization and deserialization capabilities:
Serialization to Dictionary
pulse_sequence = NMRPulseSequence("90x:t5:-90y|150f")
pulse_dict = pulse_sequence.model_dump()
# Returns:
# {'pulses': [{'active_sites': None,
# 'angle': <NMRPulseAngle.DEG_90: 90.0>,
# 'axis': <NMRPulseAxis.X: 'x'>,
# 'pulse_type': 'rotation'},
# {'pulse_type': 'time_evolution', 't': 5.0, 'timestep': 0.01},
# {'axis': <NMRPulseAxis.Y: 'y'>,
# 'fwhm_Hz': 10.0,
# 'pulse_type': 'selective_frequency',
# 'spin_frequency_target_Hz': 150.0,
# 'theta': <NMRPulseAngle.DEG_NEG_90: -90.0>}]}
Serialization to JSON
pulse_sequence = NMRPulseSequence("90x:t5:-90y|150f")
pulse_json = pulse_sequence.model_dump_json()
# Or with indentation for readability
pulse_json = pulse_sequence.model_dump_json(indent=2)
# Returns:
# ('{\n'
# ' "pulses": [\n'
# ' {\n'
# ' "pulse_type": "rotation",\n'
# ' "angle": 90.0,\n'
# ' "axis": "x",\n'
# ' "active_sites": null\n'
# ' },\n'
# ' {\n'
# ' "pulse_type": "time_evolution",\n'
# ' "t": 5.0,\n'
# ' "timestep": 0.01\n'
# ' },\n'
# ' {\n'
# ' "pulse_type": "selective_frequency",\n'
# ' "spin_frequency_target_Hz": 150.0,\n'
# ' "theta": -90.0,\n'
# ' "axis": "y",\n'
# ' "fwhm_Hz": 10.0\n'
# ' }\n'
# ' ]\n'
# '}')
Deserialization from Dictionary
pulse_dict = {
"pulses": [
{"pulse_type": "rotation", "angle": 90.0, "axis": "x", "active_sites": None},
{"pulse_type": "time_evolution", "t": 5.0, "timestep": 0.01},
{
"pulse_type": "selective_frequency",
"spin_frequency_target_Hz": 150.0,
"theta": -90.0,
"axis": "y",
"fwhm_Hz": 10.0,
},
],
}
pulse_sequence = NMRPulseSequence.model_validate(pulse_dict)
Deserialization from JSON
pulse_json = """{
"pulses": [
{"pulse_type": "rotation", "angle": 90.0, "axis": "x", "active_sites": null},
{"pulse_type": "time_evolution", "t": 5.0, "timestep": 0.01},
{"pulse_type": "selective_frequency", "spin_frequency_target_Hz": 150.0, "theta": -90.0, "axis": "y", "fwhm_Hz": 10.0}
]
}"""
pulse_sequence = NMRPulseSequence.model_validate_json(pulse_json)
Saving and Loading from Files
# Save to JSON file
pulse_sequence = NMRPulseSequence("90x:t5:-90y|150f")
with open("pulse_sequence.json", "w") as f:
f.write(pulse_sequence.model_dump_json(indent=2))
# Load from JSON file
with open("pulse_sequence.json", "r") as f:
pulse_sequence = NMRPulseSequence.model_validate_json(f.read())
These serialization methods are useful for:
- Saving pulse sequences to files
- Transmitting pulse sequences over network APIs
- Storing pulse sequences in databases
- Creating reproducible experiment configurations
Main Methods
This class has two main methods for applying pulse sequences:
apply_pulse_sequence
def apply_pulse_sequence(
self,
eigenvectors: BlockDiagonalOperator,
**kwargs: Any,
) -> np.ndarray:
"""Apply the pulse sequence operator to eigenvectors.
Starts from the density matrix rho = S^z, applies each pulse P in the sequence
to calculate rho = P @ rho @ P†, and transforms the final density matrix with
the eigenvectors.
Args:
eigenvectors (BlockDiagonalOperator): The eigenvectors.
kwargs: Additional keyword arguments for pulse application. Required kwargs depend on
the pulse types in the sequence:
num_spins (int): Required. The number of spins in the system.
hamiltonian (np.ndarray): Required for TimeEvolutionPulse and SoftPulse.
The Hamiltonian matrix to use during time evolution.
eigen_hamiltonian (Tuple[np.ndarray, np.ndarray]): Optional alternative to
hamiltonian for TimeEvolutionPulse. A tuple of (eigenvalues, eigenvectors)
of the Hamiltonian for more efficient time evolution.
nmr_parameters (NMRParameters): Required for SelectiveFrequencyPulse.
The NMR parameters containing chemical shifts.
calculation_parameters (NMRCalculationParameters): Required for SelectiveFrequencyPulse.
The calculation parameters for converting shifts to frequencies.
Returns:
np.ndarray: The result of applying the pulse sequence: eigenvectors.rdot(eigenvectors.dot_h(P @ rho @ P†))
"""
get_pulse_sequence
def get_pulse_sequence(
self,
eigenvectors: BlockDiagonalOperator,
**kwargs: Any,
) -> np.ndarray:
"""Applies the pulse sequence operator to eigenvectors.
This method computes only the pulse operator P (not applied to a density matrix).
It returns the total pulse operator for the entire sequence by multiplying
all individual pulse operators together: P_total = P_n @ ... @ P_2 @ P_1
Args:
eigenvectors (BlockDiagonalOperator): The eigenvectors.
kwargs: Additional keyword arguments for pulse application. Required kwargs depend on
the pulse types in the sequence:
num_spins (int): Required. The number of spins in the system.
hamiltonian (np.ndarray): Required for TimeEvolutionPulse and SoftPulse.
The Hamiltonian matrix to use during time evolution.
eigen_hamiltonian (Tuple[np.ndarray, np.ndarray]): Optional alternative to
hamiltonian for TimeEvolutionPulse. A tuple of (eigenvalues, eigenvectors)
of the Hamiltonian for more efficient time evolution.
nmr_parameters (NMRParameters): Required for SelectiveFrequencyPulse.
The NMR parameters containing chemical shifts.
calculation_parameters (NMRCalculationParameters): Required for SelectiveFrequencyPulse.
The calculation parameters for converting shifts to frequencies.
Returns:
np.ndarray: The total pulse operator transformed by eigenvectors:
eigenvectors.rdot(eigenvectors.dot_h(P_total))
"""
apply_pulse_sequence_to_state
def apply_pulse_sequence_to_state(
self,
state: np.ndarray,
**kwargs: Any,
) -> np.ndarray:
"""Applies the pulse sequence to a state vector.
Args:
state (np.ndarray): The state vector.
kwargs: Additional keyword arguments for pulse application. Required kwargs depend on
the pulse types in the sequence:
num_spins (int): Required. The number of spins in the system.
hamiltonian (np.ndarray): Required for TimeEvolutionPulse and SoftPulse.
The Hamiltonian matrix to use during time evolution.
eigen_hamiltonian (Tuple[np.ndarray, np.ndarray]): Optional alternative to
hamiltonian for TimeEvolutionPulse. A tuple of (eigenvalues, eigenvectors)
of the Hamiltonian for more efficient time evolution.
nmr_parameters (NMRParameters): Required for SelectiveFrequencyPulse.
The NMR parameters containing chemical shifts.
calculation_parameters (NMRCalculationParameters): Required for SelectiveFrequencyPulse.
The calculation parameters for converting shifts to frequencies.
Returns:
np.ndarray: The state vector after applying the pulse sequence: P_total @ state
"""
collect_expectation_values
def collect_expectation_values(
self,
measurement_operator: Union[np.ndarray, List[np.ndarray]],
correlation_operator: Optional[np.ndarray] = None,
**kwargs: Any,
) -> Tuple[Union[List[complex], list[List[complex]]], List[float]]:
"""Produce a list of expectation values for the given measurement operator(s).
This function will apply all the pulses in the sequence on Sz and record expectation
values after each instantaneous pulse or for each timesteps for time-based pulses.
If only one measurement operator is provided a list containing the expectation
values will be returned. A list of measurement operators can also be provided, in this
case a list containing lists of expectation values for each operator will be returned.
A list containing the timestep of each values will also be returned, starting from 0.
NOTE: instantaneous pulses will have a timestep of 1e-13s.
Args:
measurement_operator (Union[np.ndarray, List[np.ndarray]]): The measurement operator.
correlation_operator: Operator whose expectation value is recorded
kwargs: Additional keyword arguments for pulse application. Required kwargs depend on
the pulse types in the sequence:
num_spins (int): Required. The number of spins in the system.
hamiltonian (np.ndarray): Required for TimeEvolutionPulse and SoftPulse.
The Hamiltonian matrix to use during time evolution.
eigen_hamiltonian (Tuple[np.ndarray, np.ndarray]): Optional alternative to
hamiltonian for TimeEvolutionPulse. A tuple of (eigenvalues, eigenvectors)
of the Hamiltonian for more efficient time evolution.
nmr_parameters (NMRParameters): Required for SelectiveFrequencyPulse.
The NMR parameters containing chemical shifts.
calculation_parameters (NMRCalculationParameters): Required for
SelectiveFrequencyPulse.
The calculation parameters for converting shifts to frequencies.
Returns:
Union[List[complex], list[List[complex]]]: The expectation values.
List[float]: The measurement time of each value
"""
collect_expectation_values_from_state
def collect_expectation_values_from_state(
self,
state_vector: np.ndarray,
measurement_operator: Union[np.ndarray, List[np.ndarray]],
correlation_operator: Optional[np.ndarray] = None,
**kwargs: Any,
) -> Tuple[Union[List[complex], list[List[complex]]], List[float]]:
"""Collects the expectation values when applying the pulse sequence on a state.
This function will apply all the pulses in the sequence on the provided state and record
expectation values after each instantaneous pulse or for each timesteps for time-based
pulses.
If only one measurement operator is provided a list containing the expectation
values will be returned. A list of measurement operators can also be provided, in this
case a list containing lists of expectation values for each operator will be returned.
A list containing the timestep of each values will also be returned, starting from 0.
NOTE: instantaneous pulses will have a timestep of 1e-13s.
Args:
state_vector: The initial state vector as a (2^N,) complex array.
measurement_operator (Union[np.ndarray, List[np.ndarray]]): The measurement operator.
each timestep.
correlation_operator: Operator whose expectation value is recorded
kwargs: Additional keyword arguments for pulse application. Required kwargs depend on
the pulse types in the sequence:
num_spins (int): Required. The number of spins in the system.
hamiltonian (np.ndarray): Required for TimeEvolutionPulse and SoftPulse.
The Hamiltonian matrix to use during time evolution.
eigen_hamiltonian (Tuple[np.ndarray, np.ndarray]): Optional alternative to
hamiltonian for TimeEvolutionPulse. A tuple of (eigenvalues, eigenvectors)
of the Hamiltonian for more efficient time evolution.
nmr_parameters (NMRParameters): Required for SelectiveFrequencyPulse.
The NMR parameters containing chemical shifts.
calculation_parameters (NMRCalculationParameters): Required for
SelectiveFrequencyPulse.
The calculation parameters for converting shifts to frequencies.
Returns:
List[complex]: The expectation values.
List[float]: The measurement time of each value.
"""
Usage
The apply_pulse_sequence method is used by the pulse_solver in pulse_solver.py. It starts with and goes over each pulse P in the sequence to calculate . Finally the result is used to calculate .
The collect_expectation_values is used in the spin_echo notebook to perform a spin echo experiment. An inconvenience of this method is that the time evolutions need to be divided into many small time evolutions to have enough points.
Pulse Solver
The pulse solver allows you to simulate nmr experiments with custom pulse sequences and measurement operators. Unlike the standard NMR solver which assumes a simple pulse-acquire experiment, the pulse solver gives you full control over the initial pulse sequence and the observable being measured.
Basic Usage
To use the pulse solver, you need to:
- Set
solver_str="pulse_solver"inNMRSolverSettings - Optionally specify an
initial_pulse_sequenceinNMRCalculationParameters - Optionally specify a
measurement_operatorinNMRCalculationParameters
Here’s a complete working example:
from hqs_nmr_parameters.examples import molecules
from hqs_nmr import NMRCalculationParameters, calculate_spectrum, NMRSolverSettings
from hqs_nmr.pulse_sequence import NMRPulseSequence
from hqs_quantum_solver.spins import spin_x
# Load molecule from database
nmr_parameters = molecules["C2H6"].spin_system()
# Configure pulse solver
solver_settings = NMRSolverSettings(solver_str="pulse_solver")
calculation_params = NMRCalculationParameters(
field_T=11.7,
number_omegas=2000,
solver_settings=solver_settings,
initial_pulse_sequence=NMRPulseSequence().rotation_pulse(90, "x"),
measurement_operator=spin_x,
)
# Calculate spectrum
result = calculate_spectrum(nmr_parameters, calculation_params)
Default Behavior
If you don’t specify initial_pulse_sequence or measurement_operator, the pulse solver uses sensible defaults:
- Default pulse sequence: 90° rotation around the x-axis (
NMRPulseSequence().rotation_pulse(90, "x")) - Default measurement operator:
spin_x(measures the x-component of spin)
These defaults replicate the standard pulse-acquire NMR experiment.
Pulse Sequences
The pulse solver accepts any NMRPulseSequence object as the initial pulse sequence. You can create pulse sequences using method chaining, string notation, or a combination of both.
For more details on creating pulse sequences, see the NMRPulseSequence documentation.
Measurement Operators
The measurement_operator parameter specifies which physical observable is being measured at the end of the pulse sequence. This corresponds to the detector orientation in an actual NMR experiment.
Default Operator
By default, the pulse solver uses spin_x, which measures the x-component of the spin angular momentum. This is the standard detection mode for most NMR experiments.
Available Operators
You can use any operator function from hqs_quantum_solver.spins:
from hqs_quantum_solver.spins import spin_x, spin_y, spin_z, raising, lowering
# Measure along x-axis (default)
measurement_operator = spin_x
# Measure along y-axis
measurement_operator = spin_y
# Measure along z-axis
measurement_operator = spin_z
# Measure using the S+ operator
measurement_operator = raising
# Measure using the S- operator
measurement_operator = lowering
One can also define a custom operator:
from hqs_quantum_solver.spins import spin_x, spin_y
import numpy as np
def custom_op(site: int)-> np.ndarray:
return spin_y(site) + 2*spin_x(site)
measurement_operator = custom_op
Example with spin_y
Here’s a complete example using spin_y as the measurement operator:
from hqs_nmr_parameters.examples import molecules
from hqs_nmr import NMRCalculationParameters, calculate_spectrum, NMRSolverSettings
from hqs_nmr.pulse_sequence import NMRPulseSequence
from hqs_quantum_solver.spins import spin_y
nmr_parameters = molecules["cis-3-chloroacrylic_acid_exp"].spin_system()
settings = NMRSolverSettings(solver_str="pulse_solver")
calculation_params = NMRCalculationParameters(
field_T=11.7,
number_omegas=2000,
solver_settings=settings,
initial_pulse_sequence=NMRPulseSequence().rotation_pulse(90, "x"),
measurement_operator=spin_y,
)
spectrum = calculate_spectrum(nmr_parameters, calculation_params)
greens_function = calculate_greens_function(nmr_parameters, calculation_params)
Physical Interpretation
- spin_x: Detects magnetization along the x-axis of the rotating frame
- spin_y: Detects magnetization along the y-axis of the rotating frame
- spin_z: Detects magnetization along the z-axis (parallel to B₀)
The choice of measurement operator can affect which transitions are observable and their relative intensities.
The Global Phase adjustement
The pulse solver implements an automatic global phase adjustement to handle complex values when calculating transition matrix elements.
What It Is
When the pulse solver calculates transition matrix elements between energy eigenstates, these elements can be complex-valued. However, a spectral function is only well defined if the measurement operator is the hermitian conjugate of the pulse. Yet, the concept can be extended to other cases, but the spectral function may acquire a global phase, e.g. being negative, or the real part instead of imaginary part of the Greens functions. The global phase adaption tries to determine such a phase before the calculation. Note that this can not work in general, and sometimes misses cases where it could. In these cases the complex-valued matrix elements are passed through unchanged and the resulting spectrum may not be purely real; in that situation the Green’s function method can be used instead.
How It Works
This is implemented in pulse_solver.py.
If the transition matrix elements are purely real or purely imaginary, just use that. Otherwise:
- Calculate the global phase of all transition matrix elements:
global_phase = np.angle(np.sum(transition_matrix_elements)) - Remove this global phase by multiplying:
np.exp(-1j * average_phase) * transition_matrix_elements - Take only the real part of the result if the result is purely real, otherwise keep the complex values as they are.
When It’s Applied
This adjustement is automatically applied when:
- Transition matrix elements are complex-valued, AND
- The spectral function is real-valued (i.e., using
calculate_spectrum)
No user intervention is needed—the solver detects the appropriate conditions and applies the optimization automatically. If you’re calculating the Green’s function (using calculate_greens_function), the complex values are preserved as needed.
It is showcased in the pulse_solver_phase_adjustement notebook.
2D Pulse Solver
The pulse solver includes a specialized variant called pulse_solver_2d for 2D NMR experiments, particularly COSY (COrrelation SpectroscopY) experiments.
Overview
The 2D pulse solver extends the 1D pulse solver to handle two-dimensional spectral functions. Instead of measuring intensity as a function of a single frequency ω, the 2D solver produces intensity as a function of two frequencies (ω₁, ω₂), revealing correlations between different spins.
Key Differences from 1D
- Two frequency dimensions: Output is a 2D array with shape
(num_spins, number_omegas, number_omegas_2) - Two pulse sequences: The first pulse sequence (default 90° rotation around x) is applied to the initial density matrix, then the system evolves for a time t₁; the second pulse sequence (default 90° rotation around x) is applied and the system evolves for a time t₂ before measurement
- Default measurement operator:
spin_xin 2D (note: the 1D pulse solver defaults tospin_y) - Experiment type: Uses the default
experiment_type="pulse-acquire"withsolver_str="pulse_solver"; the 2D spectrum is obtained via the_calculate_2d_spectruminterface. Settingexperiment_type="COSY"instead selects the dedicatedreference_cosy_solver, which does not use pulse sequences.
Basic 2D Usage
Here’s a complete example of a 2D calculation with the pulse solver:
from hqs_nmr_parameters.examples import molecules
from hqs_nmr import NMRCalculationParameters, NMRSolverSettings
from hqs_nmr.pulse_sequence import NMRPulseSequence
from hqs_nmr._calculate_pulse_sequences import _calculate_2d_spectrum
nmr_parameters = molecules["C3H8"].spin_system()
solver_settings = NMRSolverSettings(solver_str="pulse_solver")
calculation_params = NMRCalculationParameters(
field_T=11.7,
number_omegas=200, # Frequency points for first dimension
number_omegas_2=200, # Frequency points for second dimension
solver_settings=solver_settings,
initial_pulse_sequence=NMRPulseSequence().rotation_pulse(90, "x"),
initial_pulse_sequence_2=NMRPulseSequence().rotation_pulse(90, "x"),
)
result = _calculate_2d_spectrum(nmr_parameters, calculation_params)
# result.spectrum.spin_contributions has shape (num_spins, 200, 200)
Default 2D Pulse Sequences
If you don’t specify initial_pulse_sequence or initial_pulse_sequence_2 for a 2D experiment, each defaults to:
NMRPulseSequence().rotation_pulse(90, "x")
This is the standard COSY-like pulse sequence: 90°ₓ - t₁ - 90°ₓ - t₂ - acquire FID.
Custom 2D Pulse Sequences
You can customize both pulse sequences via initial_pulse_sequence and initial_pulse_sequence_2:
from hqs_nmr.pulse_sequence import NMRPulseSequence
# Custom COSY-like sequences
pulse_sequence_1 = NMRPulseSequence().rotation_pulse(90, "x")
pulse_sequence_2 = NMRPulseSequence().rotation_pulse(45, "y")
calculation_params = NMRCalculationParameters(
field_T=11.7,
number_omegas=200,
number_omegas_2=200,
solver_settings=NMRSolverSettings(solver_str="pulse_solver"),
initial_pulse_sequence=pulse_sequence_1,
initial_pulse_sequence_2=pulse_sequence_2,
)
Interpreting 2D Results
The output spectrum has shape (num_spins, number_omegas, number_omegas_2), where:
- First dimension: Individual spin contributions
- Second dimension: Frequency ω₁ (associated with evolution period t₁)
- Third dimension: Frequency ω₂ (associated with evolution period t₂)
Diagonal peaks (ω₁ = ω₂) correspond to auto-correlation, while off-diagonal peaks indicate coupling between different spins.
Performance and Optimization
Cluster Size
The pulse solver uses the same clustering algorithm as the standard solver to handle large molecules. The key parameter is max_cluster_size in NMRSolverSettings:
# Default cluster size (good for most molecules)
settings = NMRSolverSettings(
solver_str="pulse_solver",
max_cluster_size=12
)
# Smaller clusters (faster but less accurate)
settings = NMRSolverSettings(
solver_str="pulse_solver",
max_cluster_size=9
)
# Larger clusters (more accurate but slower)
settings = NMRSolverSettings(
solver_str="pulse_solver",
max_cluster_size=15
)
For most molecules at high field strengths (>5 T), max_cluster_size=12 provides excellent accuracy with reasonable computation time.
Threshold Settings
The threshold_matrix_elements parameter controls which transition matrix elements are included in the calculation:
settings = NMRSolverSettings(
solver_str="pulse_solver",
threshold_matrix_elements=1e-4 # Default value
)
- Lower values (e.g.,
1e-6): More accurate but slower, includes more transitions - Higher values (e.g.,
1e-3): Faster but may miss weak transitions
The default value of 1e-4 is a good balance for most applications.
Performance Comparison
The pulse solver has similar performance to the standard NMR solver for for small molecules, but the standard hqs-nmr solver scales better when going to higher spin numbers. A detailed benchmark comparing both solvers is available in:
examples/pulse_sequence/benchmark_pulse_solver.py
This benchmark script:
- Compares execution time for molecules of different sizes
- Measures peak memory usage
- Validates that both solvers produce equivalent results
Typical performance characteristics:
- Small molecules (≤6 spins): ~0.01s difference
- Medium molecules (7-12 spins): Second to tens of seconds difference
- Large molecules (>12 spins): 30s slow down (with clustering)