# ASAP-Repair: API-Specific Automated Program Repair Based on API Usage Graphs

Sebastian Nielebock<EMAIL_ADDRESS>0000-0002-0147-3526 Otto-von-
Guericke University Magdeburg Germany ,  Paul Blockhaus
<EMAIL_ADDRESS>0000-0001-6910-9475 Otto-von-Guericke University
Magdeburg Germany ,  Jacob Krüger<EMAIL_ADDRESS>0000-0002-0283-248X
Eindhoven University of Technology The Netherlands and  Frank Ortmeier
<EMAIL_ADDRESS>0000-0001-6186-4142 Otto-von-Guericke University
Magdeburg Germany

(2024)

###### Abstract.

Modern software development relies on the reuse of code via Application
Programming Interfaces. Such reuse relieves developers from learning and
developing established algorithms and data structures anew, enabling them to
focus on their problem at hand. However, there is also the risk of misusing an
API due to a lack of understanding or proper documentation. While many
techniques target API misuse detection, only limited efforts have been put
into automatically repairing API misuses. In this paper, we present our
advances on our technique API-Specific Automated Program Repair (ASAP-Repair).
ASAP-Repair is intended to fix API misuses based on API Usage Graphs by
leveraging API usage templates of state-of-the-art API misuse detectors. We
demonstrate that ASAP-Repair is in principle applicable on an established API
misuse dataset. Moreover, we discuss next steps and challenges to evolve ASAP-
Repair towards a full-fledged Automatic Program Repair (APR) technique.

API Misuses, Automated Program Repair, API Usage Graphs

††journalyear: 2024††copyright: rightsretained††conference: 2024 ACM/IEEE
International Workshop on Automated Program Repair; April 20, 2024; Lisbon,
Portugal††booktitle: 2024 ACM/IEEE International Workshop on Automated Program
Repair (APR ’24 ), April 20, 2024, Lisbon, Portugal††doi:
10.1145/3643788.3648011††isbn: 979-8-4007-0577-9/24/04††ccs: Software and its
engineering Error handling and recovery††ccs: Software and its engineering
Software libraries and repositories

##  1\. Introduction

An Application Programming Interface (API) is the de facto standard for
_client developers_ to reuse algorithms and code implemented in a library or
framework developed by _API developers_. An API provides a set of _API
elements_ (e.g., methods, fields, data structures). When using these API
elements, client developers can _misuse_ them, for instance, mixing up the
order of method calls or calling methods with false parameters (Amann et al.,
2019a). If such a misuse causes negative behavior (e.g., software crashes,
performance issues), we refer to it as _API misuse_.

In the past, many techniques for detecting API misuses have been developed
(Ammons et al., 2002; Weimer and Necula, 2005; Zhong et al., 2009; Amann et
al., 2019a; Nielebock et al., 2020; Kang and Lo, 2021). Commonly, these
techniques infer or mine likely correct _template usages_ of the API (e.g.,
patterns or change rules) whose violations are reported as misuses. While
limited, such techniques provide a profound way to specifically detect API
misuses. Importantly, they _do not require dedicated tests to detect and
localize misuses_ , and they provide a patch via the template usage with which
the misuse has been detected. A natural way to advance research is to leverage
these templates for automated repair.

In this paper, we introduce the idea of an Automatic Program Repair (APR)
technique for API misuses to which we refer to as API-Specific Automated
Program Repair (ASAP-Repair). ASAP-Repair builds on a graph-based structure
representing API usages named API Usage Graphs (AUGs), which has been
introduced by Amann et al. (2019b). Particularly, we leverage different
template usages represented as AUGs (i.e., patterns and change rules) to
repair API misuses. While currently limited to repairing misuses in AUGs and
not in code, ASAP-Repair’s major benefit compared to state-of-the-art APR
techniques is that it _does not require test cases or the execution of code to
localize misuses_. We demonstrate its in-principle applicability by applying
ASAP-Repair on the real-world API misuse dataset MUBench (Amann et al., 2016).
Moreover, we discuss necessary steps and challenges to evaluate and to compare
ASAP-Repair with state-of-the-art APR techniques. We publish ASAP-Repair and
all artifacts related to this paper in a publicly available
repository.111https://doi.org/10.5281/zenodo.10527304

##  2\. API Usage and API Misuse Detection

### API Usage Graphs

Amann et al. (2019b) have developed a graph-based structure to describe API
usages in Java with the goal to improve the precision of API misuse detection.
For representational purposes, we introduce AUGs with a fictive fix222Please
refer to our replication package for real examples. of an API misuse depicted
in 1 and its AUG representation before this fix (cf. 1(a)). This fix adds a
validation for the method call B.bar() in line 9 by checking the condition
B.isBarable(). In case this resolves to false, an alternative call to B.bar2()
is required.

AUGs are directed, labeled, acyclic multigraphs consisting of different node
(i.e., action and data) and edge types (i.e., data and control flow). _Action
nodes_ are depicted as rectangles representing method calls (e.g., A.foo()) or
control structures, while ellipses visualize _data nodes_ , such as constants
and objects (e.g., A). _Data flow_ is represented with solid edges connecting
parameters to methods (i.e., para) or objects to their object methods (i.e.,
recv). _Control flow_ is displayed via dashed edges that indicate the order of
action nodes (i.e., order) or the selection after if-conditions (i.e., sel).
AUGs only represent the API usage of a single method declaration (e.g.,
method()), and thus are restricted to the intra-procedural level. However,
they have been demonstrated to improve API misuse detection and were enhanced
as well as used by different researchers (Amann et al., 2019b; Nielebock et
al., 2020; Kang and Lo, 2021); making them a valid basis for an APR technique.

Listing 1: Sample code for an API misuse and fix.

⬇

1import A;

2import B;

3public class misuse {

4 public void method() {

5 System.out.println("Start run");

6 A.foo();

7 noise();

8- B.bar();

9+ if(B.isBarable()){

10+ B.bar();

11+ } else{

12+ B.bar2();

13+ }

14 A.foobar();

15 System.out.println("finished run");

16 }

17}

### Frequent API Usage Patterns

Researchers have proposed a set of API misuse detectors, many of them applying
frequent pattern mining (Ammons et al., 2002; Weimer and Necula, 2005; Zhong
et al., 2009; Amann et al., 2019a; Kang and Lo, 2021). Their conjecture is
that the way APIs are frequently used also represents their correct usages.
Thus, they conduct pattern mining to infer API usage patterns and compare
those with actual usages of that API. If a usage violates this pattern, it is
denoted an API misuse. However, recent work showed that such misuse detectors
typically suffer from a high false positive rate (i.e., a huge number of
correct usages reported as misuses) (Goues and Weimer, 2009; Amann et al.,
2019a), and thus research has focused on improving the precision of misuse
detectors. In the following, we assume that a pattern in the form of an AUG
has been found that can correctly detect an API misuse.

### API Change Rules

Another idea leverages information from already fixed misuses by extracting
API code changes using version control (Nielebock et al., 2020, 2021). In
detail, such techniques infer so-called correction or change rules, which
represent the essential changes needed to fix an API misuse. A rule has the
form $m\rightarrow f$ where $m$ and $f$ are the misuse and fixed (sub-)AUGs of
the change, respectively. We show the rule for the fix in 1 in 1(b). To match
both graphs, heuristics are applied, which we also apply for ASAP-Repair (cf.
Section 3). A rule consists of the subgraphs describing the misuse and its fix
as well as transform-edges representing how nodes are transformed into their
respective fix. Additions are symbolized by special $\epsilon$-nodes
indicating “holes” in the misuse graph. Similarly, deletions are represented
by using $\epsilon$-nodes in the fixed AUG.

(a) AUG of the sample code.

(b) Change rule of the misuse fix.

Figure 1. AUG (left) and change rule (right) for the example code in 1.

To detect API misuses, existing techniques measure the
similarity333Originally, this was called “distance,” which is mathematically
restricted. Thus, we use the more general term “similarity.” $sim$ of a
candidate API usage $u$ and both subgraphs $m$ and $f$ of the rule. A usage
$u$ is reported as a misuse if $sim(m,u)>sim(f,u)$ holds, meaning that the
usage is more similar to the misuse part than to the fixed part. Results
indicate that such a technique can achieve high precision, but suffers from a
very low recall (Nielebock et al., 2022).

##  3\. Process of ASAP-Repair

Figure 2. Concept of Pattern- and Rule-based API-specific Automated Program
Repair

We depict ASAP-Repair‘s concept in Figure 2. It fixes API misuses in the form
of AUGs using _patterns_ (i.e., as AUGs) or _change rules_. Both variants
start with an API usage ( A), which is transformed into its respective AUG
followed by the misuse detection. For the pattern-based version ( B), we can
apply the violation-based technique by Amann et al. (2019b). When using change
rules ( C), we can use the similarity-based technique (Nielebock et al., 2021,
2022). If a misuse is detected, ASAP-Repair has different steps to match the
nodes of the misuse AUG with those of the template AUG (i.e., pattern or
change rule) through which the misuse was detected. Using this matching, we
identify which nodes have to be changed (i.e., add, delete, update).

### Matching Heuristic

