Introduction

In the era of Noisy Intermediate Scale Quantum Computers (NISQs)1 it is of paramount importance to be able to characterize the proposed quantum hardware in order to check how good these machines are in performing quantum computation with the purpose of attaining an advantage over classical computers. This paper shows how to perform accurate and robust measurements of the stabilizer Rényi entropy, which in turn is known to quantify the resource known as “magic”2.

It is well known that the preparation of stabilizer states, the implementation of Clifford gates and measurements in the computational basis can be made fault tolerant3,4,5,6,7,8,9. However, computers based on the Clifford resources can be efficiently simulated on classical computers10,11,12,13, similarly to what happens for matchgate circuits (MGCs). This means that the power of quantum advance requires resources beyond the Clifford group, like the Phase π/8 gate (T gate) or the Toffoli gate and non-Gaussian states for the MCGs14,15. The precious resource that makes quantum computers special is colloquially dubbed as ‘magic’ and a resource theory of magic has been developed in recent years2,3,4,16,17,18,19,20,21,22,23,24,25,26,27.

It is a striking fact that these resources are difficult to implement3,5,28,29,30,31,32,33. The very reason why these resources are powerful makes them fragile. Moreover, the amount of these resources that needs to be used in a computation must be calibrated accurately: just like entanglement34, too much magic is not useful for quantum computation (see Supplementary Note 1), see also21. Moreover, decoherence is not magic preserving, and it can both increase or decrease the amount of magic in a system, as we will show in the experiments. To the extent that decoherence is spoiling quantum computation, then one needs the amount of magic created and manipulated throughout the computation to be accurate: in this paper, we prove that an excess amount of unwanted magic makes the task of distinguishing the state ψ from a random state an exponentially difficult task, see Supplementary Note 1 for the proof. Moreover, since inaccurate Clifford gates can produce magic, the presence of excess magic is in fact a signal of noise. We exploit this fact to show that the measurement of magic can be used to quantify and characterize the noise in the quantum circuit. It is thus important to be able to quantify this resource and measure it to characterize the fitness of real quantum hardware. Unfortunately—until recently—proposed measures of magic4,17,22,35 have been based on extremization procedures and no experimental measurement scheme has been proposed.

In this work, we propose and experimentally demonstrate a protocol based on randomized measurements36,37,38,39,40,41,42,43,44,45,46,47,48,49 to measure magic in a quantum system with n qubits and to characterize quantum hardware. We adopt the magic measure called stabilizer 2-Rényi entropy defined as2

$${M}_{2}(\left\vert \psi \right\rangle ):= -{\log }_{2}W(\psi )-{S}_{2}(\psi )-\log d$$
(1)

where \(W(\psi ):= {{{\rm{tr}}}}(Q{\psi }^{\otimes 4})\), Qd−2PP4, and d = 2n, where the sum is taken over all multi-qubit strings of Pauli operators, applied to four copies of the state, and \({S}_{2}(\psi )=-{\log }_{2}{{{\rm{tr}}}}{\psi }^{2}\) is the 2-Rényi entropy. In order to measure M2 we propose an improved protocol compared to the one presented in ref. 2 as it only involves randomized one-qubit measurements instead of global multi-qubit measurements, with obvious advantages in terms of errors and quantum control.

As M2 depends on the state ψ, a direct evaluation of M2 would be possible by knowing all the expectation values \({{{\rm{tr}}}}(P\psi )\) of multi-qubit Pauli strings in the state ψ. This, of course, is equivalent to tomography and it is very expensive as it involves the evaluation of d2 expectation values for a total cost in resources scaling as \({{{\mathcal{O}}}}({d}^{3})\). Here, we employ a protocol based on randomized measurements which does not rely on tomographic techniques. Remarkably, randomized measurement protocols are highly favorable compared to state tomography38,39,41,43. As we shall see, we will employ a number of resources scaling as \({{{\mathcal{O}}}}({\epsilon }^{-2}{d}^{2})\) for an estimate with error ϵ.

Results

The protocol

The protocol consists in first drawing a string of random one-qubit Clifford operations, namely \(C{ = \bigotimes }_{i = 1}^{n}{c}_{i}\) and applying it to four copies of the state of interest. The protocol extracts correlations between these copies. Indeed, the quantity of interest in the first term of Eq. (1) can then be computed as

$$-{\log }_{2}{{{\rm{tr}}}}(Q{\psi }^{\otimes 4})=-{\log }_{2}\mathop{\sum}\limits_{\overrightarrow{{{{\bf{s}}}}}}{(-2)}^{-\parallel \overrightarrow{{{{\bf{s}}}}}\parallel }{{\mathbb{E}}}_{C}P({{{{\bf{s}}}}}_{1}| C)P({{{{\bf{s}}}}}_{2}| C)P({{{{\bf{s}}}}}_{3}| C)P({{{{\bf{s}}}}}_{4}| C)$$
(2)

The formula above features the expectation value over the randomized measurements of the Clifford operator C on states of the computational basis sa and the Hamming weight \(\parallel \overrightarrow{{{{\bf{s}}}}}\parallel\) of the string s1s2s3s4. The quantity \(P({{{{\bf{s}}}}}_{a}| C)={{{\rm{tr}}}}(C\psi {C}^{{\dagger} }{{{{\bf{s}}}}}_{a})\) represents the probability of finding the computational basis state sa when measuring the state CψC. The second term in Eq. (1) is the usual 2-Rényi entropy and can be measured by randomized measurements using the techniques of ref. 41. An important feature of our protocol is the fact that it only needs randomized operations over the Clifford group instead of the full unitary group as in ref. 38. In fact, by collecting the occupation probabilities P(CψCsa) one can estimate both W(ψ) and the purity P(ψ) together thanks to the fact that the Clifford group forms a 2-design. See Methods. The operational meaning of the protocol is the following: randomized measurement protocols are usually conducted on a (Haar) random basis. Here we select a (local) stabilizer basis. Clifford rotations constitute the free resources for magic state resource theory. General unitaries would result in a change in quantum magic. Clifford orbits of a given quantum state instead are filled out by iso-magic states. A Clifford randomized measurement protocol measures the magic of the entire Clifford orbit, rather than of a single quantum state.

