# A planar defect spin sensor in a two-dimensional material susceptible to
strain and electric fields

P. Udvarhelyi  Wigner Research Centre for Physics, P.O. Box 49, H-1525
Budapest, Hungary  Budapest University of Technology and Economics, Institute
of Physics, Department of Atomic Physics, Műegyetem rakpart 3., 1111 Budapest,
Hungary  T. Clua-Provost  A. Durand  Laboratoire Charles Coulomb, Université
de Montpellier and CNRS, 34095 Montpellier, France  J. Li  J. H. Edgar  Tim
Taylor Department of Chemical Engineering, Kansas State University, Kansas
66506, USA  B. Gil  G. Cassabois  V. Jacques  Laboratoire Charles Coulomb,
Université de Montpellier and CNRS, 34095 Montpellier, France  A. Gali  Wigner
Research Centre for Physics, P.O. Box 49, H-1525 Budapest, Hungary  Budapest
University of Technology and Economics, Institute of Physics, Department of
Atomic Physics, Műegyetem rakpart 3., 1111 Budapest, Hungary

###### Abstract

The boron-vacancy spin defect ($\text{V}_{\text{B}}^{-}$) in hexagonal boron
nitride (hBN) has a great potential as a quantum sensor in a two-dimensional
material that can directly probe various external perturbations in atomic-
scale proximity to the quantum sensing layer. Here, we apply first principles
calculations to determine the coupling of the $\text{V}_{\text{B}}^{-}$
electronic spin to strain and electric fields. Our work unravels the interplay
between local piezoelectric and elastic effects contributing to the final
response to the electric fields. The theoretical predictions are then used to
analyse optically detected magnetic resonance (ODMR) spectra recorded on hBN
crystals containing different densities of $\text{V}_{\text{B}}^{-}$ centres.
We prove that the orthorhombic zero-field splitting parameter results from
local electric fields produced by surrounding charge defects. By providing
calculations of the spin-strain and spin-electric field couplings, this work
paves the way towards applications of $\text{V}_{\text{B}}^{-}$ centres for
quantitative electric field imaging and quantum sensing under pressure.

##  I Introduction

Electric and strain field effects are highly important ingredients in the
tight control of solid-state spin defects for quantum technology applications.
The accurate knowledge of the couplings to these perturbations can be
harnessed either actively, e.g. for tuning the magneto-optical properties of
the defect, or passively, e.g. for sensing its close environment. Particular
attention is currently given to spin defects embedded in two-dimensional (2D)
van der Waals materials Toth and Aharonovich (2019); Chakraborty  _et al._
(2019); Ye  _et al._ (2019); Vaidya  _et al._ (2023). The main advantage of a
2D host is the proximity of the quantum defect to the surface, which improves
both its optical efficiency Li  _et al._ (2019) and sensing capabilities
Vaidya  _et al._ (2023). One of these 2D materials is hexagonal boron nitride
(hBN), which can be exfoliated down to the mono-layer limit without
compromising its chemical stability Song  _et al._ (2010); Park  _et al._
(2014). Owing to its large optical band gap ($\sim 6.0~{}\mathrm{eV}$
Cassabois  _et al._ (2016)), hBN hosts optically-active point defects in a
wide range of wavelengths. Once isolated at the individual scale, defects in
hBN were first studied as single photon emitters, providing narrow and tunable
emission lines, high brightness, and perfect photostability Tran  _et al._
(2016); Martínez  _et al._ (2016); Vogl  _et al._ (2018); Li  _et al._ (2022);
Jungwirth  _et al._ (2016); Grosso  _et al._ (2017); Noh  _et al._ (2018);
Mendelson  _et al._ (2020); Sajid  _et al._ (2020a). In addition, it was
recently shown that the electron spin resonance frequencies of some defects
can be inferred through optically detected magnetic resonance (ODMR) methods,
providing a central resource for quantum sensing applications Chejanovsky  _et
al._ (2021); Stern  _et al._ (2022); Sajid  _et al._ (2018); Weston  _et al._
(2018); Sajid  _et al._ (2020b); Auburger and Gali (2021); Gottscholl  _et
al._ (2020).

To date, the negatively-charged boron-vacancy centre
($\text{V}_{\text{B}}^{-}$) is the only point defect in hBN possessing an ODMR
response whose microscopic structure has been unambiguously identified
Gottscholl  _et al._ (2020); Abdi  _et al._ (2018); Ivády  _et al._ (2020);
Haykal  _et al._ (2022). This defect, which can be readily created by
irradiation, ion-implantation and laser writing techniques Li  _et al._
(2021); Murzakhanov  _et al._ (2021); Kianinia  _et al._ (2020); Guo  _et al._
(2022); Gao  _et al._ (2021a), has been recently employed for sensing magnetic
fields Gottscholl  _et al._ (2021); Gao  _et al._ (2021b); Huang  _et al._
(2022); Kumar  _et al._ (2022); Healey  _et al._ (2023), strain Yang  _et al._
(2022); Lyu  _et al._ (2022), and temperature Gottscholl  _et al._ (2021); Liu
_et al._ (2021); Healey  _et al._ (2023). In this context, the purpose of the
present work is to provide an accurate description of the coupling of
$\text{V}_{\text{B}}^{-}$ centres in hBN to strain and electric fields.

Under optical illumination, the $\text{V}_{\text{B}}^{-}$ centre exhibits a
very broad emission in the near-infrared Gottscholl  _et al._ (2020). Although
an experimental work has reported a zero-phonon-line (ZPL) at
$773~{}\mathrm{nm}$ Qian  _et al._ (2022), a recent theoretical study has
claimed that the full emission is phonon-assisted since optical transitions
between electronic states are forbidden Libbi  _et al._ (2022). As a
consequence, the optical emission is intrinsically dim and only dense
ensembles of $\text{V}_{\text{B}}^{-}$ centres could be detected in
experiments so far. Theory further identified that the
$\text{V}_{\text{B}}^{-}$ centre has a spin triplet ground state ($S=1$) with
$\text{D}_{\text{3h}}$ symmetry Ivády  _et al._ (2020). In the absence of
external perturbations, the ground state spin Hamiltonian only involves spin-
spin interaction and reads as

$\hat{H}_{\text{SS}}=D\left(\hat{S}_{z}^{2}-\frac{S\left(S+1\right)}{3}\right)\
.$ (1)

The axial zero-field-splitting (ZFS) parameter $D$ separates the $m_{S}=0$ and
$m_{S}=\pm 1$ spin sublevels, the notation $m_{S}$ referring to the spin
projection along the $c$-axis ($z$) of the hBN crystal. Interestingly, ODMR
spectra recorded on ensembles of $\text{V}_{\text{B}}^{-}$ centres usually
indicate two magnetic resonances at frequencies $D\pm E$, with $D\sim 3.47$
GHz and $E\sim 50$ MHz Gottscholl  _et al._ (2020). These measurements thus
reveal an additional orthorhombic splitting ($E$) of the $m_{S}=\pm 1$ levels,
which is in contradiction with the $\text{D}_{\text{3h}}$ symmetry of the
$\text{V}_{\text{B}}^{-}$ centre. While this splitting was originally
attributed to strain effects in the hBN crystal leading to a reduced symmetry
of the defect Gottscholl  _et al._ (2020); Guo  _et al._ (2022), a recent
study suggested that it results instead from the interaction with a local
electric field Gong  _et al._ (2022). However, no microscopic picture has been
applied to interpret these results. More generally, there is an urgent need to
understand the coupling of strain and electric fields to
$\text{V}_{\text{B}}^{-}$ defect spins, which is an exemplary quantum sensor
in a van der Waals material.

In this paper, we employ first principles simulations to determine the effect
of external electric and strain fields on the ground state electronic spin
structure of $\text{V}_{\text{B}}^{-}$ centres in hBN. We show that in most of
the experimental conditions the defect experiences fluctuating electric fields
that leads to an orthorhombic splitting in ODMR spectra recorded at zero
external magnetic field. We identify the microscopic origin of this effect and
determine the spin-strain and spin-electric field coupling parameters. We then
compare these results to experimental ODMR spectra recorded from neutron-
irradiated hBN crystals containing different densities of
$\text{V}_{\text{B}}^{-}$ centres. Our study proves that the orthorhombic ZFS
is caused by fluctuating electric fields and suggest that the coupling
strengths of the $\text{V}_{\text{B}}^{-}$ centres to strain and electric
fields are comparable to those of the nitrogen-vacancy (NV-) centre in diamond
Barson  _et al._ (2017); Van Oort and Glasbeek (1990).

##  II Results

###  II.1 Defect structure and spin-spin interaction

The $\text{V}_{\text{B}}^{-}$ centre in hBN is a single vacancy at the boron
site in the negatively charged state possessing $\text{D}_{3\text{h}}$ point
symmetry (Fig. 1a). In the following, the spin properties of the
$\text{V}_{\text{B}}^{-}$ centre are calculated using a large hydrogen-
terminated mono-layer flake model (see Methods). The coordinate frame is
defined as $\\{x,y\\}$ span the plane of the hBN layer, with $x$ and $y$ along
the zigzag and armchair directions of the hexagonal lattice, respectively. The
Kohn-Sham levels in the spin-polarised calculations are plotted in Fig. 1b,
showing nearby double degenerate $e^{\prime}$ and $e^{\prime\prime}$ levels
and non-degenerate $a_{1}^{\prime}$ and $a_{2}^{\prime\prime}$ levels leaving
two holes in the $e^{\prime}$ orbital of the spin minority spin channel. This
constitutes the ${}^{3}A^{\prime}_{2}$ spin triplet ground state of the
$\text{V}_{\text{B}}^{-}$ centre, in agreement with previous calculations Abdi
_et al._ (2018); Ivády  _et al._ (2020); Sajid  _et al._ (2020c). Importantly,
the spin density is dominantly localised in-plane on the three dangling bonds
of the neighbouring nitrogen atoms (Fig. 1a), as recently confirmed
experimentally by pulsed electron-nuclear double resonance techniques Gracheva
_et al._ (2023).

