Robust Magnetometry with Single NV Centers via Two-step Optimization

Shallow Nitrogen-Vacancy (NV) centers are promising candidates for high-precision sensing applications; these defects, when positioned a few nanometers below the surface, provide an atomic-scale resolution along with substantial sensitivity. However, the dangling bonds and impurities on the diamond surface result in a complex environment which reduces the sensitivity and is unique to each shallow NV center. To avoid the environment's detrimental effect, we apply feedback-based quantum optimal control. We first show how a direct search can improve the initialization/readout process. In a second step, we optimize microwave pulses for pulsed Optically Detected Magnetic Resonance (ODMR) and Ramsey measurements. Throughout the sensitivity optimizations, we focus on robustness against errors in the control field amplitude. This feature not only protects the protocols' sensitivity from drifts but also enlarges the sensing volume. The resulting ODMR measurements produce sensitivities below 1$\mu$T\,Hz$^{-\frac{1}{2}}$ for an 83\% decrease in control power, increasing the robustness by approximately one third. The optimized Ramsey measurements produce sensitivities below 100\,nT\,Hz$^{-\frac{1}{2}}$ giving a two-fold sensitivity improvement. Being on par with typical sensitivities obtained via single NV magnetometry, the complementing robustness of the presented optimization strategy may provide an advantage for other NV-based applications.