Matching graphs refers to the subgraph isomorphism problem known to be NP-
complete (Grohe and Schweitzer, 2020). Thus, we adapted the strategy to
produce change rules (Nielebock et al., 2020, 2022) for graph matching. In
detail, we applied the Kuhn-Munkres algorithm (Munkres, 1957) to find a
heuristic solution for the matching. This means that we create a bipartite
graph based on the two AUGs (e.g., $aug_{A}$ and $aug_{B}$), with one
partition containing the nodes of $aug_{A}$ and the other the nodes of
$aug_{B}$. Then, we equalize the cardinalities of both partitions by adding
special $\epsilon$-nodes. These describe the addition and deletion of nodes
(e.g., a node changed to an $\epsilon$-node represents a deletion) within the
matching. Finally, we add edges between the nodes of both partitions, which we
label with the costs to transform the respective node into the other (i.e.,
the number of node relabelings as well as adding, deleting, and relabeling
incoming and outgoing edges). The Kuhn-Munkres algorithm finds a matching that
minimizes the overall costs. Note that this matching can be invalid, since it
ignores the order of nodes and multiple matchings are possible.

### Pattern-Based Matching

In case we repair an API misuse based on patterns, we cannot directly match
the misuse with the pattern AUG. The reason is that the pattern is typically
smaller (in terms of number of nodes) than the misuse. Thus, a matching would
indicate that every node not part of the pattern must be deleted. We avoid
this issue by conducting a preprocessing of the misuse to find and match only
those nodes that relate to the pattern. Particularly, we leverage that AUGs
contain information on the API type of single nodes (e.g., java.util.List).
Then, we consider only those nodes of the misuse for matching that have the
same API type as the nodes in the pattern. Thus, we determine the API types
present in the pattern AUG ( D) and filter nodes from the misuse AUG to
extract a subgraph, consisting only of nodes of these types ( E). Then, we
match only this subgraph to the pattern ( F).

### Rule-Based Matching

Matching an API misuse with a change rule is simpler, since such rules
describe exactly which nodes have to be changed. In detail, we match the
misuse AUG with the misuse part AUG of a change rule ( G). If the rule
contains an addition of nodes (i.e., the misuse part contains
$\epsilon$-nodes), we temporarily disregard these from the matching procedure.
Then the matching can have three possible cases:

  1. (1)

A node from the misuse AUG is matched to a non-$\epsilon$-node of the misuse
part AUG. This indicates that this node has to be either deleted or updated
(i.e., depending on whether this node is connected via a transform-edge to an
$\epsilon$-node or not).

  2. (2)

A node from the misuse AUG is matched to a generated $\epsilon$-node in the
misuse part (i.e., due to the cardinality equalization step of the matching).
Then, this node will not be part of the transformation, since it cannot be
matched to the change represented by the rule.

  3. (3)

For every disregarded $\epsilon$-node in the misuse part AUG of the rule, we
add a respective $\epsilon$-node in the misuse AUG as well. We match those to
the counterparts in the misuse part AUG to indicate that nodes have to be
added according to the fixed part AUG of the change rule.

This way, we obtain a triple matching between misuse AUG, misuse part AUG, and
fix part AUG. Note that nodes of the misuse AUG falling under case (2) are not
part of the final repair step.

### AUG-Based Repair

In the final step of ASAP-Repair, we transform the misuse graph into the fixed
version ( H). We refer to the changes indicated by the respective matching,
either pattern-based or rule-based, as _corrections_. We distinguish between
three cases of transformations/corrections:

  1. (1)

_A non- $\epsilon$-node from the misuse is matched to a non-$\epsilon$-node in
the correction_: Then, ASAP-Repair _updates_ the node by updating its label,
node type, as well as adding, deleting, updating the respective incoming and
outgoing edges.

  2. (2)

_An $\epsilon$-node from the misuse is matched to a non-$\epsilon$-node in the
correction_: ASAP-Repair _adds_ the non-$\epsilon$-node to the AUG, which also
contains the addition of the respective edges represented by the correction.

  3. (3)

_A non- $\epsilon$-node from the misuse is matched to an $\epsilon$-node in
the correction_: ASAP-Repair _removes_ the non-$\epsilon$-node from the AUG,
which also contains the deletion of the respective incoming and outgoing edges
of this node.

Note that ASAP-Repair keeps nodes unmatched (i.e., filtered out in the
pattern-based repair or case (2) in the rule-based repair), except by changing
incoming or outgoing edges from previously changed neighbor nodes. Since the
Kuhn-Munkres algorithm does not always produce a valid matching, it may happen
that the transformed AUG contains cycles, which results in an invalid AUG. We
tackle this problem by conducting a graph cycle check. If the transformed AUG
contains cycles, we retry the transformation by testing another matching,
which we obtain based on an internal data structure of the Kuhn-Munkres
algorithm. In case none of the possible matchings produces a valid AUG, no
repaired AUG is generated.

##  4\. Preliminary Results

We conducted a preliminary evaluation of ASAP-Repair by applying it to
MUBench,444https://github.com/stg-tud/MUBench/ a dataset consisting of real
API misuses that has been constructed by Amann et al. (2016). More precisely,
we used the subset of 116 misuses provided in the replication package by
Nielebock et al. (2021). In our evaluation, we conducted a sanity check on
whether ASAP-Repair is applicable for real misuses. That means, for every
single API misuse, we used its fix as a pattern and the changes of the fixing
commit as the basis for the change rule. This way, we validated ASAP-Repair in
an ideal situation, in which a perfectly matched pattern and change rule are
found. Therefore, we cannot draw general conclusions about the applicability
of ASAP-Repair in practice or in comparison to state-of-the-art APR
techniques, which are subject to our future work.

Table 1. Results of repairing 116 misuses form MUBench:
D - #generated data structures for repair (i.e., pattern AUG or change rule),
C - #created, V - #valid, and U - #unique fixes.

D (%) C (%) V (%) U (%) pattern-based 110 (94.8%) 61 (52.6%) 34 (29.3%) 11
(9.5%) rule-based 86 (74.1%) 38 (32.8%) 27 (23.3%) 4 (3.4%)

For each example in the dataset, we downloaded the code version directly
before and after a fixing commit, representing the misuse and fixed version,
respectively, and used them to mimic the pattern and change rule. For each
repair, we set a timeout of five minutes. Then, the first author checked how
many repaired AUGs ASAP-Repair generated and manually validated each fix using
an AUG comparison technique to decide whether the AUG of the fixed version and
the generated fix were semantically equal. In Table 1, we summarize the
results. We observed that ASAP-Repair produced more valid pattern-based fixes
than rule-based ones (i.e., $34$ vs $27$) with more unique fixes (i.e., $11$
vs. $4$). However, the rule-based repair obtained a larger proportion of valid
fixes within the generated fixes (i.e., $27/38\approx 71\%$ vs. $34/61\approx
55.7\%$), _indicating a better precision of the rule-based repair_. We also
analyzed the reasons why the other repairs could not be generated or were
invalid. For the pattern-based repair, major issues were that fixed AUGs
contained cycles (36 cases), invalid edges in the fixed AUG (16 cases), or
timeouts during the repair (11 cases). For the rule-based repair, we
encountered timeouts and out-of-memory exceptions (59 cases) as well as
invalid edges in the fixed AUGs (11 cases). Moreover, we found 16 cases in
which MUBench had a false misuse description causing false or no repair at
all. Three of these false descriptions caused an unsuccessful pattern-based
AUG generation. For more details, we refer to our replication package.

While still limited, these results are promising to further improve and apply
ASAP-Repair as a full-fledged APR-technique. It provides a framework to
synthesize results from pattern-based and rule-based misuse detectors in a
single APR technique. The main challenge lies in improving the matching
efficiency as well as prohibiting the construction of invalid fixed AUGs.

##  5\. Conclusion and Prospects

We demonstrated that AUG-based repair is possible for real API misuses. To
obtain a valid APR technique, we will continue addressing the following
prospects.

### From AUGs to Code

While AUGs represent a good visual way to describe required API fixes, a more
practical approach is a full-fledged technique producing repaired source code.
We can imagine two possible ways to achieve this: (1) We directly transform an
AUG into its respective source code. This requires a valid specification on
how to transform code into an AUG, which is only implicitly given by the
implementation4 of MUBench (Amann et al., 2019b). Moreover, we need the back-
transformation from AUGs to code. (2) We use the matching to perform the
respective code transformations. In detail, we need to define for each
possible node and edge transformation in the AUG a proper abstract-syntax-tree
transformation.

### Representative Misuse Datasets

While MUBench (Amann et al., 2016) is a valid misuse dataset, its
representativeness is debatable, as many entries represent essentially the
same misuse. Thus, other misuse datasets, such as AU500 (Kang and Lo, 2021)
with its manually labeled misuses, must be used, too. However, these are
limited in their size, which, in turn, limits external validity. Thus, larger
validation datasets must be build.

### Comparison to State-of-the-Art APR Techniques

Many APR techniques have been developed in the past (Monperrus, 2018), to
which we have to compare ASAP-Repair. While we validated ASAP-Repair manually
and statically, we also have to validate whether the code fixes imply a
correct dynamic behavior. This requires the execution of the code using test
cases. A good starting point is the APIARTy framework by Kechagia et al.
(2021), which analyzes state-of-the-art APR techniques on API misuses that
were validated via tests. However, we noticed that some misuses are not
replicable, since repositories became outdated. Thus, their build process does
not work anymore. So, the underlying misuse data and build commands have to be
updated manually to ensure a valid evaluation and comparison.

