diff --git a/docs/source/examples/rshadows_tutorial.md b/docs/source/examples/rshadows_tutorial.md index 4e284855b..246fd5174 100644 --- a/docs/source/examples/rshadows_tutorial.md +++ b/docs/source/examples/rshadows_tutorial.md @@ -35,7 +35,10 @@ from mitiq.interface.mitiq_cirq.cirq_utils import ( np.random.seed(666) ``` -Whether to run the quantum measurement or directly use the results from the previous run. If **True**, the measurement will be run again. If **False**, the results from the previous run will be used. +There are two options: Whether to run the quantum measurement or directly use the results from the previous run. + +- If **True**, the measurement will be run again. +- If **False**, the results from the previous run will be used. ```{code-cell} ipython3 @@ -55,7 +58,7 @@ if not run_quantum_processing: saved_data = pickle.load(zf.open(f"{saved_data_name}.pkl")) ``` -The *robust shadow estimation*{cite}`chen2021robust` approach put forth in *Predicting Many Properties of a Quantum System from Very Few Measurements* {cite}`huang2020predicting` exhibits noise resilience. The inherent randomization in the protocol simplifies the noise, transforming it into a Pauli noise channel that can be characterized relatively straightforwardly. Once the noisy channel $\widehat{\mathcal{M}}$ is characterized $\widehat{\mathcal{M}}$, it is incorporated into the channel inversion $\widehat{\mathcal{M}}^{-1}$, resulting in an unbiased state estimator. The sampling error in the determination of the Pauli channel contributes to the variance of this estimator. +The *robust shadow estimation*{cite}`chen2021robust` approach based on {cite}`huang2020predicting` exhibits noise resilience. The inherent randomization in the protocol simplifies the noise, transforming it into a Pauli noise channel that can be characterized relatively straightforwardly. Once the noisy channel $\widehat{\mathcal{M}}$ is characterized, it is incorporated into the channel inversion $\widehat{\mathcal{M}}^{-1}$, resulting in an unbiased state estimator. The sampling error in the determination of the Pauli channel contributes to the variance of this estimator. ## 1. Define Quantum Circuit and Executor In this notebook, we'll use the ground state of the Ising model with periodic boundary conditions as an example to study energy and two-point correlation function estimations. We'll compare the performance of robust shadow estimation with the standard shadow protocol, taking into account the bit-flip or depolarization noise on the quantum channels. @@ -96,7 +99,7 @@ else: circuit = circuit.transform_qubits(qubit_map=qubit_map) ``` -similar with the classical shadow protocal, we define the executor to perform the computational measurement for the circuit. Here we use the simulator that adds single-qubit depolarizing noise after rotation gates and before the $Z$-basis measurement, as the noise is assumped to be gate independent, time invariant and Markovian, the noisy gate satisfies $U_{\Lambda_U}(M_z)_{\Lambda_{\mathcal{M}_Z}}\equiv U\Lambda\mathcal{M}_Z$: +Similar with the classical shadow protocol, we define the executor to perform the computational measurement for the circuit. Here, we use add single-qubit depolarizing noise after rotation gates but before the $Z$-basis measurement. As the noise is assumed to be gate independent, time invariant and Markovian, the noisy gate satisfies $U_{\Lambda_U}(M_z)_{\Lambda_{\mathcal{M}_Z}}\equiv U\Lambda\mathcal{M}_Z$: ```{code-cell} ipython3 @@ -213,7 +216,7 @@ counts = {bitstring: bitstring.count("1") for bitstring in bitstrings} order = np.argsort(list(counts.values())) reordered_bitstrings = bitstrings[order] -# solve for theoretical Pauli fidelitys +# solve for theoretical Pauli fidelities f_theoretical = {} bitstrings = list(f_est_results.keys()) for bitstring in bitstrings: @@ -228,14 +231,14 @@ import matplotlib.pyplot as plt import seaborn as sns sns.set_style("whitegrid") -# plot estimated vs theoretical Pauli fidelitys when no errors in quantum circuit +# plot estimated vs theoretical Pauli fidelities when no errors in quantum circuit plt.plot( [np.abs(f_est_results[b]) for b in reordered_bitstrings], "-*", label="Noisy Channel", ) plt.plot( - [f_theoretical[b] for b in reordered_bitstrings], label="Noisless Channel" + [f_theoretical[b] for b in reordered_bitstrings], label="Noiseless Channel" ) plt.xlabel(r"measurement basis states $b$") plt.xticks( @@ -256,7 +259,7 @@ The expectation value for a series of operators, denoted as $\{O_\iota\}_{\iota\ \hat{o}_\iota &= \langle\!\langle O_\iota|{\hat{\rho}}\rangle\!\rangle \simeq \langle\!\langle O_\iota|\widehat{\mathcal{M}}^{-1}\widetilde{\mathcal{M}}|\rho\rangle\!\rangle=\sum_{b^{(1)}\in\{0,1\}^{n}}f_{b^{(1)}}^{-1}\left(\bigotimes_{i=1}^n \langle\!\langle P_i|\Pi_{b_i^{(1)}}\widehat{\mathcal{M}}_{P_i}\right)|\rho\rangle\!\rangle\nonumber\\ &=\sum_{b^{(1)}\in\{0,1\}^{n}}f_{b^{(1)}}^{-1}\prod_{i=1}^n \langle\!\langle P_i|\Pi_{b^{(1)}_i}\bigg|U_i^{(2)\dagger}|b_i^{(2)}\rangle\langle b_i^{(2)}|U_i^{(2)}\bigg\rangle\!\bigg\rangle \end{align} -where in the last equality, $\{P_i\}_{i\in n}$ represents Pauli operators, with $P=\{I,X,Y,Z\}$. And as we did previously, we use the lable $(1)$ as the subscript to distinguish the parameters of the calibration process from the parameters of the shadow estimation process, which is labelled by $(2)$. It is assumed that $O_\iota$ are Pauli strings acting on $supp(O_\iota)$ ($|supp(O_\iota)|\leq n$) sites of the system. It can be verified that the cross product over qubit sites within the summation of the final expression in the above equation is zero, except when all sites in $supp(O_\iota)^c$ have $\Pi_0$ acting on. Similarly, it can be verified that the cross product over qubit sites within the summation of the final expression in the above equation is zero, except when all sites in $supp(O_\iota)$ have $\Pi_1$ acting on, i.e. +where in the last equality, $\{P_i\}_{i\in n}$ represents Pauli operators, with $P=\{I,X,Y,Z\}$. And as we did previously, we use the label $(1)$ as the subscript to distinguish the parameters of the calibration process from the parameters of the shadow estimation process, which is labelled by $(2)$. It is assumed that $O_\iota$ are Pauli strings acting on $supp(O_\iota)$ ($|supp(O_\iota)|\leq n$) sites of the system. It can be verified that the cross product over qubit sites within the summation of the final expression in the above equation is zero, except when all sites in $supp(O_\iota)^c$ have $\Pi_0$ acting on. Similarly, it can be verified that the cross product over qubit sites within the summation of the final expression in the above equation is zero, except when all sites in $supp(O_\iota)$ have $\Pi_1$ acting on, i.e. \begin{align} \Pi_i|I\rangle\!\rangle\equiv\delta_{i,0}|I\rangle\!\rangle,\qquad \Pi_{i}|P\rangle\!\rangle\equiv\delta_{i,1}|P\rangle\!\rangle,\qquad ~~~ \mathrm{for}~i\in\{0,1\};~P\;=\{X,\;Y,\;Z\}. \end{align} @@ -265,13 +268,13 @@ Therefore, the final expression of the expectation value estimation can be simpl \hat{o}_\iota = \left(\sum_{b^{(1)}\in\{0,1\}^{n}}f_{b^{(1)}}^{-1}\prod_{j\in supp(O_\iota)} \delta_{b_j^{(1)},1}\prod_{k\in supp(O_\iota)^c}\delta_{b_k^{(1)},0}\right)\prod_{i=1}^n \langle b_i^{(2)}|U_i^{(2)}P_i U_i^{(2)\dagger}|b_i^{(2)}\rangle \end{align} - Additionally, when $P_i =X_i$ (r.e.s.p. $Y_i,\;Z_i$), $U_i^{(2)}$ must correspond to $X$ (r.e.s.p. $Y,\;Z$)-basis measurement to yield a non-zero value, which is easy to check considered that the $P$-basis measurement channel has a PTM rep: $\widetilde{\mathcal{M}}_{P}=\frac{1}{2}(|I\rangle\!\rangle\langle\!\langle I|+|P\rangle\!\rangle\langle\!\langle P|)$, observiously the only measurement that didn't vanished by the operator's $i$-th qubit component in PTM rep: $P\rightarrow \langle\!\langle P|$, is the local $P$-basis measurement. + Additionally, when $P_i =X_i$ (r.e.s.p. $Y_i,\;Z_i$), $U_i^{(2)}$ must correspond to $X$ (r.e.s.p. $Y,\;Z$)-basis measurement to yield a non-zero value, which is easy to check considered that the $P$-basis measurement channel has a PTM rep: $\widetilde{\mathcal{M}}_{P}=\frac{1}{2}(|I\rangle\!\rangle\langle\!\langle I|+|P\rangle\!\rangle\langle\!\langle P|)$. Obviously, the only measurement that didn't vanish by the operator's $i$-th qubit component in PTM rep: $P\rightarrow \langle\!\langle P|$, is the local $P$-basis measurement. -Next steps are same with classical protocol,with the statistical method of taking an average called "median of means" to achieve an acceptable failure probability of estimation, which need $R_2=N_2K_2$ snapshots, where we use the subscript "2" to denote the index of classical shadow protocol. Acctually, +Next steps are identical to the classical protocol, where the statistical method of taking an average called "median of means" is used to achieve an acceptable failure probability of estimation. This needs $R_2=N_2K_2$ snapshots, where we use the subscript "2" to denote the index of classical shadow protocol. Actually, \begin{align} \hat{o}_\iota(N_2,K_2):=\mathrm{median}\{\hat{o}_\iota^{(1)},\cdots,\hat{o}_\iota^{(K_2)}\}~~\mathrm{where}~~\hat{o}_\iota^{(j)}=N_2^{-1}\sum_{k=N_2(j-1)+1}^{N_2j}\mathrm{Tr}(O_\iota\hat{\rho}_k),\qquad \forall~1\leq j\leq K_2, \end{align} -where we have $K_2$ estimators each of which is the average of $N_2$ single-round estimators $\hat{o}_i^{(j)}$, and take the median of these $K_2$ estimators as our final estimator $\hat{o}_\iota(N_2,K_2)$. We can calculate the median of means of each irreps with projection $\Pi_b=\bigotimes_{i=1}^n\Pi_{b_i}$, +where we have $K_2$ estimators each of which is the average of $N_2$ single-round estimators $\hat{o}_i^{(j)}$, and take the median of these $K_2$ estimators as our final estimator $\hat{o}_\iota(N_2,K_2)$. We can calculate the median of means of each irreducible representations with projection $\Pi_b=\bigotimes_{i=1}^n\Pi_{b_i}$, ### 3.1 Ground State Energy Estimation of Ising model with the RSHADOWS algorithm diff --git a/docs/source/examples/shadows_tutorial.md b/docs/source/examples/shadows_tutorial.md index 171975920..075661e12 100644 --- a/docs/source/examples/shadows_tutorial.md +++ b/docs/source/examples/shadows_tutorial.md @@ -15,7 +15,8 @@ kernelspec: **Corresponding to:** Min Li (minl2@illinois.edu) -This notebook shows how to use classical shadows estimation with the Mitiq library, focused initially on local (Pauli) measurements. We show some common scenarios such as state tomography, and operator expectation value estimation. The method creates an approximate classical description of a quantum state with few measurements while effectively characterizing and mitigating noise in the following notebook `rshadows_tutorial`. +This notebook shows how to use classical shadows estimation with the Mitiq library, focused initially on local (Pauli) measurements. We show some common scenarios such as state tomography, and operator expectation value estimation. The method creates an approximate classical description of a quantum state with few measurements while effectively characterizing and mitigating noise in the [following notebook](https://mitiq.readthedocs.io/en/stable/examples/rshadows_tutorial.html). + ```{code-cell} ipython3 import cirq import numpy as np @@ -75,12 +76,12 @@ MathJax.Hub.Config({ ## 2. Sampling random Pauli measurements -This process involves applying a random unitary selected from a randomly fixed ensemble $\mathcal{U}\in U(2^n)$ to rotate the state $\rho\rightarrow U^\dagger \rho U$, performing a computational-basis($Z$-basis) measurement, and storing a classical description $U^\dagger |\hat{b}\rangle\langle\hat{b}| U$. After the measurement, the inverse of $U$ is applied to the resulting computational basis state, collapsing $\rho$ to +This process involves applying a random unitary selected from a randomly fixed ensemble $\mathcal{U}\in U(2^n)$ to rotate the state $\rho\rightarrow U^\dagger \rho U$, followed by a computational-basis($Z$-basis) measurement, and storing a classical description $U^\dagger |\hat{b}\rangle\langle\hat{b}| U$. After the measurement, the inverse of $U$ is applied to the resulting computational basis state, collapsing $\rho$ to \begin{equation} U^\dagger|\hat{b}\rangle\langle\hat{b}| U\qquad \mathrm{where} \qquad \mathrm{Pr}[\hat{b}=b]=\langle b|U\rho U^\dagger|b\rangle. \end{equation} -If the unitary group $\mathcal{U}$ is chosen to be the local Clifford group $\mathrm{CL}(2)^n$, this equavelent to performing a random Pauli measurement on each qubit. This means that for each qubit, we randomly decide to measure one of the Pauli operators. Define the `cirq_executor` take one shot of measurement and return the measurement result. +If the unitary group $\mathcal{U}$ is chosen to be the local Clifford group $\mathrm{CL}(2)^n$, this equivalent to performing a random Pauli measurement on each qubit. This means that for each qubit, we randomly decide to measure one of the Pauli operators. Below, we define the `cirq_executor` to take one shot of measurement and return the measurement result. ```{code-cell} ipython3 @@ -95,9 +96,9 @@ def cirq_executor( ) ``` -In terms of implementation, considering that the only possible measurement to be performed is the $Z$-basis measurement, the random Pauli measurement is equivalent to randomly sampling a unitary from the unitary ensemble: $\mathcal{G}=\{\mathrm{id},\mathrm{H},\mathrm{H}\cdot \mathrm{S}^\dagger\}$. Afterward, the $Z$-basis measurement is conducted. We then record a sequence of Pauli gates $u_i:= U_i^\dagger ZU_i$ that have been measured for each qubit in the circuit. This sequence becomes one of the output lists of the measurement function random_pauli_measurement. +In terms of implementation, considering that the only possible measurement to be performed is the $Z$-basis measurement, the random Pauli measurement is equivalent to randomly sampling a unitary from the unitary ensemble: $\mathcal{G}=\{\mathrm{id},\mathrm{H},\mathrm{H}\cdot \mathrm{S}^\dagger\}$. Afterward, the $Z$-basis measurement is conducted. We then record a sequence of Pauli gates $u_i:= U_i^\dagger ZU_i$ that have been measured for each qubit in the circuit. This sequence becomes one of the output lists of the measurement function `random_pauli_measurement`. -In the main function, the quantum measurement process is encapsulated within the shadow_quantum_processing function. This function takes the quantum circuit and the number of shots as input. It returns the measurement results as bit strings, for example, '01...0' is equivelent to the measurement basis eigenstate: $|0\rangle|1\rangle...|0\rangle$. Additionally, it provides the measured Pauli gates in string format. For instance, 'XY...Z' signifies a local X-basis measurement on the first qubit, a local Y-basis measurement on the second qubit, and a local Z-basis measurement on the last qubit in the circuit. +In the main function, the quantum measurement process is encapsulated within the `shadow_quantum_processing function`. This function takes the quantum circuit and the number of shots as input. It returns the measurement results as bit strings, for example, '01...0' is equivalent to the measurement basis eigenstate: $|0\rangle|1\rangle...|0\rangle$. Additionally, it provides the measured Pauli gates in string format. For instance, 'XY...Z' signifies a local X-basis measurement on the first qubit, a local Y-basis measurement on the second qubit, and a local Z-basis measurement on the last qubit in the circuit. ```{code-cell} ipython3 @@ -123,7 +124,7 @@ which has been named a single copy of **classical shadow**. Based on *Schur's Le \end{equation} which is indeed unitary, however, not CP, so it is not a physical map as expected. -In the case of random Pauli measurement, the unitary could be represented by the tensor product of all qubits, so it is with the state $|\hat{b}\rangle\in\{0,1\}^{\otimes n}$, i.e. $U^\dagger|\hat{b}\rangle=\bigotimes_{i\leq n}U_i|\hat{b}_i\rangle$. Therefore, based on Schur's Lemma, a snapshot would takes the form: +In the case of random Pauli measurement, the unitary could be represented by the tensor product of all qubits, so it is with the state $|\hat{b}\rangle\in\{0,1\}^{\otimes n}$, i.e. $U^\dagger|\hat{b}\rangle=\bigotimes_{i\leq n}U_i|\hat{b}_i\rangle$. Therefore, based on Schur's Lemma, a snapshot would take the form: \begin{equation} \hat{\rho}=\bigotimes_{i=1}^{n}\left(3U_i^\dagger|\hat{b}_i\rangle\langle\hat{b}_i|U_i-\mathbb{I}\right),\qquad|\hat{b}_i\rangle\in\{0,1\}. \end{equation} @@ -141,16 +142,16 @@ The classical shadows state reconstruction are then obtained by taking the avera this is realized in the function `state_reconstruction`. In the main function `classical_post_processing`, we take the output of `shadow_quantum_processing`, then apply the inverse channel to obtain the snapshots, and finally take the average of the snapshots to obtain the reconstructed state if *state_reconstruction =* **True**. **In the current notebook, we don't preform Pauli twirling calibration, and we set** *rshadow* = **False**. #### 4.1.1 Error Analysis -We can take a visualization of the elementwise difference between the reconstructed state and the original state. +We can take a visualization of the element wise difference between the reconstructed state and the original state. \begin{equation} \Delta\rho_{ij}=|\rho^{\mathrm{shadow}}_{ij}-\rho_{ij}| \end{equation} -The difference is very small, which means that the classical shadow is a good approximation of the original state even in the sence of state tomography. +The difference is very small, which means that the classical shadow is a good approximation of the original state even in the sense of state tomography. It is anticipated that the fidelity will not necessarily be lower than 1, as the state reconstructed through classical shadow estimation is not guaranteed to be a physical quantum state, given that $\mathcal{M}^{-1}$ is not a quantum channel. Fidelity is defined by $F(\rho,\sigma)=\mathrm{Tr}\sqrt{\rho^{1/2}\sigma\rho^{1/2}}$, when $\rho=|v\rangle\langle v|$ is a pure state $F(\rho,\sigma)=\langle v|\sigma|v\rangle$. -Based on the theorem, if the error rate of fedelity is $\epsilon$, i.e. +Based on the theorem, if the error rate of fidelity is $\epsilon$, i.e. \begin{equation} |F(\rho,\sigma)-1|\leq\epsilon, \end{equation} @@ -158,7 +159,7 @@ then the minimum number of measurements $N$ (number of snapshots) should be: \begin{equation} N = \frac{34}{\epsilon^2}\left\|\rho-\mathrm{Tr}(\rho)/{2^n}\mathbb{I}\right\|_{\mathrm{shadow}}^2 \end{equation} -with the shadow norm upper bound of the random Pauli measurement $\left\|\cdot\right\|_{\mathrm{shadow}}\leq 2^k\|\cdot\|_\infty$ when the operator acting on $k$ qubits, we have $N\leq 34\epsilon^{-2}2^{2n}+\mathcal{O}(e^{-n})$. Based on Fuchs–van de Graaf inequalities and properties of $L_p$ norm, $\|\rho-\sigma\|_2\leq \|\rho-\sigma\|_1 \leq (1-F(\rho,\sigma))^{1/2}$, the $L_2$ norm distance between the state reconstructed through classical shadow estimation and the state prepared by the circuit is upperbounded by the fidelity error rate $\epsilon$. The dependency of the bound number of measurements $N$ to achieve the error rate $\epsilon$ is depicked in function `n_measurements_tomography_bound`. +with the shadow norm upper bound of the random Pauli measurement $\left\|\cdot\right\|_{\mathrm{shadow}}\leq 2^k\|\cdot\|_\infty$ when the operator acting on $k$ qubits, we have $N\leq 34\epsilon^{-2}2^{2n}+\mathcal{O}(e^{-n})$. Based on Fuchs–van de Graaf inequalities and properties of $L_p$ norm, $\|\rho-\sigma\|_2\leq \|\rho-\sigma\|_1 \leq (1-F(\rho,\sigma))^{1/2}$, the $L_2$ norm distance between the state reconstructed through classical shadow estimation and the state prepared by the circuit is upperbound by the fidelity error rate $\epsilon$. The dependency of the bound number of measurements $N$ to achieve the error rate $\epsilon$ is depicted in function `n_measurements_tomography_bound`. ```{code-cell} ipython3 @@ -186,13 +187,13 @@ rho_shadow = output["reconstructed_state"] ```{code-cell} ipython3 -# Compute the ideal state vector discribed by the input circuit. +# Compute the ideal state vector described by the input circuit. state_vector = test_circuits.final_state_vector().reshape(-1, 1) # Compute the density matrix. rho_true = state_vector @ state_vector.conj().T ``` -We can plot the elementwise difference between the reconstructed state and the original state as a thermal diagram: +We can plot the element wise difference between the reconstructed state and the original state as a thermal diagram: ```{code-cell} ipython3 diff --git a/docs/source/guide/shadows-1-intro.md b/docs/source/guide/shadows-1-intro.md index d19c4d11e..8e1daf36a 100644 --- a/docs/source/guide/shadows-1-intro.md +++ b/docs/source/guide/shadows-1-intro.md @@ -10,6 +10,9 @@ kernelspec: language: python name: python3 --- +```{admonition} Note: +The documentation for Classical Shadows in Mitiq is still under construction. This users guide will change in the future. +``` # How Do I Use Classical Shadows Estimation? @@ -26,7 +29,7 @@ The procedure can be broken down as follows: - Purpose: Handle classical processing of the measurement results. - Outcome: Estimation based on user-defined inputs. -For users aiming to employ the robust shadow estimation protocol, An initial step is needed which entails characterizing the noisy quantum channel. This is done by: +For users aiming to employ the robust shadow estimation protocol, an initial step is needed which entails characterizing the noisy quantum channel. This is done by: 0. `pauli_twirling_calibration` - Purpose: Characterize the noisy quantum channel. @@ -52,7 +55,7 @@ The classical shadow protocol aims to create an approximate classical representa One can use the `mitiq.shadows' module as follows. ### User-defined inputs -Define a quantum circuit, e.g., a circuit which prepares a GHZ state with $n$ = `n_qubits` qubits, +Define a quantum circuit, e.g., a circuit which prepares a GHZ state with $n$ = `3` qubits, ```{code-cell} ipython3 import numpy as np @@ -73,7 +76,7 @@ circuit = cirq.Circuit( print(circuit) ``` -Define a executor to run the circuit on a quantum computer or noisy simulator. Note that the {\it robust shadow estimation} technique can only calibrate and mitigate the noise acting on the operations associated to the classical shadow protocol. So, in order to test the technique, we assume that the state preparation part of the circuit is noiseless. In particular, we define an executor in which: +Define an executor to run the circuit on a quantum computer or a noisy simulator. Note that the *robust shadow estimation* technique can only calibrate and mitigate the noise acting on the operations associated to the classical shadow protocol. So, in order to test the technique, we assume that the state preparation part of the circuit is noiseless. In particular, we define an executor in which: 1. A noise channel is added to circuit right before the measurements. I.e. $U_{\Lambda_U}(M_z)_{\Lambda_{\mathcal{M}_Z}}\equiv U\Lambda\mathcal{M}_Z$. @@ -143,7 +146,7 @@ By setting the total calibration rounds $R$ = `num_total_measurements_calibratio ```{code-cell} ipython3 import sys -sys.modules["tqdm"] = None # distable tqdm for cleaner notebook rendering +sys.modules["tqdm"] = None # disable tqdm for cleaner notebook rendering from mitiq.shadows import * from mitiq.interface.mitiq_cirq.cirq_utils import ( sample_bitstrings as cirq_sample_bitstrings, @@ -169,16 +172,19 @@ the varible `locality` is the maximum number of qubits on which our operators of For more details, please refer to [this tutorial](../examples/rshadows_tutorial.md) ### 1. Quantum Processing -In this step, we obtain classical shadow snapshots (before apply the invert channel) from the input state +In this step, we obtain classical shadow snapshots from the input state (before applying the invert channel). #### 1.1 Add Rotation Gate and Meausure the Rotated State in Computational Basis -At present, the implementation supports random Pauli measurement. This is equivalent to randomly sampling $U$ from the local Clifford group $Cl_2^n$, followed by a $Z$-basis measurement (see [this tutorial](../examples/shadows_tutorial.md) for clear explaination). +At present, the implementation supports random Pauli measurement. This is equivalent to randomly sampling $U$ from the local Clifford group $Cl_2^n$, followed by a $Z$-basis measurement (see [this tutorial](../examples/shadows_tutorial.md) for a clear explanation). #### 1.2 Get the Classical Shadows One can obtain the list of measurement results of local Pauli measurements in terms of bitstrings, and the related Pauli-basis measured in terms of strings as follows. -Whether to run the quantum measurement or directly use the results from the previous run. If **True**, the measurement will be run again. If **False**, the results from the previous run will be used. +You have two choices: run the quantum measurement or directly use the results from the previous run. + +- If **True**, the measurement will be run again. +- If **False**, the results from the previous run will be used. ```{code-cell} ipython3 import zipfile, pickle, io, requests @@ -206,7 +212,7 @@ As an example, we print out one of those measurement outcomes and the associated ```{code-cell} ipython3 -print("one snapshot measurement reslut = ", shadow_measurement_output[0][0]) +print("one snapshot measurement result = ", shadow_measurement_output[0][0]) print("one snapshot measurement basis = ", shadow_measurement_output[1][0]) ``` @@ -260,7 +266,7 @@ for i, pauli_string in enumerate(two_pt_correlations): ```{code-cell} ipython3 -print("classical shadow estimation:", est_corrs) +print("Classical shadow estimation:", est_corrs) print("Robust shadow estimation :", cal_est_corrs) print( "Exact expectation values:", @@ -302,7 +308,7 @@ f_est ``` -Similarly to the previous case (estimation of expectation values), for estimating the density matrix the quantum processing is done as follows. +Similar to the previous case (estimation of expectation values), the quantum processing for estimating the density matrix is done as follows. ```{code-cell} ipython3 diff --git a/docs/source/guide/shadows-5-theory.md b/docs/source/guide/shadows-5-theory.md index 1394bfa43..54297006a 100644 --- a/docs/source/guide/shadows-5-theory.md +++ b/docs/source/guide/shadows-5-theory.md @@ -11,18 +11,22 @@ kernelspec: name: python3 --- -# What is the theory behand Classical Shadow Estimation +```{admonition} Note: +The documentation for Classical Shadows in Mitiq is still under construction. This users guide will change in the future. +``` + +# What is the theory behind Classical Shadow Estimation Investigating an unknown quantum system's properties is essential in quantum computing. Quantum Tomography enables a thorough classical description of a quantum state but demands exponentially large data and an equal number of experiments. Its alternative, Shadow Tomography, requires fewer computations but presupposes the capacity to perform entangling measurements across various state copies, involving exponentially large quantum operations. This section introduces an efficient alternative that constructs an approximate classical depiction of a quantum state with minimal state measurements. ## 1. Classical Shadow -The "classical shadow" technique, as proposed by {cite}`huang2020predicting`, offers an innovative approach to quantum state approximation. This method, which requires exponentially fewer measurements, is particularly advantageous for predicting the properties of complex, large-scale quantum systems. In quantum theory, the quantities of interest are often linear functionals of the quantum state $\rho$, such as the expectation values $o_i$ of a set of self-adjoint operators $\{O_i\}_{i}$: +The "classical shadow" technique, as proposed by {cite}`huang2020predicting`, offers an innovative approach to quantum state approximation. This method is particularly advantageous for predicting the properties of complex, large-scale quantum systems as it requires exponentially fewer measurements. In quantum theory, the quantities of interest are often linear functionals of the quantum state $\rho$, such as the expectation values $o_i$ of a set of self-adjoint operators $\{O_i\}_{i}$: \begin{equation} o_i=\mathrm{Tr}(O_i \rho),\qquad i\in\mathbb{N}^+,~~ 1\leq i\leq M. \end{equation} -Rather than striving for a comprehensive classical description of a many-body quantum state {cite}`aaronson2018shadow`—a task that is practically challenging due to the exponentially large quantities of classical data required—this method only demands a size of $N$ "classical shadow" to predict arbitrary $M$ linear functions $\mathrm{Tr}(O_i \rho)$ up to an additive error $\epsilon$, given that +Rather than striving for a comprehensive classical description of a many-body quantum state {cite}`aaronson2018shadow`—a task that is practically challenging due to the exponentially large quantities of required classical data—this method only demands a size of $N$ "classical shadow" to predict arbitrary $M$ linear functions $\mathrm{Tr}(O_i \rho)$ up to an additive error $\epsilon$, given that \begin{equation} N\geq \mathcal{O}(\epsilon^{-2}\log M\max_i \parallel O_i \parallel^2_{\mathrm{shadow}}). @@ -39,13 +43,13 @@ This random snapshot contains valuable information about $\rho$ in expectation: \begin{equation} \mathbb{E}[U^\dagger |\hat{b}\rangle\langle\hat{b}|U]=\sum_{b\in\{0,1\}^{\otimes n}}\mathrm{Tr}_{(1)}\left(\rho_{(1)}\mathbb{E}_{U\sim U(2^n)}[(U|b\rangle\langle b|U^\dagger)^{\otimes 2}]\right)=\mathcal{M}(\rho) \end{equation} -where the trace is only taken on one of the copies in the tensor product, and the expectation in the first expression has the form $\mathbb{E}(\cdot)=\int_{U\in\mathcal{U}}d\mu(U)\;\langle b|U^\dagger\rho U|b\rangle(\cdot)$. For any unitary ensemble $\mathcal{U}$, the expected value of the outer product of the classical snapshot corresponds to the operation of the quantum channel $\mathcal{M}$ on the quantum state $\rho$. This is indeed a depolarizing channel, as the middle portion of (4) transfigures into a blend of identity and a swap operator, based on **Schur's Lamma** {cite}`harrow2013church`, when taking the Haar average of $\mathcal{G}=U(d)$ group: +where the trace is only taken on one of the copies in the tensor product, and the expectation in the first expression has the form $\mathbb{E}(\cdot)=\int_{U\in\mathcal{U}}d\mu(U)\;\langle b|U^\dagger\rho U|b\rangle(\cdot)$. For any unitary ensemble $\mathcal{U}$, the expected value of the outer product of the classical snapshot corresponds to the operation of the quantum channel $\mathcal{M}$ on the quantum state $\rho$. This is indeed a depolarizing channel, as the middle portion of (4) transfigures into a blend of identity and a swap operator, based on **Schur's Lemma** {cite}`harrow2013church`, when taking the Haar average of $\mathcal{G}=U(d)$ group: \begin{equation} \int_{\mathcal{G}\sim U(d)}~d\mu(\mathcal{G})(U|b\rangle\langle b|U^\dagger)^{\otimes 2}=\frac{\mathbb{I}+X}{d(d+1)}~~ \Rightarrow~~ \mathcal{M}(A)=\sum_{b\in\{0,1\}^{\otimes n}}\frac{\mathrm{Tr}(A)\mathbb{I}+(A)}{2^n(1+2^n)} = \mathcal{D}_{(2^n+1)^{-1}}(A). \end{equation} -apparently the quantum channel $\mathcal{M}$ is a depolarizing channel $\mathcal{D}_p$ with $p=\frac{1}{2^n+1}$. It is easy to solve for the inverted map $\mathcal{M}^{-1}(\cdot)=[(2^n +1)-\mathbb{I}\cdot\mathrm{Tr}](\cdot)$, which is indeed unitary, however, not CP, so it is not a physical map as expected. +Thus, the quantum channel $\mathcal{M}$ is a depolarizing channel $\mathcal{D}_p$ with $p=\frac{1}{2^n+1}$. It is easy to solve for the inverted map $\mathcal{M}^{-1}(\cdot)=[(2^n +1)-\mathbb{I}\cdot\mathrm{Tr}](\cdot)$, which is indeed unitary, however, not CP, so it is not a physical map as expected. If the measurements we sample from are tomographically complete, then the protocol $\mathcal{M}$ defines an invertible linear transformation $\mathcal{M}^{-1}$, which may not be a quantum channel, since it is not CP, which means that it could not be performed in the lab. But it will only be performed on the classical data stored in a classical memory. If we apply $\mathcal{M}$ to all the snapshots, the expected value of these inverted snapshots aligns with the density operator as defined by the protocol, @@ -71,7 +75,7 @@ One can prove that a single classical shadow (6) can correctly predict *any* lin \hat{o}_i(N)=\mathbb{E}_{j\in N}(\hat{o}_i^{(j)}\hat{\rho}_j) \end{equation} -Actually in practical, with the statistical method of taking an average called "median of means" to achieve an acceptable failure probability of estimation, +Actually, in practice, we achieve an acceptable failure probability of estimation with the statistical method of taking an average called "median of means". \begin{equation} \hat{o}_i(N,K):=\mathrm{median}\{\hat{o}_i^{(1)},\cdots,\hat{o}_i^{(K)}\}~~\mathrm{where}~~\hat{o}_i^{(j)}=N^{-1}\sum_{k=N(j-1)+1}^{Nj}\mathrm{Tr}(O_i\hat{\rho}_k),\qquad \forall~1\leq j\leq K\nonumber \end{equation} @@ -85,7 +89,7 @@ Actually in practical, with the statistical method of taking an average called " \mathcal{U}=\mathrm{CL}(2)^n:\qquad \parallel O \parallel_{\mathrm{shadow}}\leq 4^{w}\parallel O \parallel^2,\qquad O\mathrm{~acting~on~}w\mathrm{~qubits} \end{equation} -The random Clifford measurement (10) involves the uniform random application of an element from the Clifford group to the state. These elements can be classically described. Afterward, the measurement is taken in a computational basis. In the context of random Clifford measurements, the shadow norm is equivalent to the Hilbert norm-- specifically, the $L_2$ norm. As a result, a large collection of (global) observables with a bounded Hilbert-Schmidt norm can be predicted efficiently. In this case based on (5), a snapshot(6) would takes the form +The random Clifford measurement (11) involves the uniform random application of an element from the Clifford group to the state. These elements can be classically described. Afterward, the measurement is taken in a computational basis. In the context of random Clifford measurements, the shadow norm is equivalent to the Hilbert norm-- specifically, the $L_2$ norm. As a result, a large collection of (global) observables with a bounded Hilbert-Schmidt norm can be predicted efficiently. In this case based on (5), a snapshot(6) would take the form \begin{equation} \hat{\rho}=(2^n+1)U^\dagger|\hat{b}\rangle\langle\hat{b}|U -\mathbb{I} \end{equation} @@ -98,7 +102,7 @@ On the other hand, a random Pauli measurement (11) means that for each qubit, we \end{equation} -The Clifford measurement requires the depth of the circuit to grow linearly with system size, which is not currently feasible for large systems, so we are going to implement the local (Pauli) measurement and integrate it into Mitiq in the current stage. However, it is worth noting that there is an intermediate step of scrambling the circuits and combining the local and global measurement {cite}`hu2023classical`. +The Clifford measurement requires the depth of the circuit to grow linearly with system size, which is not currently feasible for large systems, which is why only the local (Pauli) measurement is implemented in Mitiq in the current stage. However, it is worth noting that this method involves an intermediate step of scrambling the circuits and combining the local and global measurement {cite}`hu2023classical`. ## 2. Robust Shadow Estimation @@ -116,7 +120,7 @@ name: shadows-noisy-channel The noise in the quantum processing prevents the inversion of the original quantum channel from reversing the process. This necessitates a calibration process. Distinguishing $\Lambda_U$ from the unknown state $\rho$ is generally infeasible, so the noisy quantum channel $\widetilde{\mathcal{M}}$ must be characterized using a known state, such as $\mathbf{|0\rangle}:= |0\rangle^{\otimes n}$, to calibrate the noise. This preparation of $|0\rangle$ is also susceptible to noise, but it provides high fidelity in actual estimation. -### 2.1 Pauli Twilling of quantum channel and Pauli Fideltiy +### 2.1 Pauli Twirling of quantum channel and Pauli Fideltiy The classical shadow estimation employs a quantum channel, which is subsequently inverted. This operation essentially embodies a Pauli twirling. Within this framework, $\mathcal{G}$ represents a subset, to be further identified within the unitaries in $U(d)$. Moreover, $\mathcal{U}$ personifies the PTM representation of $U$. As $\mathcal{G}$ takes the form of a group, the PTMs ${\mathcal{U}}$ evolve into a representation of $\mathcal{G}$. The implementation of Schur’s Lemma facilitates the direct computation of the precise form of $\widehat{\mathcal{M}}$ when the noisy channel $\Lambda$, representing both the gate noise $\mathcal{U}$ and the measurement noise $\mathcal{M}_Z$, is integrated: \begin{equation} \widehat{\mathcal{M}} = \mathbb{E}_{\mathcal{G}}[\mathcal{U}^\dagger\mathcal{M}_z\Lambda\mathcal{U}] = \sum_{\lambda}\hat{f}_\lambda\Pi_\lambda,\qquad \hat{f}_\lambda:=\frac{\mathrm{Tr}(\mathcal{M}_z\Lambda\Pi_\lambda)}{\mathrm{Tr}(\Pi_\lambda)} @@ -140,24 +144,24 @@ The Pauli fidelity estimator for the local Clifford group can be computed utiliz \hat{f}^{(r)}_b = \prod_{i=1}^n \langle\!\langle b_i|\mathcal{U}_i|P_z^{b_i}\rangle\!\rangle \equiv \prod_{i=1}^n \langle b_i|\mathcal{U}_i P_Z^{b_i}\mathcal{U}^\dagger_i|b_i\rangle, \qquad \mathcal{U}_i\in\mathrm{CL}(2),~ b_i\in\{0,1\}. \end{equation} -Repeat the above step for $R = NK$ rounds. Then the final estimation of fz is given by a median of means estimator $\hat{f}_m$ constructed from the single round estimators $\{\hat{f}_m^{(r)}\}_{1\leq r\leq R}$ with parameter $N, \;K$: +Repeat the above step for $R = NK$ rounds. Then the final estimation of $f_z$ is given by a median of means estimator $\hat{f}_m$ constructed from the single round estimators $\{\hat{f}_m^{(r)}\}_{1\leq r\leq R}$ with parameter $N, \;K$: calculate $K$ estimators each of which is the average of $N$ single-round estimators $\hat{f}$, and take the median of these $K$ estimators as our final estimator $\hat{f}$. In formula, \begin{eqnarray} &\bar{f}^{(k)}=\frac{1}{N}\sum_{r=(K-1)N+1}^{KN} \hat{f}^{(r)}\\ & \hat{f} = \mathrm{median}\{\bar{f}^{(1)},\cdots\bar{f}^{(K)}\}_{1\leq k\leq K} \end{eqnarray} -the number of $\{f_m\}$ is related to the number of irreducible representations in the PTM[^1] representation of the twirling group, when the twirling group is the local Clifford group, the number of irreducible representations is $2^n$. +The number of $\{f_m\}$ is related to the number of irreducible representations in the PTM[^1] representation of the twirling group. When the twirling group is the local Clifford group, the number of irreducible representations is $2^n$. ### 2.2 Noiseless Pauli Fidelity --- Ideal Inverse channal vs Estimate Noisy Inverse channel One could check that in the absence of noise in the quantum gates ($\Lambda\equiv\mathbb{I}$), the value of the Pauli fidelity $\hat{f}_{b}^{\mathrm{ideal}}\equiv \mathrm{Tr}(\mathcal{M}_z \Pi_b)/\mathrm{Tr}\Pi_b = 3^{-|{b}|}$, where $|b|$ is the count of $|1\rangle$ found in z-eigenstates $|b\rangle:=|b_i\rangle^{\otimes n}$. -When the noisy channel is considered, the inverse channel $\widehat{\mathcal{M}}^{-1}$ can be abtained by inverse the noisy quantum channel $\widehat{\mathcal{M}}$, one has +When the noisy channel $\widehat{\mathcal{M}}$ is considered, the inverse of the noise channel $\widehat{\mathcal{M}}^{-1}$ can be obtained by: \begin{equation} \widehat{\mathcal{M}}^{-1}=\sum_{b\in\{0,1\}^{\otimes n}}\hat{f}_b^{-1}\Pi_b \end{equation} -After the above steps, we can preform robust shadow calibration as we did in the standart classical shadow protocal, the only difference is we perform the inverse channel replaced by the calibrated version $\widehat{\mathcal{M}}^{-1}$. One can see that the noisy inverse channel $\mathrm{Tr}(\mathcal{M}_z \Pi_b)$ is differed from the one added on the classical shadow protocal by there difference on the Pauli fidelity $\hat{f}_b^{-1}$. +After the above steps, we can preform robust shadow calibration as we did in the standard classical shadow protocol. The only difference is we perform the inverse channel replaced by the calibrated version $\widehat{\mathcal{M}}^{-1}$. One can see that the inverse of the noisy channel $\mathrm{Tr}(\mathcal{M}_z \Pi_b)$ is different from the one used in the classical shadow protocol by their difference on the Pauli fidelity $\hat{f}_b^{-1}$. The set of noise parameters $\{f_\lambda\}_{\lambda}$ corresponds to the number of irreducible representations of $\mathcal{G}$, called Pauli fidelity. When the unitaries are sampled from local Clifford group, the Pauli fidelities can be computed with the following formula: -Therefore the classical shadow with calibration procedure with be proceeded by, first, estimating the noise channel $\widetilde{\mathcal{M}}$ of Eq. (14) with +Therefore, the classical shadow combined with the calibration procedure will, first, estimate the noise channel $\widetilde{\mathcal{M}}$ of Eq. (14) via the calibration procedure, and then use the $\widetilde{\mathcal{M}}$ estimator as the input parameter, $\mathcal{M}\rightarrow\widetilde{\mathcal{M}}$ of the classical shadow to predict any properties of interest (referred to as the estimation procedure). diff --git a/docs/source/guide/shadows.md b/docs/source/guide/shadows.md index 68c02a0cf..20ddbec1e 100644 --- a/docs/source/guide/shadows.md +++ b/docs/source/guide/shadows.md @@ -11,6 +11,10 @@ kernelspec: name: python3 --- +```{admonition} Note: +The documentation for Classical Shadows in Mitiq is still under construction. This users guide will change in the future. +``` + # Classical Shadows Classical shadows protocol {cite}`huang2020predicting` aims to create an approximate classical representation @@ -18,7 +22,7 @@ of a quantum state using minimal measurements. The protocol is based on the idea which is a technique for reconstructing a quantum state from a small number of measurements. This approach not only characterizes and mitigates noise effectively but also retains sample efficiency and demonstrates noise resilience {cite}`chen2021robust`. For more details, see the section -([Classical Shadow Protocol and its Robust Estimation](shadows-5-theory.md)). +[Classical Shadow Protocol and its Robust Estimation](shadows-5-theory.md). ```{figure} ../img/classicalshadow_workflow.png @@ -48,6 +52,6 @@ shadows-5-theory.md Here are some examples on how to use shadows in Mitiq: -- [Classical Shadows Protocal with Cirq](../examples/shadows_tutorial.md) +- [Classical Shadows Protocol with Cirq](../examples/shadows_tutorial.md) - [Robust Shadows Estimation with Cirq](../examples/rshadows_tutorial.md) diff --git a/docs/source/img/classicalshadow_workflow.png b/docs/source/img/classicalshadow_workflow.png index f33772ee8..f6799388c 100644 Binary files a/docs/source/img/classicalshadow_workflow.png and b/docs/source/img/classicalshadow_workflow.png differ diff --git a/docs/source/img/classicalshadow_workflow.svg b/docs/source/img/classicalshadow_workflow.svg index 208acdb20..ba98e15e4 100644 --- a/docs/source/img/classicalshadow_workflow.svg +++ b/docs/source/img/classicalshadow_workflow.svg @@ -1,8 +1,8 @@ + + + @@ -106,70 +122,49 @@ + transform="translate(-392.5164,-244.00354)"> + style="stroke-width:0.373035" /> User + style="opacity:1;fill:#b3ff80;fill-opacity:1;stroke-width:0.407192" /> mitiq.shadows - - - 5.Average over all classical snapshots - + transform="matrix(0.93195,0,0,0.93195,494.26408,-68.534382)"> @@ -238,7 +233,7 @@ - - - - Post processingby mitiq - - Construct classicalsnapshot of inputstate - + transform="translate(628.98817,54.463186)"> + style="fill:#ffffe1;stroke:#000000;stroke-width:1.17707;stroke-miterlimit:8" /> ExpectationError-valuemitigated ExpectationvalueOR + + + + ReconstructedReconstructedquantuminput state + id="tspan25">state + transform="translate(602.26854,-86.779174)"> + x="544.02515" + y="372.98331"> + style="stroke-width:0.370369" /> Hardware - - - Execute onbackend - + transform="matrix(0.99327221,0,0,0.98127905,847.08004,-12.365212)"> 2.Noisy Z-basis Measurement - - - - - 3. Record Outcomes - - - + width="391.82404" + height="173.27588" + x="551.32245" + y="268.49045" /> + + transform="matrix(0.99327221,0,0,0.98127905,709.32655,97.410661)"> @@ -592,14 +505,296 @@ + width="560.01819" + height="370.82443" + x="393.59686" + y="245.08401" /> + + + + + + Prediction using eachclassical snapshot + + + + + + + Average over allclassical snapshots + + + + + Post processingby mitiq + + Construct classicalsnapshot of inputstate + + + + + + 3. Record Outcomes + + + + + + + + Option 1 + + Option 2 + + + + + + Execute onbackend + + + + 5.Median of means estimation + diff --git a/docs/source/img/rshadows_workflow.png b/docs/source/img/rshadows_workflow.png index 66c67dd14..5632919d4 100644 Binary files a/docs/source/img/rshadows_workflow.png and b/docs/source/img/rshadows_workflow.png differ diff --git a/docs/source/img/rshadows_workflow.svg b/docs/source/img/rshadows_workflow.svg index 8366dd6fe..5edabe531 100644 --- a/docs/source/img/rshadows_workflow.svg +++ b/docs/source/img/rshadows_workflow.svg @@ -1,41 +1,15 @@ - + transform="translate(-431.04965,-277.7151)"> + style="stroke-width:0.362886" /> User + style="opacity:1;fill:#b3ff80;fill-opacity:1;stroke-width:0.39588" /> mitiq.shadows + id="tspan6">Calibration in mitiq.shadows + transform="matrix(0.99293044,0,0,0.93195,500.0635,-20.623319)"> @@ -257,7 +231,7 @@ + transform="translate(649.48751,34.177174)"> + transform="translate(639.81881,-41.67255)"> + x="582.40985" + y="406.82529"> + style="stroke-width:0.360706" /> Hardware + transform="matrix(0.99327221,0,0,0.98127905,905.11876,39.404254)"> + transform="matrix(0.99327221,0,0,0.98127905,882.3055,211.65434)"> @@ -490,42 +464,42 @@ id="rect1-0" width="391.8905" height="163.59698" - x="551.8241" - y="351.52191" /> + x="584.01984" + y="305.85651" /> + width="560.07648" + height="351.06473" + x="432.10101" + y="278.76645" /> + transform="matrix(0.99327222,0,0,0.98127904,926.10108,225.12112)"> @@ -557,32 +531,36 @@ + style="fill:#ffd42a;stroke-width:0.584901" /> 6.Use estimated noise channel in Step 2of Classical Shadows workflow + id="tspan33">6.Use in Classical Shadows workflow toconstruct classical snapshots + remainingsteps therein + transform="translate(674.05644,-44.5759)"> + transform="translate(705.41765,-70.133127)"> + transform="matrix(0.99327222,0,0,0.98127904,680.46925,142.43337)"> + transform="translate(709.26584,41.628438)"> Estimated noiseEstimate noisechannel + id="tspan34">channel