Likewise, limitations and errors related to the experimental setup, such as drift, finite bandwidth, and transfer functions, restrict the performance of these sensing methods. For example, to exploit the full potential of NV-based scanning probe applications, the MW antenna has to be brought close to the cantilever [35][36][37], which can be experimentally challenging given the microscopic scale of the scanning devices. If the distance between antenna and cantilever is larger, it reduces the contrast and hence, the sensitivity of the setup. Additionally, applications with NV-based scanning probes [38,39] that move with respect to the antenna experience variations in control power. The power variations, in turn, lead to a correspondingly worsened sensitivity. Similarly, applications with single NV centers [40,41] or ensembles of NV centers [5,42,43] in bulk diamond are subject to variation in control power depending on the distance from the MW antenna. In all these cases, robustness against control power variation can simplify the experimental procedure without the need for any modification to the setup or the control pulse itself.
One strategy to partially compensate for these limitations involves quantum optimal control (QOC) [6,26,27]. QOC has previously been applied to optimize MW control pulses for quantum sensing with NV centers in a variety of settings [6,[44][45][46][47][48][49][50][51][52]. Its common objective connects the diverse family of QOC algorithms: to iteratively improve a time-dependent control pulse until a given goal The confocal setup used in combination with the RedCRAB optimization program; laser (green arrow) and MW (blue arrow) pulses are used to control the NV spin state. The fluorescence (red arrow) is collected with an optical fiber, P , connected to a single photon counter (APD), logged with a data acquisition device (DAQ), and further processed on the local control system to pass the FoM to the remote optimization server. (d) The two-step optimization strategy introduced in this work. In step 1 the laser based spin state initialization and readout processes are optimized.
Step 2 creates robust MW control pulses for pulsed ODMR (p-ODMR) and Ramsey sensing sequences via QOC. (e) Lattice structure of the NV center. The NV quantization axis is shown as a dotted black line. The component of the external magnetic DC field along the NV quantization axis is denoted as BNV and quantified via the sensing methods. The confocal schematic in (c) is is drawn with parts adapted and modified from Ref. [31] and Ref. [32] with permission under terms of reuse. For details on the setup see appendix A.
has been reached. Some of these algorithms rely on simulations (open-loop) to quantify the quality of the pulses. In contrast, others achieve the same via direct interaction with the experiment (closed-loop, Fig. 1a-c). Algorithms such as GRAPE [53,54] (gradient ascent pulse engineering) or Krotov's method [55,56] require the calculation of the derivative of the goal function (gradient-based). The dCRAB algorithm (dressed Chopped RAndom Basis) [27,57,58] can be implemented under a gradientfree strategy. Additionally, the functional parametrization approach of the dCRAB algorithm can be combined with gradient search methods via algorithms like GROUP [59] (gradient optimization using parametrization) or GOAT [60] (gradient optimization of analytic controls). Even with a moderate number of basis functions, the control pulse can contain enough information to steer the system [61,62].
With the ultimate goal of enhancing the sensitivity of the main DC magnetometry methods with NV centers (section II), this work presents a two-step strategy to exploit the full potential of feedback-based optimization algorithms and QOC [27,47,63,64] in connection with shallow single NV centers in diamond (< 10 nm below the surface, Fig. 2a). At the first step, the optical spin initialization/readout processes are optimized via a gradientfree Nelder-Mead search [65] in the parameter landscape corresponding to the properties of the experimental system and setup (section IV A). In the second step, we utilize the gradient-free dCRAB algorithm to optimize the MW pulses for spin state manipulation. The optimization routine is implemented via the QOC software package RedCRAB (Remote dCRAB) [47,64]. The opti-mized MW controls are developed for two DC magnetometry methods (section IV B), namely the pulsed ODMR sequence [12,66] and the Ramsey sensing protocol [7,8]. Two optimization bases, Fourier [67] and Sigmoid [68], (see appendix B) are compared to assess their suitability for the involved methods. All optimizations include a Figure of Merit (FoM, see Fig. 1a) based on the optical readout contrast. To include robustness against variation in MW drive strength, the FoMs are adapted to scan over control amplitudes ranging from 100% to 20% of the maximum. Finally, the optimized pulses are assessed for their enhancement of the average sensitivity and robustness (section V).

II. DC MAGNETOMETRY METHODS
The transitions in the energy level structure of the NV center strongly influence its sensitivity towards external magnetic fields (sensitivity is defined in appendix C, more details in section IV A). The optical ground state forms a spin one triplet system, with a Zero-Field Splitting (ZFS) of ≈ 2.871 GHz. In the presence of an external magnetic field along the NV center's axis (B NV ), Zeeman splitting lifts the degeneracy between the m s = ±1 states. This splitting provides a direct way to quantify B NV . A pseudo two-level system can be constructed from the m s = 0 and one of the m s = ±1 states. The two-level approximation forms the basis for various magnetometry techniques with NV centers [2,7,10,21].
The most straightforward procedure to detect DC magnetic fields is called continuous wave optically detected magnetic resonance (cw-ODMR) [66,69]. The method involves continuous polarization of the NV spin state with a green laser, while MW pulses with different drive frequencies ω mw are applied sequentially to locate the resonance peaks. The splitting between the resonance peaks is proportional to B NV . Cw-ODMR measurements are less demanding in terms of practical resources and complexity than pulsed measurement schemes, as they do not require pulsed controls. However, by nature, continuous-wave measurements have a lower spin readout fidelity and suffer from optical and MW power broadening [66].
The dephasing time T * 2 sets a limit to the achievable sensitivities with different DC magnetometry methods (see appendix C, Eq. (C4)). Short laser and MW pulses help to overcome the power broadening effect [66] and attain better sensitivities. Pulsed ODMR involves pulsed optical excitations and spin state transfer using MW π-pulses. For shallow NV centers, the spin states decay quickly. Hence, pulsed ODMR experiments with short, high power control pulses can be advantageous. The short control pulses inherently result in faster measurements, which lead to an improvement in the overall sensitivity. The pulsed ODMR method also offers enhanced readout contrast, which further improves the sensitivity. Note that the sensitivity is defined as the least detectable magnetic field within a measurement time of one second [4,70].
In general, the cw-and pulsed ODMR methods do not exploit the quantum property of spin superposition, which provides a way to make the measurements more sensitive [4]. Conversely, the double-pulse-based Ramsey sequence does utilize spin superposition states for sensing. It also has the advantage of avoiding the power broadening effects [71]. The Ramsey method consists of two π 2 -pulses, with free precession time τ in between. The optically initialized NV spin state is transferred into a superposition state by the first of the two π 2 -pulses. This superposition state interacts with the external magnetic field for the time τ , thus accumulating a phase. Eventually, the second π 2 -pulse converts the accumulated phase into an optically measurable population difference. In contrast to the ODMR-based frequency-sweep methods, the Ramsey sensing protocol is performed at a fixed ω mw . In addition, τ can be varied to measure minimal fluctuations in external magnetic fields [4]. In general, the Ramsey method can be used to sense any magnetic fields that change slowly enough, i.e., with frequencies less than 1 τ (see appendix C). The saturation behavior can be studied to obtain the excitation power with the best signal-to-background ratio for the experiments. Ideally, this lies below the saturating laser power. (c) Typical emission spectra of the single NV centers in the sample. The NV charge states have different spectral signature, the given spectrum indicates negatively charged NV state. (d) Exemplary second order correlation measurement, which is performed to identify single NV centers in the sample. τc is the delay time in the photon antibunching measurement with the NV (g2(0) ≈ 0.27).

III. SAMPLE AND EXPERIMENTAL SETUP
All experiments in this work involve an electronicgrade diamond sample (300µm ×100µm ×40µm) with implanted NV centers (Fig. 2). The nitrogen ion implantation was performed with a fluence of 3 × 10 11 cm −2 at 6 keV, which results in an average depth on around 9.3 ± 3.6 nm [72]. The implantation was followed by annealing (850 • C), which forms NV centers in the implanted layer, which shows a uniform density over the sample. Afterward, a second oxidation annealing at 400 • C was performed, followed by tri-acid cleaning. This process removes the top layer of the diamond, resulting in reduced the NV density and depth. The average NV density is estimated via confocal fluorescence maps to be around 7×10 7 cm −2 . The value is obtained by analyzing confocal scans of the sample surface (see Fig. 2a).
For the experiment, the sample is mounted on an Ωshaped strip-line MW antenna (Fig. 1c, A) [73]. The antenna is mounted on a piezoelectric scanner to perform multi-axial scans. The dichroic mirrors (Fig. 1c, D 1 , and D 2 ) filter the excitation laser pulse and direct the fluorescence along the collection arm of the confocal setup. Additionally, a 600 nm long-pass filter (Fig. 1c, F) in the collection arm is used for spectral filtering. The optical initialization and readout are assisted by an objective (Fig. 1c, O), which delivers and collects the light to/from the in-focus diamond sample containing shallow single NV centers. Laser pulsing is achieved with a digitally modulated diode laser (modulation bandwidth: 125 MHz). The MW control pulses are generated by mixing the in-phase (I) and quadrature (Q) components ( Fig. 1b) with a carrier signal. The resulting pulse is subsequently amplified and delivered to the confocal setup via the strip-line antenna. For more details on the setup, see appendix A.
In principle, the implementation of these magnetometry methods with NV centers is straightforward and well understood. In practice, however, various factors may affect the performance of these sensing schemes. For example, custom-built MW antennas with unknown instrument response functions are often used to deliver the control pulses. In such cases, the control pulses delivered to the NV center may slightly defer from their actual design. Additionally, for shallow NV centers it is difficult to model all surface effects with adequate precision. This lack of information a priori makes it a challenge to accurately model the system. A closed-loop optimization circumvents this issue.

IV. OPTIMIZATION METHODS
The sensing protocols described in section II rely on the efficiency of two types of control: Readout/initialization via the laser and spin manipulation via the MW field. Here, two complementary optimization strategies are presented using the RedCRAB optimization suite. The first adapts the laser pulse parameters (section IV A) and the second the MW control pulses (section IV B). The cloudbased optimization incorporates re-evaluations according to the accuracy of the measurements as well as restrictions of the parameters (see section V), pulses, and superparameters (see section IV B) chosen to compliment the experimental hardware.
In both optimization steps, we first quantify the goal with an FoM that can be measured in the experiment. Subsequently, the controllable constant parameters and time-dependent controls of the system are identified. The initialization/readout is optimized with a direct search, while the MW pulses are optimized via the dCRAB algorithm [27,57,58,64].

A. Parameter optimization for Spin State Initialization and Readout
Strong spin polarization and spin state dependent fluorescence are fundamental to the readout of single NV centers. These properties primarily originate from the transition rates of the spin-preserving radiative and the non-radiative decay channels between the NV energy levels (see Fig. 3a). The non-radiative inter-system crossing via the metastable state does not preserve the spin state [74]. Figure 3a shows how an NV, which is originally in m s = 0 or m s = ±1, decays via Path I or II, respectively after being excited by a green laser pulse (λ = 520 − 530 nm). The excited m s = 0 state decays radiatively to the ground state, while the m s = ±1 state might take the non-radiative route via Path II. If the laser pulse is long enough, all population ends up in the m S = 0 ground state. Figure 3b shows the readout procedure. To obtain a contrast, a laser pulse first initializes the system to the ground state m s = 0 via Paths I and II. In the top part of Fig. 3b no MW is applied (m s = 0 readout) and a second laser pulse leads to a decay via Path I. An intermediate MW pulse (m s = ±1 readout) can transfer the spin state to m s ± 1 via Path III. The subsequent laser pulse induces a decay via Path II which leads to a drop in fluorescence ( Fig. 3c) because of the decay via the longlived metastable state. Hence, the photon count allows differentiating the spin states during optical readout. A simulation-based (open-loop) optimization for optical spin state initialization and readout can be done considering the NV rate equations with experimentally obtained transition rates [75]. Such methods may require specialized apparatus for optical pulse shaping [76], and in general, do not account for experimental limitations. It is noteworthy that pulse width induced polarization enhancement methods have been previously studied for improving the for spin state initialization using short laser pulses [77]. In comparison, closed-loop parameter search offers straightforward enhancement. In the following, we develop the method for an optical readout with laser pulses of constant power and finite duration,  2)) obtained with the 1µs laser pulse (Cinit, red) and the optimized laser pulse (Copt, striped). Ropt and Sopt indicate the optimized windows for the readout and saturation, respectively. (d) Parameters for the optical readout optimization. Ri and Si correspond to the photon collection windows described in Eq. (5). A spin inverting rectangular MW pulse (inv.) is used for the parameter optimization. Readout 0 (1) corresponds to the readout of the ms = 0 (±1) spin states. After an initial laser pulse (init.), each measurement is repeated N times to enhance the signal-to-noise ratio.
subjecting to the limitation of the setup. The photon shot noise is the primary limitation to an efficient optical readout of the NV spin state. Consequently, the statistical determination of the spin state requires an averaged readout over a large number of experimental repetitions. The spin state readout fidelity F for such probabilistic measurements is expressed in terms of the noise parameter σ R [10,78]: such that F = 1 at the spin projection noise limit of the sensitivity (see appendix C). R 1 (R 0 ) is the total number of collected photons from the readout of the spin state initialized in m s = ±1 (m s = 0). Experimentally, the readout contrast C is given by Its relation to F is given in appendix C. Intrinsically, the contrast depends on several system properties and experimental parameters, where γ ij is the transition rate between levels i ↔ j, L p is the laser pulse intensity, L d is the laser pulse duration, Ω max corresponds to the maximum amplitude of the spin inversion control pulse, B ⊥ and E xy are off-axial magnetic and electric field components at the position of the NV center respectively, and T is the ambient temperature. In addition, several other factors, including crystal field strain and charge state stability, may affect the fluorescence of the NV center and ultimately influence the readout contrast. The majority of the parameters in Eq. (3) depend on the system properties, material characteristics, and ambient conditions that are generally not fully controllable. In practice, some of the system properties can be characterized before the optimization of the readout contrast. For example, the charge state of the NV center can be determined from the emission spectrum (Fig. 2b). Similarly, external factors such as crystal field strain and temperature directly influence the ZFS of the NV center. In this regard, pre-characterized single NV centers ( Fig. 2c) with ZFS ≈ 2.871 GHz, and stable photoluminescence that do not exhibit charge state related blinking allow to fully exploit the scope of laser pulse parameter optimization (Note that this does not rule out photochromism on short timescale). Likewise, a wellaligned static magnetic field B NV is a prerequisite for the optimizations performed in presence of a magnetic bias field. It is noteworthy that photons originating from NV 0 can be filtered from the readout signal (Fig. 1a). As a result, charge state instability leads to blinking of the NV fluorescence signal [79].
Other experimental parameters in Eq.
(3) such as L p and L d , directly influence the optically induced transitions, as well as the charge state stability [78,79]. In contrast, the effect of the wait time t w between the initialization and readout/spin manipulation pulses (shaded region between pulses in (see Fig. 3d)) is more indirect. Hence, it is commonly set to ca. 300 ns, which corresponds to the lifetime of the metastable state [80]. Similarly, the photon collection window W ro is often calculated in advance to obtain the best SNR for every readout [3]. Consequently, Eq. (3) can be reduced to a simpler form based on the variables that can be controlled experimentally, Although it is not straightforward to find an analytical form to characterize the dependence of C on these parameters, they can be directly adjusted in a closedloop optimization on the experiment. Figure 3 shows the two-shot scheme for the contrast measurement used in the optimization routine. This strategy is devised keeping in mind that the experimental setup does not enable time-tagged photon counting. Each laser pulse (Readout 0 (1)) is divided into a spin readout window R 0 (R 1 ), and a spin state saturation window S 0 (S 1 ). Their durations are determined by the optimization parameters L d and W ro . A spin inversion MW pulse flips the spin state between the laser pulses. The FoM, which is minimized during the optimization, is given by Here,C is the readout contrast averaged over N experimental repetitions as shown in Fig. 3d. In addition to maximizing the readout contrast in the spin readout win-dows, FoM RO also ensures uniform spin state initialization, as the optimized value tends to minimize the variance in the photon counts from the two spin states in the saturation windows (Fig. 3). The closed-loop optimization of readout parameters is generally relevant for a variety of methods, such as readout based on spinto-charge-state conversion [78] and photoelectric readout [81], which inherently involves laser pulses. Furthermore, the optimized readout can be integrated directly with MW-free, all-optical magnetometry methods [82]. The standard approach of obtaining the optimal readout parameters usually requires multiple measurements, or otherwise time-tagged photon counting. The closed-loop optimization approach presented reduces the measurement time that otherwise will be required to manually optimize all the parameters under consideration. This is further improved by reducing the measurement to two readouts only for assessing R i and S i .

