Optimal control for one-qubit quantum sensing

Quantum systems can be exquisite sensors thanks to their sensitivity to external perturbations. This same characteristic also makes them fragile to external noise. Quantum control can tackle the challenge of protecting quantum sensors from environmental noise, while leaving their strong coupling to the target field to be measured. As the compromise between these two conflicting requirements does not always have an intuitive solution, optimal control based on numerical search could prove very effective. Here we adapt optimal control theory to the quantum sensing scenario, by introducing a cost function that, unlike the usual fidelity of operation, correctly takes into account both the unknown field to be measured and the environmental noise. We experimentally implement this novel control paradigm using a Nitrogen Vacancy center in diamond, finding improved sensitivity to a broad set of time varying fields. The demonstrated robustness and efficiency of the numerical optimization, as well as the sensitivity advantaged it bestows, will prove beneficial to many quantum sensing applications.


I. INTRODUCTION
Quantum control has been demonstrated to be a crucial tool both in quantum information processing [1] and in quantum sensing [2,3] on a variety of experimental platforms, ranging from trapped ions [4,5], to ultracold atoms [6,7], superconducting qubits [8,9], as well as nuclear [10,11] and electronic spin qubits [12,13]. Quantum sensing poses peculiar challenges to control, as sensor qubits need to interact strongly with the target field to be probed, but this also leads to undesired coupling with external noise of the same nature of the target field, which often gives rise to either energy losses or decoherence. A paradigmatic scenario is when one wants to measure a frequency shift of a spin qubit sensor, as due to a magnetic field, in the presence of magnetic dephasing noise.
Typically, the optimal control problem involves the search for the optimal transformation that, given a system Hamiltonian H dependent on a set of time-dependent control fields, drives the system from an initial state into a target state, whose desired properties are expressed by a cost function F that one wants to minimize. Often this means maximizing the fidelity of the unitary operation, which describes this transformation, with the desired one.
The goal of quantum sensing is however different. Since in principle we do not have any a priori knowledge of the external field to be measured, we do not know what is the expected unitary dynamics, and thus we cannot use the fidelity to optimize control. In addition, quantum sensing is usually concerned with optimizing sensitivity, a quantity that intrinsically * Electronic address: fabbri@lens.unifi.it includes noise, also arising from the external environment.
Here we devise and experimentally demonstrate a robust and efficient scheme for optimal control of a sensing qubit, which enhances its sensitivity as a probe of time-varying target fields. To this purpose, we use an unconventional optimization metric, the sensitivity, and develop a practical way of computing it (which allows for fast numerical searches). Furthermore our search method does include in the cost metric itself the presence of an environment and consequent decoherence induced on the qubit. While optimal control has been used before for sensing [31,32], the optimization was only targeted at improving the control fidelity and bandwidth, not the sensitivity itself.
We tackle here the complex task of measuring multichromatic AC target fields, and different significant waveforms, such as trains of magnetic impulses, which are relevant for applications in biology, physiology, and neuroscience [33][34][35][36]. We show that in these cases optimal control demonstrates better performance than traditional dynamical decoupling, since it allows for both a larger accumulation of the spin phase that encodes the field information, and for an improved compensation of environment-induced decoherence, thus boosting qubit's sensitivity and enabling detection of very weak magnetic fields.

