levels#

torchspin.levels(sys, phi, theta, B, *, return_vectors=False, quadratic_zeeman=False)[source]#

Compute energy levels of a spin system vs magnetic field.

Parameters:
  • sys (SpinSystem) – Spin system specification.

  • phi (float | Tensor) – Azimuthal angle(s) of the static field in the molecular frame (radians). Scalar for a single orientation.

  • theta (float | Tensor) – Polar angle(s) of the static field in the molecular frame (radians). Scalar for a single orientation.

  • B (Tensor | list | tuple) –

    Magnetic field values (mT). Can be:

    • A 1-D tensor / list of field values, used directly.

    • A 2-element [B_min, B_max] range, expanded to 101 points (matching MATLAB levels.m default).

    • A scalar tensor or float giving a single field value.

  • return_vectors (bool) – If True, also return eigenvector array V.

  • quadratic_zeeman (bool) – If True, include the field-quadratic and field-cubic higher-order Zeeman terms of sys.Ham (orders lB = 2, 3), so that H(B) = H0 − B·muzL + B²·n·G2·n + B³·G3[n,n,n] equals ham(sys, B·n) (same meaning as Options.QuadraticZeeman). Default False: these terms are dropped, as before.

Returns:

  • B_axis – Field axis, shape (nB,) (mT).

  • E – Eigenvalues (MHz), sorted ascending, shape (nStates, nB) for a single orientation, or (nOri, nStates, nB) for multiple orientations.

  • V ((only when return_vectors=True )) – Eigenvectors, shape (nStates, nStates, nB) for single orientation, or (nOri, nStates, nStates, nB) for multiple orientations.

Return type:

tuple[Tensor, Tensor] | tuple[Tensor, Tensor, Tensor]

Notes

The Hamiltonian is:

H(B) = H0 - B * muzL
muzL = sin(theta)*cos(phi)*mux + sin(theta)*sin(phi)*muy + cos(theta)*muz

where (phi, theta) define the static field direction in the molecular frame (same convention as resfields()).