Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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:

  1. Set solver_str="pulse_solver" in NMRSolverSettings
  2. Optionally specify an initial_pulse_sequence in NMRCalculationParameters
  3. Optionally specify a measurement_operator in NMRCalculationParameters

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:

  1. Calculate the global phase of all transition matrix elements: global_phase = np.angle(np.sum(transition_matrix_elements))
  2. Remove this global phase by multiplying: np.exp(-1j * average_phase) * transition_matrix_elements
  3. 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_x in 2D (note: the 1D pulse solver defaults to spin_y)
  • Experiment type: Uses the default experiment_type="pulse-acquire" with solver_str="pulse_solver"; the 2D spectrum is obtained via the _calculate_2d_spectrum interface. Setting experiment_type="COSY" instead selects the dedicated reference_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)