II. OPTIMAL CONTROL OF A QUBIT SENSOR WITH DEPHASING NOISE.
We use the electron spin states of a negatively-charged nitrogen-vacancy center (NV) in diamond as a sensing qubit of time-varying magnetic fields in the presence of magnetic noise, which induces dephasing of the sensing qubit. While the NV center forms a spin S = 1, a static bias magnetic field removes the degeneracy of levels with spin projection S z = ±1, and a microwave excitation selectively addresses the S z = 0 → −1 transition, therefore the NV center can be effectively described as a single two-level system [63]. The electron spin qubit can be prepared in a well defined initial state, coherently manipulated, and read out [37].
We can model the coupling of the spin qubit with a general, FIG. 1: One-qubit optimization strategy. (a) The electronic spin of a single NV center is optically initialized in the |0 state, and read out at the end of the sensing period, by means of a confocal microscope. An antenna delivers both the resonant control field (in blue) and the target magnetic field to be measured (in red) in the proximity of the spin qubit. (b) Illustration of the optimization protocol. The starting point is an initial guess for the control sequence described by a modulation function y 0 n (t), which depends on a given number of parameters. While in the paper we consider more general cases, for the sake of simplicity the central panel shows a search in a two-parameter space (sensing time T and phase shift α) for a Carr-Purcell (CP) control sequence used to detect a monochromatic AC field b(t) = b cos(2πν0t + α), with frequency ν0 = 20.5 kHz, and unknown amplitude b to be measured. The map represents the experimentally-measured inverse sensitivity E = C/η (see text). The algorithm computes the sensitivity η under the initial control sequence, then produces and evaluates a number of other trial points y (i) n (t) and moves in the multidimensional parameter space, until global convergence is reached. The final point, described by y opt n , represents the optimal control sequence. time-dependent, external field b(t) = b f (t) to be measured, and the coupling with noise, through the Hamiltonian where β(t) is a stochastic variable with power spectral density S(ω) in the frequency domain, γ = 2.81 × 10 4 Hz/µT is the NV gyromagnetic ratio, and σ z is the z component of the spin operator,ẑ being the NV symmetry axis. Performing metrology means reaching a compromise between two conflicting tasks, i.e., minimizing the noise effects while maximizing the signal stemming from the field, during the sensing time. Here, in particular, we assume to know the temporal dependence f (t) of the field, and we aim at measuring its amplitude b (we are thus interested in a parameter estimation task). While different control strategies can be used for sensing, here we consider control via pulsed dynamical decoupling, which is realized with series of π-pulses that repeatedly flip the spin, thus reversing its evolution [64]. The control field can be thus described by a modulation function y n (t), with a sign switch at the position of each π pulse, indicating the direction of time evolution, forward or backward. The squared Fourier transform of y n (t) defines the filter control function Y n,T (ω).
The phase accumulated by the spin during the sensing time T , under the action of the control field is To read out the phase due to the target field, we embed the control sequence within a Ramsey interferometer, which enables the mapping of the phase accumulated into observable population of the spin projection S z = −1.
As said, during the sensing process, the sensor qubit is also subject to noise. In the case of the NV center, this is mainly due to the nuclear spin bath that generates a stochastic timevarying field. Therefore, the qubit acquires a random phase during its coherent evolution, which leads to a reduction of the observed population.
The state of the qubit after the sensing process is described, as in a Ramsey interferometer, by population and coherence of the density matrix where χ n (T ) is temporal coherence function, describing noise-induced decoherence, that also depends on the control field through Y n,T (ω) Thus, a projective measurement on the σ x basis, |± = (|0 ± |1 )/ √ 2) gives a signal To assess the quality of parameter estimation, as achievable under a given control protocol and within the experimental constraints, we can evaluate the Fisher information (FI) [38,39] associated with the measurement, Here are conditional probabilities of obtaining x as measurement result for a given field b over N repeated measurements, E x being the measurement estimator and ρ the density matrix of each independent copy of the system. Sensitivity, that is, the minimum detectable signal per unit-time, is simply related to the Fisher information by where T = N T is the total sensing experiment time. For the one-qubit sensing schemes we are considering, this reduces to This is indeed the cost function that we want to minimize. In practice, for a given field b(t), we are searching for the optimal control field that steers the spin trajectory of the electronic spin on the Bloch sphere in such a way that, while the accumulated phase ϕ n (T ) is maximized, the effect of non-markovian noise described by χ n (T ) is minimized.
To this purpose, we have designed a direct and fast search method that looks for the optimal modulation function y opt n (t) that minimizes the cost function η. We have investigated various multi-dimensional parameter spaces, up to dimension M = 51, and analyzed which optimization parameters (e.g., total sensing time, π-pulse positions, signal phase, signal trigger time) do provide the largest improvement without requiring excessive computational resources, as we will detail in the following. The constraints of the parameter space are chosen to describe realistic experimental condition. The search of the optimal control field is performed by means of a Simplex (Nelder Mead) minimization numerical algorithm that allows for reaching global convergence in the parameter space, as illustrated in Fig. 1b. The method requires a precise knowledge of the temporal coherence function of the electronic spin sensor, which depends on the noise spectrum induced by its spin bath, as detailed in the Method section V B.

