    Counting atoms faster: policy-based nuclear magnetic resonance pulse sequencing for atomic abundance measurement

present in the sample. From the peak frequencies (also                in the NMR), at timestep t. Intuitively, this phenomenon
known as chemical shifts), we infer the gyromagnetic ratios           should occur when roughly all spins in the system have been
γ of constituent spins. We use the exponential decay of the           coherently driven into the transverse plane, pointing in the
FIDs to derive the average T2 for the spins, and sample T1 s          same direction for a short period of time. This alignment
from a Gaussian distribution as a source of training data             causes a sharp and high-frequency jump in the observed
variability.                                                          magnetization, which we refer to as a chirp.
Next, we simulate a standard baseline one-dimensional                 To formalize our approach, we define a Markov Decision
NMR experiment using the identified gyromagnetic ratios.              Process (MDP), (S, A, Πsa , Γ, r), as follows:
At this stage, the frequencies of the peaks in the simulated
spectrum match those of the real samples. However, the in-               • The state space, S, is defined above.
tensities (peak heights) typically do not. We therefore adjust           • The action space, A, is the continuous interval cor-
the weights associated with each spin so that the simulated                responding to B1 ∈ [−0.2, 0.2] Tesla, with B0 = 1
peak heights align with those in the empirical spectrum.                   Tesla. This magnetic field configuration is inexpen-
These weights roughly represent the relative numbers of                    sive to implement relative to those typically used
each class of spin in the overall sample. By iterating this                in laboratory-grade NMR setups (generally ranging
process of matching the simulated spectrum to the measured                 from 2 to 45 Tesla for B0 and up to ∼ 0.1 Tesla for
spectrum, we arrive at a spin set whose composition closely                Helmholtz coil-pulsed B1 ) (Moser et al., 2017; Con-
aligns with the true samples’.                                             radi, 2024). Importantly, unlike setups with stronger
                                                                           B0 , this choice of parameters can be achieved inexpen-
3. Experiments                                                             sively using a Faraday coil3 for B1 and a neodymium-
                                                                           based permanent magnet for B0 (Barón et al., 2023).
In pursuit of a framework for counting atoms with NMR, we                • The policy Πsa denotes the learned distribution of ac-
explored and evaluated three reinforcement learning strate-                tions conditioned on the observed state.
gies which build on one another. The guiding idea behind                 • The discount factor Γ is set to 0.99.
our approach is to devise policies capable of manipulating               • The reward function at any timestep T is given by:
nuclear spins to squeeze the largest response possible out
of a given sample, under realistic environmental conditions.                                             2        2
                                                                                                                    
                                                                                         rT = max Mx,t      + My,t    .        (1)
                                                                                                0≤t≤T
The “loudness” or intensity of this signal is expected to be
directly proportional to the abundance of nuclear spins in a
sample (Nishimura, 1996). First, we ask whether an agent              To evaluate the efficacy of this approach, we train the PPO
trained to maximize this signal can perform better than a             model (as described above) for 5M timesteps of length
baseline NMR pulse. Second, we test whether a policy to               0.2 ms across 50 distinct environments with στ = 0.01.
quickly reset the system to its initial undisturbed state can         Each environment is initialized with a unique set of atomic
permit this maximum response to be quickly reproduced.                spins determined by the procedure outlined in Section 2.1.
Lastly, we demonstrate that the decision to reset the system          We repeat model training on 5 fixed random seeds.
can also be delegated to an agent.                                    Figure 1 plots the maximum magnetization observed for a
For all policy trainings in these experiments, we employed            collection of samples versus the concentration of caffeine
the Proximal Policy Optimization (PPO) algorithm (Schul-              in those samples, comparing the learned pulse against the
man et al., 2017) provided in Stable-Baselines3 (Raffin et al.,       standard 1D NMR pulse. The depicted relationship for
2021). Additional details regarding this approach are pro-            the learned policy is steeper and more monotone than that
vided in Appendix B.                                                  of the 1D NMR pulse, indicating that a more precise and
                                                                      direct relationship with concentration is achieved. The time
3.1. Model 1: Making Samples Chirp by Maximizing                      required for these maximum concentrations to be reached
     Transverse Magnetization                                         was roughly 16% of the time required for a 1D NMR pulse
                                                                      sequence to complete, representing a significant reduction.
In the first experiment, we aim to design a pulse which ac-           However, because the policy drives the sample to a state
curately and quickly determines the maximum achievable                of higher magnetization, it may require significantly more
magnetization response for a compound of interest. For-                  3
mally, we imagine placing a sample of material in a mild                   The field strength of a solenoid made of wound copper wire
                                                                      is µN I/L, with µ the magnetic permeability of air, I the cur-
background magnetic field along the z axis, and we reward             rent flowing through the wire, and N the number of coil turns in
                                           2        2
an agent for achieving large values of Mx,t  + My,t    , where        length L. For a 43-gauge enamel-wound copper wire of diameter
Mx,t and My,t are the net observed magnetizations in the x            ∼0.006 cm, the required current for a singly-wound coil to achieve
and y directions (the directions of the magnetic field sensors        a B1 of 0.2 Tesla is 9.7 Amperes, comparable to the current drawn
                                                                      by a home microwave.

                                                                  4