## References

  * (1)
  * Amann et al. (2016) Sven Amann, Sarah Nadi, Hoan A. Nguyen, Tien N. Nguyen, and Mira Mezini. 2016.  MUBench: A Benchmark for API-Misuse Detectors. In _Proc. 13th Int. Work. Min. Softw. Repos. (MSR)_. ACM, 464–467.  https://doi.org/10.1145/2901739.2903506
  * Amann et al. (2019a) Sven Amann, Hoan A. Nguyen, Sarah Nadi, Tien N. Nguyen, and Mira Mezini. 2019a.  A Systematic Evaluation of Static API-Misuse Detectors.  _IEEE Trans. Softw. Eng._ 45, 12 (2019), 1170–1188.  https://doi.org/10.1109/TSE.2018.2827384
  * Amann et al. (2019b) Sven Amann, Hoan A. Nguyen, Sarah Nadi, Tien N. Nguyen, and Mira Mezini. 2019b.  Investigating Next Steps in Static API-Misuse Detection. In _Proc. 16th Int. Work. Min. Softw. Repos. (MSR)_. IEEE, 265–275.  https://doi.org/10.1109/MSR.2019.00053
  * Ammons et al. (2002) Glenn Ammons, Rastislav Bodík, and James R. Larus. 2002\.  Mining Specifications. In _Proc. 29th Symp. Princ. Program. Lang. (POPL)_. ACM, 4–16.  https://doi.org/10.1145/503272.503275
  * Goues and Weimer (2009) Claire Le Goues and Westley Weimer. 2009.  Specification Mining with Few False Positives. In _Proc. 15th Int. Conf. Tools Algorithms Constr. Anal. Syst. (TACAS)_. Springer, 292–306.  https://doi.org/10.1007/978-3-642-00768-2_26
  * Grohe and Schweitzer (2020) Martin Grohe and Pascal Schweitzer. 2020.  The Graph Isomorphism Problem.  _Commun. ACM_ 63, 11 (2020), 128–134.  https://doi.org/10.1145/3372123
  * Kang and Lo (2021) Hong Jin Kang and David Lo. 2021.  Active Learning of Discriminative Subgraph Patterns for API Misuse Detection.  _IEEE Trans. Softw. Eng._ (2021), 1–1.  https://doi.org/10.1109/TSE.2021.3069978
  * Kechagia et al. (2021) Maria Kechagia, Sergey Mechtaev, Federica Sarro, and Mark Harman. 2021.  Evaluating Automatic Program Repair Capabilities to Repair API Misuses.  _IEEE Trans. Softw. Eng._ (2021), 1–1.  https://doi.org/10.1109/TSE.2021.3067156
  * Monperrus (2018) Martin Monperrus. 2018\.  Automatic Software Repair: A Bibliography.  _ACM Comput. Surv._ 51, 1, Article 17 (2018), 24 pages.  https://doi.org/10.1145/3105906
  * Munkres (1957) James Munkres. 1957\.  Algorithms for the Assignment and Transportation Problems.  _Journal of the Society for Industrial and Applied Mathematics_ 5, 1 (1957), 32–38.  https://www.jstor.org/stable/2098689
  * Nielebock et al. (2021) Sebastian Nielebock, Paul Blockhaus, Jacob Krüger, and Frank Ortmeier. 2021.  An Experimental Analysis of Graph-Distance Algorithms for Comparing API Usages. In _Proc. 21st Int. Work. Conf. Sour. Code Anal. Manip (SCAM) - RENE Track_. IEEE, 214–225.  https://doi.org/10.1109/SCAM52516.2021.00034
  * Nielebock et al. (2022) Sebastian Nielebock, Paul Blockhaus, Jacob Krüger, and Frank Ortmeier. 2022.  Automated Change Rule Inference for Distance-Based API Misuse Detection.  arXiv:2207.06665 [cs.SE]
  * Nielebock et al. (2020) Sebastian Nielebock, Robert Heumüller, Jacob Krüger, and Frank Ortmeier. 2020.  Cooperative API Misuse Detection Using Correction Rules. In _Proc. 42nd Int. Conf. Softw. Eng. - New Ideas Emerg. Results (ICSE-NIER)_. ACM, 73–76.  https://doi.org/10.1145/3377816.3381735
  * Weimer and Necula (2005) Westley Weimer and George C. Necula. 2005.  Mining Temporal Specifications for Error Detection. In _Proc. 11th Int. Conf. Tools Algorithms Constr. Anal. Syst. (TACAS)_. Springer, 461–476.  https://doi.org/10.1007/978-3-540-31980-1_30
  * Zhong et al. (2009) Hao Zhong, Tao Xie, Lu Zhang, Jian Pei, and Hong Mei. 2009.  MAPO: Mining and Recommending API Usage Patterns. In _Proc. 23rd Eur. Conf. Object-Oriented Program. (ECOOP)_. Springer, 318–343.  https://doi.org/10.1007/978-3-642-03013-0_15

  *[API]: Application Programming Interface
  *[APR]: Automatic Program Repair
  *[AUGs]: API Usage Graph
  *[AUG]: API Usage Graph
  *[APIs]: Application Programming Interface


# Quantum Diamond Microscope for Narrowband Magnetic Imaging with High Spatial
and Spectral Resolution

Zechuan Yin  Department of Electrical Engineering and Computer Science,
University of Maryland, College Park, Maryland 20742, USA  Quantum Technology
Center, University of Maryland, College Park, Maryland 20742, USA  Jiashen
Tang  Quantum Technology Center, University of Maryland, College Park,
Maryland 20742, USA  Department of Physics, University of Maryland, College
Park, Maryland 20742, USA  Connor A. Hart  Department of Electrical
Engineering and Computer Science, University of Maryland, College Park,
Maryland 20742, USA  Quantum Technology Center, University of Maryland,
College Park, Maryland 20742, USA  John W. Blanchard  Department of Electrical
Engineering and Computer Science, University of Maryland, College Park,
Maryland 20742, USA  Quantum Technology Center, University of Maryland,
College Park, Maryland 20742, USA  Xinyan Xiang  Quantum Technology Center,
University of Maryland, College Park, Maryland 20742, USA  Saipriya Satyajit
Quantum Technology Center, University of Maryland, College Park, Maryland
20742, USA  Department of Physics, University of Maryland, College Park,
Maryland 20742, USA  Smriti Bhalerao  Quantum Technology Center, University of
Maryland, College Park, Maryland 20742, USA  Fischell Department of
Bioengineering, University of Maryland, College Park, Maryland 20742, USA  Tao
Tao  Quantum Technology Center, University of Maryland, College Park, Maryland
20742, USA  Department of Physics, University of Maryland, College Park,
Maryland 20742, USA  Stephen J. DeVience  Quantum Technology Center,
University of Maryland, College Park, Maryland 20742, USA  Ronald L. Walsworth
<EMAIL_ADDRESS>Department of Electrical Engineering and Computer Science,
University of Maryland, College Park, Maryland 20742, USA  Quantum Technology
Center, University of Maryland, College Park, Maryland 20742, USA  Department
of Physics, University of Maryland, College Park, Maryland 20742, USA

###### Abstract

The quantum diamond microscope (QDM) is a recently developed technology for
near-field imaging of magnetic fields with micron-scale spatial resolution. In
the present work, we integrate a QDM with a narrowband measurement protocol
and a lock-in camera; and demonstrate imaging of radiofrequency (RF) magnetic
field patterns produced by microcoils, with spectral resolution $\approx 1$
Hz. This RF-QDM provides multi-frequency imaging with a central detection
frequency that is easily tunable over the MHz-scale, allowing spatial
discrimination of both crowded spectral peaks and spectrally well-separated
signals. The present instrument has spatial resolution $\approx 2\,\mathrm{\mu
m}$, field-of-view $\approx 300\times 300\,\mathrm{\mu m^{2}}$, and per-pixel
sensitivity to narrowband fields $\sim{1}\,$nT$\cdot$Hz-1/2. Spatial noise can
be reduced to the picotesla scale by signal averaging and/or spatial binning.
The RF-QDM enables simultaneous imaging of the amplitude, frequency, and phase
of narrowband magnetic field patterns at the micron-scale, with potential
applications in real-space NMR imaging, AC susceptibility mapping, impedance
tomography, analysis of electronic circuits, and spatial eddy-current-based
inspection.

##  I Introduction

Figure 1: (a) Widefield imaging of near-field patterns of narrowband magnetic
fields utilizing a quantum diamond microscope (QDM). RF test signals are
applied to microcoils to produce distinct magnetic field patterns across the
NV-diamond surface. Laser and MW fields are employed for NV electronic spin
manipulation, with spin state population readout through fluorescence
detection. (b) NV energy-level diagram. The NV- center in diamond exhibits
spin-dependent fluorescence with triplet ground-state spin transitions. (c) RF
magnetic field imaging protocol with a lock-in camera. Within one camera
demodulation cycle, four exposures occur, with phase-modulated XY8-n sequences
applied to NV spins to generate internal I & Q frames. After $N_{d}$ cycles,
an external frame of the RF magnetic field image is obtained. The CASR
protocol comprises interspersed blocks of single external frame measurements
(red boxes) with a frame rate of $f_{\mathrm{SR}}$. The RF magnetic signal
(black curve) with frequency $f_{s}$ is sampled via CASR readout (red dashed
curve) at an alias frequency $f_{s}-mf_{\mathrm{SR}}$, where $m$ is the
integer closest to $f_{s}/f_{\mathrm{SR}}$.