III. RESULTS
The experiment is sketched in Fig. 1a (see also Method Sec. V A). The electronic spin of a single NV centre is optically initialized and read out by means of a confocal microscope. The spin qubit is coherently controlled with a resonant field, and radiated with the off-resonance time-varying target magnetic field to be measured. We obtain sensitivity of the spin qubit to the target field by sweeping the amplitude of the magnetic field and measuring the slope of the signal s(T ) = 1 + e −χn(T ) cos(φ n b) /2 at the points of maximum slope (where s = 0.5), as shown in Fig. 2a. From this quantity we extract the experimental observable E = max{∂ b s}/ √ T . Following Eq. 8, this experimental observable is simply related to sensitivity through the relation E = C/η, where C is the only calibration constant, independent of the target field strength and of the control sequence (see Method Sec. V D).

A. Optimized sensing of an oscillating field
We first focus on the simple case of monochromatic sinusoidal signals b(t) = b cos(2πνt + α), in a rather wide frequency range, ν = 20 − 125 kHz (see Supplemental Material). We start from a common pulsed dynamical decoupling sequence, the Carr-Purcell (CP) multi-pulse sequence, originally devised in nuclear magnetic resonance [40,41], which has been demonstrated to extend the qubit's coherence [42] and has been successfully employed in sensing to measure monochromatic AC magnetic fields (see, e.g., [43][44][45][46][47][48]). CP is composed by n π-pulses, equally spaced by τ = T /n, which periodically flip the spin qubit. This kind of sequence is highly selective in frequency: its filter function Y n,T (ω) is indeed peaked at ν = 1/(2τ ) [65]. In the case of interest, the qubit is subject to colored noise due to a nuclear spin bath in diamond, where the main component is due to Carbon-13. If the target signal has frequency close to the center of the noise spectrum, CP control may be not the best choice, since the sequence achieving noise cancellation also leads to a significant attenuation of the signal to be measured.
As a warmup for the full optimization, we optimize the control over a restricted space of two parameters, the sensing time T , and the initial phase shift α, with a fixed number of pulses, n = 8. First, fixing α = 0 we find the optimal sensing time as a function of the AC frequency, as reported in Fig. 2b. Taking into account decoherence effects appreciably modifies the optimal sensing time (purple curve), compared to the results obtained in the absence of noise sources (black curve), where the optimization routine recovers the expected analytic solutions T opt = n/(2ν). Then, we optimize both T and α. To evaluate the global convergence of the optimization, we have also mapped 1/η in the two-dimensional (2D) parameter space (T, α). Figure 1b shows this map for an AC field of frequency ν 0 = 20.5 kHz. This allows the results of the optimization to be compared with the brute-force approach of an extensive search in the parameters space. The full optimization of the two parameters, including noise effects, is able to find the global minimum of sensitivity (the optimized a.u. parameters are T = 216 µs and α = 102 • , corresponding to an initial delay time t 0 = 0.28/ν 0 of the control sequence). Figure 2c shows some cuts of the previous 2D-map as a function of T , with α = 0 (red solid line), and with α = 102 • (the optimal value resulting from the numerical search, blue line). Using Eq. 8, we also calculate the experimental observable E = C/η (left side vertical axis in Fig. 2c), which can be directly compared with the experimental findings at fixed α = 0 (red dots), and with α = 102 • (blue dots). We find good agreement of the experiments with the results of the optimization. We also remark that even in the simple case of one parameter optimization, including the noise effects yields a different optimal sensing time than what calculated in the absence of noise (T = n/(2ν 0 ), gray vertical dashed line), and this is also reflected in the observed experimental peak of E -vs -T .
B. Optimized sensing of multitone AC signals.
We then tackle the more complex task of measuring arbitrary time-dependent signals. We consider multitone magnetic fields, in the form b where m is the number of Fourier components, b i = b w i their amplitudes (with i w i = 1), ν i their frequencies, and α i the initial phases. We employ our optimization tool to engineer optimal control sequences of non-equidistant π-pulses that may extract information from multitone target signals, while refocusing spin dephasing better than common dynamical decoupling solutions.
As said, common multipulse control sequences like CP are in general highly selective in frequency. For this reason, these sequences may exhibit sub-optimal performances when probing a multitone target field, due to attenuation of some frequency components. In addition, increasing the interrogation time to enable a larger phase accumulation, thus improving measurement sensitivity, also further narrows the width of the filter function Y n,T as ∼ 1/(T ) [65]. If the magnetometry task consists in measuring the signal amplitude of a spectrally characterized source as we are considering here, CP collects information about mostly one frequency component at a given sensing time. When fixing the number of pulses n and sweeping the total time T = nτ , the phase accumulated by the spin qubit sensor under CP control reflects the spectral composition of the signal, showing peaks at times τ i = 1/(2ν i ). This is exemplified in Fig. 3b, where we consider a field made of m = 3 Fourier components under a CP train of n = 50 pulses (green solid line). However, the sensor's decoherence influences the final sensitivity by suppressing the response of one of the three frequency components, as shown both in theory and in experiment (green solid line and yellow dots in Fig. 3c).
For this kind of scenario, optimal control strategies offer a key advantage. Optimal control can indeed be exploited to find optimal distributions of the π-pulse positions. Sequences of non-equally distributed pulse spacings, devised by means of analytical models, have been indeed demonstrated to correct for selectivity of CP in certain cases [49][50][51]. In the case of multitone AC signals to be measured, such sequences enable to simultaneously collect signal from all the various frequency components thus achieving a faster phase accumulation. The results of optimization for the multitone field considered above are shown in Fig. 3 [66]. For the optimization, we keep the number of pulses fixed to n = 50, and optimize all the π-pulse positions and the initial phase (α i = α), in all 51 free parameters. We impose the time intervals around to each π-pulse to be symmetric with respect to the pulse position in order to ensure cancellation of static noise and better refocusing of low-frequency noise (see Method Sec. V D). As shown in Fig. 3b, the optimization method leads to a remarkable improvement in the accumulated phase per unit field amplitude φ n (blue squares) compared to CP (green line), over an extremely wide range of sensing times. The overall-optimal control sequence (obtained with sensing time T = 187 µs, and phase shift α = 0.3) realizes a sensitivity η best opt = 12 nT/ √ Hz. Since each of our sensing experiments is typically obtained by averaging over N = 2 × 10 5 measurement shots, the optimized control sequence enables the measurement of a local field of 2 nT. The improvement in sensitivity is almost two orders of magnitude compared to sensitivity of CP (η CP = 82.6 mT/ √ Hz) at the same sensing time. We remark that the best sensitivity obtained with CP control is still a factor of 1.75 worse than the best sensitivity achieved with the optimized control (η best CP = 21 nT/ √ Hz), and with an acquisition time (T = 260 µs) that is 40% longer than the optimal sequence. In addition, optimized control is able to achieve the same η best CP three times faster then CP (T = 75 µs, compared to T = 260 µs, see black arrow in Fig. 3c). Thus, optimal control tools have allowed to obtain both a remarkable enhancement of sensitivity and a speed-up of the measurement of multitone AC fields.