Starting with an unperturbed $\text{D}_{3\text{h}}$ symmetry, the mono-layer
flake model leads to an axial ZFS parameter $D=3263~{}\mathrm{MHz}$ [see Eq.
(1)], a value in fair agreement with the experimental results $D\approx
3470~{}\mathrm{MHz}$. In the Supplementary Note 1, we show that the mono-layer
flake model well represents the bulk environment around the defect because the
spin density is fully localised into the hBN plane. Although there is about 6%
discrepancy in the absolute value of the $D$ parameters obtained from our
theoretical approach and the experimental data, the accuracy in the variation
of the zero-field-splitting parameters upon external perturbations can be
reliably obtained thanks to the cancellation of numerical errors in partial
derivatives.

In the next sections, we compute the coupling coefficients of the
$\text{V}_{\text{B}}^{-}$ centre to strain and electric fields. We assume that
the strengths of theses fields are much smaller than the Coulomb interaction
between the electrons, so that strain and electric fields can be considered as
perturbations. We analyse how these interactions can lead to an orthorhombic
ZFS parameter, that splits the $m_{S}=\pm 1$ levels upon axial symmetry
breaking.

Figure 1: Geometry and electronic structure of the $\text{V}_{\text{B}}^{-}$
centre in hBN. a Visualisation of the $\text{V}_{\text{B}}^{-}$ centre and its
ground state electronic spin density (yellow) in the mono-layer flake model
used in the calculations. Green, blue, and white balls represent boron,
nitrogen and hydrogen atoms, respectively. b Kohn-Sham level structure of the
defect calculated with PBE0 functional. The orbitals are labelled according to
the irreducible representations in $\text{D}_{\text{3h}}$ point group
symmetry. The subspace of occupied orbitals are indicated in the figure. As
these lie close to each other, their labels are ordered in ascending energy
from bottom to top. The spin density of the triplet ground state originates
from the unoccupied double-degenerate $e^{\prime}$ orbital.

###  II.2 Strain field interaction

We start the analysis of external perturbations with the strain field case. We
construct the interaction Hamiltonian analogously to previous calculations
performed on the $\text{NV}^{-}$ centre in diamond featuring
$\text{C}_{\text{3v}}$ symmetry Udvarhelyi  _et al._ (2018). In the linear
approximation, the bi-linear forms of the second rank tensor of the external
strain are connected to the bi-linear forms of the $\hat{S}\circ\hat{S}$
second rank tensor by scalar coupling coefficients $g$. We write the
interaction in a symmetry-adapted form to retain only the linearly independent
couplings, i.e. we construct the irreducible representation basis in
$\text{D}_{\text{3h}}$ symmetry from the components of the strain and spin
tensors. These transform as quadratic spatial coordinates, e.g. $zz$ and
$xx+yy$ both belong to the totally symmetric $A_{1}^{\prime}$ irreducible
representation. The interaction Hamiltonian is totally symmetric, thus only
the products of the same irreducible representations of the strain and spin
are valid combinations.

According to the theory of invariants, the Hamiltonian describing the
interaction of the $\text{V}_{\text{B}}^{-}$ centre with strain can be
formulated as

$\displaystyle\hat{H}_{\text{strain}}=\left(\varepsilon_{xx}+\varepsilon_{yy}\right)g_{1}\hat{S}_{z}^{2}$
$\displaystyle+\left(\varepsilon_{xx}-\varepsilon_{yy}\right)\left[g_{2}\left(\hat{S}_{x}^{2}-\hat{S}_{y}^{2}\right)+g^{\prime}_{2}\left(\hat{S}_{x}\hat{S}_{y}+\hat{S}_{y}\hat{S}_{x}\right)\right]$
$\displaystyle+\left(\varepsilon_{xy}+\varepsilon_{yx}\right)\left[g_{3}\left(\hat{S}_{x}^{2}-\hat{S}_{y}^{2}\right)+g^{\prime}_{3}\left(\hat{S}_{x}\hat{S}_{y}+\hat{S}_{y}\hat{S}_{x}\right)\right]$
(2)

where $g_{i}$ are the coupling coefficients between $\varepsilon$ bi-linear
forms of the strain matrix elements and $\hat{S}$ bi-linear forms of the spin
projection operators reflecting a symmetry-adapted form of
$A^{\prime}_{1}\times A^{\prime}_{1}+E^{\prime}\times E^{\prime}$. Note that
the out-of-plane strain components
$\\{\varepsilon_{zx},\varepsilon_{zy},\varepsilon_{zz}\\}$ are not included in
the Hamiltonian because we consider a single-layer flake model. However, these
couplings are expected to be minor for a bulk model as well since out-of-plane
($z$) distortions have a small impact on the in-plane hBN structure that
contains the spin density of the $\text{V}_{\text{B}}^{-}$ centre (see
Supplementary Note 2).

To calculate the coupling coefficients $g_{i}$, we specify uniform strain of a
single element in the strain tensor in each calculation and apply it to our
model (see Methods). We do so for each symmetrically non-equivalent components
in Eq. (II.2), i.e. for $\varepsilon_{xx}$ and $\varepsilon_{xy}$. We then
calculate the resulting axial and orthorhombic ZFS parameters (see red data in
Fig. 2a,b) and obtain the coupling coefficients $g_{i}$ as the partial
derivatives

$\displaystyle g_{1}$ $\displaystyle=\frac{\partial
D}{\partial\varepsilon_{xx}}=(-19.2\pm
0.2)~{}\frac{\mathrm{GHz}}{\mathrm{strain}}\ \text{,}$ (3) $\displaystyle
g_{2}$
$\displaystyle=\frac{\partial(D_{xx}-D_{yy})/2}{\partial\varepsilon_{xx}}=(2.6\pm
0.8)~{}\frac{\mathrm{GHz}}{\mathrm{strain}}\ \text{,}$ (4) $\displaystyle
g^{\prime}_{2}$
$\displaystyle=\frac{\partial(D_{xy}+D_{yx})/2}{\partial\varepsilon_{xx}}=0\
\text{,}$ (5) $\displaystyle g_{3}$
$\displaystyle=\frac{\partial(D_{xx}-D_{yy})/2}{\partial\varepsilon_{xy}}\approx
0\ \text{,}$ (6) $\displaystyle g^{\prime}_{3}$
$\displaystyle=\frac{\partial(D_{xy}+D_{yx})/2}{\partial\varepsilon_{xy}}=(5.8\pm
0.1)~{}\frac{\mathrm{GHz}}{\mathrm{strain}}\ \text{,}$ (7)

where $D_{ij}$ are components of the ZFS $\bf D$-matrix defined by alternate
form of the interaction $\hat{H}=\hat{\bf S}\bf D\hat{\bf S}$. The negative
sign of $g_{1}$ implies a larger (smaller) axial ZFS parameter $D=3D_{zz}/2$
for compressive (tensile) external strain, in line with the decrease
(increase) of the distance between the localised spin density lobes.

We now analyse how the microscopic structure of the defect affects the
response to the strain. To this end, we carried out calculations for which we
strained the lattice of the defective model without allowing any local ionic
relaxation around the defect and then calculated the change in the ZFS
parameters (blue points in Fig. 2a,b). After allowing local relaxation under
the externally applied strain (red points in Fig. 2a,b), we can define the
local geometry changes in the close vicinity of the defect. The local strain
is defined from the deformation of the triangle spanned by the three
neighbouring nitrogen atom positions. We plot this local strain as a function
of the external $\varepsilon_{xx}$ strain in Fig. 2c. We identify a large
increase in the same component ($\varepsilon_{xx}$) of the local strain after
relaxation. Moreover, an additional $\varepsilon_{yy}$ component is also
activated. We attribute this effect to the smaller local stiffness at the
vacancy site originating from the broken bonds. During the relaxation of the
defect under external strain, it is energetically favourable for the vacancy
site to accommodate an increased strain compared to the externally applied
one, thus lowering the strain enthalpy of the host material in its vicinity.
Generally, the same strain enhancement is expected in defects with dangling or
weaker bonds than the ones in its host material, making this type of qubits
more sensitive strain sensors. The increased local strain after relaxation
reflects in the increase of the ZFS strain coupling strength as well. Note
that relaxation under external sheer strain ($\varepsilon_{xy}$) shows similar
trend in the ZFS coupling strength.

Figure 2: Response to an external strain component $\varepsilon_{xx}$. a Axial
ZFS parameter $D$ as a function of the external strain. b Orthorhombic ZFS
parameter $E$ as a function of the external strain. The cases of fixed and
relaxed atomic positions are compared (blue and red points), as explained in
the main text. c Local strain response for the externally applied strain. Blue
dots show the one-to-one correspondence when the atomic positions are not
allowed to relax. Red and green points show the enhanced local
$\varepsilon_{xx}$ strain and the activated local $\varepsilon_{yy}$ strain
around the defect, respectively, when its close vicinity is allowed to relax
under the strain applied to the crystal.

Finally, we convert the strain-spin coupling strengths to stress couplings, as
stress is usually more accessible and controllable in experiments than strain.
Based on the experimentally available elastic parameters of
$C_{11}=811~{}\mathrm{GPa}$ and $C_{12}=168~{}\mathrm{GPa}$ in bulk hBN Bosak
_et al._ (2006), we calculate the $h_{i}$ stress coupling coefficients as

$\displaystyle h_{1}$ $\displaystyle=\frac{g_{1}}{C_{11}+C_{12}}=(-19.6\pm
0.2)~{}\frac{\mathrm{MHz}}{\mathrm{GPa}}$ (8) $\displaystyle h_{2}$
$\displaystyle=\frac{g_{2}}{C_{11}-C_{12}}=(4.04\pm
1.2)~{}\frac{\mathrm{MHz}}{\mathrm{GPa}}\ ,$ (9)

where $C_{ij}$ elements are the second order elastic constants (stiffness
tensor) in the Voigt notation. The relation between the strain ($\varepsilon$)
and stress ($\sigma$) can be then formulated in linear elasticity as

$\left(\begin{matrix}\sigma_{xx}\\\
\sigma_{yy}\end{matrix}\right)=\left(\begin{matrix}C_{11}&C_{12}\\\
C_{12}&C_{11}\end{matrix}\right)\left(\begin{matrix}\varepsilon_{xx}\\\
\varepsilon_{yy}\end{matrix}\right)\text{.}$ (10)

With the above conversion, the Hamiltonian takes a similar form to Eq. (II.2)
using the stress coupling coefficients

$\displaystyle\hat{H}_{\text{stress}}$
$\displaystyle=h_{1}\left(\sigma_{xx}+\sigma_{yy}\right)\hat{S}_{z}^{2}$ (11)
$\displaystyle+h_{2}\left(\sigma_{xx}-\sigma_{yy}\right)\left(\hat{S}_{x}^{2}-\hat{S}_{y}^{2}\right)\text{.}$
(12)