Nitrogen-vacancy (NV) centers in diamond are robust and sensitive detectors of
magnetic fields, among other physical quantities [1, 2]. In particular, the
quantum diamond microscope (QDM), utilizing a dense, micron-scale near-surface
layer of NV centers in a diamond substrate, is a versatile platform for near-
field magnetic field imaging, with diverse applications across the physical
and life sciences [3]. To date, QDMs have mostly been used to image patterns
of static or broadband magnetic fields, though narrowband measurements have
also been performed, e.g., for real-space NMR imaging [4], AC magnetic
susceptibility mapping [5], eddy current imaging [6], and phase-sensitive
images of a narrowband signal produced by stripline wire [7]. However, the
CCD-camera-based measurements used in most QDMs to date have typically been
slow due to low frame rate and long exposure time, compared to single-channel
measurements using a photodiode and digitizer, restricting the measurement
bandwidth and temporal resolution. Moreover, the long exposure time of a CCD
camera leads to low NV spin-state optical contrast and a limited ability to
mitigate broadband laser intensity noise, both of which result in reduced
magnetic signal sensitivity [2]. Fortunately, the development of QDMs
utilizing high-speed lock-in cameras provides a platform to acquire magnetic
images at a faster rate, while applying phase modulation to bandpass the
signal. In a recent demonstration [8], broadband magnetic imaging using Ramsey
interferometry and a lock-in camera was realized, with sub-millisecond
temporal resolution, micron-scale spatial resolution, and nanotesla-scale per-
pixel sensitivity.

Here, we demonstrate narrowband magnetic imaging protocols, integrated with a
QDM using a lock-in camera, and capable of high spectral and spatial
resolution measurements of patterns of near-field radiofrequency (RF) signals.
Beginning with dynamical decoupling sequences, we image in-phase, multi-
frequency sub-nanotesla RF signals ($\sim{1}$ MHz) produced by spiral-shaped
microcoils [Fig. 1(a)]; and find good agreement with numerical simulations.
This RF-QDM provides lateral spatial resolution $\approx 2\,\mathrm{\mu m}$,
field-of-view (FOV) $\approx 300\times 300\,\mathrm{\mu m^{2}}$, and per-pixel
sensitivity to narrowband fields $\sim{1}\,$nT$\cdot$Hz-1/2. The central
detection frequency can be easily tuned over several orders of magnitude by
simple adjustments to the NV measurement protocol. We also show that RF-QDM
measurements have good temporal stability by averaging down the spatial
magnetic noise background to a few picotesla after 1 hour. Subsequently, we
apply the coherently averaged synchronous readout (CASR) [9] protocol,
enabling simultaneous acquisition of amplitude and phase images of multi-
frequency RF signals with a 250 Hz spectral window and single-Hz resolution.
Given the current performance of the RF-QDM, we evaluate its feasibility for
real-space NV-NMR imaging of samples placed on the diamond imager chip,
estimating the required acquisition times for imaging both thermally polarized
and hyperpolarized NMR signals with various spatial resolutions. Notably, our
approach is applicable to other quantum sensors, such as spin defects in
silicon carbide (SiC) [10, 11, 12] and hexagonal boron nitride (hBN) [13, 14],
thus providing a benchmark for narrowband magnetic imaging based on dynamical
decoupling with any spin sensor.

##  II Experimental Methods

Figure 2: RF-QDM imaging of narrowband, multi-frequency magnetic fields of
known phase, using a dynamic decoupling (XY8-8) protocol. (a) Simultaneously,
a 1 MHz alternating current (Sig1) is applied to the upper microcoil on the
NV-diamond surface [Fig. 1(a)]; and a 0.8 MHz current (Sig2) is applied to the
lower microcoil. By adjusting the spacing between $\mathrm{\pi}$ pulses, the
dynamical decoupling sequence becomes sensitive to different signal
frequencies. The diagram presented here illustrates simple XY8 sequences,
while in the experiment, an XY8-8 sequence with phase modulation is employed,
as depicted in [Fig. 1(c)]. (b) Comparison of measured (left) and simulated
(right) magnetic field images resulting from alternating currents with a peak
value of 6.6$\,\mathrm{\mu A}$. After averaging over 1.2 s (for each signal
frequency), the experimental images exhibit a signal-to-noise ratio (SNR) of
approximately 50, calculated as the ratio of the maximum signal contrast to
the standard deviation within a quarter of the FOV excluding the signal. By
adjusting the $\pi$ pulse spacing $\tau$, the XY8-8 sensing sequence can be
synchronized with either Sig1 or Sig2, enabling imaging of RF magnetic field
patterns generated by each microcoil individually, with no observable ”cross-
talk” in the images between the simultaneously applied signals. (c) Calculated
FFT spectra of the two signals applied to the microcoils (blue and red solid
lines); and filter functions of the XY8-8 sequences used for spectrally-
selective RF-QDM measurements (shaded blue and red regions). Both signal and
filter function amplitudes are normalized. A 10 Hz linewidth is added to the
signal peaks for better visualization. (d) Demonstration of sub-nanotesla
scale RF magnetic imaging. By reducing the peak value of the applied
alternating currents to 26.4$\,\mathrm{nA}$, images are obtained of the RF
magnetic field patterns from Sig1 and Sig2, with SNR of about 15 after one
hour of signal averaging (for each signal frequency). All scale bars
correspond to 50$\,\mathrm{\mu m}$ in this figure.

The present study employs a custom-built QDM utilizing a CVD-grown diamond
plate [Fig. 1(a)] with a nitrogen-doped surface layer produced by Element Six,
Ltd. Unless otherwise noted, measurements and simulations in the main text are
for a diamond with a $10\,\mathrm{\mu m}$ layer [$\mathrm{NV}\approx
2.7\,$ppm]. Results for a diamond with a $1.7\,\mathrm{\mu m}$ layer with the
same $\mathrm{NV}$ concentration, as well as other RF-QDM technical details,
are reported in the Supplemental Material [15]. The negatively charged
$\mathrm{NV^{-}}$ (otherwise referred to as NV throughout this paper)
possesses a triplet ground-state spin [Fig. 1(b)] that can be optically
initialized and read out. In this work, 532 nm green laser light is coupled
into a polished side facet of the diamond plate, undergoing total internal
reflection (TIR) and illuminating a $\sim{400\times 600}\,\mathrm{\mu m^{2}}$
region of the NV-doped layer. A nominal 22 mT bias magnetic field is aligned
to one of the four NV ensemble orientations in diamond for sensing the
projection of signal RF magnetic fields along that particular sensing NV
orientation (defined as the z axis). The spin-state-dependent $\mathrm{NV}$
fluorescence is subsequently collected with an objective (20$\times$/0.75 NA
Nikon) and imaged with a lock-in camera (Heliotis heliCam C3) capable of
external frame rates up to 3.8 kHz.

An arbitrary waveform generator (AWG) is used to control the experimental
apparatus, including direct synthesis of the microwave (MW) pulses that drive
transitions between the NV $\mathrm{m}_{\mathrm{s}}=0$ and
$\mathrm{m}_{\mathrm{s}}=+1$ sub-levels [Fig. 1(b)]. The AWG marker channels
synchronize the camera acquisition and gating of an acoustic optical modulator
(AOM) used to generate optical pulses exciting the NV layer. In addition, the
AWG produces independent RF test signals applied to two spiral-shaped
microcoils on the NV-diamond surface, which provide spatially-varying magnetic
field patterns across the FOV.

For each imaging pixel, the local RF magnetic fields generated by the
microcoils can be measured using a dynamic decoupling protocol (XY8-n), where
the net phase acquired by the $\mathrm{NV}$ electronic spin due to the
oscillating field is mapped onto a difference in the ground state spin
population by a final $\pi/2$ pulse prior to readout. Four different XY8-n
sequences, comprising a single demodulation cycle, are interwoven between
exposures of the lock-in camera, as depicted in [Fig. 1(c)], and the
corresponding NV fluorescence signals are labeled as $S_{1,2,3,4}$. By
controlling the phase of the final $\mathrm{\pi/2}$ pulse, one can enhance the
readout signal contrast and subtract broadband laser intensity noise by
leveraging the in-pixel lock-in camera demodulation. In each cycle and for
every pixel, the camera generates an in-phase (I) signal by analog subtraction
of the third exposure from the first; and a quadrature (Q) signal as the
difference of the second and fourth exposures. The accumulated difference
signals after $N_{d}$ demodulation cycles are digitized to produce external I
and Q frames, which are stored in an on-board, memory buffer. Finally, since
each I and Q frame pair contains the same information due to the specific
phase alternation pattern employed, they are summed to form one external frame
of the readout signal after transfer to the host computer. For an applied RF
test signal with $B_{\mathrm{RF}}=B_{z}\cos(2\pi f_{s}t+\phi_{0})$, where
$B_{z}$ is the projection of the signal magnetic field amplitude on the NV
axis, $f_{s}$ is the oscillation frequency, and $\phi_{0}$ is the phase
offset, the readout signal for each pixel of an external frame can be written
as $S=N_{d}[(S_{1}-S_{3})-(S_{2}-S_{4})]=4N_{d}S_{a}\sin(kB_{z}\cos\phi_{0})$,
where $S_{a}$ is the signal oscillation amplitude, $k=2\gamma
N_{\mathrm{\pi}}/f_{s}$ is a constant, and $N_{\mathrm{\pi}}$ is the
$\mathrm{\pi}$ pulse number in the XY8-n sequence. When sensing a magnetic
field shift $\Delta B_{z}$, the change in NV fluorescence readout is given by
$\Delta S=4N_{d}S_{a}k\Delta B_{z}$. By calibrating the magnetometry slope of
each pixel, the RF magnetic field image can be extracted from the fluorescence
image as shown in the Supplemental Material [15].

