Main

Large-scale quantum computers and quantum networks will require quantum error correction to overcome inevitable imperfections15,16,17,18,19. The central idea is to encode each logical qubit of information into several physical data qubits. Non-destructive multi-qubit measurements, called stabilizer measurements, can then be used to identify and correct errors15,16,17,18. If the error rates of all the components are below a certain threshold, it becomes possible to perform arbitrarily large quantum computations by encoding into increasingly more physical qubits15,17,18. A crucial requirement is that all logical building blocks, including the error-syndrome measurement, must be implemented fault tolerantly. At the lowest level, this implies that any single physical error should not cause a logical error.

Over the past several years, steps towards fault-tolerant quantum error correction have been made using spin qubits in silicon6,7,8 and in diamond13,14, as well as in various other hardware platforms, such as superconducting qubits23,24,25,26,27 and trapped-ion qubits20,21,31,32. Pioneering experiments have demonstrated codes that can detect but not correct errors22,25,26,33, quantum error-correction protocols that can correct only one type of error13,14,34, as well as non-fault-tolerant quantum error-correction protocols20,24,34,35. A recent experiment with trapped-ion qubits has demonstrated the fault-tolerant operation of an error-correction code, albeit through destructive stabilizer measurements and post-processing21.

In this work, we realize fault-tolerant encoding, gate operations and non-destructive stabilizer measurements for a logical qubit of a quantum error-correction code. Our logical qubit is based on the five-qubit code and we use a total of seven spin qubits in a diamond quantum processor (Fig. 1). Fault tolerance is made possible through the recently discovered paradigm of flag qubits28,29,30. First, we demonstrate a new fault-tolerant encoding protocol based on repeated multi-qubit measurements, which herald the successful preparation of the logical state. Then, we realize the (non-universal) set of transversal single-qubit Clifford gates. Finally, we demonstrate stabilizer measurements on the logical qubit and include a flag qubit to ensure compatibility with fault tolerance. Our stabilizer measurements are non-destructive, the post-measurement state is available in real time and we use feedforward based on the measurement outcomes. Although the logical qubit fidelities do not yet outperform the constituent physical qubits, these results demonstrate the key components of fault-tolerant quantum error correction in a solid-state spin-qubit processor.

Fig. 1: Diamond quantum processor, logical qubit and fault tolerance.
figure 1

a, Our processor consists of a single NV centre and 27 13C nuclear-spin qubits, for which the lattice sites and qubit–qubit interactions are known38. We select five 13C qubits as data qubits that encode the logical state (yellow). The other qubits (grey) are not used here. We use the NV electron spin (purple) as an auxiliary qubit for stabilizer measurements and the NV 14N nuclear spin (green) as a flag qubit to ensure fault tolerance. Purple lines indicate the electron–nuclear two-qubit gates used here (Methods). Grey lines indicate dipolar nuclear–nuclear couplings greater than 6 Hz. b, Illustration of the main components of the experiment. We realize fault-tolerant encoding, gates and stabilizer measurements with real-time processing on a logical qubit of the five-qubit quantum error-correction code. To ensure that any single fault does not cause a logical error, an extra flag qubit is used to identify errors that would propagate to multi-qubit errors and corrupt the logical state28. An illustration of such an error E is shown in red.

The logical qubit

Stabilizer error-correction codes use auxiliary qubits to perform repeated stabilizer measurements that identify errors. A key requirement for fault tolerance is to prevent errors on the auxiliary qubits from spreading to the data qubits and causing logical errors18,28 (Fig. 1b). The paradigm of flag fault tolerance provides a solution with minimal qubit overhead28,29,30. Auxiliary qubit errors that would cause logical errors are detected using extra flag qubits, so that they can be subsequently corrected (Fig. 1b).

Our logical qubit is based on the five-qubit code, the smallest distance-3 code which can correct any single-qubit error35,36. Any logical state is a simultaneous +1 eigenstate of the four stabilizers s1 = XXYIY, s2 = YXXYI, s3 = IYXXY and s4 = YIYXX, and the logical operators are XL = XXXXX and ZL = ZZZZZ. Because any error on a single data qubit corresponds to a unique 4-bit syndrome, given as the eigenvalues of the stabilizers, arbitrary single-qubit errors can be identified and corrected. Combined with an auxiliary qubit for stabilizer measurements and a flag qubit to capture harmful auxiliary qubit errors, this makes fault-tolerant error correction possible using seven qubits in total28.

System: spin qubits in diamond