B. Quantum Optimal Control for Spin State Manipulation
An optimally initialized spin state and its efficient readout are two of the essential criteria for a practical quantum sensor [4]. In addition, the spin state has to be controlled accurately to implement a sensing protocol. Following parts of the text describe the optimization of MW control pulses for spin inversion and for a π 2 x -gate via the dCRAB algorithm [27,57,58,64].
Before proceeding to the specifics of the optimization schemes, we discuss the dynamical equations of the system to introduce the basic concept of QOC. The system is described by a constant drift Hamiltonian H d , and control Hamiltonians H i c , which are modulated by control pulses u i (t): where the complete Hamiltonian H(t) is given in the rotating wave approximation (RWA) with the detuning ∆ = ω mw − ω nv , the NV's resonant frequency ω nv , the Pauli matrices σ i , and the controls u 1 (t) = Ω(t) cos(φ(t)) and u 2 (t) = Ω(t) sin(φ(t)). These controls correspond to the in-phase and quadrature components of a MW drive, with Rabi frequency Ω(t) ∈ [0, Ω max ] and phase φ(t) applied for the duration t p . The control objective for the MW pulses is to efficiently transfer the initial spin state |Ψ i to the final state |Ψ f . Hence, the FoM is defined as the state fidelity, where where T indicates a time-ordered exponential propagator. At this point, the FoM is a functional of the control pulses. The controls are subsequently parametrized by a set of N set × M basis elements f i (ω n ; t). Each element is defined by its superparameter ω n , which is randomly selected from ω min < ω n < ω max , where ω min and ω max are the minimum and maximum allowed values. The number of basis functions M per superparameter depends on the basis. These superparameters can be the frequencies of a set of trigonometric functions (Fourier basis [58]; in this case ω min and ω max set the allowed bandwidth of the control pulse) or the offsets for a set of step functions (Sigmoid basis [68]). The resulting pulses take the following form: Here, u i 0 (t) represents the initial guess for the pulse. Following the parametrization, the goal of the QOC routine is to find the optimal values for the coefficients A n , maximizing the FoM (Eq. (5)). Especially in closedloop optimization, only a limited number of parameters can be optimized at a given time. Therefore, additional steps are required to avoid local optima. The dCRAB algorithm tackles this issue by switching the set of basis elements every time the optimization has converged under the given constraints [58]. Every new optimization (superiteration) is started with the previous optimum as an initial guess, i.e. u i 0 (t) = u i opti (t). The optimizations are performed with both, the Fourier and Sigmoid basis separately. To ensure the pulse amplitude and duration are limited, i.e., that the amplitude stays within an upper and a lower limit and the pulse is zero at t = 0 and t = t p , two different strategies are applied and illustrated in Fig. 4. In the cut-off approach, the pulses are cut off at t = 0 and the t = t p  to limit the duration. Similarly, they are cut off at the top and bottom to force the amplitude limits. Instead, the bandwidth-limited approach involves re-scaling the pulse to fit within the amplitude limits, followed by multiplication with a smooth window function like a flat-top Gaussian to avoid discontinuities at initial and final time.
In the cut-off approach (see Fig. 4), the Fourier basis is expected to produce high-frequency components when the optimization algorithm maximizes the pulse area. Conversely, in the bandwidth-limited approach, the Fourier basis will have difficulties to significantly expand the pulse area. At the same time, the Sigmoid basis has the ability to exploit the pulse area without producing high frequencies [68] when combined with the bandwidth-limited approach. The inherent smoothness offered by the Sigmoid basis (see appendix B) provides a particular advantage for frequency-sweep-based spectroscopic measurements, where spurious harmonics are to be avoided.
The first MW optimization presented here concerns the spin-inversion pulse in the pulsed ODMR sequence (see Fig. 3). The efficiency of the spin state transfer is estimated through the optical readout contrast C (Eq. (3)). The previously obtained parameters for the laser-based initialization/readout are used as the default for the MW control pulse optimization experiments. To achieve robustness, the control field amplitude variation is incorporated in the FoM by averaging the contrast over a range of Rabi frequencies Ω max .
where N p is the total number of sampled Ω max and R k i are the photon counts from the corresponding spin state collected during Readout 0 (1) (see Fig. 3). The goal of the optimization is to minimize FoM podmr . The Ramsey protocol does not involve spin inversion, but instead a π 2 x -gate. In the sensing procedure, this pulse plays two roles: First, it maps the spin eigenstates to a superposition state with a given phase. Second, it converts the phase back to a spin population. Gates cannot be directly quantified using the contrast. Instead, their quality is commonly quantified via gate tomography, which requires additional state preparations and related measurements. We develop a protocol to translate the π 2 x -gate's unitary properties into a readout contrast that takes the same number of measurements as the evaluation of the spin state inversion. Figure 5 shows the scheme connecting the pulse performance to the readout fluorescence contrast C from two spin states. Similar to the case of pulsed ODMR, the FoM is defined as where P k i is the photon count for the kth amplitude value after projection into spin state i. The photon counts P k i Figure 5. Exemplary measurement protocol for the Ramsey sequence optimization. The spin is projected into ms = 0 and ms = ±1, similarly to the spin state measurement in Fig. 3b. are related to the spin transfer to the different states using the following series of transformations, where U (t p ) is the parametrized unitary operator for the optimized control pulse of duration t p , and π x denotes the unitary transformation for the rectangular πpulse applied along the x-axis. The maximization of the contrast ideally corresponds to the following conditions: | 1|U (t p ) U (t p )|0 | 2 = 1.
Here, |0 and |1 denote the two spin states of the system under the two-level approximation: |0 is given by the m s = 0 state and |1 represents either m s = +1 or m s = −1 depending on the corresponding experiment specified in section V. We introduce the parametrization of the unitary transformation generated by the control pulse as with coefficients c j for j = {x, y, z}, andĉ j = c j /c, with and thus c = 1 2 π 2 + kπ , for integer k, and c z = 0. Substituting this into Eq. (12) gives finally indicating that c x = c = 1 2 π 2 + kπ , and hence c y = 0. In other words, FoM ram in Eq. (11) is minimized for a π 2 rotation around the x-axis (in the positive or negative direction):