Importantly, our calculations already indicate that $|h_{1}|>|h_{2}|$, such
that spin-stress coupling mostly leads to variations of the axial ZFS
parameter $D$ without inducing a significant orthorhombic splitting.

###  II.3 Electric field interaction

The Hamiltonian of the electric field coupling is constructed analogously to
the strain coupling as

$\hat{H}_{\text{elec}}=d_{\perp}\left[\mathcal{E}_{y}\left(\hat{S}_{x}^{2}-\hat{S}_{y}^{2}\right)+\mathcal{E}_{x}\left(\hat{S}_{x}\hat{S}_{y}+\hat{S}_{y}\hat{S}_{x}\right)\right]$
(13)

where $d_{\perp}$ is the coupling coefficient between $\mathcal{E}$ linear
forms of the electric field vector and $\hat{S}$ bi-linear forms of the spin
projection operators reflecting a symmetry-adapted form of $E^{\prime}\times
E^{\prime}$. We note that the parallel component of the electric field
($\mathcal{E}_{z}$) with respect to the hBN plane normal vector belongs to the
$A^{\prime\prime}_{2}$ irreducible representation, and it cannot couple to
either $A^{\prime}_{1}$ or $E^{\prime}$ representations of the spin operators.
As a result, the interaction with an electric field only introduces an
orthorhombic splitting without inducing variations of the axial ZFS parameter
$D$.

Figure 3: Response to an external electric field component $\mathcal{E}_{y}$.
a Orthorhombic ZFS parameter $E$ as a function of the external electric field.
The cases of fixed and relaxed atomic positions are compared (blue and red
points). b Estimated local strain from the piezo effect induced by the
external electric field.

We apply homogeneous electric field to the defective hBN model. The coupling
coefficient can be calculated as the partial derivative of the ZFS parameter
(see Fig. 3a)

$d_{\perp}=\frac{\partial E}{\partial\mathcal{E}_{y}}=(20.72\pm
0.02)~{}\frac{\mathrm{Hz~{}cm}}{\mathrm{V}}\ .$ (14)

We note that a recent experimental work has estimated the coupling strength of
the $E$ parameter to the electric field as $d_{\perp}=35~{}\mathrm{Hz\ cm/V}$
Gong  _et al._ (2022), which is in the same order of magnitude as our first-
principle results.

We now analyse the geometry relaxation under the applied external electric
field similarly as for the case of the strain perturbation discussed above. We
can identify an accompanying piezo effect resulting in
$(\varepsilon_{xx}-\varepsilon_{yy})$ and
$(\varepsilon_{xy}+\varepsilon_{yx})$ additional strains for the applied
$\mathcal{E}_{y}$ and $\mathcal{E}_{x}$ electric fields, respectively. The
former calculation is shown in Fig. 3b. This effect enhances the coupling
strength to the electric field by a factor of $1.5$ (compare blue and red
points in Fig. 3a).

The microscopic origin of the piezo enhancement effect can be explained by
changes in electrostatic interactions in the vicinity of the defect under the
applied external electric field. We obtain a significant change in the partial
charge distribution of the strongly polarised bonds around the vacancy defect
depending on the relative direction of the external electric field. This
implies a redistribution of the electron density by the electric fields, which
generates quantum mechanical forces on the ions. The origin of this effect can
be detailed in the example of the vacancy-neighbour nitrogen atom in the $y$
direction and its boron neighbours. Without the external electric field
applied, the B-N bonds are polarised with an effective dipole moment pointing
from N to B. The applied field in the positive (resp. negative) direction
increases (resp. decreases) the original charge separation and the effective
dipole moment. This results in an additional effective attractive (resp.
repulsive) force along the B-N bond and an outward (resp. inward) relaxation
of the nitrogen atom relative to the vacancy site. We identify this additional
relaxation as the $\varepsilon_{yy}$ piezo response. Thus, the relaxation of
the nitrogen atom is in the same direction as the electric field. This finally
enhances the symmetry-breaking effect of the electric field and consequently
its coupling to the $E$ parameter.

###  II.4 ODMR simulation

In the experimental ODMR signal of the $\text{V}_{\text{B}}^{-}$ centre, a
small orthorhombic $E$ splitting was reported despite its contradiction with
the defect symmetry. Recently, the effect was attributed to the local electric
field originating from nearby charged defects without providing arguments from
microscopic picture of the defect Gong  _et al._ (2022). Firstly, we simulate
the ZFS parameters of a single $\text{V}_{\text{B}}^{-}$ defects affected by
the presence of uniformly distributed random local strain or electric fields
of given magnitudes. We sample 1000 random configurations for each given
magnitude and visualise the corresponding $m_{S}=\pm 1$ eigenvalues of the ZFS
Hamiltonian as a scatter plot as a function of the perturbation magnitude.
From the simulations shown in Fig. 4, we conclude that the $E$ splitting can
be exclusively attributed to the local electric field, while the effect of
strain leads to a broadening of the signal. The sharp splitting originates
from the large difference in the magnitudes of the couplings of the $D$ and
$E$ parameters to the external fields. We can describe this with the ratio
$g_{D}/g_{E}$ of the coupling strengths. In the case of the electric field,
the $D$ parameter coupling is symmetry forbidden, $g_{D}/g_{E}\approx 0$,
leading to a sharp splitting of the magnetic resonances. In the case of the
strain field, we calculate $g_{D}/g_{E}=7.38$, which leads to a dominating
shift in the ODMR signal with broadening of the integrated spectrum. We note
that a similar effect was already observed and simulated for the
$\text{NV}^{-}$ centre in diamond Mittiga  _et al._ (2018).

Figure 4: Simulated ZFS of the $\text{V}_{\text{B}}^{-}$ centre in random
perturbation environments. a ZFS distribution as a function of normal strain
magnitude. b ZFS distribution as a function of electric field magnitude.

We now use our theoretical predictions to analyse experimental ODMR spectra
recorded at zero external magnetic field. To this end, we rely on three hBN
crystals (S1, S2 and S3) with different densities of V${}_{\rm B}^{-}$ centres
created by neutron irradiation. Their optical properties are analysed with a
confocal microscope operating at room temperature under green laser
illumination (see Methods). All crystals exhibit the characteristic emission
spectrum of V${}_{\rm B}^{-}$ centres with a broad spectral line in the near
infrared. The relative density of V${}_{\rm B}^{-}$ centres is estimated by
recording the evolution of the PL signal with the optical excitation power
[Fig. 5a]. In the considered power range, the PL signal increases linearly
with a slope proportional to the density $\rho$ of V${}_{\rm B}^{-}$ centres.
By analysing these data, we obtain $\rho_{\rm S2}\sim 4.8\times\rho_{\rm S1}$
and $\rho_{\rm S3}\sim 8.5\times\rho_{\rm S1}$.

The ODMR spectra recorded at zero external magnetic field are shown in Fig.
5b. For the three crystals, we detect the two characteristic magnetic
resonances of the V${}_{\rm B}^{-}$ centre with frequencies at $D\pm E$.
Importantly, the $D$ parameter is identical for all spectra while the
$E$-splitting increases with the density of V${}_{\rm B}^{-}$ centre. To
understand these results, we rely on a microscopic charge model originally
introduced for $\text{NV}^{-}$ defects in diamond Mittiga  _et al._ (2018). We
consider that negatively-charged V${}_{\rm B}^{-}$ centres are associated to
positively-charged defects in order to ensure charge neutrality of the hBN
crystal. These charges produce a local electric field that depends on the
charge density $\rho_{c}$. The Hamiltonian describing the interaction of a
central V${}_{\rm B}^{-}$ centre with this electric field is constructed from
Eqs. (1) and (13) using the calculated coupling coefficient $d_{\perp}$.
Moreover, we include the hyperfine splittings from the first neighbour
${}^{14}\text{N}$ nuclear spins with a coupling strength of
$47~{}\mathrm{MHz}$ Ivády  _et al._ (2020); Liu  _et al._ (2022). We keep this
value fixed in the simulation according to the obtained negligible effect of
the external electric field on the hyperfine parameters. We note that the
hyperfine constants are, however, sensitive to the strain (see Supplementary
Note 3). For the microscopic model of the charge environment, a specific
number of elementary point charges are placed at the atomic sites of hBN in a
simulation radius of $10~{}\mathrm{nm}$, corresponding to an average charge
density $\rho_{c}$ in the simulation sphere. Their position vectors relative
to the $\text{V}_{\text{B}}^{-}$ defect at the origin of the sphere is sampled
randomly from the standard normal distribution. The resulting total electric
field at the origin is supplied to the Hamiltonian as an effective external
electric field. The obtained spectrum is collected for $10^{4}$ different
random charge configurations. We then optimise the charge density and the ODMR
contrast to fit to the experimental spectra.

As shown in Fig. 5b, all ODMR spectra are well fitted by this microscopic
charge model, leading to charge densities $\rho_{c,{\rm S1}}=0.018(3)$ nm-3,
$\rho_{c,{\rm S2}}=0.046(4)$ nm-3 and $\rho_{c,{\rm S3}}=0.141(8)$ nm-3. These
results indicate that the density of charges increases linearly with the
density of V${}_{\rm B}^{-}$ centres in the hBN crystal (Fig. 5c), as expected
from the microscopic charge model.

Figure 5: PL properties and ODMR spectra of hBN crystals with different
concentration of $\text{V}_{\text{B}}^{-}$ centres. a PL signal as a function
of the optical excitation power for the three hBN crystals (S1, S2 and S3).
The dashed lines are linear fit. b ODMR spectra recorded for S1, S2 and S3
crystals from top to bottom. The solid lines are the results of the fitting
procedure based on the microscopic charge model described in the main text,
from which we extract the charge density $\rho_{c}$. c Charge density
$\rho_{c}$ as a function of the relative density of V${}_{\rm B}^{-}$ centres
in the hBN crystal. The dashed line is a linear fit.

##  III Discussion