Our processor consists of a single nitrogen-vacancy (NV) centre and its surrounding nuclear-spin environment at 4 K (Fig. 1a). These spins are high-quality qubits with coherence times up to seconds for the NV electron spin37 and minutes for the nuclear spins3. The NV electron spin can be read out optically, couples strongly to all other spins and is used as an auxiliary qubit for stabilizer measurements3,14 (Methods). We use the intrinsic 14N nuclear spin as the flag qubit. Unlike the other qubits, the flag qubit does not need to maintain coherence during the optical readout. In this device, 27 13C nuclear-spin qubits and their lattice positions have been characterized, so that the 406 qubit–qubit interactions are known38. Each 13C qubit can be controlled individually owing to their distinct couplings to the NV electron spin (Methods). Here we use five of the 13C spin qubits as the data qubits to encode the logical qubit.

A challenge for controlling such a quantum processor is that the spins continuously couple to each other. We realize selective control gates through various echo sequences that isolate interactions between the targeted spins, while also protecting them from environmental decoherence. For all two-qubit gates, we use previously developed electron–nuclear gates, which are based on decoupling sequences on the electron spin3 (Methods). Furthermore, we introduce interleaved and asynchronous echo stages that cancel unwanted couplings between the data qubits (Methods). These extra echo stages are essential for the relatively long gate sequences realized here.

Non-destructive stabilizer measurements

We start by demonstrating non-destructive four-qubit stabilizer measurements with real-time feedforward operations based on the measurement outcomes (Fig. 2). Despite the central role of such measurements in many error-correction codes, including the five-qubit code, the Steane code and the surface code15,16,17,18, experimental implementations with feedforward have remained an outstanding challenge.

Fig. 2: Non-destructive stabilizer measurements with real-time feedforward.
figure 2

a, Circuit diagram for the deterministic preparation of a four-qubit GHZ entangled state \(({| {\rm{\psi }}\rangle }_{+}=(| 0000\rangle +| 1111\rangle )/\sqrt{2})\) using a measurement of the stabilizer XXXX. b, Measured expectation values of the 15 operators that define the ideal state. The obtained fidelity with the target state is 0.86(1), confirming genuine multipartite entanglement. Grey bars show the ideal expectation values. Error bars are one standard deviation.

We benchmark the measurement by using it to deterministically create a four-qubit entangled state. We prepare the state \(|0000\rangle \) and measure the operator XXXX. This projects the qubits into the Greenberger–Horne–Zeilinger (GHZ) state \(({| {\rm{\psi }}\rangle }_{\pm }=(| 0000\rangle \pm | 1111\rangle )/\sqrt{2}),\) with the sign determined by the measurement outcome. We process the measurement outcomes in real time using a microprocessor and apply the required correction to deterministically output the state \(({| {\rm{\psi }}\rangle }_{+})\) with a fidelity of 0.86(1). Because this result is obtained without any post-selection, it highlights that the post-measurement state is available for all measurement outcomes, satisfying one of the key requirements for error correction.

Fault-tolerant encoding