C. Optimized sensing of trains of magnetic impulses.
We have applied optimal control to the different scenario where the target magnetic field is a train of impulses. This is in general the case of the temporal shape of electric and magnetic fields associated to cardiac, neural, and nervous activities of human and animal organs [33][34][35][36]. For this kind of application, the NV sensors may offer the remarkable advantages of subcellular spatial resolution, in addition to high sensitivity, and biocompatibility [52].
As illustrative models for these biological applications, we consider a train of gaussian-shaped impulses. The target field is thus of the general form, shown in Fig. 4a, where 1/∆t is the repetition rate, and m r is the number of repetitions, with m r ∆t T . In this case, standard dynamical decoupling may be underperforming, since the target signal b(t) is positive-defined in the whole temporal domain, thus the product y n (t)b(t) may be alternately positive and negative, reducing the accumulation of useful phase (see Eq. 2). In other words, each time a π-pulse reverses the spin dynamics, it can partially cancel not only the effect of unwanted noise, but also the phase associated to the field to be measured. Figure 4b-i compares the results of CP control (greencolored curves) and optimal control (in blue), when varying the width σ of the target gaussian pulse train and its repetition rate 1/∆t. In this case as well, we evaluate the effect of control sequences made of n = 50 π-pulses. In the optimization, all the time intervals between the control pulses, symmetrized around the π-pulse positions, are free parameters. Left panels represent the modulus of the phase accumulated by the spin qubit sensor per unit of the target magnetic field amplitude |φ n |, whereas right panels represent the inverse of sensitivity 1/η, as a function of the sensing time T .
Optimal control outperforms CP in accumulating useful phase due to b(t) over a large sensing time range. Both CP and optimal control do lead to their largest phase accumulation when T n∆t/2, where they give similar results in φ n . This condition corresponds of having couples of π-pulses located in each "empty" time window between two gaussian pulses of the target field, albeit optimal control corrects in a non-trivial way the distribution of π-pulse positions to minimize η, as represented in Fig. 4a (CP, green vertical bars; optimal control, blue vertical bars). This way, the π pulses partially reverse the spin qubit dynamics due to undesired noise, but do not cancel the phase due to the target field b(t).
We note that, even when the phase accumulated with CP and with optimal control is comparable, optimal control compensates better than CP for decoherence, leading to better overall sensitivity. While here we did not explore this result further, this seems to indicate that numerically optimized sequences might be useful also for other quantum information tasks, such as building a robust memory. Fig. 4e, g, and i show that optimal control of the spin qubit improves its sensitivity to gaussian multipulse signals up to a factor of two, enabling the measurement of multipulse magnetic fields down to 3 nT. As shown in Fig. 4c, the measurement of the experimental observable E = C/η confirms the theoretical prediction of sensitivity both for the CP control (yellow dots) and optimal control (blue diamonds).

