# Spinach (software)

Spinach is an open-source magnetic resonance simulation package initially released in 2011 and continuously updated since. The package is written in Matlab and makes use of the built-in parallel computing and GPU interfaces of Matlab.
The name of the package whimsically refers to the physical concept of spin and to Popeye the Sailor who, in the eponymous comic books, becomes stronger after consuming spinach.

## Overview

Spinach implements magnetic resonance spectroscopy and imaging simulations by solving the equation of motion for the density matrix $\mathbf {\rho } \left(t\right)$ in the time domain:
${\begin{matrix}{\frac {\partial }{\partial t}}\mathbf {\rho } \left(t\right)=-i\mathbf {L} \left(t\right)\mathbf {\rho } \left(t\right)\\\Downarrow \\\mathbf {\rho } \left(t+dt\right)=\exp \left[-i\mathbf {L} \left(t\right)dt\right]\mathbf {\rho } \left(t\right)\\\end{matrix}}$
where the Liouvillian superoperator $\mathbf {L} \left(t\right)$ is a sum of the Hamiltonian commutation superoperator $\mathbf {H} \left(t\right)$, relaxation superoperator $\mathbf {R}$, kinetics superoperator $\mathbf {K}$, and potentially other terms that govern spatial dynamics and coupling to other degrees of freedom:
$\mathbf {L} \left(t\right)=\mathbf {H} \left(t\right)+i\mathbf {R} +i\mathbf {K} +...$
Computational efficiency is achieved through the use of reduced state spaces, sparse matrix arithmetic, on-the-fly trajectory analysis, and dynamic parallelization.

## Standard functionality

As of 2023, Spinach is cited in over 300 academic publications. According to the documentation and academic papers citing its features, the most recent version 2.8 of the package performs:
* Time-domain nuclear magnetic resonance (NMR) simulations of:
** Standard NMR experiments (DEPT, COSY, NOESY, HSQC, TOCSY, etc.).
** Protein and nucleic acid NMR experiments (HNCA, HNCOCA, HNCO, etc.).
** Magic angle spinning NMR experiments (CP-MAS, MQMAS, WISE, etc.).
** Experiments involving residual dipolar coupling and other residual order effects.
** Zero- and ultra-low field experiments, including Earth's field NMR.
** Nuclear quadrupole resonance, including overtone NMR.
* Time-domain magnetic resonance imaging (MRI) simulations, including:
** Standard and user-specified imaging sequences.
** Diffusion coefficient and diffusion tensor imaging.
** Three-dimensional point-resolved NMR spectroscopy.
** Ultrafast spatially encoded NMR spectroscopy.
* Time-domain electron spin resonance (ESR) simulations of:
** Standard pulsed ESR experiments (HYSCORE, ENDOR, ESEEM, etc.).
** Pulsed dipolar spectroscopy (DEER, RIDME, etc.).
** Dynamic nuclear polarization for static and spinning samples.
Common models of spin relaxation (Redfield theory, stochastic Liouville equation, Lindblad theory) and chemical kinetics are supported, and a library of powder averaging grids is included with the package.

## Optimal control module

Spinach contains an implementation the gradient ascent pulse engineering (GRAPE) algorithm for quantum optimal control. The documentation and the book describing the optimal control module of the package list the following features:
* L-BFGS quasi-Newton and Newton-Raphson GRAPE optimizers.
* Spin system trajectory analysis by coherence and correlation order.
* Spectrogram analysis of the pulse waveform.
* Prefixes, suffixes, keyholes, and freeze masks.
* Optimization of cooperative pulses and phase cycles.
* Waveform penalty functionals and instrument response.
Dissipative background evolution generators and control operators are supported, as well as ensemble control over distributions in common instrument calibration parameters, such as control channel power and offset.