The experiments have been conducted on two IBM Quantum Falcon processors: a 5 qubit system, ibmq_quito and a 7 qubit system ibmq_casablanca50.

The experiment can be schematized as follows (see Fig. 1). Starting with a n-qubit state initialized in the \({\left\vert 0\right\rangle }^{\otimes n}\) state, we pass it through a unitary quantum circuit U resulting in the state preparation \(\left\vert \psi \right\rangle\). We want to characterize the fitness of such a circuit in providing a state with the promised magic. At this point, one extracts n one-qubit Clifford operations ci, applies them to the state \(\left\vert \psi \right\rangle\), and measures the state in the computational basis.

Fig. 1: Schematic of the implementation of the experiment for measuring magic on a quantum processor.
figure 1

From left to right: initialization of the system in the state \({\left\vert 0\right\rangle }^{\otimes n}\); preparation of the target state \(\left\vert \psi \right\rangle\) by a unitary quantum circuit Ut containing a number t of non-Clifford gates; intervention of the noise \({{{{\mathcal{N}}}}}_{p}\) affecting the system effectively prepares the (mixed) state ψp; measurement. The measurement apparatus is composed of n local Clifford operators \(C{ = \bigotimes }_{i = 1}^{n}{c}_{i}\) randomly sampled from the single qubit Clifford group \({c}_{i}\in {{{{\mathcal{C}}}}}_{1}\), followed by n measurements in the computational basis \(\{\left\vert {{{\bf{s}}}}\right\rangle \}\) that are performed to estimate the occupation probabilities P(CψCs). The gate imperfection in the application of the Clifford operators is denoted by c(ϵ).

At this point, we want to analyze the scaling of the cost of necessary resources, both analytically and numerically. The experiment is repeated NM times for every string \(C{ = \bigotimes }_{i = 1}^{n}{c}_{i}\) in order to collect statistics to compute the occupation probabilities P(CψCsa). Then, in order to compute the expectation value over the whole Clifford group \({{\mathbb{E}}}_{C}\), one samples the Clifford group with NU elements. In order to sample the Clifford group properly and to build sufficient statistics we simulate numerically the total number of measurements needed for M2, i.e. NTOT = NM × NU. By evaluating the variance of the estimator for W, through the use of standard statistical analysis (Bernstein inequality), one can bound the probability of making an error ϵ as a function of the total resources NU × NM employed. In Methods, we prove that by employing a total number of resources \({{{\mathcal{O}}}}({\epsilon }^{-2}{d}^{2})\) the randomized measurement protocol is able to estimate the purity within an error ≤ϵ and the stabilizer purity within an error ϵd−1. These theoretical bounds can be optimized by numerical analysis. The optimal number of unitaries NU and of measurements NM is found by numerical simulations imposing that the relative error on the theoretical value of stabilizer purity to be below 12% and an average value of the purity greater than 0.88, thus imposing a relative error of 12% on the purity as well. An important remark is that both NU and NM depend on the state ψ. Remarkably, low-magic states (like the states in the computational basis—which have exactly zero magic) require a higher NU × NM compared to states with high magic, see Supplementary Table I in Supplementary Note 4.

In order to characterize the fitness of a quantum processor in producing resources beyond stabilizer states, we adopt the model of a t-doped Clifford circuit51,52,53. This circuit consists of a block of Clifford gates in which t non-Clifford gates are injected. The non-Clifford gates we inject are \({P}_{\vartheta }=\left\vert 0\right\rangle \left\langle 0\right\vert +{e}^{i\vartheta }\left\vert 1\right\rangle \left\langle 1\right\vert\) gates: these constitute the resources that are injecting magic in the system, while the Clifford circuits are free resources. For ϑ = π/2 one obtains the phase flip gate that still belongs to the Clifford group and thus is a free resource. The value ϑ = π/4 instead, the so-called T gate, yields the maximal amount of magic achievable for a Pϑ gate. The T-gates will be called the “magic seeds” of the quantum circuit. These circuits are efficient in entangling so the output state of the circuit is in general not a trivial product state but a state that is both entangled and possesses magic.

Measuring magic

We start with the characterization of the quantum processor on single-qubit states, and thus without entanglement. The single-qubit magic states are obtained by applying Pϑ on the states \(\left\vert +\right\rangle =\frac{1}{\sqrt{2}}(\left\vert 0\right\rangle +\left\vert 1\right\rangle )\) obtaining \(\left\vert {P}_{\vartheta }\right\rangle \equiv {P}_{\vartheta }\left\vert +\right\rangle =\frac{1}{\sqrt{2}}(\left\vert 0\right\rangle +{e}^{i\vartheta }\left\vert 1\right\rangle )\) whose stabilizer 2-Rényi entropy reads \({M}_{2}(\left\vert {P}_{\vartheta }\right\rangle )=-{\log }_{2}\left(\frac{7+\cos (4\vartheta )}{8}\right)\), achieving its maximum for \({M}_{2}(\left\vert {P}_{\pi /4}\right\rangle )=1-{\log }_{2}3/2\) and its minimum for \({M}_{2}(\left\vert {P}_{\pi /2}\right\rangle )=0\).

The results of the experiment on the ibmq_quito are shown in Fig. 2. As we can see, the experimental data are in very good accordance with the theoretical prediction for the target state, showing the fitness of ibmq_quito in preparing single-qubit magic states. Decoherence effects are also very low, as we can see from the purity, see Fig. 2.

Fig. 2: Stabilizer 2-Rényi entropy for \(\left\vert {P}_{\vartheta }\right\rangle\).
figure 2