We calculated the coupling strengths of the ZFS parameters to the symmetry
adapted components of strain and electric fields for the
$\text{V}_{\text{B}}^{-}$ defect in 2D hBN from first principles. In order to
place these values in a broader context, we briefly compare them to those of
the popular $\text{NV}^{-}$ defect in diamond. The calculated and experimental
values of the stress coupling on the axial ZFS parameter of the
$\text{NV}^{-}$ centre are $-5.17~{}\mathrm{MHz/GPa}$ Udvarhelyi  _et al._
(2018) and $-4.4~{}\mathrm{MHz/GPa}$ Barson  _et al._ (2017), respectively.
These values are weaker than that obtained for the $\text{V}_{\text{B}}^{-}$
centre $h_{1}=-19.6~{}\mathrm{MHz/GPa}$. On the other hand, the experimental
perpendicular electric field coupling $d_{\perp}$ was reported to be
$17~{}\mathrm{Hz\,cm/V}$ for $\text{NV}^{-}$ centres Van Oort and Glasbeek
(1990), which is close to the result of our calculation of about
$21~{}\mathrm{Hz\,cm/V}$ for the $\text{V}_{\text{B}}^{-}$ centre in hBN.

For the strain perturbation, the coupling to the axial ZFS parameter $D$
dominates, while it is forbidden by symmetry for electric field perturbations.
Consequently, we identify the electric field perturbation as the source of the
symmetry-breaking orthorhombic $E$ splitting in the ODMR experiments. Our
numerical simulations based on the calculated couplings can accurately model
the experimental ODMR signal in the presence of electric field perturbations
originating from point charges in the lattice sites of the bulk hBN host.
Furthermore, we identify a correlation in the defect density created by
neutron irradiation with the simulated density of point charges and ultimately
with the effective $E$ splitting in the ODMR signal.

Moreover, our DFT calculations reveal a local enhancement effect in the
coupling strengths for both strain and electric fields. The former is specific
to the vacancy nature of the defect. The latter is attributed to the local
piezo effect, i.e. an accompanying strain response under the external electric
field effect. In other words, the local piezo effect strengthens the response
of the ZFS to the external electric field which is a significant effect of
about 50%. We note that this effect goes to the opposite of one 3D qubit
system, the divacancy qubit in 4H silicon carbide Falk  _et al._ (2014). In
that case, the local piezo effect rather generates such quantum mechanical
forces on the ions that compensate the effect of the external electric field
and reduce the spin-electric field coupling by about 10%.

In summary, the spin-strain and spin-electric field couplings of V${}_{\rm
B}^{-}$ centre in hBN were determined. We proved that fluctuating electric
fields around the V${}_{\rm B}^{-}$ spins are at the origin of the
orthorhombic splitting commonly observed in ODMR spectra of ensembles
V${}_{\rm B}^{-}$ centres at zero external magnetic field. We showed that the
piezo effect is strong in the spin-electric field coupling parameters, which
may be generalised for other vacancy-type defects in hBN. This work might
guide future applications of V${}_{\rm B}^{-}$ centres in hBN, such as quantum
sensing under high pressure and electrometry.

##  IV Methods

###  IV.1 Density functional theory calculations

The nano-flake model used in the calculation is a hydrogen terminated hBN
mono-layer of up-to the fourth hexagon neighbours around the boron vacancy
defect, consisting of 147 atoms in total. Its electronic structure was
calculated by density functional theory (DFT) using the PBE0 functional Adamo
and Barone (1999) and split valence polarisation Karlsruhe basis set
(def2-SVP) Weigend and Ahlrichs (2005), as implemented in the ORCA code Neese
(2012). The spin-spin ZFS tensor is calculated using the restricted spin
density obtained from the singly occupied unrestricted natural orbitals
Sinnecker and Neese (2006). During the relaxation of the defect structure
under external strain applied, the outshell hBN hexagons were fixed in the
atomic positions that are associated the strain environment acting to the
inner part of the model.

###  IV.2 Numerical simulations

ZFS simulation of the defect under external strain and electric field
perturbations is calculated in a home-built Python code. We iterate over the
magnitude of the perturbation in $1000$ steps for the simulation range. In
each iteration, we adaptively sample (scaled by the magnitude) the random
configurations of effective perturbation vectors
$\varepsilon=(\varepsilon_{xx},\varepsilon_{yy})$ and
$\mathcal{E}=(\mathcal{E}_{x},\mathcal{E}_{y})$ for the normal strain and
electric fields, respectively. The component are sampled from uniform random
distribution and the vector is normalised. In the ODMR simulation, we sample a
uniform random distribution of positions inside a sphere, representing
parasitic charged defects around the target boron-vacancy defect. To this end,
we sample the elements of the position vector from standard normal
distribution and multiply with $d=r\sqrt[3]{x}$ random distance, where $x$ is
sampled from a uniform random normalised distribution and $r$ is the
simulation sphere radius. Then we define an underlying lattice of bulk hBN and
project the positions to the nearest lattice sites. We optimise two
parameters, the charge density and the ODMR contrast, to fit to the
experimental spectrum. The optimisation is done by the least squares method
using parallelized brute force method on a two dimensional parameter grid
refined in three consecutive optimisation cycles.

###  IV.3 hBN crystals

The three hBN crystals used in this work were synthesised through the metal
flux growth method described in Ref. [57], and irradiated at the Ohio State
University Research Reactor, which produces a thermal neutron flux of
$10^{12}$ cm-2s-1. The crystal S1 is isotopically purified with 11B ($99.4\%$)
and was irradiated with a neutron dose of $2.6\times 10^{17}$ cm-2. The two
other crystals, S2 and S3, are purified with 10B ($99.2\%$) and irradiated
with a dose of $2.6\times 10^{16}$ cm-2 and $2.6\times 10^{17}$ cm-2,
respectively. Neutron irradiation creates V${}_{\rm B}^{-}$ centres (i)
through damages induced by neutron scattering across the crystal and (ii) via
neutron absorption leading to nuclear transmutation doping. The latter process
strongly depends on the isotopic content of the hBN crystal since the neutron
capture cross-section of 10B is orders of magnitude larger than that of 11B.
As a result, neutron irradiation creates more efficiently V${}_{\rm B}^{-}$
centres in hBN crystals isotopically enriched with 10B.

###  IV.4 Experimental details

The optical properties of hBN crystals are studied with a confocal microscope
operating at ambient conditions. A laser excitation at 532 nm is focused onto
the sample with a high numerical aperture microscope objective (NA=0.95). The
PL signal is collected by the same objective and directed to a silicon
avalanche photodiode operating in the single-photon counting regime. ODMR
spectra are recorded by monitoring the PL signal while sweeping the frequency
of a microwave field applied through a copper microwire deposited on the hBN
crystal surface.

## Declarations

  * •

Data Availability The authors declare that the main data supporting the
findings of this study are available within the paper and its Supplementary
files. Part of source data is provided with this paper. The data that support
the findings of this study are available from the corresponding author upon
reasonable request.

  * •

Code Availability The codes that were used in this study are available upon
request to the corresponding author.

  * •

Acknowledgement This work was supported by the National Excellence Program for
the project of Quantum-coherent materials (NKFIH Grant No. KKP129866), the
Ministry of Culture and Innovation and the National Research, Development and
Innovation Office within the Quantum Information National Laboratory of
Hungary (Grant No. 2022-2.1.1-NL-2022-00004), the French Agence Nationale de
la Recherche under the program ESR/EquipEx+ (grant number ANR-21-ESRE-0025),
the Institute for Quantum Technologies in Occitanie through the project BONIQs
and Qfoil. Support for hBN crystal growth is provided by the Office of Naval
Research, awards numbers N00014-22-1-2582 and N00014-20-1-2474. The neutron
irradiation was supported by the U.S. Department of Energy, Office of Nuclear
Energy under DOE Idaho Operations Office Contract DE-AC07- 051D14517 as part
of a Nuclear Science User Facilities experiment. We acknowledge the support of
The Ohio State University Nuclear Reactor Laboratory and the assistance of
Susan M. White, Lei Raymond Cao, Andrew Kauffman, and Kevin Herminghuysen for
the irradiation services provided. We acknowledge the high-performance
computational resources provided by KIFÜ (Governmental Agency for IT
Development) institute of Hungary.

  * •

Funding Open access funding provided by ELKH Wigner Research Centre for
Physics.

  * •

Author contribution PU carried out the DFT calculations and analysed the
results under the supervision of AG. TCP, AD, BG, GC and VJ performed the
experiments. JL and JHE provided the neutron-irradiated hBN crystals. PU, VJ
and AG wrote the paper. All authors contributed to the discussion and
commented on the manuscript. AG conceived and led the entire scientific
project.

  * •

Competing Interests The authors declare that there are no competing interests.

  * •

Correspondence Correspondence should be addressed to A.G. (email:
gali.adam@wigner.hu).