The CASR protocol [9] is employed for simultaneous near-field imaging of the
amplitude, frequency, and phase of RF signal patterns, with high spectral
resolution ($\approx 1$ Hz). By recording consecutive lock-in camera external
frames [Fig. 1(c)], the time-dependent RF magnetic field is captured by
interspersed NV spin-state readouts at a sampling rate of $f_{\mathrm{SR}}$,
equal to the external frame rate. Using the CASR protocol, an oscillating NV
fluorescence signal in the time domain is read out for each pixel. Applying a
Fast Fourier Transform (FFT) yields frequency domain CASR spectra for each
pixel, centered at the alias frequency $f_{s}-mf_{\mathrm{SR}}$, with peak
amplitude $S_{\mathrm{CASR}}=4N_{d}(2\pi)^{\frac{3}{2}}S_{a}J_{1}(kB_{z})$
[15], where $J_{1}(x)$ is the first-order Bessel function and $m$ is the
integer closest to $f_{s}/f_{\mathrm{SR}}$. For the demonstrations presented
here, we experimentally ensure that $kB_{z}<1$ such that the first-order
Bessel function closely approximates a linear function, leading to
$S_{\mathrm{CASR}}\propto B_{z}$.

##  III Results

###  III.1 In-phase RF magnetic imaging

To characterize the performance of the RF-QDM, we begin by imaging narrowband
magnetic fields with known phase. We simultaneously apply alternating current
(AC) signals at $\mathrm{1\,MHz}$ and $\mathrm{0.8\,MHz}$, each with a peak
value of $\mathrm{6.6\,\mu A}$, to two separate microcoils on the NV-diamond
surface [Fig. 2(a)]. We first align the XY8-8 sensing sequence in-phase with
the $\mathrm{1\,MHz}$ signal by setting the $\mathrm{\pi}$ pulse spacing to
$\tau_{1}=0.5\,\mathrm{\mu s}$ and sweep the test signal phase to obtain
maximum signal. After a 1.2 second acquisition (200 camera external frames),
we obtain an RF magnetic field amplitude image with a peak-to-peak difference
of about 130 nT, revealing the near-field pattern generated by only the upper
microcoil with $\mathrm{SNR}\approx{50}$ [Fig. 2(b)]. Adjusting the spacing
between XY8-8 $\mathrm{\pi}$ pulses ($\tau_{2}=0.625\,\mathrm{\mu s}$) to be
in phase with the $\mathrm{0.8\,MHz}$ signal, we record the magnetic field
pattern produced only by the lower microcoil. The RF-QDM provides clean near-
field images of either signal frequency, with no significant ”cross-talk”
between the simultaneously-applied signal patterns, because the test signal
frequencies are spectrally separated by more than the narrowband filter
function of the sensing sequence [Fig. 2(c)]. The center frequency of the
filter function is easily tunable, as it is determined by the spacing $\tau$
between pulses in the sensing sequence; while the bandwidth (spectral
detection window) is determined by the number of repetitions, $n$, of the
XY8-n sequence [16]. For the present demonstration, using an XY8-8 sensing
sequence and signal frequencies $\sim{1}$ MHz, the filter function bandwidth
$\approx 30$ kHz. This demonstration illustrates the ability of the RF-QDM to
perform micron-scale spatial discrimination of spectrally well-separated
narrowband signals.

In Figure 2(b), the measured RF magnetic field distributions are compared to
numerical simulations. We calculate the three-dimensional magnetic field
generated by a static current of $\mathrm{6.6\,\mu A}$ constrained to the
physical geometry of the spiral-shaped microcoils using finite element
software [17]. Averaging the magnetic field at a $\mathrm{10\,\mu m}$ stand-
off distance over the $\mathrm{10\,\mu m}$ NV layer thickness and projecting
the field vector onto the sensing NV orientation, the simulated magnetic field
images are consistent with measurements. To highlight the sensitivity and
stability of the RF-QDM, we reduce the amplitude of the two test signals to
26.4 nA. Following an acquisition time of 3600 s for each signal frequency, we
obtain images of the in-phase narrowband magnetic field amplitude generated by
each microcoil, with a peak-to-peak difference of about 500 pT and
$\mathrm{SNR}\approx{15}$ [Fig. 2(d)].

###  III.2 RF magnetic imaging performance

Figure 3:  Suppressing spatial noise in RF-QDM magnetic images by averaging
over time and/or space. (a) Measured magnetic spatial noise floor
$\sigma_{\mathrm{Spatial}}$ with no RF signal applied to the microcoils and a
1 MHz detection frequency, as a function of data acquisition time
$T_{\mathrm{acq}}$, for $\mathrm{2\,\mu m}$ single pixel resolution and
$\mathrm{10\,\mu m}$ (binned) super pixel resolution. A dashed black line
depicts the power law scaling behavior $\propto T_{\mathrm{acq}}^{-1/2}$ as a
guide to the eye. (b) Example RF-QDM magnetic images of the noise background
and resulting $\sigma_{\mathrm{Spatial}}$: i) 2$\,\mathrm{\mu m}$ single pixel
resolution with $T_{\mathrm{acq}}=1.2\,\mathrm{s}$; ii) 10$\,\mathrm{\mu m}$
super pixel resolution with $T_{\mathrm{acq}}=1.2\,\mathrm{s}$; iii)
10$\,\mathrm{\mu m}$ super pixel resolution with
$T_{\mathrm{acq}}=3600\,\mathrm{s}$. Corresponding values for
$\sigma_{\mathrm{Spatial}}$ are labeled in (a). All scale bars correspond to
50$\,\mathrm{\mu m}$ in this image. The magnetic noise is distributed randomly
without significant spatial correlation. The comparison between i) and ii)
shows that spatial noise can be averaged over space, while the comparison
between ii) and iii) illustrates an average over time. Note that the frame
rate of the lock-in camera is constant in all measurements Figure 4: RF-QDM
imaging of arbitrary phase, multi-frequency magnetic fields using the CASR
protocol. As described in the main text, a single tone alternating current (1
MHz + 125 Hz) is applied to the upper microcoil; and a two tone current (1 MHz
+ 65 Hz and 1 MHz + 185 Hz) is simultaneously applied to the lower microcoil.
Both currents have peak amplitude $I_{p}=\mathrm{3.3\,\mu A}$. The phases of
each signal frequency component (tone) are arbitrary but fixed during the
measurements. Total image acquisition time is 200 s. (a) CASR images of RF
magnetic field amplitudes at each signal frequency component, as well as at
alias frequencies (1 Hz and 250 Hz) far from any signal tone. The imaged
arbitrary phase magnetic field amplitudes have SNR of approximately 70
(calculated with the same method as [Fig. 2(b)]) for each signal frequency
component, and exhibit good agreement with images of in-phase signals shown in
[Fig. 2(b)]. (b) CASR spectra (FFT) of three test pixels $\mathrm{P_{1,2,3}}$
with positions labeled in (a). Peaks corresponding to three frequency
components $f_{1,2,3}$ are clearly illustrated in the spectra, with peak
heights proportional to the RF magnetic field amplitude. Linewidths of all
spectral peaks $\approx 1$ Hz. Amplitude offsets are added to the spectra of
$\mathrm{P_{1,2}}$ for a better visualization. (c) CASR images of RF magnetic
field phases at each signal frequency component, as well as at alias
frequencies far from any signal tone (1 Hz and 250 Hz). Noisy phase
distributions in no-signal regions are attributed to the ratio of near-zero
real and imaginary amplitudes of the CASR FFT. (d) Polar plots illustrate the
phases of each signal frequency component, derived from CASR measurements.
$\phi_{1,3}$ are determined from the real and imaginary parts of the FFT of
pixel $\mathrm{P_{3}}$; and $\phi_{2}$ is determined from $\mathrm{P_{1}}$.
Red and blue dots represent the real and imaginary amplitudes of the
associated FFT spectra, respectively, while black arrows indicate the absolute
value of the amplitude as shown in (a). Grids are depicted as circles with a
step size equivalent to the radius of 10 arbitrary units of FFT amplitude. All
scale bars in (a) and (c) correspond to 50$\,\mathrm{\mu m}$.