To prepare the logical qubit, we introduce a new scheme that uses repeated stabilizer measurements and a flag qubit to herald successful preparation (Fig. 3a). In contrast to the scheme introduced by Chao and Reichardt28, no direct two-qubit gates between the data qubits are required (fifth section of the Supplementary Information). We demonstrate the preparation of the logical state \(({|-\rangle }_{{\rm{L}}}=\frac{1}{\sqrt{2}}({|0\rangle }_{{\rm{L}}}-{|1\rangle }_{{\rm{L}}})\). This state is the unique +1 eigenstate of five independent weight-3 logical -X operators, namely, p1 = IZXZI, p2 = ZIIZX, p3 = XZIIZ, p4 = ZXZII and p5 = IIZXZ. Therefore, one can prepare \({|-\rangle }_{{\rm{L}}}\) by initializing the data qubits into the product state \(|00+0+\rangle \), which is an eigenstate of p1 and p2, and subsequently measuring p3 to p5 (Fig. 3a). This preparation scheme is not fault tolerant because faults involving the auxiliary qubit can cause weight-2 errors, which can result in logical errors (Fig. 3a). We refer to these steps as the non-FT encoding scheme.

Fig. 3: Fault-tolerant encoding of the logical qubit.
figure 3

a, Encoding circuit. The first stage prepares \({|-\rangle }_{{\rm{L}}}\) non-fault-tolerantly (‘non-FT preparation’) by starting with |00+0+ (an eigenstate of p1, p2) and measuring the logical operators p3 to p5. The second ‘FT verification’ stage consists of two stabilizer measurements, T1 = p2·p4·p5, T2 = p1·p3·p5, and a flag qubit measurement. Echo sequences are inserted between the measurements to decouple the qubits (not shown, see Supplementary Figs. 8 and 9). Successful preparation is heralded by satisfying a set of conditions for the measurement outcomes (see main text). Red indicates an example of an auxiliary qubit fault (an XY error in a two-qubit gate) that would propagate to a logical error but is detected by the T1 verification step. Orange indicates an example of a single fault in the verification stage that would propagate into a logical error but is detected by the flag qubit. b,c, Probabilities to obtain the desired logical state \({|-\rangle }_{{\rm{L}}}\) without error (P0,−) or with a single-qubit Pauli error (P1,−), and the probabilities to obtain the opposite logical state \({|+\rangle }_{{\rm{L}}}\) with zero error (P0,+) or with a single-qubit Pauli error (P1,+). Note that P1,± are summed over all 15 possible errors. These 32 states are orthogonal and span the full five-qubit Hilbert space.

We make the preparation circuit fault tolerant by adding two stabilizer measurements, T1 = p2·p4·p5 = IXIYY and T2 = p1·p3·p5 = XIYYI with a flag qubit (Fig. 3a). Successful preparation is heralded by the following conditions: (1) the measurement outcomes of T1 and T2 are compatible with the measurement outcomes mi of the logical operators \({p}_{{\rm{i}}}\), that is, \({m}_{{T}_{1}}={m}_{2}{\times m}_{4}{\times m}_{5}\) and \({m}_{{T}_{2}}={m}_{1}{\times m}_{3}{\times m}_{5}\); (2) the flag is not raised (that is, the flag qubit is measured to be in \(|0\rangle \)). Otherwise, the state is rejected. The order of two-qubit gates is carefully chosen to ensure fault tolerance while minimizing the number of operations. Further details and a proof of the fault tolerance of this scheme are given in the sixth section of the Supplementary Information. We refer to this preparation as the FT encoding scheme.

To reduce the impact of auxiliary qubit measurement errors10,14, we also require all stabilizer measurement outcomes to be +1 (that is, the NV electron spin is measured to be in \(|0\rangle \)). These outcomes are more reliable3 (Methods), increasing the fidelity of the state preparation, at the cost of a lower success probability (Supplementary Table 1).

We compare the non-FT and FT encoding schemes. We define the logical state fidelity FL as (Methods)

$${F}_{{\rm{L}}}=\sum _{E\in \varepsilon }{\rm{Tr}}(E{| -\rangle }_{{\rm{L}}}{\langle -| }_{{\rm{L}}}E\rho ),$$
(1)

in which ρ is the prepared state and \(\varepsilon =\left\{I,{X}_{i},{Y}_{i},{Z}_{i},{i}=\mathrm{1,2},\ldots ,5\right\}\) is the set of all single-qubit Pauli errors. The fidelity FL gives the probability that there is at most a single-qubit error in the prepared state, that is, there is no logical error. We characterize the prepared state by measuring the 31 operators that define the target state (Extended Data Fig. 2 and Methods). We find that the FT encoding scheme (FL = 95(2)%) outperforms the non-FT scheme (FL = 81(2)%).

To understand this improvement, we analyse the underlying error probability distributions (Figs. 3b,c). For the five-qubit code, the \({|-\rangle }_{{\rm{L}}}\) state plus any number of Pauli errors is equivalent to either \({|-\rangle }_{{\rm{L}}}\) with at most one Pauli error (no logical error) or to \({|+\rangle }_{{\rm{L}}}\) with at most one Pauli error (a logical error). We calculate the overlaps between the prepared state and those states. The results show that the FT scheme suppresses logical errors, consistent with fault tolerance preventing single faults propagating to multi-qubit errors. The overall logical state fidelity FL is improved, despite the higher probability of single-qubit errors owing to the increased complexity of the sequence.

Fault-tolerant logical gates

The five-qubit code supports a complete set of transversal single-qubit Clifford gates, which are naturally fault tolerant16,39. We apply four transversal logical gates to \({|-\rangle }_{{\rm{L}}}\) (Fig. 4): XL = X1X2X3X4X5, YL = Y1Y2Y3Y4Y5, the Hadamard gate HL=Pπ H1H2H3H4H5 and the phase gate SL=Pπ S1S2S3S4S5, in which Pπ is a permutation of the data qubits16,39 (Fig. 4b). These permutations are fault tolerant because we realize them by relabelling the qubits rather than by using SWAP gates39. For completeness, we note that universal computation requires further non-transversal gates, constructed—for example—with auxiliary logical qubits, which are not pursued here18.

Fig. 4: Fault-tolerant gates on the logically encoded qubit.
figure 4

a, We apply transversal logical gates on the encoded state \({|-\rangle }_{{\rm{L}}}\) and measure the resulting logical state fidelity FL (equation (1)) with respect to the targeted state. b, Logical XL, YL, HL (Hadamard) and SL (π/2 rotation around the z-axis) are realized by five single-qubit gates. For HL and SL, this is followed by a permutation of the qubits by relabelling them. c, Grey bars indicate logical state fidelity when compiling the logical gates with subsequent operations (0.95(2) for all gates). Blue bars are logical state fidelities after physically applying the transversal logical gates (0.90(2), 0.90(2), 0.83(2), 0.95(2) for XL, YL, HL and SL, respectively). Error bars are one standard deviation.

Our control system performs the underlying single-qubit gates by tracking basis rotations and compiling them with subsequent gates or measurements (Methods). In the sequence considered here (Fig. 4a), such compilation does not increase the physical operation count and there is no reduction of fidelity (Fig. 4c). For comparison, we also implement the ‘worst-case’ scenario, in which the logical gates are applied physically (Fig. 4c). This includes five single-qubit gates and the corresponding extra echo sequences between the state preparation and the measurement stage. Together, the demonstrated transversal logical gates enable the fault-tolerant preparation of all six eigenstates of the logical Pauli operators.

Fault-tolerant stabilizer measurements

Finally, we demonstrate and characterize a flagged stabilizer measurement on the encoded state (Fig. 5a). Such measurements are a primitive for fault-tolerant quantum error-correction protocols28. To ensure that the measurement is compatible with fault tolerance, the two-qubit gates are carefully ordered and a flag qubit is added to capture the auxiliary qubit errors that can propagate to logical errors28.

Fig. 5: Fault-tolerant stabilizer measurement.
figure 5

a, Circuit diagram to measure the stabilizer XXYIY on the encoded state. As an example to illustrate the compatibility with fault tolerance, we insert a Y error on the auxiliary qubit. This error will propagate to the two-qubit error Y3Y5 on the data qubits, which leads to a logical Z error. However, because the error also triggers the flag qubit, it can be accounted for (Methods). b, Probability of the measurement outcomes of the auxiliary (ma) and flag (mf) qubits when inserting (pe = 1) or not inserting (pe = 0) the Y error on the auxiliary qubit. The results show that the flag qubit successfully detects this error. c, Logical state fidelity FL after the stabilizer measurement as a function of the error probability pe. The non-FT case does not take the flag outcome into account. Values between pe = 0 and pe = 1 are calculated as weighted sums (Methods).

We prepare the logical state \({|-\rangle }_{{\rm{L}}}\) and measure the stabilizer s1 = XXYIY (Fig. 5a). The resulting output consists of the post-measurement state and two classical bits of information from the measurements of the auxiliary and flag qubits (Fig. 5b). The logical state fidelity FL is given by the probability that the logical information can be correctly extracted (no logical error) when taking into account the flag measurement outcome. The interpretation of the error syndrome changes if the flag is raised (Methods). We find FL = 0.77(4) for the post-measurement state without any post-selection. Higher logical state fidelities can be obtained by post-selecting on favourable outcomes, but this is incompatible with error correction.

To illustrate the benefit of the flag qubit, we compare the logical state fidelities with and without taking the flag measurement outcome into account. Because auxiliary qubit errors that propagate to logical errors are naturally rare, no marked difference is observed (Fig. 5c). Therefore, we introduce a Pauli Y error on the auxiliary qubit (Fig. 5a). This error propagates to the two-qubit error Y3Y5. For the case without flag information, this error causes a logical flip ZL (Methods) and the logical state fidelity drops below 0.5. By contrast, with the flag qubit, this non-trivial error is detected (Fig. 5b) and remains correctable, so that the logical state fidelity is partly recovered (Fig. 5c).

Conclusion

In conclusion, we have demonstrated encoding, gates and non-destructive stabilizer measurements for a logical qubit of an error-correction code in a fault-tolerant way. Our results advance solid-state spin qubits from the physical-qubit level to the logical-qubit level, at which fault-tolerant operations become possible. Such fault tolerance is a necessity for large-scale quantum computation, in which error rates must ultimately be suppressed to extremely low levels.

Future challenges are to perform complete quantum error-correction cycles, encode several logical qubits, realize universal fault-tolerant gates and—ultimately—suppress logical error rates exponentially below physical error rates. Although the demonstrated operations are of high fidelity—the experiments consist of up to 40 two-qubit gates and eight mid-circuit auxiliary qubit readouts (Fig. 5a)—improvements in both the fidelities and the number of qubits will be required.

Improved gates might be realized through tailored optimal control schemes that leverage the precise knowledge of the system and its environment40 (Fig. 1a). Coupling to optical cavities can further improve readout fidelities1,41. Scaling to large code distances and several logical qubits can be realized through already-demonstrated magnetic40 and optical42 NV–NV connections that enable modular, distributed, quantum computation based on the surface code and other error-correction codes19. Therefore, our demonstration of the building blocks of fault-tolerant quantum error correction is a key step towards quantum information processing based on solid-state spin qubits.

Methods

Sample

We use a naturally occurring NV centre in a homo-epitaxially chemical-vapour-deposition-grown diamond with a 1.1% natural abundance of 13C and a \(\langle 111\rangle \) crystal orientation (grown by Element Six). A solid-immersion lens is used to enhance the photon-collection efficiency43. The NV centre has been selected for the absence of 13C spins with hyperfine couplings >500 kHz. These experiments are performed at a temperature of 4 K, at which the electron-spin relaxation is negligible (T1 = 3.6(3) × 103 s)37.

Qubits and coherence times

The NV electron-spin auxiliary qubit is defined between the states ms = 0 \((|0\rangle )\) and ms = −1 \((|1\rangle )\). The NV electron-spin coherence times are \({T}_{2}^{* }=4.9(2)\,{\rm{\mu }}{\rm{s}}\), T2 = 1.182(5) ms and up to seconds under dynamical decoupling37. The 14N nuclear-spin flag qubit is defined between the states mI = 0 \((|0\rangle )\) and mI = −1 \((|1\rangle )\). The 13C nuclear-spin data qubits in this device have been characterized in detail in previous work3,38,44 (Fig. 1a). See Supplementary Tables 25 for the hyperfine parameters, coherence times and qubit–qubit interactions for the qubits used here.

Magnetic field

A magnetic field of about 403 G is applied using a room-temperature permanent magnet on a XYZ translation stage. This applied field lifts the degeneracy of the ms = ±1 states owing to the Zeeman term (first section of the Supplementary Information). We stabilize the magnetic field to <3 mG using temperature stabilization and an automatic recalibration procedure (every few hours). We align the magnetic field along the NV axis using thermal echo sequences with an uncertainty of 0.07° in the alignment38.

Single-qubit and two-qubit gates

Single-qubit gates and echo pulses are applied using microwave pulses for the NV electron spin (ms = 0 ↔ ms = −1 transition, Hermite pulse shapes37,45, Rabi frequency of about 15 MHz) and using radio-frequency (RF) pulses for the 13C spin qubits (error function pulse shapes3, typical Rabi frequency of about 500 Hz) and the 14N spin qubit (error function pulse shapes, Rabi frequency of about 2 kHz).

The Hermite pulse envelopes of the microwave pulses are defined as

$$A\,\left[1-c{\left(\frac{t-\mu }{T}\right)}^{2}\right]\cdot {\rm{\exp }}\left[-{\left(\frac{t-\mu }{T}\right)}^{2}\right],$$
(2)

in which c = 0.956 for π pulses and c = 0.667 for π/2 pulses, μ = 0.5tpulse, T = 0.1667tpulse, tpulse is the microwave pulse length and A is the pulse amplitude, which is experimentally calibrated to achieve a π or π/2 rotation. For this work, we use tpulse = 168 ns for π pulses and tpulse = 100 ns for π/2 pulses. The envelope of the RF pulses is defined as

$$f\left(t\right)=1-\frac{1}{2}{\rm{erf}}\left(\frac{2\left(\triangle t-t+{t}_{0}\right)}{\triangle t}\right)-\,\frac{1}{2}{\rm{erf}}\left(\frac{2\left(\triangle t+t-{t}_{{\rm{pulse}}}\right)}{\triangle t}\right),$$
(3)

in which Δt is the rise time, t0 is the start time of the pulse, tpulse is the pulse length and erf(x) is the error function3. We ensure that the RF pulses consisted of an integer number of periods of the RF waveform, that is, we ensure that ωtpulse = 2πn for integer n. This ensures that any phase picked up on the electron spin owing to the RF pulse is cancelled. Note that the 13C spin qubits (data qubits) are distinguishable in frequency owing to their hyperfine coupling to the NV electron spin (Supplementary Table 2).

Electron–nuclear two-qubit gates are realized using two different gate designs, depending on the properties of the targeted nuclear spin. For data qubits 1, 2, 4 and 5, two-qubit gates are realized through dynamical decoupling sequences of N equally spaced π-pulses on the electron spin of the form3,46 (τr − π − τr)N. This design requires a notable hyperfine component perpendicular to the applied magnetic field46. For data qubit 3 and the flag qubit (the 14N spin), the perpendicular hyperfine coupling is small and we perform two-qubit gates by interleaving the dynamical decoupling sequence with RF pulses3. Both gate designs simultaneously decouple the NV electron spin from the other qubits and the environment3. The parameters and fidelities for the two-qubit gates are given in Supplementary Table 4. Note that direct nuclear–nuclear two-qubit gates can also be constructed47, but because the natural interaction is much weaker than the electron–nuclear interaction, we don't use such gates here and designed the FT encoding circuit based on electron–nuclear gates only.

Compilation of gate sequences

Our native two-qubit gates are electron-controlled nuclear-spin rotations and are equivalent to the CNOT gate up to single-qubit rotations (Supplementary Fig. 4). To implement the sequences shown in the figures, we first translate all gates into these native gates and compile the resulting sequence. Afterwards, the circuit is translated into the actual pulse sequence. At the core of this compilation process is the tracking and synchronization of the qubit phases and the corresponding pulse timings. See Supplementary Information for the details of this compilation process (Supplementary Figs. 49 and pseudocode 18).

Echo sequences for the data qubits

To mitigate decoherence of the data qubits owing to their spin environment, we use echo sequences that are interleaved throughout the experiments. These echo sequences ensure that the data qubits rephase each time they are operated on. Furthermore, the sequence design minimizes the time that the auxiliary electron-spin qubit is idling in superposition states, which are prone to dephasing. We use two echo stages between stabilizer measurements, as well as before and after the logical gates of Fig. 4, which provides a general and scalable solution for the timing of all gates and echoes (third section of the Supplementary Information).

An extra challenge is that, owing to the length of the sequences (up to 100 ms), we need to account for the small unwanted interactions between the nuclear-spin data qubits. The measured coupling strengths show that the strongest couplings are between qubits 3 and 2 (16.90(4) Hz) and between qubits 3 and 5 (12.96(4) Hz)38 (Supplementary Table 5). Such interactions can introduce correlated two-qubit errors that are not correctable in the distance-3 code considered here, which can only handle single-qubit errors in the code block.

To mitigate these qubit–qubit couplings, we decouple qubit 3 asynchronously from the other qubits (Supplementary Fig. 8). Ultimately, such local correlated errors can be suppressed entirely by larger distance codes.

Real-time control and feedforward operations

Real-time control and feedforward operations are implemented through a programmable microprocessor (Jaeger ADwin Pro II) operating on microsecond timescales. The microprocessor detects photon events coming from the detectors, infers the measurement outcomes and controls both the subsequent sequences in the arbitrary waveform generator (Tektronix AWG 5014c) and the lasers for the auxiliary qubit readout. The precise timing for quantum gates (1-ns precision) is based on the clock of the arbitrary waveform generator. Furthermore, the microprocessor operates various control loops that prepare the NV centre in the negative charge state, on resonance with the lasers and in the focus of the laser beam (see second and third sections of the Supplementary Information).

Readout of the auxiliary qubit

The electron spin (auxiliary qubit) is read out by resonantly exciting the ms = 0 to Ex optical transition43. For one or more photons detected, we assign the ms = 0 outcome; for zero photons, we assign ms = ±1. The single-shot readout fidelities are F0 = 90.5(2)% and F1 = 98.6(2)% for ms = 0 and ms = −1, respectively (average fidelity 94.6(1)%).

Uncontrolled electron-spin flips in the excited state cause dephasing of the nuclear spins through the hyperfine interaction. To minimize such spin flips, we avoid unnecessary excitations by using weak laser pulses, so that a feedback signal can be used to rapidly turn off the laser on detection of a photon (within 2 μs). The resulting probability that the electron spin is in state ms = 0 after correctly assigning ms = 0 in the measurement is 0.992 (ref. 14).

For measurements that are used for heralded state preparation, that is, for which we only continue on a ms = 0 outcome (see, for example, Fig. 3), we use shorter readout pulses. This improves the probability that a ms = 0 outcome correctly heralds the ms = 0 state, at the cost of reduced success probability (Supplementary Table 1).

System preparation and qubit initialization

At the start of the experiments, we first prepare the NV centre in its negative charge state and on resonance with the lasers. We then initialize the NV electron spin in the ms = 0 state through a spin pumping process (fidelity > 99.7%)43. We define the electron-spin qubit between the states ms = 0 \((|0\rangle )\) and ms = −1 \((|1\rangle )\). We initialize the data qubits through SWAP sequences (Supplementary Fig. 6) into \(|0\rangle \) and subsequent optical reset of the auxiliary qubit (initialization fidelities 96.5–98.5%; see Supplementary Table 4). The flag qubit is initialized through a projective measurement that heralds preparation in \(|0\rangle \) (initialization fidelity 99.7%). Other product states are prepared by subsequent single-qubit gates.

Final readout of the data qubits

Measuring single-qubit and multi-qubit operators of the data qubits is performed by mapping the required correlation to the auxiliary qubit (through controlled rotations) and then reading out the auxiliary qubit14. To provide best estimates for the measurements, we correct the measured expectation values (Fig. 2 and Extended Data Figs. 1 and 2) for infidelities in the readout sequence; see Bradley et al.3 for the correction procedure.

Fidelity of the GHZ state

The fidelity of the prepared state ρ (in Fig. 2 and Extended Data Fig. 1) with respect to the target GHZ state \(({|{\rm{\psi }}\rangle }_{+})\) is obtained as

$$\begin{array}{l}F={\rm{Tr}}({|{\rm{\psi }}\rangle }_{+}{\langle {\rm{\psi }}|}_{+}\rho )\\ \,=\,\frac{1}{16}(1+\langle IIZZ\rangle +\langle IZZI\rangle +\langle ZZII\rangle +\,\langle ZIZI\rangle +\langle IZIZ\rangle \\ \,+\langle ZIIZ\rangle +\langle XXXX\rangle +\,\langle ZZZZ\rangle +\langle YYYY\rangle -\langle YXYX\rangle \\ \,-\langle YYXX\rangle -\langle YXXY\rangle -\langle XXYY\rangle -\langle XYYX\rangle -\,\langle XYXY\rangle ).\end{array}$$
(4)

Assessing the logical state fidelity

The logical state fidelity FL is defined in equation (1) and gives the probability that the state is free of logical errors. Said differently, FL is the fidelity with respect to the ideal five-qubit state after a round of perfect error correction, or the probability to obtain the correct outcome in a perfect fault-tolerant logical measurement. Although fault-tolerant circuits for logical measurement exist28, we do not experimentally implement these here. Instead, we extract FL from a set of measurements, as described in the following using \({|-\rangle }_{{\rm{L}}}\) as an example.

The logical state \({|-\rangle }_{{\rm{L}}}\) is the unique simultaneous eigenstate of the five weight-3 operators \({p}_{{\rm{i}}}\) with eigenvalue +1. Thus, we can describe the state \(E{|-\rangle }_{{\rm{L}}}\) (with E a Pauli error) as the projector

$$E{| -\rangle }_{{\rm{L}}}{\langle -| }_{{\rm{L}}}E={\prod }_{i=1}^{5}\frac{(1+{m}_{i}{p}_{i})}{2},$$

in which mi = ±1 is the measurement outcome of \({p}_{{\rm{i}}}\) and mi = −1 when E anticommutes with \({p}_{{\rm{i}}}\). This projector can be expanded as a summation of 31 multi-qubit Pauli operators (including a constant), which are listed in Extended Data Fig. 2. The logical state fidelity \({p}_{{\rm{i}}}\) in equation (1) can then be written as

$$\begin{array}{l}{F}_{{\rm{L}}}=\sum _{E\in \varepsilon }{\rm{Tr}}(E{| -\rangle }_{{\rm{L}}}{\langle -| }_{{\rm{L}}}E\rho )\\ \,=\frac{1}{2}+\frac{1}{8}\left(\right.\langle IZXZI\rangle +\langle ZIIZX\rangle +\langle XZIIZ\rangle +\langle ZXZII\rangle +\langle IIZXZ\rangle \\ \,+\langle YIXIY\rangle +\langle IYYIX\rangle +\langle XIYYI\rangle +\langle IXIYY\rangle +\langle YYIXI\rangle +\langle ZZYXY\rangle \\ \,+\langle YXYZZ\rangle +\langle ZYXYZ\rangle \left.\,+\langle XYZZY\rangle +\langle YZZYX\rangle +\langle XXXXX\rangle \right).\end{array}$$
(5)

Here \(\varepsilon =\left\{I,{X}_{i},{Y}_{i},{Z}_{i},{i}=\mathrm{1,2},\ldots ,5\right\}\) is the set of correctable errors for the five-qubit code. To obtain FL experimentally, we measure this set of expectation values.

Logical state fidelity with flag

If the flag in the circuit in Fig. 5a is not raised, then a cycle of error correction would correct any single-qubit error on a logical state. The logical state fidelity is then given by equation (1), which we now refer to as \({F}_{{\rm{L}}}^{{\rm{not\; raised}}}\). A raised flag leads to a different interpretation of the error syndrome28 (Supplementary Table 7).

For example, the Y error on the auxiliary qubit in Fig. 5a leads to the output state \({Y}_{3}{Y}_{5}{|-\rangle }_{{\rm{L}}}\), for which the eigenvalues of s1 = XXYIY, s2 = YXXYI, s3 = IYXXY and s4 = YIYXX give the syndrome [+1, −1, −1, −1]. Without flag, the corresponding single-qubit recovery is Z4, which changes the syndrome back to all +1 (Supplementary Table 7). This recovery leads to the remaining error Y3Z4Y5, which is a logical Z error. However, taking the flag measurement outcome into account, the syndrome is interpreted differently and the recovery is Y3Y5, so that no error is left (Supplementary Table 7).

For the cases in which the flag is raised, the logical state fidelity with respect to \({|-\rangle }_{{\rm{L}}}\) is now given by:

$$\begin{array}{l}{F}_{{\rm{L}}}^{{\rm{raised}}}=\sum _{E\in \varepsilon {\prime} }{\rm{Tr}}(E{| -\rangle }_{{\rm{L}}}{\langle -| }_{{\rm{L}}}E\rho )\\ \,=\,\frac{1}{2}+\frac{1}{32}\left(\right.6\langle IIZXZ\rangle +6\langle ZXZII\rangle +6\langle YYIXI\rangle -2\langle ZIIZX\rangle \\ \,+6\langle IXIYY\rangle +2\langle YZZYX\rangle +2\langle XYZZY\rangle -2\langle IZXZI\rangle \\ \,+2\langle ZYXYZ\rangle +6\langle XIYYI\rangle +2\langle YXYZZ\rangle +2\langle ZZYXY\rangle \\ \left.\,+6\langle IYYIX\rangle -2\langle YIXIY\rangle +2\langle XXXXX\rangle -2\langle XZIIZ\rangle \right),\end{array}$$
(6)

with \({\varepsilon }^{{\prime} }\) being another set of correctable errors

$${{\rm{\varepsilon }}}^{{\prime} }=\{I,{X}_{1},{X}_{3}{Y}_{5},{Z}_{1},{X}_{2},{Y}_{2},{Z}_{3}{Y}_{5},{X}_{1}{Y}_{2},{Y}_{3},{Z}_{3},{X}_{4},{Y}_{4},{Y}_{3}{Y}_{5},{X}_{5},{Y}_{5},{X}_{1}{Z}_{2}\}.$$
(7)

A detailed derivation for this set of errors and their corresponding syndromes are given in the fifth section of the Supplementary Information.

The logical state fidelity after the stabilizer measurement (Fig. 5) is calculated as the weighted sum of the fidelities conditioned on the two flag outcomes:

$${F}_{{\rm{L}}}={p}_{{\rm{f}}}\cdot {F}_{{\rm{L}}}^{{\rm{raised}}}+\left(1-{p}_{{\rm{f}}}\right)\cdot {F}_{{\rm{L}}}^{{\rm{not\; raised}}},$$
(8)

with pf being the probability that the flag is raised and \({F}_{{\rm{L}}}^{{\rm{raised}}}\) and \({F}_{{\rm{L}}}^{{\rm{not\; raised}}}\) are as defined above.

Finally, to construct the logical state fidelity as a function of pe (Fig. 5c), we measure FL with (pe = 0) and without (pe = 1) the auxiliary qubit error and calculate the outcomes for other error probabilities pe from their weighted sum:

$${F}_{{\rm{L}}}({p}_{{\rm{e}}})=(1-{p}_{{\rm{e}}}){\cdot F}_{{\rm{L}}}({p}_{{\rm{e}}}=0)+{p}_{{\rm{e}}}\cdot {F}_{{\rm{L}}}({p}_{{\rm{e}}}=1)$$
(9)

Error distribution in the prepared state

The overlaps between the prepared state ρ and the state \(E{|-\rangle }_{{\rm{L}}}\) with E identity or a single-qubit error are written as P0,− and P1,−, respectively. These correspond to the cases that there is no logical error. The overlaps between the prepared state ρ and the state \(E{|+\rangle }_{{\rm{L}}}\) with E identity or a single-qubit error are written as P0,+ and P1,+, respectively. In these cases, there is a logical error. These overlaps are shown in Fig. 3b,c and calculated as (\(\alpha =\pm \))

$${P}_{0,{\rm{\alpha }}}={\rm{Tr}}({| {\rm{\alpha }}\rangle }_{{\rm{L}}}{\langle {\rm{\alpha }}| }_{{\rm{L}}}\rho ),$$
(10)
$${P}_{1,{\rm{\alpha }}}=\sum _{E\in \varepsilon }{\rm{Tr}}(E{| {\rm{\alpha }}\rangle }_{{\rm{L}}}{\langle {\rm{\alpha }}| }_{{\rm{L}}}E\rho ).$$
(11)

These overlaps can be explicitly expressed in terms of the 31 measured expectation values (see seventh section of the Supplementary Information).

Error analysis

The uncertainties in the measured fidelities, logical state fidelities and probabilities (P0/1,±) are obtained from the uncertainties in the measured expectation values using error propagation. For example, the logical state fidelity FL is calculated as

$${F}_{{\rm{L}}}=\frac{1}{2}+\frac{1}{8}\left(\sum _{i}{A}_{i}\right),$$
(12)

in which Ai are the 16 expectation values shown in equation (5). Assuming that the errors in the measured expectation values are independent, the standard deviation in FL is:

$${\sigma }_{{F}_{{\rm{L}}}}=\frac{1}{8}{\left(\sum _{i}{\sigma }_{{A}_{i}}^{2}\right)}^{\frac{1}{2}},$$
(13)

in which \({\sigma }_{{A}_{i}}\) is the standard deviation of the expectation value Ai and is given by a binomial distribution42. Note that \({\sigma }_{{A}_{i}}\) is also corrected for the readout correction process described in Bradley et al.3.

Note added

While finalizing this manuscript, two related preprints appeared that demonstrate destructive stabilizer measurements with a flag qubit48 and flag fault-tolerant quantum error correction49 with trapped-ion qubits. Furthermore, during the revision process, three related preprints appeared that demonstrate quantum error correction on a surface code using superconducting qubits50,51 and realize a flag-based universal fault-tolerant gate set using trapped ions52.