## References

  * Toth and Aharonovich (2019) M. Toth and I. Aharonovich, Annual Review of Physical Chemistry 70, 123 (2019).
  * Chakraborty  _et al._ (2019) C. Chakraborty, N. Vamivakas, and D. Englund, Nanophotonics 8, 2017 (2019).
  * Ye  _et al._ (2019) M. Ye, H. Seo, and G. Galli, npj Computational Materials 5, 44 (2019).
  * Vaidya  _et al._ (2023) S. Vaidya, X. Gao, S. Dikshit, I. Aharonovich, and T. Li, “Quantum sensing and imaging with spin defects in hexagonal boron nitride,” (2023), arXiv:2302.11169 .
  * Li  _et al._ (2019) X. Li, R. A. Scully, K. Shayan, Y. Luo, and S. Strauf, ACS Nano 13, 6992 (2019).
  * Song  _et al._ (2010) L. Song, L. Ci, H. Lu, P. B. Sorokin, C. Jin, J. Ni, A. G. Kvashnin, D. G. Kvashnin, J. Lou, B. I. Yakobson, and P. M. Ajayan, Nano Letters 10, 3209 (2010).
  * Park  _et al._ (2014) J.-H. Park, J. C. Park, S. J. Yun, H. Kim, D. H. Luong, S. M. Kim, S. H. Choi, W. Yang, J. Kong, K. K. Kim, and Y. H. Lee, ACS Nano 8, 8520 (2014).
  * Cassabois  _et al._ (2016) G. Cassabois, P. Valvin, and B. Gil, Nature Photonics 10, 262 (2016).
  * Tran  _et al._ (2016) T. T. Tran, C. Zachreson, A. M. Berhane, K. Bray, R. G. Sandstrom, L. H. Li, T. Taniguchi, K. Watanabe, I. Aharonovich, and M. Toth, Phys. Rev. Appl. 5, 034005 (2016).
  * Martínez  _et al._ (2016) L. J. Martínez, T. Pelini, V. Waselowski, J. R. Maze, B. Gil, G. Cassabois, and V. Jacques, Phys. Rev. B 94, 121405 (2016).
  * Vogl  _et al._ (2018) T. Vogl, G. Campbell, B. C. Buchler, Y. Lu, and P. K. Lam, ACS Photonics 5, 2305 (2018).
  * Li  _et al._ (2022) S. Li, A. Pershin, G. Thiering, P. Udvarhelyi, and A. Gali, The Journal of Physical Chemistry Letters 13, 3150 (2022).
  * Jungwirth  _et al._ (2016) N. R. Jungwirth, B. Calderon, Y. Ji, M. G. Spencer, M. E. Flatté, and G. D. Fuchs, Nano Letters 16, 6052 (2016).
  * Grosso  _et al._ (2017) G. Grosso, H. Moon, B. Lienhard, S. Ali, D. K. Efetov, M. M. Furchi, P. Jarillo-Herrero, M. J. Ford, I. Aharonovich, and D. Englund, Nature Communications 8, 705 (2017).
  * Noh  _et al._ (2018) G. Noh, D. Choi, J.-H. Kim, D.-G. Im, Y.-H. Kim, H. Seo, and J. Lee, Nano Letters 18, 4710 (2018).
  * Mendelson  _et al._ (2020) N. Mendelson, M. Doherty, M. Toth, I. Aharonovich, and T. T. Tran, Advanced Materials 32, 1908316 (2020).
  * Sajid  _et al._ (2020a) A. Sajid, M. J. Ford, and J. R. Reimers, Reports on Progress in Physics 83, 044501 (2020a).
  * Chejanovsky  _et al._ (2021) N. Chejanovsky, A. Mukherjee, J. Geng, Y.-C. Chen, Y. Kim, A. Denisenko, A. Finkler, T. Taniguchi, K. Watanabe, D. B. R. Dasari, P. Auburger, A. Gali, J. H. Smet, and J. Wrachtrup, Nature Materials 20, 1079 (2021).
  * Stern  _et al._ (2022) H. L. Stern, Q. Gu, J. Jarman, S. Eizagirre Barker, N. Mendelson, D. Chugh, S. Schott, H. H. Tan, H. Sirringhaus, I. Aharonovich, and M. Atatüre, Nature Communications 13, 618 (2022).
  * Sajid  _et al._ (2018) A. Sajid, J. R. Reimers, and M. J. Ford, Phys. Rev. B 97, 064101 (2018).
  * Weston  _et al._ (2018) L. Weston, D. Wickramaratne, M. Mackoit, A. Alkauskas, and C. G. Van de Walle, Phys. Rev. B 97, 214104 (2018).
  * Sajid  _et al._ (2020b) A. Sajid, J. R. Reimers, R. Kobayashi, and M. J. Ford, Phys. Rev. B 102, 144104 (2020b).
  * Auburger and Gali (2021) P. Auburger and A. Gali, Phys. Rev. B 104, 075410 (2021).
  * 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, Nature Materials 19, 540 (2020).
  * Abdi  _et al._ (2018) M. Abdi, J.-P. Chou, A. Gali, and M. B. Plenio, ACS Photonics 5, 1967 (2018).
  * Ivády  _et al._ (2020) V. Ivády, G. Barcza, G. Thiering, S. Li, H. Hamdi, J.-P. Chou, Ö. Legeza, and A. Gali, npj Computational Materials 6, 41 (2020).
  * Haykal  _et al._ (2022) A. Haykal, R. Tanos, N. Minotto, A. Durand, F. Fabre, J. Li, J. H. Edgar, V. Ivády, A. Gali, T. Michel, A. Dréau, B. Gil, G. Cassabois, and V. Jacques, Nature Communications 13, 4347 (2022).
  * Li  _et al._ (2021) J. Li, E. R. Glaser, C. Elias, G. Ye, D. Evans, L. Xue, S. Liu, G. Cassabois, B. Gil, P. Valvin, T. Pelini, A. L. Yeats, R. He, B. Liu, and J. H. Edgar, Chemistry of Materials 33, 9231 (2021).
  * Murzakhanov  _et al._ (2021) F. F. Murzakhanov, B. V. Yavkin, G. V. Mamin, S. B. Orlinskii, I. E. Mumdzhi, I. N. Gracheva, B. F. Gabbasov, A. N. Smirnov, V. Y. Davydov, and V. A. Soltamov, Nanomaterials 11 (2021), 10.3390/nano11061373.
  * Kianinia  _et al._ (2020) M. Kianinia, S. White, J. E. Fröch, C. Bradac, and I. Aharonovich, ACS Photonics 7, 2147 (2020).
  * Guo  _et al._ (2022) N.-J. Guo, W. Liu, Z.-P. Li, Y.-Z. Yang, S. Yu, Y. Meng, Z.-A. Wang, X.-D. Zeng, F.-F. Yan, Q. Li, J.-F. Wang, J.-S. Xu, Y.-T. Wang, J.-S. Tang, C.-F. Li, and G.-C. Guo, ACS Omega 7, 1733 (2022).
  * Gao  _et al._ (2021a) X. Gao, S. Pandey, M. Kianinia, J. Ahn, P. Ju, I. Aharonovich, N. Shivaram, and T. Li, ACS Photonics 8, 994 (2021a).
  * Gottscholl  _et al._ (2021) A. Gottscholl, M. Diez, V. Soltamov, C. Kasper, D. Krauße, A. Sperlich, M. Kianinia, C. Bradac, I. Aharonovich, and V. Dyakonov, Nature Communications 12, 4480 (2021).
  * Gao  _et al._ (2021b) X. Gao, B. Jiang, A. E. Llacsahuanga Allcca, K. Shen, M. A. Sadi, A. B. Solanki, P. Ju, Z. Xu, P. Upadhyaya, Y. P. Chen, S. A. Bhave, and T. Li, Nano Letters 21, 7708 (2021b).
  * Huang  _et al._ (2022) M. Huang, J. Zhou, D. Chen, H. Lu, N. J. McLaughlin, S. Li, M. Alghamdi, D. Djugba, J. Shi, H. Wang, and C. R. Du, Nature Communications 13, 5369 (2022).
  * Kumar  _et al._ (2022) P. Kumar, F. Fabre, A. Durand, T. Clua-Provost, J. Li, J. Edgar, N. Rougemaille, J. Coraux, X. Marie, P. Renucci, C. Robert, I. Robert-Philip, B. Gil, G. Cassabois, A. Finco, and V. Jacques, Phys. Rev. Appl. 18, L061002 (2022).
  * Healey  _et al._ (2023) A. J. Healey, S. C. Scholten, T. Yang, J. A. Scott, G. J. Abrahams, I. O. Robertson, X. F. Hou, Y. F. Guo, S. Rahman, Y. Lu, M. Kianinia, I. Aharonovich, and J.-P. Tetienne, Nature Physics 19, 87 (2023).
  * Yang  _et al._ (2022) T. Yang, N. Mendelson, C. Li, A. Gottscholl, J. Scott, M. Kianinia, V. Dyakonov, M. Toth, and I. Aharonovich, Nanoscale 14, 5239 (2022).
  * Lyu  _et al._ (2022) X. Lyu, Q. Tan, L. Wu, C. Zhang, Z. Zhang, Z. Mu, J. Zúñiga-Pérez, H. Cai, and W. Gao, Nano Letters 22, 6553 (2022).
  * Liu  _et al._ (2021) W. Liu, Z.-P. Li, Y.-Z. Yang, S. Yu, Y. Meng, Z.-A. Wang, Z.-C. Li, N.-J. Guo, F.-F. Yan, Q. Li, J.-F. Wang, J.-S. Xu, Y.-T. Wang, J.-S. Tang, C.-F. Li, and G.-C. Guo, ACS Photonics 8, 1889 (2021).
  * Qian  _et al._ (2022) C. Qian, V. Villafañe, M. Schalk, G. V. Astakhov, U. Kentsch, M. Helm, P. Soubelet, N. P. Wilson, R. Rizzato, S. Mohr, A. W. Holleitner, D. B. Bucher, A. V. Stier, and J. J. Finley, Nano Letters 22, 5137 (2022).
  * Libbi  _et al._ (2022) F. Libbi, P. M. M. C. de Melo, Z. Zanolli, M. J. Verstraete, and N. Marzari, Phys. Rev. Lett. 128, 167401 (2022).
  * Gong  _et al._ (2022) R. Gong, G. He, X. Gao, P. Ju, Z. Liu, B. Ye, E. A. Henriksen, T. Li, and C. Zu, “Coherent dynamics of strongly interacting electronic spin defects in hexagonal boron nitride,” (2022), arXiv:2210.11485 [quant-ph] .
  * Barson  _et al._ (2017) M. S. J. Barson, P. Peddibhotla, P. Ovartchaiyapong, K. Ganesan, R. L. Taylor, M. Gebert, Z. Mielens, B. Koslowski, D. A. Simpson, L. P. McGuinness, J. McCallum, S. Prawer, S. Onoda, T. Ohshima, A. C. Bleszynski Jayich, F. Jelezko, N. B. Manson, and M. W. Doherty, Nano Letters 17, 1496 (2017).
  * Van Oort and Glasbeek (1990) E. Van Oort and M. Glasbeek, Chemical Physics Letters 168, 529 (1990).
  * Sajid  _et al._ (2020c) A. Sajid, K. S. Thygesen, J. R. Reimers, and M. J. Ford, Communications Physics 3, 153 (2020c).
  * Gracheva  _et al._ (2023) I. N. Gracheva, F. F. Murzakhanov, G. V. Mamin, M. A. Sadovnikova, B. F. Gabbasov, E. N. Mokhov, and M. R. Gafurov, The Journal of Physical Chemistry C 127, 3634 (2023).
  * Udvarhelyi  _et al._ (2018) P. Udvarhelyi, V. O. Shkolnikov, A. Gali, G. Burkard, and A. Pályi, Phys. Rev. B 98, 075201 (2018).
  * Bosak  _et al._ (2006) A. Bosak, J. Serrano, M. Krisch, K. Watanabe, T. Taniguchi, and H. Kanda, Phys. Rev. B 73, 041402 (2006).
  * Mittiga  _et al._ (2018) T. Mittiga, S. Hsieh, C. Zu, B. Kobrin, F. Machado, P. Bhattacharyya, N. Z. Rui, A. Jarmola, S. Choi, D. Budker, and N. Y. Yao, Phys. Rev. Lett. 121, 246402 (2018).
  * Liu  _et al._ (2022) W. Liu, V. Ivády, Z.-P. Li, Y.-Z. Yang, S. Yu, Y. Meng, Z.-A. Wang, N.-J. Guo, F.-F. Yan, Q. Li, J.-F. Wang, J.-S. Xu, X. Liu, Z.-Q. Zhou, Y. Dong, X.-D. Chen, F.-W. Sun, Y.-T. Wang, J.-S. Tang, A. Gali, C.-F. Li, and G.-C. Guo, Nature Communications 13, 5713 (2022).
  * Falk  _et al._ (2014) A. L. Falk, P. V. Klimov, B. B. Buckley, V. Ivády, I. A. Abrikosov, G. Calusine, W. F. Koehl, A. Gali, and D. D. Awschalom, Phys. Rev. Lett. 112, 187601 (2014).
  * Adamo and Barone (1999) C. Adamo and V. Barone, The Journal of Chemical Physics 110, 6158 (1999).
  * Weigend and Ahlrichs (2005) F. Weigend and R. Ahlrichs, Phys. Chem. Chem. Phys. 7, 3297 (2005).
  * Neese (2012) F. Neese, WIREs Computational Molecular Science 2, 73 (2012).
  * Sinnecker and Neese (2006) S. Sinnecker and F. Neese, The Journal of Physical Chemistry A 110, 12267 (2006).
  * Liu  _et al._ (2018) S. Liu, R. He, L. Xue, J. Li, B. Liu, and J. H. Edgar, Chem. Mater. 30, 6222 (2018).