With a $20\times$ magnification objective and $39.6\,\mathrm{\mu m}$ camera
pixel size, the RF-QDM has a $2\,\mathrm{\mu m}$ lateral optical resolution,
corresponding to the size of a single pixel in the magnetic images [Fig.
2(b,d)]. For the dynamic-decoupling-based experiments discussed above, we vary
the number of XY8 sequences and measure the narrowband sensitivity of each
pixel across the FOV for a 1 MHz signal frequency, as shown in the
Supplemental Material [15]. The optimized sequence is determined to be XY8-8,
with a median per-pixel sensitivity of $0.93(1)\,$nT$\cdot$Hz-1/2. Given a
sensing voxel volume $\approx 40\,\mathrm{\mu m}^{3}$ for the $10\,\mathrm{\mu
m}$ NV layer diamond, the volume-normalized sensitivity is about 6
nT$\cdot$Hz${}^{-1/2}\cdot\mathrm{\mu m}^{3/2}$, which is comparable to state-
of-the-art ensemble NV sensitivity using XY8 sequences and photodiode (non-
imaging) measurements [18]. We estimate the contribution of two per-pixel
noise sources: photon shot noise $\approx 0.3\,$nT$\cdot$Hz-1/2 and camera
quantization noise $\approx 0.86\,$nT$\cdot$Hz-1/2 [15], indicating that the
present RF-QDM’s narrowband sensitivity is limited by the camera. A similar
performance analysis is presented in the Supplemental Material [15] for the
RF-QDM using the 1.7 $\mathrm{\mu m}$ NV layer diamond.

In addition to per-pixel sensitivity, spatial noise is another significant
parameter for evaluating RF-QDM performance. To characterize the spatial
noise, we conduct single-frame measurements without an applied RF magnetic
signal. At a given narrowband detection frequency, the spatial noise floor of
an RF-QDM image, $\sigma_{\mathrm{Spatial}}$, is defined as the pixel-to-pixel
standard deviation of the measured in-phase magnetic field amplitude at the
detection frequency, determined across the entire FOV; and is inversely
proportional to the square root of the averaging frame number. During magnetic
imaging, a trade-off arises between spatial resolution and spatial noise. One
strategy to mitigate spatial noise is to bin pixels: i.e., group adjacent
pixels together to create ”super pixels”; and thereby, effectively reduce the
overall spatial noise in the image, albeit with coarser spatial resolution.
For applications where the highest spatial resolution of the RF-QDM is not
required, averaging noise over the space domain rather than the time domain
allows for shorter acquisition times with similar SNR. We demonstrate this
approach for 5$\times$5 binned super pixels of size 10$\,\mathrm{\mu m}$. With
no signal applied to the microcoils, we observe
$\sigma_{\mathrm{Spatial}}\propto T_{\mathrm{acq}}^{-1/2}$ for RF-QDM images
and a 1 MHz detection frequency, where $T_{\mathrm{acq}}$ is the data
acquisition time for both 2 $\mathrm{\mu m}$ (unbinned) pixels and 10
$\mathrm{\mu m}$ super pixels [Fig. 3(a)]. This behavior indicates that the
RF-QDM’s spatial magnetic noise can be suppressed with time averaging out to
hour timescales. The spatial noise floor of a single frame with 1.2 s
acquisition time and 10$\,\mathrm{\mu m}$ super pixel resolution is similar to
that of a 30 s average of multiple frames with 2$\,\mathrm{\mu m}$ single
pixel resolution. With 10$\,\mathrm{\mu m}$ super pixel spatial resolution and
1 hour acquisition time, the RF-QDM can average the spatial noise background
down to 7.1(1) pT [Fig. 3(b)]. The median per-pixel narrowband sensitivity for
a 10$\,\mathrm{\mu m}$ super pixel is also improved to
$0.18(1)\,$nT$\cdot$Hz-1/2.

###  III.3 Simultaneous amplitude and phase imaging

Next, we implement the CASR protocol in the RF-QDM, and simultaneously image
the amplitude and phase of a multi-frequency RF magnetic field, with spectral
resolution $\approx 1$ Hz as shown in [Fig. 4]. For these demonstration
measurements, we simultaneously apply a dual-tone alternating current
$I_{1}=I_{p}\cos[2\pi(f_{0}+f_{1})t+\theta_{1}]+I_{p}\cos[2\pi(f_{0}+f_{3})t+\theta_{3}]$
to the lower microcoil, and a single-tone current
$I_{2}=I_{p}\cos[2\pi(f_{0}+f_{2})t+\theta_{2}]$ to the upper microcoil. Here,
$f_{0}=1\,$MHz is the carrier frequency; $f_{1,2,3}$ are 65 Hz, 125 Hz and 185
Hz, respectively; and $\theta_{1,2,3}$ are arbitrary phase offsets of each
frequency component, which are fixed during the measurements. The current peak
value of $I_{p}=\mathrm{3.3\,\mu A}$ is half that of the in-phase signals used
in the above results [Fig. 2(b)], with estimated peak RF magnetic field
amplitude less than 40 nT, which is in the linear response range of the
measurement. The lock-in camera frame rate is set to $f_{\mathrm{SR}}=500$ Hz
with 8 demodulation cycles per frame. After 1 second of measurement, a series
of NV fluorescence images are acquired [Fig. 1(c)], which gives the RF
magnetic field change in the time domain, sampled at the CASR alias frequency.
We repeat the measurement 100 times to average down the noise floor, then
perform an FFT to the measured time-dependent fluorescence change of each
pixel. Finally, we normalize the CASR signal amplitude to the steady-state NV
fluorescence for each pixel (in the absence of any NV spin-state modulation)
to extract the magnetic field amplitude images of each frequency component, as
described in the Supplemental Material [15].

Three test pixels are selected to illustrate the CASR amplitude imaging
results [Fig. 4(a,b)]. For pixel $\mathrm{P_{1}}$, which is located at the
upper microcoil, only one frequency peak at $f_{2}$ is observed in the CASR
spectrum. Pixel $\mathrm{P_{3}}$, located at the lower microcoil, exhibits two
frequency components in its CASR spectrum, corresponding to $f_{1}$ and
$f_{3}$. The central pixel $\mathrm{P_{2}}$, located between the two
microcoils, exhibits frequency components from both sources, with reduced
amplitude. In all cases, the linewidth of CASR spectral peaks $\Delta f\approx
1$ Hz (see Supplemental Material [15]). From the CASR spectra of each pixel,
we can generate magnetic field amplitude images for each frequency component
within the measurement bandwidth, as shown in [Fig. 4(a)] for five different
example frequencies. The illustration of CASR amplitude images at frequencies
$f_{1,2,3}$ provides insight into the RF magnetic field patterns generated by
both microcoils, consistent with the in-phase magnetic field images and
simulation results presented in [Fig. 2(b)].

The CASR protocol also enables simultaneous magnetic field phase imaging for
each frequency component within the measurement bandwidth. The phases of each
measured frequency component, $\phi_{1,2,3}$, are dependent on the phases,
$\theta_{1,2,3}$, of the input test signals, the impedance of the microcoils,
and the time delay between the current signal and the CASR sensing sequence.
All of these parameters are fixed during measurement, which provides arbitrary
but stable phases for the imaged RF magnetic field patterns. When extracting
the CASR amplitude from time-domain NV fluorescence measurements, it is
essential to consider the phase-dependent real and imaginary components of the
CASR FFT spectra. The root mean square of these components yields the
amplitude, while the phase can be extracted from the ratio. Based on the
measured real and imaginary components of the CASR spectra at locations P1 and
P3, we determine phases $\phi_{1,2,3}$ for each signal frequency components
with accuracy of $0.6^{\circ}$ calculated from the noise floor in [Fig. 4(b)].
Repeating the same phase calculation for each pixel across the FOV, we obtain
phase images at each frequency step of the CASR spectra, as shown in [Fig.
4(c)]. Further details are discussed in the Supplemental Material [15].

##  IV Discussion

Figure 5: RF-QDM CASR imaging of weighted frequency and phase of magnetic
signals generated by the two microcoils for the same experimental conditions
and CASR imaging protocol as in [Fig. 4]. (a) The weighted frequency
$f_{w}=\int f\cdot S_{\mathrm{CASR}}\,df/\int S_{\mathrm{CASR}}\,df$. A
transparency mask, based on the integral of the CASR amplitude of each pixel,
is applied to the image data to filter out the no-signal region for better
visualization. The colorbar indicates the frequency integration range, which
in this example includes $f_{1}$ and $f_{2}$, but excludes $f_{3}$. The near-
field, micron-scale spatial distribution of the two included frequency
components is clearly illustrated in the image. (b) Weighted phase image of
the RF magnetic signal generated by the two microcoils. The weighted phase
$\phi_{w}=\int\phi\cdot S_{\mathrm{CASR}}\,df/\int S_{\mathrm{CASR}}\,df$. The
transparency mask and integration range are the same as in (a). The spatial
distribution of $\phi_{1}=-6^{\circ}$ and $\phi_{2}=80^{\circ}$ is clearly
illustrated. The $\sim{180^{\circ}}$ phase shifted signal outside each spiral
is due to the opposite direction of RF magnetic field generated by the
microcoils. All scale bars correspond to 50$\,\mathrm{\mu m}$ in this image.