IV. DISCUSSION
Summarizing, we have devised a versatile and robust method of optimal control for quantum metrology with one qubit, and we have applied this optimal control method to the measurement of weak time-varying magnetic fields with a NV spin sensor.
In practice, we have introduced an unconventional optimization metric, the qubit sensor's sensitivity. The minimization of sensitivity is made by searching the optimal control field that realizes the optimal compromise between useful accumulation of spin phase due to the external field to be measured, and noise refocusing. The developed optimization algorithm offers the advantage of fast convergence and simplicity.
We have further investigated the robustness of this method for different kinds of real target fields. Optimal control outperforms standard dynamical decoupling in different scenarios, ranging from multicomponent AC target fields in a wide frequency range of the radiofrequency domain, to trains of impulses, which are illustrative examples of the typical shape of electromagnetic field of interest in biology and physiology.
In the cases investigated, optimal control enables larger phase accumulation over wide sensing-time windows, as well as better cancellation of the effect of external noise on the spin dynamics. Sensitivity of the qubit sensor under optimized control shows an improvement up to a factor of 2, enabling the measurement of pulsed magnetic field down to amplitudes of 3 nT. The comparison of 1/η with the experimental observable E demonstrates the reliability of this optimal control method applied to the NV spin sensor.
Beyond the results obtained in exemplary situations, our novel method is one of the first extensions of optimal control methods to quantum sensing. This rises novel challenges and opportunities, in particular related to the need for new metrics for optimization as well as the challenge to include nonunitary evolution in the numerical optimization. We underline that our optimization method can be extended to larger multidimensional space of parameters, e.g., one can optimize the number of π-pulses that flip the spin qubit during the sensing time, according to the target signal to be measured. Moreover, while we always considered control sequences given by series of π pulses, our scheme can be also generalized to other control strategies of the NV spin qubit.
Our strategy can be useful for metrology in the face of more and more demanding requirements for the NV spin qubit for applications to sensing of weak time-varying electric and magnetic fields originating, e.g., from individual biological molecules, neuronal networks, nanostructured antiferromagnetic or multiferroic materials, and can be also applied to other physical platforms, such as ultracold atoms or trapped ions. Furthermore, the demonstrated enhanced protection of the spin qubit against noise-induced decoherence, makes optimal control a strategic tool also for building memories in solid-state systems.