#  _EM-Assist_ : Safe Automated ExtractMethod Refactoring with LLMs

Dorin Pomian*, Abhiram Bellur*, Malinda Dilhara  and  Zarina Kurbatova, Egor
Bogomolov, Andrey Sokolov, Timofey Bryksin, Danny Dig  University of Colorado
Boulder, JetBrains ResearchUSA, Cyprus, the Netherlands, Serbia dorin.pomian,
abhiram.bellur, malinda.malwala<EMAIL_ADDRESS>zarina.kurbatova,
egor.bogomolov,andrey.sokolov<EMAIL_ADDRESS>
(2024; 2024-01-29; 2024-04-15)

###### Abstract.

Excessively long methods, loaded with multiple responsibilities, are
challenging to understand, debug, reuse, and maintain. The solution lies in
the widely recognized Extract Method refactoring. While the application of
this refactoring is supported in modern IDEs, recommending which code
fragments to extract has been the topic of many research tools. However, they
often struggle to replicate real-world developer practices, resulting in
recommendations that do not align with what a human developer would do in real
life. To address this issue, we introduce _EM-Assist_ , an IntelliJ IDEA
plugin that uses LLMs to generate refactoring suggestions and subsequently
validates, enhances, and ranks them. Finally, _EM-Assist_ uses the IntelliJ
IDE to apply the user-selected recommendation. In our extensive evaluation of
1,752 real-world refactorings that actually took place in open-source
projects, EM-Assist’s recall rate was 53.4% among its top-5 recommendations,
compared to 39.4% for the previous best-in-class tool that relies solely on
static analysis. Moreover, we conducted a usability survey with 18 industrial
developers and 94.4% gave a positive rating.

The source code, datasets, and distribution of the plugin are available on
GitHub (JetBrains, 2024c). It is also available to install on JetBrains
Marketplace (JetBrains, 2024b). A demon video and screencast is on YouTube
(JetBrains, 2024a).

Refactoring, LLMs, Code smells, Long Methods, Java, Kotlin

††copyright: rightsretained††doi: 10.1145/3663529.3663803††journalyear:
2024††submissionid: fsecomp24demo-p37-p††isbn:
979-8-4007-0658-5/24/07††conference: Companion Proceedings of the 32nd ACM
International Conference on the Foundations of Software Engineering; July
15–19, 2024; Porto de Galinhas, Brazil††booktitle: Companion Proceedings of
the 32nd ACM International Conference on the Foundations of Software
Engineering (FSE Companion ’24), July 15–19, 2024, Porto de Galinhas,
Brazil††ccs: Software and its engineering Software maintenance tools††ccs:
Computing methodologies Artificial intelligence**footnotetext: These authors
contributed equally to this work.

##  1\. introduction

Long methods encapsulate multiple responsibilities and are challenging to
comprehend, debug, reuse, and evolve (Tsantalis and Chatzigeorgiou, 2011;
Banker et al., 1993; Fowler, 1999). They often lead to code that is not only
difficult to understand but also error-prone, thereby becoming a significant
source of technical debt in software projects (Fowler, 1999). To alleviate
this, developers frequently use the Extract Method refactoring, which divides
methods into smaller, more manageable units. This refactoring consistently
ranks among the top five most commonly performed in practice (Negara et al.,
2013; Murphy-Hill et al., 2012; Tsantalis et al., 2022).

The Extract Method refactoring process comprises two phases:  (i) Selecting
statements for extraction from the original method, involving a meticulous
examination of code segments that encapsulate specific responsibilities or
logic, with the goal of isolating them into a distinct method,  (ii) Applying
the refactoring, involving moving the selected statements into a brand new
method, passing necessary variables as parameters, and invoking the new method
from the original context.  While the application part has been a staple
feature of all modern IDEs, they leave it up to developers to choose which
statements to extract. The research community has developed diverse techniques
to suggest statements for extraction. Some research tools use static analysis
in conjunction with software quality metrics (Maruyama, 2001; Tsantalis and
Chatzigeorgiou, 2011; Charalampidou et al., 2017; Yang et al., 2009; Tiwari
and Joshi, 2022), such as statement cohesion (Tsantalis and Chatzigeorgiou,
2011), while others employ machine learning-based classifiers (Xu et al.,
2017; Cui et al., 2023)

While the existing tools adhere to software quality metrics based on software
engineering principles, they often generate suggestions that do not align with
real-world Extract Method instances. We attribute this to the fact that
refactoring requires both technical rigor and subjective discernment. In other
words, developers rely on their understanding of software engineering
principles and their subjective interpretation of code context when deciding
what makes a good method. While existing techniques excel in the former, they
often fall short in the latter. This gap may explain developers’ reluctance to
use automated refactoring support (Silva et al., 2016).

To bridge the gap and generate Extract Method suggestions that developers are
likely to accept, we utilize Large Language Models (LLMs). They are trained on
extensive code repositories containing millions of methods authored by actual
developers. Thus, they are more likely to mimic human behavior and replicate
how developers create methods, making them likely to propose refactorings that
developers would embrace (Dilhara et al., 2024; Pomian et al., 2024). Our
formative study, conducted on an extended corpus size of 1,752 Extract Method
real refactorings from open-source systems, demonstrated that LLMs are highly
effective in providing expert suggestions, generating 27 suggestions per
method. This highlights the substantial potential of integrating LLMs into the
domain of code refactoring. Nevertheless, LLM output cannot be directly
adopted. Our findings indicate that 76.3% of LLM suggestions are
_hallucinations_ of two kinds. First, 57.4% of the LLM suggestions are invalid
(e.g., syntactically incorrect), potentially causing compilation errors.
Second, 18.9% of the suggestions are not useful (e.g., suggest to extract the
entire method body).

To advance the field of refactoring, we introduce _EM-Assist_ , the _first
automated refactoring tool to use LLMs_. _EM-Assist_ is an IntelliJ IDEA
plugin. It employs LLMs to generate refactoring suggestions, then filters
those that are invalid and non-useful, and further enhances valid suggestions
using program slicing techniques. It then ranks them to offer high-quality
options to developers. Moreover, _EM-Assist_ bridges the gap between
suggesting and applying refactorings by encapsulating selected suggestions
into refactoring commands and executing them within the IDE. This process
leverages the IDE to ensure the safe execution of refactorings, providing a
seamless transition from suggestion to execution.

In section 3.1, we first quantitatively evaluate the _EM-Assist_ in terms of
its effectiveness compared to baseline tools. Then in section 3.2 we
qualitatively evaluate _EM-Assist_ ’s usability. In evaluations, we observed
that EM-Assist really shines over previous approaches when replicating real-
world refactorings in contemporary codebases. When replicating 1,752 actual
Extract Method refactorings that took place in open-source projects, EM-
Assist’s recall rate was 53.4%, compared to 39.4% for the previous best-in-
class tool that relies solely on static analysis (JExtract). For our
qualitative evaluation (section 3.2), we surveyed 18 industrial developers,
confirming the tool’s usability at an approval rate of 94.4%.

##  2\. _EM-Assist_

Figure 1. Workflow for using _EM-Assist_ within IntelliJ IDEA: 1) the user
triggers the plugin to generate suggestions, 2) the plugin displays three
refactoring options in a popup window, 3) the user selects one of the options
and inspects the final code. Figure 2. The workflow of generating refactoring
suggestions

###  2.1. Example Usage of _EM-Assist_

Figure 1 shows an example of using _EM-Assist_ to suggest and execute
refactorings on the method writeJvmClass() within the project
JetBrains/intellij-community. The user triggers _EM-Assist_ by choosing the
“Extract method suggestions” option from the intention actions (yellow light
bulb)111https://www.jetbrains.com/help/idea/intention-actions.html for the
method ( 1 in Figure 1). In a popup window, _EM-Assist_ shows three distinct
suggestions for extracting code fragments, accompanied by a suggested method
name ( 2 in Figure 1). In this example, the plugin suggests three extract
methods: writeInterfaces(), writeFields(), and writeMethods(), and it
indicates the size of the code fragment for each suggestion. To make it
convenient for the user to assess each suggestion, _EM-Assist_ helps the user
preview the signature of the proposed new method (including method parameters)
directly within the popup window. Additionally, for each suggestion, _EM-
Assist_ highlights the code fragment slated for extraction. For example, the
blue-colored statements in Figure 1 (line 85-90 of 1) represent the proposed
statements for extraction within the writeMethods() suggestion. Likewise, the
plugin affords flexibility to users, allowing them to choose the most
appropriate extract method suggestion and seamlessly apply it to the
writeJvmClass() method. To do so, the user clicks the “Extract” button
conveniently located within the popup window. In this example, the user chose
to apply the writeMethods suggestion to the code, and the resulting code is
shown as 3 in Figure 1.