V. EXPERIMENTAL RESULTS AND SENSITIVITY ANALYSIS
A straightforward way to test the general applicability of the optimization strategies discussed in the preceding section is to apply them to different single NV centers and compare the readout contrast enhancement on a casespecific basis. In addition, the average sensitivities from the experiment quantify the optimization benefits. The optimization schemes from section IV are implemented and compared in the following section. First, we assess the improvements resulting from optimized readout (OR) (section V A) and the additionally optimized spin transfer pulses (section V B) for the pulsed ODMR method. Second, OR is applied with optimized control pulses for the Ramsey protocol, and the results are discussed in section V C. Finally, the robustness of the pulses is tested over a range varying from 100% to 10% of the maximum control power. This variation is artificially introduced in the experiment by changing the power at the MW source.

A. Initialization and Readout
Experimental restrictions are directly included in the closed-loop optimization of the initialization and readout process by limiting the optimization parameters. The bounds on the parameter set {L p , L d , W ro , t w } are given as: and t w ∈ [0, 1000] (ns).
Limits on L p correspond to the available source laser power. The incident laser power experienced by the NV center lies between 0.05 mW and 1 mW ( Fig. 2(b)). However, as the instrument values are used as a parameter in the optimization, they are the ones referenced throughout the text. The initial guess for the optimization is chosen to be {L p ≤ P sat , 1000 ns, 450 ns, 300 ns}, where P sat is the saturation laser power for the single emitter). In cases where the saturation limit cannot be reached with the available laser intensity, the initial guess is obtained by considering the saturation curve to identify the approximate laser intensity with the most favorable signalto-background ratio. Some of the readout optimization results are summarized in table I. As a general observation, the optimized laser pulses are shorter than the corresponding initial guesses, while the t w values remain almost unchanged after the optimization. Moreover, reduction of the measurement time improves the overall sensitivity of the NV center (Eq. (C5)). Figure 3c shows the photoluminescence behavior of one of the NV centers involved in the experiment (table I, NV3). The collected signal reflects the improvement in the average readout contrast after the optimization.  Fig. 8,   9, 10, 11 a Pulse optimization restriction via the cut-off approach. b Pulse optimization restriction via the bandwidth-limited approach.
The optimized laser parameters are tested by combining them with a standard pulsed ODMR sequence with rectangular spin inversion pulses (pulse duration of 57 ns). Their readout contrast is quantified asC = 1 − min[N ph ], where N ph is the normalized photon count (see appendix C for details). Figure 6 shows a comparison between the measurement with and without optimized parameters (zero bias field, table I, NV2). The optimized parameters account for a 33% improvement in peak contrast. This result can be improved even further by also optimizing the spin inversion pulses.