A promising application of the RF-QDM is micron-scale real-space NMR imaging.
The RF-QDM’s Hz-scale spectral resolution will enable micron-scale spatial
identification of chemical shifts and J-couplings in samples on the diamond
surface [9] — key spectral identifiers of molecular structure that are
critical to NMR applications in chemistry, life science, and materials
research. Based on the results from previous NV-NMR research [9, 19], we
estimate a spatial noise threshold of 50 pT for resolvable micron-scale NMR
images with $\mathrm{SNR}>1$ for a thermally polarized sample in an external
(bias) magnetic field $\sim{0.1}$ T; and a spatial noise threshold of 10 nT if
using dynamic nuclear polarization (DNP) of the sample. Assuming the current
RF-QDM performance, an acquisition time around 40 s is required for a
thermally polarized sample to surpass this threshold with spatial resolution
of 10$\,\mathrm{\mu m}$; whereas DNP hyperpolarization would allow the
threshold to be passed in about 30 ms with 2$\,\mathrm{\mu m}$ spatial
resolution [Fig. 3(a)]. (Other hyperpolarization techniques can provide even
greater nuclear spin polarization, and NV-NMR signal amplitude [20], albeit
with operation typically limited to lower bias fields.) With the millisecond-
scale time resolution of the RF-QDM’s lock-in camera, it may thus be possible
to image real-time NMR dynamics in micron-scale samples, including chemical
processes in living systems at the cellular scale and samples within
microfluidic structures [21]. To demonstrate the ability of the RF-QDM to
spatially discriminate narrowband signals that are spectrally crowded (i.e.,
differ in frequency by a few tens of Hz, as in many NMR signals of interest),
we show example CASR images of weighted frequency and phase from the two
microcoils with 60 Hz offset in their signals (around a 1 MHz carrier
frequency) [Fig. 5]. The near-field RF signals produced by the two microcoils
are clearly distinguished at the micron scale by their different frequencies
and phases. Similar weighted frequency (or phase) NV-NMR images could allow
detailed spatial monitoring of chemical concentratons in samples of interest.

In this work, the amplitude of RF magnetic signals is held constant during
measurements. However, it is worth noting that RF-QDM measurement protocols
are applicable to signals with decay envelopes, such as free-inductive-decay
(FID) in NMR experiments. A time domain decay envelope manifests as finite FFT
peak linewidths in the CASR spectra of each pixel, yielding spatial
information from both the imaged signal frequencies and linewidths. This
capability of real-space imaged NV-NMR spectroscopy holds promise for
applications involving micron-scale samples with spatially varying
concentrations and local environments of NMR-detectable nuclear species.
Furthermore, technical advances in lock-in camera frame rate and storage
capacity can lead to corresponding improvements in sensing bandwidth and
spectral resolution, for micron-scale NMR and other applications. In addition,
the RF-QDM should be compatible with magnetic field gradients applied to the
NV spins [22, 23], potentially allowing combined Fourier and real-space NMR
imaging with nanoscale spatial resolution and FOV approaching a millimeter.

We also highlight other promising applications of the RF-QDM. Previous work
demonstrated single-channel measurement of the AC susceptibility of 2D
magnetic materials using NV centers and XY8 sequences [24]. The RF-QDM will
allow micron-scale mapping of the spatial distribution of AC susceptibility,
which could serve as an important calibration process for material science
investigations. The RF-QDM can also be applied to imaging inductive eddy
currents in electrical conductors [25]. By simultaneously imaging the
amplitude and phase of the inductive AC magnetic signal, the RF-QDM could
provide higher spatial resolution and better sensitivity than conventional
techniques for eddy current inspection. For integrated circuits and other
micron-scale electronics, the RF-QDM may be useful for failure analysis [26,
27] and hardware Trojan detection [28]. Mapping circuit magnetic field
activity across various operating frequencies, the RF-QDM could provide
complementary information to static field images [29, 30] and other
electromagnetic properties. Additionally, the RF-QDM can be applied to
impedance tomography, which is widely used in biological science [31]. Instead
of using arrays of coplanar electrodes to detect the spatial distribution of
modulated stimulation currents in living cells and tissue — which is invasive
and only provides a limited set of point measurements — the RF-QDM may provide
a noninvasive direct image of the current amplitude and phase, with full
spatial coverage across the FOV.

In summary, we characterize the performance of a lock-in-camera-based quantum
diamond microscope (QDM) for narrowband magnetic field imaging; and
demonstrate imaging protocols for multi-frequency widefield imaging of near-
field patterns of radiofrequency (RF) signals generated by fabricated
microcoils, with $\approx 1$ Hz spectral resolution. This RF-QDM provides per-
pixel sensitivity to narrowband fields $\sim{1}\,$nT$\cdot$Hz-1/2 with
$\approx 2$ $\mathrm{\mu m}$ spatial resolution across a
300$\times$300$\,\mathrm{\mu m^{2}}$ field-of-view (FOV). With temporal
averaging and spatial binning, the spatial noise floor is reduced to the
picotesla-scale, similar to the expected signals from potential applications
of real-space NV-NMR imaging using thermally-polarized samples. The RF-QDM
simultaneously captures the micron-scale spatial distribution of the
amplitude, frequency, and phase of RF magnetic fields, also paving the way for
other application areas such as AC susceptibility imaging, eddy current
inspection, assessment of electronic circuits, and impedance tomography.

Note that for the results presented here, the RF magnetic field amplitude
images depict the projected value on the sensing NV orientation. However, as
there are four NV orientations in diamond, the RF-QDM should be applicable to
imaging of the RF magnetic field vector [32]. This capability, to be
demonstrated in future work, could also allow the correction of image
artifacts related to variation of RF magnetic field direction across the
finite NV layer. In addition, the present RF-QDM is limited to signal
frequencies $<10$ MHz by technical constraints on the NV Rabi frequency.
Future advances in delivered MW signal strength (and hence increased NV Rabi
frequency), or the application of a quantum frequency mixing protocol [33],
will allow imaging of higher signal frequencies ($\sim{10}$ MHz to GHz range).
We also note that the RF-QDM is compatible with narrowband sensing protocols
that provide improved sensitivity compared to XY8, e.g., DROID-60 [34, 35] and
quantum logic enhanced repetitive readout [18]. However, these protocols
typically have increased complexity, resulting in a longer sensing time.
Currently, due to limitations imposed by the memory of the RF-QDM’s lock-in
camera, employing such protocols would reduce the frame rate and available
measurement time, leading to a reduced spectral detection window for CASR
imaging. Improvements in camera technology and data processing algorithms may
help alleviate these limitations, further enhancing RF-QDM performance for
narrowband magnetic field imaging.

###### Acknowledgements.

We acknowledge the Maryland NanoCenter and its FabLab for providing
instruments and assistance to fabricate the microwave waveguide and
microcoils. This work is supported by, or in part by, the U.S. Army Research
Laboratory under Contract No. W911NF1920181; the U.S. Army Research Office
under Grant No. W911NF2120110; the U.S. Air Force Office of Scientific
Research under Grant No. FA9550-22-1-0312; the Gordon & Betty Moore Foundation
under Grant No. 7797.01; and the University of Maryland Quantum Technology
Center.