###  2.2. Workflow

In this section, we present the internal workflow that _EM-Assist_ uses to
automatically suggest and perform Extract Method. As shown in Figure 2, the
developer invokes the plugin by clicking the “Extract Method” option in the
intention actions for a particular method they intend to apply the extract
method refactoring to ( 1 in Figure 2). Then _EM-Assist_ creates a LLM few-
shot learning prompt ( 2 in Figure 2) that contains the method declaration,
and it invokes the LLM programmatically to generate refactoring suggestions (
3 in Figure 2). To tame the non-determinism of LLMs, _EM-Assist_ repeats the
same prompt request, using optimal values that we determined empirically
(i.e., between 5-10 iterations). Then _EM-Assist_ enhances the LLM suggestions
by adjusting the code fragments, using program slicing ( 4 in Figure 2).
However, not all these enhanced suggestions are safe to apply, as some may
result in non-compilable and erroneous code. In fact, our analysis conducted
on a dataset of 1,752 methods, reveals that 76.3% of the suggestions generated
by the LLM are invalid, potentially resulting in non-compilable code. To
filter invalid suggestions, _EM-Assist_ employs the IntelliJ IDEA APIs for
determining whether a code fragment meets the refactoring preconditions for
extract method ( 5 in Figure 2).

Some valid suggestions are not practical for real-world implementation (e.g.,
those that contain the whole method body). Therefore, _EM-Assist_ filters
unuseful suggestions ( 6 in Figure 2). In the end, _EM-Assist_ generates
numerous suggestions per long method ( 7 in Figure 2), which could potentially
overwhelm the user if all are presented at once. To address this, we employ a
ranking mechanism ( 8 in Figure 2) that relies on the frequency of LLM
suggestions. The ranking mechanism is centered on the idea that if a
particular code fragment is repeatedly suggested for extraction by the LLM
during subsequent triggers, it must represent an important functional concern
that is suitable to be in a separate method. Then, _EM-Assist_ presents the
ranked suggestions (top-n, default is top-3) to developers through our user
interface, offering previews of extracted method signatures and associated
code fragments. After the user selects their preferred suggestion, _EM-Assist_
encapsulates it in a refactoring command, seamlessly invoking IntelliJ IDEA to
execute the refactoring correctly ( 9 in Figure 2).

###  2.3. Implementation

_EM-Assist_ is implemented as an IntelliJ IDEA plugin and works as an
intention action. IntelliJ Platform has a suite of APIs for Abstract Syntax
Tree (AST) manipulation and refactoring. _EM-Assist_ leverages IntelliJ’s AST
manipulation through the Program Structure Interface (PSI), which is the layer
in the IntelliJ Platform responsible for code, file, and project model.
Extract Method refactoring is applied through IntelliJ’s refactoring
framework. We employ few-shot learning (Brown, 2020; Radford et al., 2019) to
form the LLM prompt. _EM-Assist_ is configurable to use various LLMs, by
default we use OpenAI’s GPT-3.5-turbo. The communication with LLM is
asynchronous, thus, the IntelliJ code editor is not frozen and multiple
requests are sent to the LLM in parallel. Not all suggestions provided by the
LLM are valid and ready to be extracted, therefore, we use PSI elements to
adjust the scope of these Extract Method candidates. We utilize IntelliJ’s
Extract Method framework to further filter out the candidates which cannot be
extracted. We enhanced the Extract Method framework to support custom method
names suggested by the LLM.

Extensions: _EM-Assist_ currently supports Java and Kotlin, but its
implementation offers an extensible framework for supporting other languages
as well. For example, to support Extract Method in Python code, extensions
would be needed in steps 1, 3, 5, and 7 (Figure 2), whereas 2, 4, 6, 8, 9
(Figure 2), could be reused. To support other IDEs that provide good support
for ExtractMethod refactoring, steps 5 and 9 (Figure 2) would need change,
whereas all remaining steps could be reused. Moreover, our preliminary
experiments with using LLMs to suggest other kinds of refactorings show that
LLMs are prolific, but we need to employ a similar approach: use the LLM for
developer-aligned suggestions, but carry out the refactoring plan with the
safety of the IDE.

Limitations: Currently, using _EM-Assist_ requires sending the user’s host
method code to OpenAI. As a result, the response time can vary. Our
experiments involving 25,235 invocations of the tool revealed that, on
average, _EM-Assist_ takes 2 seconds to suggest refactorings. This includes
the time taken to invoke the LLM and the additional processing time required
by the tool. According to Nielsen’s usability guidelines (Nielsen, 1994), this
response time is acceptable for human-operated software tools, ensuring that
the user’s flow of thought remains uninterrupted. Moreover, when developers
work on proprietary code, or a company policy prohibits using Generative AI
solutions, this currently limits the use of _EM-Assist_. Emerging solutions
such as deploying LLM servers fully on-premise (for enterprise customers or
government organizations) or using medium-size models locally, could make
Generative AI-based solutions even more broadly applicable. Moreover, _EM-
Assist_ ’s architecture make it easy to switch between different LLMs.

##  3\. evaluation

We conducted a comprehensive evaluation of _EM-Assist_ , using a harness of
two complementary approaches.  (i) Quantitative (section 3.1): we compared the
effectiveness of EM-Assist with two state-of-the-art extract method
recommendation tools by replicating a large dataset of 1,752 actual extract
methods performed by open-source developers. Unlike previous studies that used
a small, synthetic corpus of artificially-created ExtractMethods, we are
replicating real-world refactorings from famous, contemporary open-source
projects. We believe this is more indicative of a tool’s capabilities in
dealing with the complexities of real-world refactorings, and the large scale
of experiments guards against over-fitting a tool for a small corpus.  (ii)
Qualitative (section 3.2): we evaluated the usability of _EM-Assist_ ’s
approach when guiding industrial developers to follow our novel workflow for
performing ExtractMethod refactorings. Unlike previous studies that have
employed large number of students, we believe our study employing 18 full-
time, experienced professional developers is more indicative of the quality of
the suggested refactorings.

###  3.1. Effectiveness of _EM-Assist_

To determine _EM-Assist_ ’s effectiveness in recommending refactorings that
align with developer preferences, we compare it to previous state-of-the-art
approaches that rely solely on static analysis and software quality metrics.
In our technical report (Pomian et al., 2024), we compare _EM-Assist_ to six
other tools that suggest Extract Method and we use a synthetic corpus used by
other researchers. We found that JExtract (Silva et al., 2015) outperforms the
previous tools, thus we use JExtract as the strongest competitor to _EM-
Assist_.

First, we construct an oracle. We mined the complete version history of
projects using RefactoringMiner (Tsantalis et al., 2022), a state-of-the-art
tool for refactoring detection. Thus we identified 1,752 cases when the open-
source developers performed ExtractMethod refactorings. We then replicate all
these refactorings by running the tools on the original host methods (the
version before refactoring was applied) and compare the suggested refactorings
against the actually-performed refactorings in the oracle. The oracle
exhibited a diverse range of method involved in refactoring, with
min/max/mean/median values of 3/1494/30/5 LOC for the host method, and
2/95/6/3 LOC for extract methods. Given the size of host methods, there can be
many ways to group code fragments for extraction.

We executed the two tools on each host method in the oracle. We then compared
the refactorings performed by these tools and cross-referenced them with the
oracle to ensure that the extracted code lines matched those in the oracle.
Following best practices established in prior research (Silva et al., 2015;
Tsantalis and Chatzigeorgiou, 2011; Charalampidou et al., 2017; Fernandes et
al., 2022; Cui et al., 2023; Xu et al., 2017) we evaluate top-5 suggestions
generated by the tools, calculating Recall@5 at tolerance level 3%. We
selected Recall as the performance metric as this is the metric used by all
previous tools. Given the size of host methods, even at 3% tolerance, it means
in many cases we must have an exact match between one of the top-5 suggestions
and the extracted code fragment in the oracle. We compute the recall by
dividing the total number of refactoring suggestions that matched the oracle
to the total number of extract methods in the oracle.

_EM-Assist_ has a recall rate of 53.4%, significantly surpassing its
counterparts whose best recall was 39.4%. Acknowledging the probabilistic
nature of _EM-Assist_ ’s underlying LLM, we conducted further statistical
analysis for robust validation. We repeated each prediction from _EM-Assist_
50 times and performed a one-sample t-test against JExtract, the second-best
performing tool. The test decisively rejected the null hypothesis asserting
that JExtract had a better recall rate than EM-Assist, with a p-value of
$<10^{-5}$.

###  3.2. Usability of _EM-Assist_

We complement the previous quantitative evaluation with a survey to evaluate
developers’ perception of using the novel workflow of _EM-Assist_ in
comparison to the current workflow in the IDEs. We intentionally designed the
survey to allow participants to view a brief, feature-focused demo of the
plugin, rather than requiring them to install and execute it. Previous
research (Davidoff et al., 2007; Kery et al., 2020) demonstrated that this
approach is useful for evaluating the fundamental concepts and ideas behind a
tool, as high-fidelity prototypes might overwhelm survey participants with
excessive implementation details.

We surveyed 18 qualified professional software developers. Among these
participants, 50% have more than a decade of experience in software
development, 33% have between one and three years of experience, while others
have between three and seven years of experience. We used Likert-type
questions (Boone Jr and Boone, 2012) to evaluate _EM-Assist_ ’s features, and
we included open-ended questions to capture their insights regarding future
enhancements. We adhered to established best practices in usability studies
(Barnum, 2010).

Figure 3 shows the results of the Likert-type questions. The respondents
expressed highly positive attitudes towards the overall usability and user
interfaces of _EM-Assist_. 94.4% of the respondents found the additional
workflow stages, including triggering the plugin, generating LLM suggestions,
and selecting one to apply, to be convenient and easy to handle. _EM-Assist_
uses a popup window to present the suggestions, and over 77.7% of respondents
agreed that it is somewhat helpful or greater. Additionally, 94.4% of
respondents liked the suggested method names. While users could get name
suggestions from an LLM at a later time, we conveniently offer meaningful
names at the time of performing extract method.

Figure 3. Results of the conducted usability survey.

In the open-ended questions, the respondents suggested valuable enhancements
to _EM-Assist_ in terms of usability, configurability, and mode of execution,
which we will use in a future release.

