Technical Articles

Creating Spinach, an Open-Source MATLAB Library for Simulating Spin Dynamics

By Professor Ilya Kuprov, University of Southampton


Much of the technology we use every day, including magnetic resonance imaging, electric motors, and magnetic data storage, depends on magnetic interactions. These proceed from a quantum mechanical quantity called spin. An elementary particle carrying spin is the smallest permanent magnet possible—a quantum of magnetic material. All magnetic processes can be traced back to spin.

Chemists, material scientists, physicists, and other researchers are all interested in designing materials or experiments that manipulate magnetic properties in various ways. To do this, they need a way to simulate spin dynamics. In response to this need, the Spin Dynamics Group developed Spinach, a MATLAB® based open-source library for spin dynamics simulation (Figure 1).

Figure 1. A diagram of Spinach capabilities.

Figure 1. A diagram of Spinach capabilities. The library covers quantum mechanics of magnetism and the related areas of chemistry and medical imaging.

We used MATLAB because we wanted to minimize the time our team spent on development—CPU time is cheap, but brain time is very expensive. Of the many languages we could have used, MATLAB requires by far the least amount of time to develop and debug the code for computational physics. Its native support for sparse matrices, as well as GPU and parallelization, saved us years of brain time.

Solving the Exponential Scaling Problem

Exponential complexity scaling has always been the principal difficulty in simulating quantum processes. The same is true for spin physics: A single spin has just two quantum states, spin-up and spin-down. Two spins have four possible combinations, three spins have eight, and so on. The number of combinations determines the size of the matrices needed to perform the calculations. A relatively modest simulation involving just 20 spins would require a matrix with 220 (or approximately 1 million) rows and columns—an exceedingly difficult thing to even store. Before 2007, all algorithms for spin dynamics simulations scaled exponentially, severely limiting their usefulness.

In a 2007 paper (DOI: 10.1016/j.jmr.2007.09.014), we described an algorithm for simulating spin dynamics that scaled polynomially rather than exponentially. The paper, authored by Nicola Wagner-Rundell, Peter Hore, and myself, led to the first implementation of the algorithm, and consequently, of Spinach. We managed to show that most quantum states in large spin systems are not essential for magnetic resonance simulations and can be dropped without compromising accuracy. While this approximation made it possible to develop an algorithm that scaled polynomially, implementing the algorithm in code was daunting. The underlying mathematics was exceptionally complex and involved exotic representations of Lie algebras. Even the utility functions were difficult to code from scratch. For example, we needed a function for implicit indexing, in which one array is indexed with another array. We also needed a function for dimension reordering that could, for example, take a 25-dimensional array and swap the 21st dimension with the 11th.

As chemists, we dreaded having to write and debug these functions, but when we looked at MATLAB documentation, we found that highly optimized versions of all the functions we needed—implicit indexing, dimension reordering, manipulating sparse arrays, and more—were already available in MATLAB. To our surprise, the functions that we thought would take a year to write required just a few lines of MATLAB.

From the start, native support for sparse matrices in MATLAB made life easier—most operators in spin physics are very sparse. We found that with every new MATLAB release we installed, sparse indexing speed improved, and so did the speed of our simulations.

Accelerating Simulations with GPUs and Parallel Computing

Further acceleration was achieved when we started using GPUs and parallel processing. When we asked MathWorks engineers to implement sparse GPU operations, their response was “No problem—we’ll do that for you.” They did, and we clocked another three orders of magnitude in speed by using NVIDIA® Tesla® cards with Parallel Computing Toolbox™ on a large cluster running MATLAB Parallel Server™ that the MathWorks team configured for us. Most Spinach functions are now parallelized, and the library will run on as many cores as are available.

Typical Use Cases for Spinach

Spinach simulations are most often used for two research objectives: interpreting experimental results and identifying promising lines of experimentation. Chemists often ask us to interpret the results of their experiments to determine what is happening within the chemical system that they are studying. We set up a simulation and then adjust the parameters to fit the simulation to the experimental results. We then run system trajectory analysis in MATLAB to see exactly what is going on.

Chemists also use simulation to reduce the need for costly experimental processes. Two years ago, for example, we worked with a team of chemists who were looking at ways to insert fluorine atoms into proline, a small biomolecule. There were 128 possible fluorination patterns to test. Performing 128 instances of organic synthesis, where each stage could take weeks, would have been prohibitively costly and time-consuming. We were asked to find out which of the 128 patterns had the narrowest spectral line for the fluorine-19 isotope in a strong magnetic field (Figure 2). After running simulations on our local supercomputer, we found the optimal combination. When the chemists synthesized this pattern, it produced the required narrow line—we saved them years of experimental time (DOI: 10.1021/acs.joc.8b02920).

Figure 2. Spectral signal widths for three quantum states in a 13C-19F spin system in a large protein as a function of the applied magnetic field.

Figure 2. Spectral signal widths for three quantum states in a 13C-19F spin system in a large protein as a function of the applied magnetic field. 

In another recent project, we simulated dozens of fluorinated amino acids and nucleotides in MATLAB with Spinach to find the one that produced the longest lifetime for a particular quantum state. The resulting improvement was so dramatic that our paper went straight to Nature Methods, a prestigious chemistry journal (DOI: 10.1038/s41592-019-0334-x).

Deep Neural Networks and Other Enhancements

The Spinach library is continuously updated, both by us and by our collaborators around the world. One area of active development is deep learning. We are using Spinach to generate training data for neural networks that can process double electron-electron resonance (DEER) data. Experimental DEER data is expensive to generate, but Spinach produced realistic simulations very rapidly (Figure 3). We added noise and distortions to mimic real experimental data. Another high-profile publication, this time in Science Advances, (DOI: 10.1126/sciadv.aat5218), was the result.

Figure 3. A schematic of DEERNet, a deep neural network infrastructure written in MATLAB with Statistics and Machine Learning Toolbox to solve a well-known structural biology problem: distance measurement between unpaired electrons.

Figure 3. A schematic of DEERNet, a deep neural network infrastructure written in MATLAB with Statistics and Machine Learning Toolbox to solve a well-known structural biology problem: distance measurement between unpaired electrons. The fascinating aspect is that the ability to simulate a quantum mechanical process is apparently being turned directly into the ability to interpret experimental data without fitting.

In the next few years, we expect to release a version of Spinach capable of simulating all known magnetic resonance processes. By that point, development of the Spinach kernel will be complete, but the number and variety of applications for the library remains limitless.

Acknowledgement

I would like to thank MathWorks engineers Alison Eele, Jos Martin, and Raymond Norris for their invaluable assistance in implementing GPU operations and configuring a parallel computing cluster for our team.

University of Southampton is among the nearly 1300 universities worldwide that provide campus-wide access to MATLAB and Simulink. With a Campus-Wide License, researchers, faculty, and students have access to a common configuration of products, at the latest release level, for use anywhere—in the classroom, at home, in the lab, or in the field.

About the Author

Ilya Kuprov is an Associate Professor of Chemical Physics at the University of Southampton. He was previously an EPSRC Early Career Fellow at the University of Oxford, a Lecturer in Physical Chemistry at the University of Durham, and a Fellow by Examination at Magdalen College, Oxford. His research focuses on computational methods in quantum theory.

Published 2019

View Articles for Related Capabilities