A. Experimental setup
The host diamond crystal used in this study is a monocrystalline electronic-grade sample (Element Six), grown via chemical vapor deposition, with natural 1.1% abundance of 13 C impurities and 14 N concentration 5 ppb. All the experiments have been performed on a single negatively charged NV center, located at ∼ 13.5 µm below the diamond surface.
We exploit a confocal microscope to focus a 532 nm laser beam on the defect and collect the red fluorescence light coming from the diamond. The laser excitation initializes the NV spin in the m S = 0 state and we perform state readout after spin manipulation by measuring the fluorescence intensity with a single photon detector.
A permanent NdFeB magnet produces an external static magnetic field, B = 39.4 mT, aligned along the symmetry axis of the NV center (ẑ-axis). The field lifts the degeneracy of m s = +1 and m s = −1 energy levels.
Control of the NV spin dynamics is obtained by irradiating the defect with microwave (MW) pulses. We routinely use MW π pulses that repeatedly flip the spin, in order to periodically reverse its temporal evolution and refocus the noise effect. The MW pulses trains are applied through a 60 µm thin copper wire that works as an antenna. We exploit the same wire to deliver time-varying magnetic fields in the radio frequency (RF) range generated by an Arbitrary Waveform Generator (AWG). These RF signals are the target magnetic fields to be measured by the NV. Using two different terminals of the wire we can simultaneously apply both MW and RF fields.

B. Measuring the sensor noise spectrum
Our optimal control strategy depends on the the knowledge of the coherence function χ n (T ). As shown in the main text in Eq. 4, χ n (T ) depends on both the noise spectrum and the frequency filter Y n,T (ω) given by the specific sensing sequence. In order to compute the sensitivity η (Eq. 8), the optimization algorithm calculates the value of χ n (T ) for different trial sequences, thus it needs the noise spectral density S(ω) as an input. Various methods to measure the noise spectrum have been suggested in the literature [53][54][55][56][57][58][59]. We followed here the procedure described in [53].
The filter function Y n,T (ω) = |y n (ω, T )| 2 , where y n (ω, T ) represents the Fourier transform of the modulation function y n (t), has a simple form for periodic sequences. In the limit of large pulse numbers, it can be approximated by a delta function at the angular frequency π/τ , where τ is the pulse spacing. Then, the coherence signal decays as s(t) ∼ e −t/T CP 2 (τ ) , where T CP 2 (τ ) is a coherence time directly related to the noise spectral density via [53] 1 T CP 2 (τ ) 4 π 2 S(π/τ ).
For each pulse spacing time τ (which sets the noise frequency that we are considering) we measured the signal decay as a function of the number of pulses, obtaining T CP 2 (τ ). By varying the time τ between MW π-pulses, we can extract the main frequency components of the noise spectral density using Eq. 10. The spectrum was finally obtained by fitting the raw data with a sum of gaussian functions.
The experimental spectrum S(ω) obtained for sequences with different number of pulses showed some variation. Thus, we further refine the S(ω) by fitting the decoherence function χ n (T ) for the CP sequence as in [60] (e.g. Fig. 5). This procedure is aimed at correcting S(ω) for sequence-dependent noise and control imperfection, e.g., due to the finite π-pulses duration [61], which are not taken into account in our model. We finally find a noise spectrum that, for a fixed number of pulses n is completely independent from the timing at which each π-pulse occurs, and thus it can be used for sequences very different from CP in the optimization procedure.
We emphasize that good agreement between predicted and measured sensitivity is a further proof of the robustness of our model against imperfection in the empirical function χ n (T ).