Plot of the magic of the single qubit \(\left\vert {P}_{\vartheta }\right\rangle\)-states, for \(\theta =0,\frac{\pi }{16},\frac{\pi }{8},\frac{\pi }{6},\frac{\pi }{5},\frac{\pi }{4}\). The data displayed (blue dots) are obtained from the quantum processor ibmq_quito. The blue dashed curve represents the theoretical value of the magic for \(\left\vert {P}_{\vartheta }\right\rangle\)-states, i.e. \({M}_{2}(\left\vert {P}_{\vartheta }\right\rangle )=3-\!\!{\log }_{2}\left(7+\cos (4\vartheta )\right)\). Additionally, a plot of the purity for these states is displayed in the upper right corner: as the data show, the purity is 1 within the experimental errors, showing that the decoherence affecting the system is negligible for n = 1 and also the experimental values of magic are in perfect agreement with the theoretical ones. See Supplementary Table II in the Supplementary Note 4 for the data.

We now proceed to the more difficult task of characterizing a quantum processor capable of preparing entangled states. Starting from the computational basis state \({\left\vert 0\right\rangle }^{\otimes n}\), i.e. the input state of the quantum processor, we first apply a layer of Hadamard H-gates to obtain \({\left\vert +\right\rangle }^{\otimes n}={H}^{\otimes n}{\left\vert 0\right\rangle }^{\otimes n}\). Then, we apply T-gates on n1 qubits, with n1 = 0, … , n. The T-gates inject magic into the system. For n1 = n, the state obtained is the maximal magic product state achievable. If one wants to pump more magic into the system, one needs to create some entanglement between the qubits. To do so, we apply a layer of CX-gates, i.e. Clifford entangling 2-qubit gates defined as CXi,j = Ii (Ij + Xj) + Zi (Ij − Xj) and nested in the following way: CXn−1,nCXn−2,n−1CX1,2. Then we can inject some more magic in the system by applying another layer of n2T-gates with n2 = 1, … , n − 1 followed by another layer of CX: CX1,2CXn−2,n−1CXn,n−1. For the pictorial representation of the previously described architecture see Fig. 3. At the end of the state preparation, the magic seeds in the circuit are t = n1 + n2 and the state prepared is the \(\left\vert {\Gamma }_{({n}_{1},{n}_{2})}^{(n)}\right\rangle\)-state, where 1 ≤ t ≤ 2n − 1. In the following, we fill in T-gates starting from (0, 1), then (n1, 1) with (n1, = 1, … , n; n2 = 1), and finally (n, n2), with n2 = 2, … , n − 1. With this prescription, the label t uniquely describes the circuit. For example, t = 4 on a system with n = 6 qubits means three T-gates on the first layer and one T-gate on the second layer, see Fig. 3. The optimal number of NU, NM for a system with n = 3, 4, 5 qubits can be found in Supplementary Table I in Supplementary Note 4 and Fig. 7 in “Methods.”

Fig. 3: Preparation of \(\left\vert {\Gamma }_{t}^{(n)}\right\rangle\)-states.
figure 3

The magic seeds (T-gates) are placed either on the first layer (immediately after the Hadamard gates H), or in the second layer (immediately after the first layer of C − NOT gates). We start with a T-gate in the second layer, then start filling up the first layer. Upon completion of the first layer, we start filling up the second layer again. The figure shows: \({{{\bf{(a)}}}}\left\vert {\Gamma }_{1}^{(n)}\right\rangle\), \({{{\bf{(b)}}}}\left\vert {\Gamma }_{n+1}^{(n)}\right\rangle\) and \({{{\bf{(c)}}}}\left\vert {\Gamma }_{2n-1}^{(n)}\right\rangle\) which is the final doped Clifford circuit which we consider in this paper.

In a system with n qubits, we can prepare the states \(\vert {\Gamma }_{t}^{(n)}\rangle\) with t = 1, … , 2n − 1. The results of the experiment for n = 3, 4, 5 are shown in Figs. 4, 5, and 6, respectively. We can see that, for larger values of n, the purity of the prepared state is compromised, due to decoherence. The measured experimental values of magic shoot off the theoretical prediction, especially for low magic states. Somewhat counterintuitively, the experimental value of magic is higher than the theoretical one. As we mentioned above, spurious injection (or subtraction) of magic can happen for two reasons. Inaccurate implementation of the Clifford gates—and thus turning them into non-free resources—or noise affecting them, or decoherence. That is, our experimental characterization of how magic is created in a quantum circuit tests not only the quantity of magic, but the accuracy with which the desired magic is created. The fact that the circuit must not only create magic, but must do it so with a certain accuracy, allows us to use the experimental data obtained from our protocol to characterize the noise affecting the system. A first insight comes from the realization, see Figs. 4, 5, and 6 that the noise is affecting more the preparation of low-magic states than that of high-magic states, mostly because of imperfection in the implementation of the resource-free Clifford gates like the CX gate. Let us see how we can characterize the noise affecting the system. A very general error model for the target state ψ is through a quantum channel \({{{\mathcal{E}}}}(\psi ):= {\sum }_{i}{q}_{i}{P}_{i}\psi {P}_{i}\). Random states are a good model for high-magic states2 and thus, to understand why the noise affecting the system does not disturb the magic injected in high-magic states, we compute the average difference in magic between a random state ψ and the noisy state \({{{\mathcal{E}}}}(\psi )\) as: \({\langle \delta M\rangle }_{{\rm{Haar}}}:= {\langle | M({{{\mathcal{E}}}}(\psi ))-M(\psi )| \rangle }_{{\rm{Haar}}}\). Calculation shows (see Supplementary Note 2) that \({\langle \delta M\rangle }_{{\rm{Haar}}}={{{\mathcal{O}}}}({S}_{2}({{{\bf{q}}}}))\). In other words, at high levels of magic, this quantity is robust under the noise model provided that the distribution q = {qi} is low in entropy S2(q).

Fig. 4: Stabilizer 2-Rényi entropy for n = 3.
figure 4