## References

  * Taylor  _et al._ [2008] J. M. Taylor, P. Cappellaro, L. Childress, L. Jiang, D. Budker, P. R. Hemmer, A. Yacoby, R. Walsworth, and M. D. Lukin, High-sensitivity diamond magnetometer with nanoscale resolution, Nature Physics 4, 810 (2008).
  * Barry  _et al._ [2020] J. F. Barry, J. M. Schloss, E. Bauch, M. J. Turner, C. A. Hart, L. M. Pham, and R. L. Walsworth, Sensitivity optimization for nv-diamond magnetometry, Reviews of Modern Physics 92, 10.1103/RevModPhys.92.015004 (2020).
  * Levine  _et al._ [2019] E. V. Levine, M. J. Turner, P. Kehayias, C. A. Hart, N. Langellier, R. Trubko, D. R. Glenn, R. R. Fu, and R. L. Walsworth, Principles and techniques of the quantum diamond microscope, Nanophotonics 8, 1945 (2019).
  * Devience  _et al._ [2015] S. J. Devience, L. M. Pham, I. Lovchinsky, A. O. Sushkov, N. Bar-Gill, C. Belthangady, F. Casola, M. Corbett, H. Zhang, M. Lukin, H. Park, A. Yacoby, and R. L. Walsworth, Nanoscale nmr spectroscopy and imaging of multiple nuclear species, Nature Nanotechnology 10, 129 (2015).
  * Dasika  _et al._ [2023] S. Dasika, M. Parashar, and K. Saha, Mapping ac susceptibility with quantum diamond microscope, Review of Scientific Instruments 94, 10.1063/5.0138301 (2023).
  * Chatzidrosos  _et al._ [2019] G. Chatzidrosos, A. Wickenbrock, L. Bougas, H. Zheng, O. Tretiak, Y. Yang, and D. Budker, Eddy-current imaging with nitrogen-vacancy centers in diamond, Physical Review Applied 11, 10.1103/PhysRevApplied.11.014060 (2019).
  * Mizuno  _et al._ [2020] K. Mizuno, H. Ishiwata, Y. Masuyama, T. Iwasaki, and M. Hatano, Simultaneous wide-field imaging of phase and magnitude of ac magnetic signal using diamond quantum magnetometry, Scientific Reports 10, 10.1038/s41598-020-68404-5 (2020).
  * Tang  _et al._ [2023] J. Tang, Z. Yin, C. A. Hart, J. W. Blanchard, J. T. Oon, S. Bhalerao, J. M. Schloss, M. J. Turner, and R. L. Walsworth, Quantum diamond microscope for dynamic imaging of magnetic fields, AVS Quantum Science 5, 10.1116/5.0176317 (2023).
  * Glenn  _et al._ [2018] D. R. Glenn, D. B. Bucher, J. Lee, M. D. Lukin, H. Park, and R. L. Walsworth, High-resolution magnetic resonance spectroscopy using a solid-state spin sensor, Nature 555, 351 (2018).
  * Klimov  _et al._ [2015] P. V. Klimov, A. L. Falk, D. J. Christle, V. V. Dobrovitski, and D. D. Awschalom, Quantum entanglement at ambient conditions in a macroscopic solid-state spin ensemble, Science Advances 1, 10.1126/sciadv.1501015 (2015).
  * Widmann  _et al._ [2015] M. Widmann, S. Y. Lee, T. Rendler, N. T. Son, H. Fedder, S. Paik, L. P. Yang, N. Zhao, S. Yang, I. Booker, A. Denisenko, M. Jamali, S. A. Momenzadeh, I. Gerhardt, T. Ohshima, A. Gali, E. Janzén, and J. Wrachtrup, Coherent control of single spins in silicon carbide at room temperature, Nature Materials 14, 164 (2015).
  * Jiang  _et al._ [2023] Z. Jiang, H. Cai, R. Cernansky, X. Liu, and W. Gao, Quantum sensing of radio-frequency signal with nv centers in sic, Science Advances 9, eadg2080 (2023).
  * Gottscholl  _et al._ [2020] A. Gottscholl, M. Kianinia, V. Soltamov, S. Orlinskii, G. Mamin, C. Bradac, C. Kasper, K. Krambrock, A. Sperlich, M. Toth, I. Aharonovich, and V. Dyakonov, Initialization and read-out of intrinsic spin defects in a van der waals crystal at room temperature, Nature Materials 19, 540 (2020).
  * Rizzato  _et al._ [2023] R. Rizzato, M. Schalk, S. Mohr, J. C. Hermann, J. P. Leibold, F. Bruckmaier, G. Salvitti, C. Qian, P. Ji, G. V. Astakhov, U. Kentsch, M. Helm, A. V. Stier, J. J. Finley, and D. B. Bucher, Extending the coherence of spin defects in hbn enables advanced qubit control and quantum sensing, Nature Communications 14, 10.1038/s41467-023-40473-w (2023).
  * [15] See supplemental material at http://link.aps.org/ supplemental/ for additional details of this paper.
  * Łukasz Cywiński  _et al._ [2008] Łukasz Cywiński, R. M. Lutchyn, C. P. Nave, and S. D. Sarma, How to enhance dephasing time in superconducting qubits, Physical Review B - Condensed Matter and Materials Physics 77, 10.1103/PhysRevB.77.174509 (2008).
  * com [2021] COMSOL Multiphysics® v. 6.0 (2021).
  * Arunkumar  _et al._ [2023] N. Arunkumar, K. S. Olsson, J. T. Oon, C. A. Hart, D. B. Bucher, D. R. Glenn, M. D. Lukin, H. Park, D. Ham, and R. L. Walsworth, Quantum logic enhanced sensing in solid-state spin ensembles, Physical Review Letters 131, 10.1103/PhysRevLett.131.100801 (2023).
  * Bucher  _et al._ [2020] D. B. Bucher, D. R. Glenn, H. Park, M. D. Lukin, and R. L. Walsworth, Hyperpolarization-enhanced nmr spectroscopy with femtomole sensitivity using quantum defects in diamond, Physical Review X 10, 10.1103/PhysRevX.10.021053 (2020).
  * Arunkumar  _et al._ [2021] N. Arunkumar, D. B. Bucher, M. J. Turner, P. Tomhon, D. Glenn, S. Lehmkuhl, M. D. Lukin, H. Park, M. S. Rosen, T. Theis, and R. L. Walsworth, Micron-scale nv-nmr spectroscopy with signal amplification by reversible exchange, PRX Quantum 2, 10.1103/PRXQuantum.2.010305 (2021).
  * Briegel  _et al._ [2024] K. D. Briegel, N. R. V. Grafenstein, J. C. Draeger, P. Blümler, R. D. Allert, and D. B. Bucher, Optical widefield nuclear magnetic resonance microscopy, arXiv (2024).
  * Arai  _et al._ [2015] K. Arai, C. Belthangady, H. Zhang, N. Bar-Gill, S. J. DeVience, P. Cappellaro, A. Yacoby, and R. L. Walsworth, Fourier magnetic imaging with nanoscale resolution and compressed sensing speed-up using electronic spins in diamond, Nature Nanotechnology 10, 859 (2015).
  * Zhang  _et al._ [2017] H. Zhang, K. Arai, C. Belthangady, J. C. Jaskula, and R. L. Walsworth, Selective addressing of solid-state spins at the nanoscale via magnetic resonance frequency encoding, npj Quantum Information 3, 10.1038/S41534-017-0033-3 (2017).
  * Zhang  _et al._ [2021] X.-Y. Zhang, Y.-X. Wang, T. A. Tartaglia, T. Ding, M. J. Gray, K. S. Burch, F. Tafti, and B. B. Zhou, ac susceptometry of 2d van der waals magnets enabled by the coherent control of quantum sensors, PRX Quantum 2, 030352 (2021).
  * Wang  _et al._ [2018] C. Wang, M. Fan, B. Cao, B. Ye, and W. Li, Novel noncontact eddy current measurement of electrical conductivity, IEEE Sensors Journal 18, 9352 (2018).
  * Oliver  _et al._ [2021] S. M. Oliver, D. J. Martynowych, M. J. Turne, D. A. Hopper, R. L. Walswort, and E. V. Levine, Vector Magnetic Current Imaging of an 8 nm Process Node Chip and 3D Current Distributions Using the Quantum Diamond Microscope, ISTFA 2021 , 96 (2021).
  * Kehayias  _et al._ [2023] P. Kehayias, J. Walraven, A. Rodarte, and A. Mounce, High-resolution short-circuit fault localization in a multilayer integrated circuit using a quantum diamond microscope, Phys. Rev. Appl. 20, 014036 (2023).
  * Ashok  _et al._ [2022] M. Ashok, M. J. Turner, R. L. Walsworth, E. V. Levine, and A. P. Chandrakasan, Hardware trojan detection using unsupervised deep learning on quantum diamond microscope magnetic field images, J. Emerg. Technol. Comput. Syst. 18, 10.1145/3531010 (2022).
  * Turner  _et al._ [2020] M. J. Turner, N. Langellier, R. Bainbridge, D. Walters, S. Meesala, T. M. Babinec, P. Kehayias, A. Yacoby, E. Hu, M. Lončar, R. L. Walsworth, and E. V. Levine, Magnetic field fingerprinting of integrated-circuit activity with a quantum diamond microscope, Physical Review Applied 14, 10.1103/PhysRevApplied.14.014097 (2020).
  * Kehayias  _et al._ [2022] P. Kehayias, E. V. Levine, L. Basso, J. Henshaw, M. S. Ziabari, M. Titze, R. Haltli, J. Okoro, D. R. Tibbetts, D. M. Udoni, E. Bielejec, M. P. Lilly, T. M. Lu, P. D. Schwindt, and A. M. Mounce, Measurement and simulation of the magnetic fields from a 555 timer integrated circuit using a quantum diamond microscope and finite-element analysis, Physical Review Applied 17, 10.1103/PhysRevApplied.17.014021 (2022).
  * Bounik  _et al._ [2022] R. Bounik, F. Cardes, H. Ulusan, M. M. Modena, and A. Hierlemann, Impedance imaging of cells and tissues: Design and applications, BME Frontiers 2022, 10.34133/2022/9857485 (2022).
  * Schloss  _et al._ [2018] J. M. Schloss, J. F. Barry, M. J. Turner, and R. L. Walsworth, Simultaneous broadband vector magnetometry using solid-state spins, Phys. Rev. Appl. 10, 034044 (2018).
  * Wang  _et al._ [2022] G. Wang, Y. X. Liu, J. M. Schloss, S. T. Alsid, D. A. Braje, and P. Cappellaro, Sensing of arbitrary-frequency fields using a quantum mixer, Physical Review X 12, 10.1103/PhysRevX.12.021061 (2022).
  * Zhou  _et al._ [2020] H. Zhou, J. Choi, S. Choi, R. Landig, A. M. Douglas, J. Isoya, F. Jelezko, S. Onoda, H. Sumiya, P. Cappellaro, H. S. Knowles, H. Park, and M. D. Lukin, Quantum metrology with strongly interacting spin systems, Physical Review X 10, 10.1103/PHYSREVX.10.031003 (2020).
  * Choi  _et al._ [2020] J. Choi, H. Zhou, H. S. Knowles, R. Landig, S. Choi, and M. D. Lukin, Robust dynamic hamiltonian engineering of many-body spin systems, Physical Review X 10, 10.1103/PHYSREVX.10.031002 (2020).