Spin simulations in Julia

Julia is a programming language designed for numerical and scientific computation. It combined the convenience of an interpreted scripting language with the efficiency of compiled ones like C, Fortran, or C++. The goal of this effort is to provide a very simple and lightweight infrastructure for quick, but accurate, quantum mechanical simulations of small spin systems. NMR.jl represents spin operators in a Hilbert basis formed from the Kronecker
products of the individual \(|\alpha\rangle, |\beta\rangle,\dots\) spin states. It uses sparse matrices wherever possible. In this way, simulations of up to ten spins 1/2 tend to run in a matter of minutes. Larger spin systems, up to about twenty spins or so, are possible, but require significant computational resources.

Here is an example of such a calculation. These commands are meant to be run either as a Julia script, line-by-line on the Julia command line, or, most usefully, as an IJulia notebook in a web browser. If you have not done so, install the current version of NMR.jl in your Julia environment by running the Julia command


This will take a few moments. Once it’s done, you are ready to go.

First, we create a clean workspace and declare usage of the NMR modules SpinSim and PauliMatrix:

using NMR.SpinSim;
using NMR.PauliMatrix;

Then, we need to define the properties of the spin system. In the present case, we are modelling hydroxy-ethyl propionate (HEP). The molecule looks like this:

There are five protons on the propionate side, three methyl (a) and two methylene (b), and two pairs of methylene protons on the ethyl (right) side (c) and (d). We assume the hydroxyl proton to be in fast exchange with the solvent. In addition, there is a 13C label at the carboxylate site (x). Therefore, we are simulating a ten spin system overall.
The parameters are


The Zeeman Hamiltonian (in the rotating frame) is defined by taking into account the chemical shifts of the protons and the Z component of the spin operators:

   δd*(SpinOp(10,Sz,9)+SpinOp(10,Sz,10)) ;

In addition, we need the J coupling Hamiltonian. We use strong coupling between chemically equivalent protons, and weak coupling in all other cases:


2*pi*Jab*sum([OpJweak(10,k,l) for k=1:3,l=4:5])+
2*pi*Jcd*sum([OpJweak(10,k,l) for k=7:8,l=9:10]);

We also need some of the total spin operators, such as \(F_z\) and \(F_y\):

FzH=sum([SpinOp(10,Sz,k) for k=1:5])+sum([SpinOp(10,Sz,k) for k=7:10])
FyH=sum([SpinOp(10,Sy,k) for k=1:5])+sum([SpinOp(10,Sy,k) for k=7:10])
FmH=sum([SpinOp(10,Sm,k) for k=1:5])+sum([SpinOp(10,Sm,k) for k=7:10])
Fz=sum([SpinOp(10,Sz,k) for k=1:10])

Here, FzH is the \(F_z\) operator for the protons only, where as Fz applies
to all spins, including 13C.


The Utz lab is a research group within the School of Chemistry at the University of Southampton. It is one of five research groups that together make up the magnetic resonance research section at the University (MagRes@Soton).

Our research mainly concerns the study of live systems (cells, tissues, and small animals) by nuclear magnetic resonance. We are developing miniaturised “Lab-on-a-chip” devices for the culture of such biological systems, and integrate them with in-situ observation by nuclear magnetic resonance. This combination of microfluidic culture technology with advanced nuclear magnetic resonance techniques provides unique insight into the metabolic processes of life. Our work is motivated by applications across the life sciences, in particular in drug discovery and safety testing, and in the development of disease models.

Other research areas include the study of complex materials such as hydrogels, solid polymers, and composites. We are also actively contributing to an effort to develop a novel approach to neurostimulation of the spinal cord.

Ali Yilmaz

Ali obtained his PhD from the University of Copenhagen in 2012, working on NMR metabolomic observations. After a first postdoc at SUNY Buffalo, he joined the Utz research team at Southampton in 2014. His main research interests include microfluidic systems, metabolomics, and micro-fabrication techniques for biocompatible microdevices.



to the Utz research group’s blog site. We post recent advances, insights, and news about our activities here. Thank you for visiting!