Plot of the stabilizer 2-Rényi entropy for a ibmq_quito and b ibmq_casablanca. Both figures contain the experimental values (blue dots), the theoretical values (green triangles) of the magic for the desired pure state, and the noise model values (gray squares) of the magic for the mixed state prepared on the quantum processor. The values of the magic of the \(\left\vert {\Gamma }_{t}^{(3)}\right\rangle\)-states for t = 1, … , 5 are plotted as functions of the number of T-gates t in the doped random Clifford circuits (see Supplementary Tables III and IV in the Supplementary Note 4 for the data). See Fig. 3 for the preparation of such states. Both figures contain in the upper left corner the purity (orange dots) of the output state prepared on the quantum processor and its average value (dashed line). Here the number of resources NTOT ≡ NU × NM depends on the number of T-gates t thrown in the circuits as \({N}_{{\rm{TOT}}}={2}^{{A}_{3}+{B}_{3}(5-t)}\), where A3 = 10.6 ± 0.3, B3 = 0.56 ± 0.08, see Supplementary Table I in Supplementary Note 4 and Fig. 7 in “Methods.” Note that the experimentally observed magic can be—and typically is—higher than the theoretically predicted magic. This is because imperfectly performed Clifford gates are no longer exactly Clifford and can inject uncontrolled/unwanted magic into the system. This effect is enhanced for more qubits and deeper circuits.

Fig. 5: Stabilizer 2-Rényi entropy for n = 4.
figure 5

Plot of the stabilizer 2-Rényi entropy for a ibmq_quito and b ibmq_casablanca. Both figures contain the experimental values (blue dots), the theoretical values (green triangles) of the magic for the desired pure state, and the noise model values (gray squares) of the magic for the mixed state prepared on the quantum processor. The values of the magic of the \(\left\vert {\Gamma }_{t}^{(4)}\right\rangle\)-states for t = 1, … , 7 are plotted as functions of the number of T-gates t in the doped random Clifford circuits, (see Supplementary Tables III and IV in Supplementary Note 4 for the data). See Fig. 3 for the preparation of such states. Both figures contain in the upper left corner the purity (orange dots) of the output state prepared on the quantum processor and its average value (dashed line). Here the number of resources NTOT ≡ NU × NM depends on the number of T-gates t thrown in the circuits as \({N}_{{\rm{TOT}}}={2}^{{A}_{4}+{B}_{4}(7-t)}\), where A4 = 11.3 ± 0.3, B4 = 0.49 ± 0.05, see Supplementary Table I in Supplementary Note 4 and Fig. 7 in “Methods.” Note that the experimentally observed magic can be—and typically is—higher than the theoretically predicted magic. See the caption of Fig. 4 for an explanation.

Fig. 6: Stabilizer 2-Rényi for n = 5.
figure 6

Plot of the stabilizer 2-Rényi for a ibmq_quito and b ibmq_casablanca. Both figures contain the experimental values (blue dots), the theoretical values (green triangles) of the magic for the pure state one would have liked to obtain, and the noise model values (gray squares) of the magic for the mixed state prepared on the quantum processor. The values of the magic of the \(\left\vert {\Gamma }_{t}^{(5)}\right\rangle\)-states for t = 1, … , 9 are plotted as functions of the number of T-gates t in the doped random Clifford circuits,(see Supplementary Tables III and IV in Supplementary Note 4 for the data). See Fig. 3 for the preparation of such states. Both figures contain in the upper left corner the purity (orange dots) of the output state prepared on the quantum processor and its average value (dashed line). Here the number of resources NTOT ≡ NU × NM depends on the number of T-gates t thrown in the circuits as \({N}_{{\rm{TOT}}}={2}^{{A}_{5}+{B}_{5}(9-t)}\), where A5 = 13.7 ± 0.1, B5 = 0.041 ± 0.02 , see Supplementary Table I in Supplementary Note and Fig. 7 in “Methods.” Note that the experimentally observed magic can be—and typically is—higher than the theoretically predicted magic. See the caption of Fig. 4 for an explanation.

Guided by this result, we model the noise in two factors (i) noise in state preparation due to decoherence and (ii) imperfection in the realization of the ci gates in the randomized measurement. This latter error is unitary. We then tune the factors quantifying the noise in our model to match the difference between the experimentally measured and the theoretically predicted amounts of magic.

The ansatz for the (non-unitary) quantum channel \({{{{\mathcal{N}}}}}_{p}\) affecting the state preparation is

$${\psi }_{p}\equiv {{{{\mathcal{N}}}}}_{p}(\left\vert \psi \right\rangle \left\langle \psi \right\vert ):= p\left\vert \psi \right\rangle \left\langle \psi \right\vert +\frac{(1-p)}{n}\mathop{\sum }\limits_{i=1}^{n}{Z}_{i}\left\vert \psi \right\rangle \left\langle \psi \right\vert {Z}_{i}$$
(3)

where Zi is a phase flip error on the i-th qubit happening with probability (1 − p)/n. This channel is not the simple phase-flip channel as the probability p in principle depends on the target state \(\left\vert \psi \right\rangle\). The imperfection in the gates ci is modeled by the unitary phase displacement \({c}_{i}\to {c}_{i}^{\varepsilon }\equiv {c}_{i}{P}_{\varepsilon }{c}_{i}^{{\prime} }\), where use the Pε-gate described above. The measured stabilizer purity will be denoted by \({W}_{{\rm{exp}}}(\left\vert \psi \right\rangle )\).

Our ansatz on how the noise affects the measurement results is then \({W}_{{\rm{exp}}}(\left\vert \psi \right\rangle )\mathop{=}\limits^{!}{{{\rm{tr}}}}({\psi }_{p}^{\otimes 4}{Q}_{2}^{\varepsilon \otimes n})\) where \({Q}_{2}^{\varepsilon }\) represents the correction to the projector onto the single-qubit stabilizer code due to the gate imperfection error ε. The two free parameters p and ε can be determined experimentally, see Supplementary Note 2.

Several points are in order here. First, notice that the purity \({{{\rm{tr}}}}{\psi }_{p}^{2}\) is protected against gate imperfection errors, so it can be measured independently. Second, one can measure the ε error directly by measuring the purity of the initial state \({\left\vert 0\right\rangle }^{\otimes n}\), thus avoiding the decoherence effect altogether. The values of the stabilizer 2-Rényi entropy given by the noise model are represented by the Gray squares in Figs. 4, 5, and 6 which show that they provide a better approximation to the experimental data, an approximation which in fact improves as the number of T gates in the circuit increases. By measuring the stabilizer 2-Rényi entropy, thus, we provide a characterization of the noise model and an estimate of its parameters p, ε.