B. Pulsed ODMR measurements with optimized MW pulses
The spin inversion pulse that is part of the pulsed ODMR protocol provides a target for further optimization on top of the optimized optical readout. In this regard, we investigate the additional improvement by optimizing the pulses under a bias field B NV to emulate a spin resonance sensing scenario. The FoM is calculated by averaging the contrast over a set of N p = 5 measurements (see Eq. (10)) leading to control pulses in the range of 4% -100% of the maximum control power (or equivalently, 20% -100% of the maximum control amplitude, Ω max ). The initial guess resembles a standard rectangular π-pulse, whose length is determined by observing Rabi oscillations. Figure 7 shows two maps representing the normalized count obtained with two optimized MW pulses. In this example, the laser pulses were pre-optimized according to the method described in V A, and the m s = 0 ↔ +1 transition is used for the optimization as well as the assessment via pulsed ODMR. The MW pulses are optimized according to Eq. (10) at the center frequency of 3.22 GHz. The pulse corresponding to the left is optimized with the Fourier basis, while the right pulse is optimized with the Sigmoid basis, both with a pulse duration of 200 ns. Both pulses exhibit robustness with respect to the amplitude variations, improving the contrast compared to the initial guess. However, the Sigmoid basis pulse is spectrally narrow, while the Fourier pulse has a distinct sideband. The spectral shape of the pulses can explain these features. The Fourier basis contains highfrequency elements caused by the cut-off limitation (see Fig. 4), which the Sigmoid basis avoided. The small offresonant area addressed by the Sigmoid basis covers only a fraction of the Fourier basis' sideband and is significantly weaker. This is illustrated in the average plot at the bottom.
To test the general applicability of this method for generating bandwidth-limited control pulses, similar optimization and pulsed ODMR experiments are performed with a different NV center (table I, NV3), this time using the m s = 0 ↔ -1 transition, and the bandwidth-limited approach (Fig. 4). The results are shown in Fig. 8. Here, a readout contrast of ca. 0.24 is obtained with optimized laser parameters (pulse duration of 134 ns, obtained from observing Rabi oscillations). The Sigmoid pulse (pulse duration of 200 ns) enhances the readout contrast further to ca. 0.30. Pulsed ODMR experiments with different peak control power are performed to test the robustness of the control pulse. The readout contrast and Full Width Half Maximum (FWHM) of the resonance profile are obtained by fitting the data with a Gaussian profile (see Eq. (C6)). Figure 9 shows the achievable average sensitivity η of the pulsed ODMR method. It depends on the resonance profile, its FWHM, contrast, and the measurement time   Fig. 7 the optimization was done with the Sigmoid basis, using the bandwidth-limited restriction approach. The data shows the improvement in contrast with each step of the optimization. The initial contrast for the resonance peaks is ca. 0.22 (initial guess, red), which is further improved to ca. 0.24 with optimized laser parameters (blue). The MW pulse optimized in the Sigmoid basis on top improves the contrast to ca. 0.30 (green). the solid lines show the Gaussian fits for the respective data (see appendix C).
involved in the experiment (see Eq. (C5)). In addition, the spin-projection noise sets a lower limit to η. The full optimization, including the laser parameters and the robust Sigmoid pulse, leads to a sub-µT Hz − 1 2 average sensitivity considering up to almost 83% variation in the control power (see appendix C for details on the sensitivity calculation). The lack of straight-forward interpretability of solutions lies in the nature of QOC. Still, it is notable that the Sigmoid pulse's FWHM is broader for lower Ω, possibly hinting at the algorithm compensating for the reduced coverage of the hyperfine lines in that regime.
Off-axial magnetic field components lead to spinmixing, reducing the readout contrast [82]. This effect becomes apparent when comparing the contrast at ZFS (Fig. 6) and in presence of an external magnetic field (Fig. 8). The degree of spin-mixing and its effects on the transition rates cannot be straightforwardly simulated for the presented experiments. Using closed-loop optimization of the laser pulse parameters allows to nevertheless incorporate such effects into the FoM. Up to this point, all three NV centers from table I were investigated. As the improvements are of the same order of magnitude, only NV3 is considered in the following without loss of generality. The Ramsey sensing method, which is addressed next, fulfills a similar role to the pulsed ODMR sequence and offers better sensitivities towards external DC magnetic fields [2,5,7] (see appendix C).