Overall, we received positive praises from the survey respondents about _EM-
Assist_ : “It looks super cool so far! :fire:”. Another said: “Thank you for
interesting suggestions! Hope to see this in production in the future.” And
another: “…these suggestions made me look at this code with new eyes once
more, and I will try to refactor it.”

##  4\. related work

Researchers developed tools to suggest code fragments to extract. Many
primarily rely on static analysis-based rules like program slicing (Lakhotia
and Deprez, 1998; Maruyama, 2001; Abadi et al., 2009), the separation of
concerns (Silva et al., 2014), or the single responsibility principle
(Charalampidou et al., 2017). While these tools excel at adhering to software
quality principles, they face a significant limitation. They are not able to
adapt suggestions to developers’ subjective intentions, possibly because of
their lack of access to real-world data illustrating actual developer
refactoring practices.

As an alternative to rule-based techniques, machine learning-based classifiers
(Cui et al., 2023; Xu et al., 2017) have been proposed for refactoring
suggestions. Despite having access to relevant data, they grapple with
practical challenges such as data scarcity, protracted training periods, and
resource-intensive training procedures. Furthermore, these ML models are often
specific to particular tasks, necessitating periodic retraining to maintain
their effectiveness (Dilhara et al., 2021, 2023; Dilhara, 2021; Dilhara et
al., 2022). In contrast to these existing tools, _EM-Assist_ leverages LLMs as
a promising solution. LLMs are general-purpose models pre-trained on extensive
source code and text, enabling them to generate refactoring suggestions
aligned with developer intentions using knowledge from both code and textual
documents. _EM-Assist_ takes this a step further by removing hallucinations,
validating and enhancing the LLM output, and providing developers with
tailored recommendations..

While obtaining the training set for every kind of refactoring is a formidable
challenge for previous approaches, it is not for LLM-based tools. While
previous approaches require continuous/custom model training or custom
analysis, given the broad knowledge-base of LLMs, our solution is easier to
use, maintain, and deploy in IDEs.

##  5\. Conclusions

We present _EM-Assist_ , the first automated refactoring tool that employs
LLMs, implemented as an IntelliJ IDEA plugin. It bridges the gap between
suggesting and applying Extract Method refactoring, and shrinks the gap
between refactoring suggestions and developer practices. _EM-Assist_ generates
refactoring suggestions using LLMs, then validates and enhances them with
static analysis in the IDE. To avoid overwhelming developers, _EM-Assist_
ranks and presents only the highest-quality suggestions, and then correctly
executes the user-selected refactoring suggestion with the IDE. We
comprehensively evaluate _EM-Assist_ and it outperforms state-of-the-art.
Additionally, a developer survey confirmed the high usability of the plugin.
_EM-Assist_ uses a novel way to check LLM results and make them useful for
refactoring tasks. Being the first tool to employ LLMs for Extract Method
refactoring, _EM-Assist_ demonstrates the potential of LLMs as effective
refactoring assistants. We are now expanding _EM-Assist_ to support many more
kinds of refactorings.

##  6\. Acknowledgements

We thank the ML Methods in Software Engineering Lab at JetBrains Research, and
the FSE-2024 reviewers for their insightful and constructive feedback for
improving the paper. This research was partially funded through the NSF grants
CNS-1941898, CNS-2213763, and the Industry-University Cooperative Research
Center on Pervasive Personalized Intelligence.

## References

  * (1)
  * Abadi et al. (2009) Aharon Abadi, R Ettinger, and YA Feldman. 2009.  Fine slicing for advanced method extraction.  _3rd workshop on refactoring tools_.
  * Banker et al. (1993) Rajiv D Banker, Srikant M Datar, Chris F Kemerer, and Dani Zweig. 1993.  Software complexity and maintenance costs.  _Commun. ACM_.
  * Barnum (2010) Carol M Barnum. 2010\.  Usability testing essentials.  _Elsevier_.
  * Boone Jr and Boone (2012) Harry N Boone Jr and Deborah A Boone. 2012.  Analyzing likert data.  _The Journal of extension_.
  * Brown (2020) Tom et. al Brown. 2020\.  Language Models are Few-Shot Learners.  _Curran Associates, Inc._
  * Charalampidou et al. (2017) Sofia Charalampidou, Apostolos Ampatzoglou, Alexander Chatzigeorgiou, Antonios Gkortzis, and Paris Avgeriou. 2017.  Identifying Extract Method Refactoring Opportunities Based on Functional Relevance.  _TSE_.
  * Cui et al. (2023) Di Cui, Qiangqiang Wang, Siqi Wang, Jianlei Chi, Jianan Li, Lu Wang, and Qingshan Li. 2023.  REMS: Recommending Extract Method Refactoring Opportunities via Multi-view Representation of Code Property Graph. In _ICPC_.
  * Davidoff et al. (2007) Scott Davidoff, Min Kyung Lee, Anind K Dey, and John Zimmerman. 2007\.  Rapidly exploring application design through speed dating. In _UbiComp_. Springer.
  * Dilhara (2021) Malinda Dilhara. 2021\.  Discovering repetitive code changes in ML systems. In _Proceedings of the 29th ACM Joint Meeting on European Software Engineering Conference and Symposium on the Foundations of Software Engineering_ (Athens, Greece) _(ESEC/FSE 2021)_. Association for Computing Machinery, New York, NY, USA, 3 pages.
  * Dilhara et al. (2024) Malinda Dilhara, Abhiram Bellur, Timofey Bryksin, and Danny Dig. 2024.  Unprecedented Code Change Automation: The Fusion of LLMs and Transformation by Example.  _FSE_ (2024).  arXiv:2402.07138
  * Dilhara et al. (2023) Malinda Dilhara, Danny Dig, and Ameya Ketkar. 2023.  PYEVOLVE: Automating Frequent Code Changes in Python ML Systems. In _2023 IEEE/ACM 45th International Conference on Software Engineering (ICSE)_.
  * Dilhara et al. (2021) Malinda Dilhara, Ameya Ketkar, and Danny Dig. 2021.  Understanding Software-2.0: A Study of Machine Learning Library Usage and Evolution.  _ACM Trans. Softw. Eng. Methodol._ , Article 55 (July 2021), 42 pages.
  * Dilhara et al. (2022) Malinda Dilhara, Ameya Ketkar, Nikhith Sannidhi, and Danny Dig. 2022.  Discovering repetitive code changes in python ML systems. In _Proceedings of the 44th International Conference on Software Engineering_ (Pittsburgh, Pennsylvania) _(ICSE ’22)_. Association for Computing Machinery, New York, NY, USA, 13 pages.
  * Fernandes et al. (2022) Sara Fernandes, Ademar Aguiar, and André Restivo. 2022\.  A Live Environment to Improve the Refactoring Experience.
  * Fowler (1999) Martin Fowler. 1999\.  _Refactoring: Improving the Design of Existing Code_.  Addison-Wesley.
  * JetBrains (2024a) JetBrains. 2024a.  EM-Assist demo video and screencast.  https://youtu.be/3E6KsHAg3js
  * JetBrains (2024b) JetBrains. 2024b.  EM-Assist installation from Marketplace.  https://plugins.jetbrains.com/plugin/23403-llm-extract-function
  * JetBrains (2024c) JetBrains. 2024c.  EM-Assist source code and datasets.  https://github.com/llm-refactoring/llm-refactoring-plugin
  * Kery et al. (2020) Mary Beth Kery, Donghao Ren, Fred Hohman, Dominik Moritz, Kanit Wongsuphasawat, and Kayur Patel. 2020\.  mage: Fluid moves between code and graphical work in computational notebooks.
  * Lakhotia and Deprez (1998) Arun Lakhotia and Jean-Christophe Deprez. 1998.  Restructuring programs by tucking statements into functions. Elsevier.
  * Maruyama (2001) Katsuhisa Maruyama. 2001\.  Automated Method-Extraction Refactoring by Using Block-Based Slicing.
  * Murphy-Hill et al. (2012) Emerson Murphy-Hill, Chris Parnin, and Andrew P. Black. 2012\.  How We Refactor, and How We Know It.  _TSE_.
  * Negara et al. (2013) Stas Negara, Nicholas Chen, Mohsen Vakilian, Ralph E. Johnson, and Danny Dig. 2013.  A Comparative Study of Manual and Automated Refactorings.  _ECOOP_.
  * Nielsen (1994) Jakob Nielsen. 1994\.  _Usability engineering_.  Morgan Kaufmann.
  * Pomian et al. (2024) Dorin Pomian, Abhiram Bellur, Malinda Dilhara, Zarina Kurbatova, Egor Bogomolov, Timofey Bryksin, and Danny Dig. 2024\.  Together We Go Further: LLMs and IDE Static Analysis for Extract Method Refactoring.  arXiv:2401.15298 [cs.SE]
  * Radford et al. (2019) Alec Radford, Jeffrey Wu, Rewon Child, David Luan, Dario Amodei, Ilya Sutskever, et al. 2019\.  Language models are unsupervised multitask learners.  _OpenAI blog_.
  * Silva et al. (2014) Danilo Silva, Ricardo Terra, and Marco Tulio Valente. 2014\.  Recommending automated extract method refactorings.  _ICPC_.
  * Silva et al. (2015) Danilo Silva, Ricardo Terra, and Marco Túlio Valente. 2015\.  Jextract: An eclipse plug-in for recommending automated extract method refactorings.
  * Silva et al. (2016) Danilo Silva, Nikolaos Tsantalis, and Marco Tulio Valente. 2016\.  Why We Refactor? Confessions of GitHub Contributors.  _FSE_.
  * Tiwari and Joshi (2022) Omkarendra Tiwari and Rushikesh Joshi. 2022.  Identifying Extract Method Refactorings.  _ISEC_.
  * Tsantalis and Chatzigeorgiou (2011) Nikolaos Tsantalis and Alexander Chatzigeorgiou. 2011.  Identification of extract method refactoring opportunities for the decomposition of methods.
  * Tsantalis et al. (2022) Nikolaos Tsantalis, Ameya Ketkar, and Danny Dig. 2022.  RefactoringMiner 2.0.  _TSE_.
  * Xu et al. (2017) Sihan Xu, Aishwarya Sivaraman, Siau-Cheng Khoo, and Jing Xu. 2017.  GEMS: An Extract Method Refactoring Recommender.  _ISSRE_.
  * Yang et al. (2009) Limei Yang, Hui Liu, and Zhendong Niu. 2009.  Identifying Fragments to Be Extracted from Long Methods.  _APSEC_.