Discussion

Magic is a quantity of central importance for quantum computation: no quantum advantage can be obtained without it. This paper showed how to measure the amount of magic produced by a quantum circuit in terms of stabilizer Rényi entropy, and evaluated experimentally how that amount of magic scales as a function of the number of T-gates in the circuit. A central result of our experimental demonstration is that it is not enough just to create magic: the circuit must create an “accurate amount” of magic. Imperfectly implemented Clifford gates inject or subtract uncontrolled/unwanted magic into the circuit: just as excess entanglement can hinder the ability of a quantum circuit to perform some desired task34, uncontrolled excess magic can result in the degradation of the performance of a quantum computation. Generating the correct amount of stabilizer Rényi entropy is thus an important component of the certification process for quantum hardware. More generally, in a quantum device, e.g. a circuit based on superconducting qubits, there can be errors beyond decoherence, like gate implementation errors, or other unitary errors. Typically, these errors are investigated through gate fidelity while the loss of purity is a good figure of merit to quantify decoherence. However, not always gate fidelity is available as a tool. As we can see in Figs. 4 and 6, an inaccurate level of magic compared to the theoretical one signals the presence of unitary errors: a measurement of magic can then be used as a further tool to evaluate the accuracy of an experimental set-up.

Methods

Theoretical framework

In ref. 2, we proved that a global randomized measurements protocol can be employed to measure the stabilizer 2-Rényi entropy for multiqubit states.

Here, we make a decisive improvement by establishing a protocol that only requires local measurements. Local measurements are usually the best measurements in terms of quantum control an experimenter has access to. Let us recall the definition of stabilizer 2-Rényi entropy: for ψ a n-qubit quantum state, the stabilizer 2-Rényi entropy of ψ is defined as \({M}_{2}(\psi ):= -{\log }_{2}W(\psi )-{S}_{2}(\psi )-\log d\), where \(W(\psi ):= {{{\rm{tr}}}}(Q{\psi }^{\otimes 4})\), \({S}_{2}(\psi )={{{\rm{tr}}}}(T{\psi }^{\otimes 2})\) and the operator T is the swap operator while Qd−2PP4. The local randomized measurements protocol we introduce here aims at measuring W(ψ) and P(ψ) by only using single qubit gates and then measuring the qubits in the computational basis. In this way, we reduce access to multi-qubit gates that are typically noisier and whose control is poorer. The logic behind any randomized measurement protocol is to reconstruct operators (e.g., the swap operator for the purity or higher order permutations for higher order purities, see refs. 36,38,39,40,43,54,55) from correlations between randomized measurements. The measurement is randomized by means of Clifford single qubit gates. It is fundamental to use Clifford gates as magic is invariant under these unitary operations. The ideal experimental protocol for measuring simultaneously W(ψ) and P(ψ) is (see Fig. 1 for a pictorial schematization):

  1. (I)

    pick NU random local Clifford operators \(C{ = \bigotimes }_{i = 1}^{n}{c}_{i}\) where \({c}_{i}\in {{{{\mathcal{C}}}}}_{1}\) are single qubit Clifford gates. For each C do:

    1. (i)

      obtain the desired state ψ from the quantum circuit U,

    2. (ii)

      apply C on the state ψC ≡ CψC,

    3. (iii)

      measure in the computational basis,

    4. (iv)

      redo steps (i), (ii) and (iii) NM times to estimate the occupation probabilities \({\rm{Pr}}({\psi }_{C}| s)\equiv {{{\rm{tr}}}}(\left\vert s\right\rangle \left\langle s\right\vert {\psi }_{C})\) for s = 1, … , 2n,

  2. (II)

    Estimate the probabilities \(\tilde{{\rm{Pr}}}({\psi }_{C}| s)\) by measuring the frequencies of obtaining the bit-string s2 in the state ψC. The estimator \(\tilde{{\rm{Pr}}}({\psi }_{C}| s)\) for such probability converges to the true probability Pr(ψCs) in the limit NM → .

  3. (III)

    Obtain P(ψ) and W(ψ) can be computed from the ideal probabilities Pr(ψCs) by:

    $$P(\psi )=\frac{1}{2{4}^{n}}\mathop{\sum}\limits_{C\in {{{{\mathcal{C}}}}}_{1}^{\otimes n}}\mathop{\sum }\limits_{{s}_{1},{s}_{2}=1}^{{2}^{n}}{O}_{2}({s}_{1},{s}_{2}){\rm{Pr}}({\psi }_{C}| {s}_{1}){\rm{Pr}}({\psi }_{C}| {s}_{2})$$
    (4)
    $$\begin{array}{ll}W(\psi )=\frac{1}{2{4}^{n}}\mathop{\sum}\limits_{C\in {{{{\mathcal{C}}}}}_{1}^{\otimes n}}\mathop{\sum }\limits_{{s}_{1},\ldots ,{s}_{4}=1}^{{2}^{n}}{O}_{4}({s}_{1},{s}_{2},{s}_{3},{s}_{4}){\rm{Pr}}({\psi }_{C}| {s}_{1})\\\qquad\quad{\rm{Pr}}({\psi }_{C}| {s}_{2}){\rm{Pr}}({\psi }_{C}| {s}_{3}){\rm{Pr}}({\psi }_{C}| {s}_{4})\end{array}$$
    (5)

the weighting coefficients O2(s1, s2) and O4(s1, s2, s3, s4) are obtained in the following way. First, define two diagonal operators defined in \({{{{\mathcal{H}}}}}^{\otimes 2}\) and \({{{{\mathcal{H}}}}}^{\otimes 4}\) respectively:

