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

At each timestep, the agent observes the current state st , chooses an action at ∼ πθ (·|st ) according to its policy, receives a
reward rt = r(st , at ), and transitions to a new state st+1 ∼ P(·|st , at ). The goal of the agent is to learn parameter values
that maximize the expected return, defined as the discounted sum of rewards:
                                                            "∞        #
                                                             X
                                                                  t
                                                        Eπ      Γ rt .                                                        (7)
                                                             t=0

The structure provided by an MDP allows for the development of gradient-based policy optimization methods such as
the Proximal Policy Optimization (PPO) agent developed in this work. The agent’s behavior is iteratively improved by
updating a control policy which samples its actions probabilistically. The policy is learned in a data-driven fashion through
interactions with the environment (of which it observes a fraction, the state-space S) and estimating updates. As reward
function will generally be noisy or sparse, it can be difficult to estimate the payoff attributable to a given state. In the PPO
agent we use in this analysis, we model it by a value function Vϕ , which has learnable parameters ϕ. To guide learning, it
estimates the expected future return R starting from state s. Formally,
                                                            "∞                  #
                                                             X
                                                                    t
                                              Vϕ (s) ≈ Eπ         Γ rt s0 = s .                                               (8)
                                                              t=0

The parameters ϕ are learned by minimizing the squared difference between the predicted value and the observed return:
                                                   1 X                   2
                                         Lvalue =           Vϕ (st ) − R̂t ,                                        (9)
                                                   M t

where the estimated return R̂t is defined as the value of the state at time t plus some estimate of advantage Ât :
                                                      R̂t = Vϕ (st ) + Ât .                                                (10)
In our PPO implementation, the advantage Ât is estimated from temporal differences δt using Generalized Advantage
Estimation (GAE) (Schulman et al., 2018). This means that after collecting experience, we compute the following recursively,
using knowledge of future states:
                                                δt = rt + ΓVϕ (st+1 ) − Vϕ (st )                                            (11)
                                               Ât = δt + ΓλÂt+1                                                           (12)
where λ is a tunable parameter. These definitions enable the basic training pipeline we outline in Algorithm 2, which learns
ϕ and θ as defined in Algorithm 1.
We list below the major similarities of the Stable-Baselines3 PPO implementation with respect to the original work
in (Schulman et al., 2017).

   • On-policy. Policy is updated based on the behavior of the very policy being trained, not on the behaviors of other
     policies. Samples are not reused as in off-policy approaches.
   • Clipping to constrain updates. Gradient clipping for actor by default, optional gradient clipping for value function
     (disabled by default). No trust region, no use of second-order derivatives or constraints to tune or stabilize.
   • Advantage estimation. By default, uses Generalized Advantage Estimation (GAE) (Schulman et al., 2018).
   • Actor-critic. Policy network πθ is distinct and separate from value network Vϕ .
   • Environment handling. Adapts to vectorized environments and allows for both continuous and discrete action spaces.

We emphasize that our policy network is a simple dense network, see Algorithm 1. We did not consider timeseries variants
of this architecture which apply LSTMs or similar recurrent approaches to condition model actions on the history of the
state. We also note that the Bloch equations (written in Appendix A) are local in time t, so that complete information about
the state at any given time is sufficient to infer the next timestep (i.e. the dynamics are Hamiltonian). Within those equations,
we effectively mask the Mz , M0 , T1 , T2 , and γ values from the agent, so a non-recurrent architecture is not guaranteed
to perfectly reconstruct or manipulate these dynamics. Nevertheless, it is interesting that our approach is performant
as-is without such architectural considerations. Future work will investigate whether recurrent approaches improve the
performance above this baseline.

                                                               13