C. Ramsey Measurement
The Ramsey method is a type of interference measurement for DC magnetic fields. As discussed in section II it consists of two π 2 -pulses and offers a higher sensitivity in comparison to the ODMR methods. It should be noted that previous optimizations for D-Ramsey pulse sequences with NV centers were performed in an open-loop scheme using a cooperative design [13]. Our results are obtained through a closed-loop optimization and directly quantified on the setup. The π 2 -pulses are optimized via assessment of the contrast for a range of drive amplitudes (see Eq. (11)) via the bandwidth-limited approach discussed in section IV B (see Fig. 4). The initial guess resembles a standard rectangular π 2 -pulse, whose length is determined by observing Rabi Oscillations. The resulting interference fringes are shown in Fig. 10. This optimization is carried out in presence of a bias external  Figure 10. Optimized Ramsey measurements. The top plot shows the measurements performed at peak drive power with rectangular control pulses (blue) as well as optimized pulses in the Fourier basis (black) and Sigmoid basis (green). The optimized pulses exhibit almost double the contrast in comparison to the rectangular control pulse, with a similar precession frequency corresponding to the hyperfine levels of the transition. The length of the rectangular π 2 -pulse is determined by performing Rabi measurements, and in this case is 67 ns. The bottom plot shows the variation in readout contrast with respect to the change in relative control power of the control pulse. The performance of the robust optimized pulses surpasses the rectangular control pulse over the entire range of tested control power (90% variation). magnetic field (B NV = 12 mT) and on-resonance with the m s = 0 ↔ -1 transition. The fringe visibility is enhanced from 0.15 to ca. 0.24 with the Fourier basis pulse, and to ca. 0.25 with the Sigmoid basis pulse using the maximum control amplitude (pulse duration of 100 ns). The fringe visibility is directly related to the readout contrast. An improvement in the readout contrast leads to a proportional improvement in the sensitivity of the sensor (see Eq. (C7)).
The performance of the optimized pulses is further tested by performing Ramsey measurements with different drive frequencies in the vicinity of the spin transition frequency. These detunings correspond to a range of fields that could be measured in a sensing setup. The resulting readout signal summed over repeated iterations of the experiment is assessed for average sensitivity [10] η of the NV center. The sensitivity of the Ramsey sequence depends on the readout contrast and the dephasing time during the measurements (see appendix C). Figure 11 shows the readout contrasts, the T * 2 -times, and the average sensitivities obtained by a series of Ramsey measurements. The Fourier pulse displays a constant readout contrast in the frequency range of ±10 MHz. This  Figure 11.
Comparison of Ramsey sequences with standard and optimized MW π 2 -pulses. All measurements are performed with optimized laser pulses for the readout and the optimizations were carried out with the amplitude-robust FoM from Eq. (11). The performance of the pulses optimized with the Fourier (black) and Sigmoid (green) basis is compared to the rectangular control pulse (blue) over a range of drive detunings ∆. This range is equivalent to a variation of 0. 35  range in the frequency corresponds to around ±0.35 mT of variation in B NV . In comparison, the Sigmoid pulse shows a marginally betterC around the resonance frequency but varies strongly for different detunings. The frequency components of the pulse depend on the pulse shape (see appendix B). Here, the spectrum of the Sigmoid pulse contains minima at a detuning of approximately ±5 MHz (Fig. 11). From a control perspective, such frequency selective applications are attractive for spectral hole burning [83] and quantum logic gates for superconducting qubits [84]. The T * 2 -time is comparatively lower on resonance than off resonance for all pulses. This is due to the destructive interference of the hyperfine transition associated with the spin resonance [85]. Readout contrast enhancement inherently involves strong contributions from all the hyperfine transitions, resulting in a trade-off betweenC and T * 2 . The measurements with the Fourier pulse exhibit a robust η of less than 65 nT Hz − 1 2 . These levels of sensitivities are on par with the ones reported for single NV-based diamond scanning probes [86][87][88].
The method from this section could be generalized to replace the spin-refocusing π-pulse in other sensing methods. This would require applying the optimized π 2 -pulses twice, using a strategy similar to the one discussed in section IV B. Such refocusing pulses form the main building block for AC magnetic field sensing [6].