$${\hat{O}}_{2}:= \mathop{\sum}\limits_{{s}_{1},{s}_{2}}{O}_{2}({s}_{1},{s}_{2})\left\vert {s}_{1}{s}_{2}\right\rangle \left\langle {s}_{1}{s}_{2}\right\vert$$
(6)
$${\hat{O}}_{4}:= \mathop{\sum}\limits_{{s}_{1},{s}_{2},{s}_{3},{s}_{4}}{O}_{4}({s}_{1},{s}_{2},{s}_{3},{s}_{4})\left\vert {s}_{1}{s}_{2}{s}_{3}{s}_{4}\right\rangle \left\langle {s}_{1}{s}_{2}{s}_{3}{s}_{4}\right\vert$$
(7)

Let us now prove Eqs. (4) and (5) and show the exact form of O2 and O4. Let us rewrite Eqs. (4) and (5) writing the purity as \(P(\psi )={{{\rm{tr}}}}(T{\psi }^{\otimes 2})\) and \(W(\psi )={{{\rm{tr}}}}(Q{\psi }^{\otimes 4})\)

$$\begin{array}{l}{{{\rm{tr}}}}(T{\psi }^{\otimes 2})=\frac{1}{2{4}^{n}}\mathop{\sum}\limits_{C}{{{\rm{tr}}}}({C}^{{\dagger} \otimes 2}{\hat{O}}_{2}{C}^{\otimes 2}{\psi }^{\otimes 2})\\ {{{\rm{tr}}}}(Q{\psi }^{\otimes 4})=\frac{1}{2{4}^{n}}\mathop{\sum}\limits_{C}{{{\rm{tr}}}}({C}^{{\dagger} \otimes 4}{\hat{O}}_{4}{C}^{\otimes 4}{\psi }^{\otimes 4})\end{array}$$
(8)

from the above equation is clear that the task is to find two diagonal operators \({\hat{O}}_{2}\) and \({\hat{O}}_{4}\) whose local Clifford average gives T and Q respectively. Recalling that \(T=\frac{1}{{2}^{n}}{({{\mathbb{1}}}^{\otimes 2}+{X}^{\otimes 2}+{Y}^{\otimes 2}+{Z}^{\otimes 2})}^{\otimes n}\), and \(Q=\frac{1}{{4}^{n}}{({{\mathbb{1}}}^{\otimes 4}+{X}^{\otimes 4}+{Y}^{\otimes 4}+{Z}^{\otimes 4})}^{\otimes n}\), it is sufficient to find two single qubit diagonal operators \({\hat{o}}_{2}\) and \({\hat{o}}_{4}\) living in \({{\mathbb{C}}}^{2\otimes 2}\) and \({{\mathbb{C}}}^{2\otimes 4}\) respectively, such that their Clifford average gives \({T}_{1}\equiv \frac{1}{2}({{\mathbb{1}}}^{\otimes 2}+{X}^{\otimes 2}+{Y}^{\otimes 2}+{Z}^{\otimes 2})\) and \({Q}_{1}\equiv \frac{1}{4}({{\mathbb{1}}}^{\otimes 4}+{X}^{\otimes 4}+{Y}^{\otimes 4}+{Z}^{\otimes 4})\) respectively. At this point, it is straightforward to verify that one should choose \({\hat{o}}_{2},{\hat{o}}_{4}\) to be

$${\hat{o}}_{2}\equiv \frac{{{\mathbb{1}}}^{\otimes 2}}{2}+\frac{3}{2}{Z}^{\otimes 2}$$
(9)
$${\hat{o}}_{4}\equiv \frac{{{\mathbb{1}}}^{\otimes 4}}{4}+\frac{3}{4}{Z}^{\otimes 4}$$
(10)

To conclude the proof is sufficient to write \({\hat{o}}_{2}\) and \({\hat{o}}_{4}\) in the computational basis to restore the forms of Eqs. (4) and (5). It’s easy to verify:

$$\begin{array}{rcl}{O}_{2}({s}_{1},{s}_{2})&=&{(-2)}^{-\mathop{\sum }\nolimits_{i = 1}^{n}{s}_{1}^{i}\oplus {s}_{2}^{i}}\\ {O}_{4}({s}_{1},{s}_{2},{s}_{3},{s}_{4})&=&{(-2)}^{-\mathop{\sum }\nolimits_{i = 1}^{n}{s}_{1}^{i}\oplus {s}_{2}^{i}\oplus {s}_{3}^{i}\oplus {s}_{4}^{i}}\end{array}$$
(11)

where \({s}_{k}={s}_{k}^{1}{s}_{k}^{2}\ldots {s}_{k}^{n}\) a n-length bit string for k = 1, 2, 3, 4 and  is the logic sum between bits.

Statistical analysis

In this section, we discuss the effect of a finite number of realizations of the experiment. In our scheme, statistical errors arise from two factors: (i) a partial sampling of the local (single qubit) Clifford group, that is, NU < 24n, and (ii) the finite number of measurement shots NM per unitary selected to estimate the occupation probability \(\tilde{Pr}({\psi }_{C}| s)\), introduced in the previous section, that converge to the true probability only in the limit NM → . The total number of resources is thus NU × NM. We assume that different rounds of random local unitary and different shots for a given unitary are generated independently and identically distributed. One describes the i-th shot for a given sampled unitary C as \({\tilde{s}}_{C}(i)\) which takes value \(\left\vert s\right\rangle \left\langle s\right\vert\) with probability \({\rm{Pr}}({\psi }_{C}| s)\equiv {{{\rm{tr}}}}(\left\vert s\right\rangle \left\langle s\right\vert C\psi {C}^{{\dagger} })\). An unbiased estimator for the stabilizer purity is given by:

$$\tilde{W}(\psi )=\frac{1}{{N}_{U}}\mathop{\sum}\limits_{C}{\tilde{W}}_{C}(\psi )$$
(12)

where \({\tilde{W}}_{C}(\psi ):= {\left(\begin{array}{c}{N}_{M}\\ 4\end{array}\right)}^{-1}{\sum }_{i < j < k < l}{{{\rm{tr}}}}({\tilde{s}}_{C}(i)\otimes {\tilde{s}}_{C}(j)\otimes {\tilde{s}}_{C}(k)\otimes {\tilde{s}}_{C}(l){\hat{O}}_{4})\). Let us prove that it is an unbiased estimator:

$$\begin{array}{ll}{{\mathbb{E}}}_{C}{{\mathbb{E}}}_{s}\tilde{W}(\psi )\,=\,{{\mathbb{E}}}_{C}{\left(\begin{array}{c}{N}_{M}\\ 4\end{array}\right)}^{-1}\mathop{\sum}\limits_{i < j < k < l}{{{\rm{tr}}}}({{\mathbb{E}}}_{s}{\tilde{s}}_{C}(i)\otimes {{\mathbb{E}}}_{s}{\tilde{s}}_{C}(j)\otimes {{\mathbb{E}}}_{s}{\tilde{s}}_{C}(k)\otimes {{\mathbb{E}}}_{s}{\tilde{s}}_{C}(l){\hat{O}}_{4})\\ \qquad\quad\qquad\,\,\,=\,{{\mathbb{E}}}_{C}{{{\rm{tr}}}}({C}^{\otimes 4}{\psi}^{\otimes 4}{C}^{{\dagger} \otimes 4}{\hat{O}}_{4})=W(\psi )\end{array}$$
(13)

where we used the fact that \({{\mathbb{E}}}_{s}{\tilde{s}}_{U}(i)\equiv {\sum }_{s}\left\vert s\right\rangle \left\langle s\right\vert Pr({\psi }_{C}| s)={\psi }_{C}\). Our task now is to bound the number of resources needed to estimate W within an error ϵ. We compute the variance given a finite number of shot measurements NM and a finite sample NU of the local Clifford group. The variance of the estimator \(\tilde{W}(\psi )\) can be written as:

$$\begin{array}{ll}{{{\rm{Var}}}}[\tilde{W}(\psi )]\,=\,\frac{1}{{N}_{U}}{{{\rm{Var}}}}[{\tilde{W}}_{C}(\psi )]\\ \qquad\qquad\quad\,=\,\frac{1}{{N}_{U}}{{\mathbb{E}}}_{C}\left[{{\mathbb{E}}}_{{{{\bf{s}}}}}({\tilde{W}}_{C}^{2}(\psi )| C)\right]-\frac{1}{{N}_{U}}{\left[{{\mathbb{E}}}_{C}{{\mathbb{E}}}_{{{{\bf{s}}}}}({\tilde{W}}_{C}(\psi )| C)\right]}^{2}\end{array}$$
(14)

After some lengthy algebra (see Supplementary Note 3), it is possible to bound the above variance as:

$$\begin{array}{rcl}{{{\rm{Var}}}}[\tilde{W}(\psi )]&\le &\frac{1}{{N}_{U}}\left[\frac{8}{\sqrt{d}}+\frac{192}{{d}^{1/3}{N}_{M}^{4}}+\frac{6792}{{d}^{1/2}{N}_{M}^{4}}\right.\\ &+&\left.\frac{5056}{{N}_{M}^{3}}+\frac{8179}{{d}^{1/2}{N}_{M}^{2}}+\frac{128}{{N}_{M}}-{{{\rm{tr}}}}{[Q{\psi }^{\otimes 4}]}^{2}\right]\end{array}$$
(15)

Finally, we make use of Bernstein’s inequality to bound the probability of an estimation within an error ϵ:

$$\Pr \left(| \tilde{W}(\psi )-W(\psi )| \ge \epsilon \right)\le {2}^{-\frac{{N}_{U}{\epsilon }^{2}}{{{{\bf{Var}}}}({\tilde{W}}_{U}(\psi ))+\frac{2\epsilon }{3}}}$$
(16)

In the regime of interest, i.e., d 1 and NM 1 the variance scales like \({{{\rm{Var}}}}[\tilde{W}(\psi )]\lesssim \frac{1}{{N}_{U}}\left(\frac{{c}_{1}}{\sqrt{d}}+\frac{{c}_{2}}{{N}_{M}}\right)\), where c1, c2 are two constants. Setting \({N}_{M}={{{\mathcal{O}}}}(\sqrt{d})\), in order to have an error ϵ, and an exponentially small probability to fail, the total number of resources NU × NM for the stabilizer purity scales as \({{{\mathcal{O}}}}({\epsilon }^{-2})\).

At this point, a comment is necessary. The stabilizer purity is bounded between \({d}^{-2}\le \tilde{W}(\psi )\le {d}^{-1}\), which means that, to have a faithful measurement of W(ψ), the error ϵ must be (at least) exponentially small in the number of qubits, ϵ ~ d−1. This makes the number of resources exponentially large in n. Similarly, for the purity P(ψ) (see Supplementary Note 3), setting \({N}_{M}={{{\mathcal{O}}}}(\sqrt{d})\), the variance is \({{{\rm{Var}}}}[\tilde{{{\mathrm{Pur}}}\,}(\psi )]={{{\mathcal{O}}}}(d{N}_{U}^{-1})\). Thus the number of resources to estimate the purity up to an error ϵ scales as \({{{\mathcal{O}}}}({\epsilon }^{-2}\sqrt{{d}^{3}})\).

Therefore, employing a total number of resources

$${N}_{U}\times {N}_{M}={{{\mathcal{O}}}}({\epsilon }^{-2}{d}^{2})$$
(17)

the randomized measurement protocol is able to estimate the purity within an error ≤ϵ and the stabilizer purity within an error ϵd−1. In the next section, we describe the experimental protocol used to perform the experiments on IBM quantum processors.

Experimental protocol

To measure the magic of multiqubit states on a quantum processor via statistical correlations between randomized measurements we need three steps: (i) state preparation, (ii) the application of NU random local Clifford unitaries to sample the local n-qubit Clifford group, whose dimension is Cloc(2n) = 24n, and (iii) NM projective measurements to estimate the probabilities \({\tilde{P}}_{M}({\psi }_{C}| s)\). Then, the experimental purity and stabilizer purity are measured as:

$$\begin{array}{ll}P(\psi )\,=\,\frac{1}{{N}_{U}}\mathop{\sum}\limits_{C}\mathop{\sum }\limits_{{s}_{1},{s}_{2}=1}^{{2}^{n}}{O}_{2}({s}_{1},{s}_{2}){\tilde{P}}_{M}({\psi }_{C}| {s}_{1}){\tilde{P}}_{M}({\psi }_{C}| {s}_{2})\\ W(\psi )\,=\,\frac{1}{{N}_{U}}\mathop{\sum}\limits_{C}\mathop{\sum }\limits_{{s}_{1},\ldots ,{s}_{4}}^{{2}^{n}}{O}_{4}({s}_{1},{s}_{2},{s}_{3},{s}_{4}){\tilde{P}}_{M}({\psi }_{C}| {s}_{1}){\tilde{P}}_{M}({\psi }_{C}| {s}_{2}){\tilde{P}}_{M}({\psi }_{C}| {s}_{3}){\tilde{P}}_{M}({\psi }_{C}| {s}_{4})\end{array}$$
(18)

We proved that one needs \({N}_{U}\times {N}_{M}={{{\mathcal{O}}}}({\epsilon }^{-2}{d}^{2})\) total measurements to estimate the stabilizer purity within a error ϵ−1d−1. Since we obtained such an accuracy guarantee through crude bounds, we expect fewer resources to be spent. We thus follow the protocol employed in41 to find the optimal number of unitaries NU and measurements NM. We first build a preliminary 10 × 10 grid and make 100 numerical simulation for 10 different values of NU = 8, … , 1024 and 10 different values NM = 32, … , 1024 (the latter taken with logarithmic spacing) for 2 extreme states, namely the input state \({\left\vert 0\right\rangle }^{\otimes n}\) and the final doped Clifford state \(\left\vert {\Gamma }_{2n-1}^{(n)}\right\rangle\), see Fig. 3. Then, for each value of NU and NM we compute the average \(\overline{{W}_{{N}_{U},{N}_{M}}(\left\vert \psi \right\rangle )}\) over 100 different realizations, the average purity \(\overline{{P}_{{N}_{U},{N}_{M}}(\left\vert \psi \right\rangle )}\) and the average percent distance \({\delta }_{{N}_{U},{N}_{M}}\) from the average

$${\delta }_{{N}_{U},{N}_{M}}(\left\vert \psi \right\rangle ):= \frac{\overline{| {W}_{{N}_{U},{N}_{M}}(\left\vert \psi \right\rangle )-\overline{{W}_{{N}_{U},{N}_{M}}(\left\vert \psi \right\rangle )}| }}{\overline{{W}_{{N}_{U},{N}_{M}}(\left\vert \psi \right\rangle )}}$$
(19)

To obtain the optimal number of NU and NM for the given states \({\left\vert 0\right\rangle }^{\otimes n}\) and \(\vert {\Gamma }_{2n-1}^{(n)}\rangle\), we set a threshold on the average distance \({\delta }_{{N}_{U},{N}_{M}}\) and on the average purity \(\overline{{P}_{{N}_{U},{N}_{M}}(\left\vert \psi \right\rangle )}\):

  1. (i)

    \({\delta }_{{N}_{U},{N}_{M}}(\left\vert \psi \right\rangle ) < 12 \%\)

  2. (ii)

    \(| \overline{{P}_{{N}_{U},{N}_{M}}(\left\vert \psi \right\rangle )}-1| < 12 \%\)

and pick the pair of NU, NM satisfying conditions (i) and (ii) minimizing their product NUNM, i.e. the optimal number of resources. Indeed the product of NUNM is the number of physical times that one redoes the actual experiment and thus the number of necessary resources to perform an experiment. Remarkably, the number of unitaries NU and the number of measurements NM do depend on the state of interest \(\left\vert \psi \right\rangle\). In particular, denoting \({N}_{U}^{t = 2n-1},{N}_{M}^{t = 2n-1}\) and \({N}_{U}^{t = 0},{N}_{M}^{t = 0}\) the number of resources for \(\left\vert {\Gamma }_{2n-1}^{(n)}\right\rangle\) and \({\left\vert 0\right\rangle }^{\otimes n}\) respectively, we find \({N}_{U}^{t = 2n-1} \,<\, {N}_{U}^{t = 0}\) and \({N}_{M}^{t = 2n-1} \,<\, {N}_{M}^{t = 0}\). These findings suggest that the optimal number of resources NU × NM do depend on the number t of magic seeds, i.e. T-gates, thrown in the circuit. Thus, in order to find optimal values for NU and NM for all the states of interest \(\left\vert {\Gamma }_{t}^{(n)}\right\rangle t=1,\ldots ,2n-1\), we build a linear spaced 10 × 10 grid for 10 different value of NU ranging in \([{N}_{U}^{t = 2n-1},\ldots ,{N}_{U}^{t = 0}]\) and 10 different values of NM ranging in \([{N}_{M}^{t = 2n-1},\ldots ,{N}_{M}^{t = 0}]\) for fixed n; then we make 100 numerical simulations and pick the optimal number of resources satisfying conditions (i) and (ii). In this way, we are able to determine the optimal number of resources state by state, see Supplementary Table I in Supplementary Note 4 for the results. The data are fitted to depend exponentially upon the number t of magic-seeds, as NTOT = 2a+b[(2n−1)−t], see Fig. 7. The experimental errors on the estimated P(ψ) and W(ψ) are chosen to be the standard error of the average over NU, i.e. over the local Clifford operators used to estimate these two quantities from randomized measurements (see Supplementary Note 4).

Fig. 7: Number of optimal resources NU and NM.
figure 7

The figure shows the log-plot of the optimal number of resources NTOT = NU × NM for n = 3 (blue dots), n = 4 (orange dots) and n = 5 (gray dots) as a function of the number of T-gates t injected in the circuit. The dashed lines represent the fit NTOT = 2a+b[(2n−1)−t]; the values for a and b with the respective errors are reported in the bottom-left corner. The fitted curves are in perfect agreement with the experimental data, whose error bars are due to the finite resolution of the grid: the R-squared parameters are \({R}_{(3)}^{2}=0.985\) for n = 3, \({R}_{(4)}^{2}=0.985\) for n = 4, and \({R}_{(5)}^{2}=0.995\) for n = 5.