C. Optimization algorithm
The core of our optimal control technique for sensing is an optimization algorithm that minimizes the sensitivity as a function of the parameters of the control function, e.g. total sensing time, phase of the AC field, and time intervals between π-pulses.
We use a MATLAB R routine based on the Simplex minimization algorithm to achieve global optimization of the control figure-of-merit, the sensitivity η. The two main ingredients of this quantity are the electron spin phase, ϕ n (T ) and the coherence function χ n (T ). We consider pulsed con- trol sequences described by the π-pulse times {t j }. For any time-varying external magnetic field b(t) = bf (t) to be measured, we can define F (t) = 1 t t 0 f (t ) dt , the integral of the magnetic field (known) temporal profile f (t). The phase ϕ n = b φ n acquired by the NV qubit can then be calculated for any given control sequence as The coherence χ n (T ) is instead obtained from the experimentally measured spectrum via Eq. 4. From χ n (T ) and φ n (T ) we can calculate η for each trial sequence according to Eq. 8. In order to verify the global convergence of the optimization algorithm, we tested different initial guesses and found the same optimized parameters for a given AC target field. In most cases, we used a constrained search, by setting bounds for each parameter or constraining the overall result, for instance to keep the total time T constant.
We note that our procedure is quite general and could be applied to a broad range of sensing scenarios. To demonstrate its reliability, in this work we considered a few exemplary target fields and related control models, varying, e.g., the number of parameters tackled by the optimization algorithm. We first considered AC fields with a single or multiple frequencies, and we started optimizing η as a function of total time T and the AC field phase α, while fixing the number of pulses (n = 8) and setting τ = T /n for all time intervals between the π-pulses. We then proceeded to allow more flexibility in the optimization, by varying the duration of each time interval between π-pulses, starting from an initial guess given by a periodic (CP) sequence with n = 50. We optimized the time intervals by keeping the time symmetric around each pulse, as shown in Fig. 6. Including also the optimization of the phase of the multitone field, or equivalently, the initial time of the measurement sequence, this optimization manages 51 free parameters. We performed different optimization runs as a function of the total measurement time, keeping T constant in each of them. The only additional constraint that we imposed was to force the times τ j between different π-pulses to be longer than about 10 times the π pulse duration, which in our case means τ j > 600 ns. This restriction was to ensure that no MW pulse would be very close to each other, as that would have resulted in the π pulse to cancel each other giving an effective sequence with a different n. Here, τj = (tj + tj+1)/2, where tj are the n + 1 time intervals between the π pulses, with j = 0, ..., n, and t0 = tn+1 = 0.

D. Comparison between optimal control theory and experiment
To experimentally validate the optimal control, we compared the optimized sensitivity η with the corresponding measured quantity. However, since we do not have an independent measure of the local amplitude of the magnetic field at the position of the NV center, in the experiment we measure E = max{∂ b s}/ √ T = C/η, where s is the normalized signal and C represents a conversion factor between the generated RF field amplitude and the unknown magnetic field at the defect. As C does not depend on the control sequence, it can be evaluated once and then used for all the control scenarios considered in the paper.
In particular, we estimated C from the experimental results for CP sequences. We evaluated experimentally E(T ) as a function of the sequence total time T and fitted the curve to extract the maximum E CP M . Similarly, we evaluated the theoretical value of η and obtained its minimum η CP M . We then defined C as the product C = E CP M η CP M . This procedure allows us to define not only C but also to estimate its uncertainty ∆C, from the fit error. We can then compare the (inverse) experimental sensitivity E i and the theoretical sensitivity η i for each control sequence by rescaling the theoretical sensitivity by C.
Finally we investigated the effect of the finite MW pulse duration. Considering the case of a gaussian-shaped train of magnetic impulses under a CP control sequence, we calculated phase accumulated and sensitivity when excluding from the spin evolution the time intervals where the π-pulses occurs, finding the same theoretical values of η, for the all considered total sensing times T . We underline that this procedure does not correct the model for the contribution to χ n (T ) given by MW pulses imperfections or finite duration [61], but confirms that the pulses can be considered instantaneous in our picture for the accumulated spin phase ϕ n (T ); we verified that this approximation is valid up to π M W ≤ 0.5 µs for n = 50 and T ∼ 250 µs and was very effective for pulses duration π M W = 56 ns and T ≥ 50 µs.