VI. CONCLUSION
The optimizations in this work focused on three essential parts of quantum sensing with NV centers: optical spin state readout, population inversion, and π 2 -pulses. All three were improved for sensing methods with single NV centers, considering control power variations of up to 90%. Such robustness enables the sensing of larger microstructures by increasing the explorable sample area and makes the pulses more robust against experimental drift over time. The resulting protocols are realized by replacing the building blocks of common laser and MW based schemes with optimized equivalents. The optimizations are based on a set of figures of merit which are directly measurable via contrast using a varying MW power. The feedback-based approach inherently takes experimental imperfections and unknown system parameters into account. Initially, we optimized the optical readout/initialization process, improving the spin readout contrast by 32% in comparison to the standard protocol. Moreover, additionally optimizing the spin inversion pulse in a pulsed ODMR protocol allowed for an overall contrast improvement by 36% leading to sub-µT Hz − 1 2 sensitivity that is maintained over a large range of MW amplitudes. Such robust excitation pulses lead to a large interrogation volume. Especially, for ensembles of NV centers this results in improved readout counts for a larger area, and in turn, enhanced sensitivity [5]. To maintain frequency sensitivity, different optimization bases were explored. The Sigmoid basis leads to spin transfer within a limited bandwidth envelope, reducing the off-resonant excitation. Additionally, we obtained an optimized π 2 -pulse for Ramsey measurements, enhancing the fringe contrast by 67% with respect to the square pulse with pre-optimized optical readout at maximum control power. Consequently, we obtained a two-fold enhancement in the average sensitivities, ranging below 100 nT Hz − 1 2 over a set of induced bias field strengths. While we applied the optimization to shallow NV centers, the approach is straightforwardly applicable to other NV-based systems like diamond scanning probes and NV ensembles used for wide-field imaging where similar control robustness features are required.
where, is the reduced Plank's constant, g e is the Landé factor, µ B is the Bohr magneton, and t m is the measurement time. In addition, optical readout processors are subjected to photon shot noise that further adheres the sensitivity. For the averaged readout process discussed in section I, Eq. (1), the readout fidelity can be equivalently written as (C2) C is the average readout contrast between the two spin states of the system andR is the average count rate. Further, an overhead cost is always involved in an experimental scenario. Really long spin initialization and readout duration deteriorates the overall sensitivity of the sensor, this can be expressed as a scaling factor for the sensitivity under the assumption that the initialization and readout duration are equal (t i ). Finally, for DC magnetometry methods, the dehpasing time T * 2 further limits the sensitivity, this can be expressed the decoherence function of the T * 2 -limited processes, where, m is the order of decoherence. For spectroscopic measurement around the NV resonance peaks, the sensitivity depends on the resonance profile itself [5]. In case of Gaussian resonance profiles for the pulsed ODMR measurements, the overall sensitivity can be computed as Here, σ f is the resonance peak FWHM, γ nv = geµ B , is the gyromagnetic ratio of the NV spin, and T π is the pulse duration. The factor P relates to the shape of the resonance, for a Gaussian profile P = e 8ln2 [66]. For shorthand notation, the measurement time is assumed to involve the overhead experimental time t m = t w + 2 × t i . The relevant parameters for the sensitivity calculation in section V were obtained by fitting the normalized count with the following function: where f 0 is the resonance peak, the normalized counts are calculated by dividing the data with the baseline counts (counts away from the resonance, where no spin transfer occurs). For a Gaussian profile, σ f = 2 √ 2 ln 2 × ∆f . It is noteworthy that for pulsed ODMR measurements at low MW power, T * 2 -limit becomes relevant and has to be considered for sensitivity calculations, the reader is advised to refer to Ref. [66] for more details.
The average sensitivity for the Ramsey sequence based methods can be expressed under the T * 2 -limit as, The free induction decay of the Ramsey fringes for single NV centers highlight the hyperfine structure originating from the electron-nuclear spin coupling. Likewise, the related normalized readout counts can be fitted with a sum of the three precessing hyperfine transitions, where, ν i and φ i are the precession frequency and phase corresponding to the hyperfine transitions. The values for ν i depend on the detuning of the drive with respect to the transitions in an external bias field (see Fig. 11). The sensitivities in Fig. 11 are obtained at τ = 0.5×T * 2 . The normalized readout count in this case is obtained by dividing the data with the m s = 0 readout count.