{
"cells": [
{
"cell_type": "markdown",
"id": "damaged-friendship",
"metadata": {},
"source": [
"# The Classical Shadow of Unknown Quantum States"
]
},
{
"cell_type": "markdown",
"id": "quarterly-leone",
"metadata": {},
"source": [
"* Copyright (c) 2021 Institute for Quantum Computing, Baidu Inc. All Rights Reserved. *"
]
},
{
"cell_type": "markdown",
"id": "widespread-clone",
"metadata": {},
"source": [
"## Overview\n",
"\n",
"For the quantum state $\\rho$ in an unknown quantum system, obtaining the information it contains is a fundamental and essential problem. This tutorial will discuss how to use the classical shadow to describe an unknown quantum state with classical data so that many quantum state properties can be efficiently estimated. In the era of NISQ (noisy intermediate-scale quantum), the classical shadow helps us trade quantum for classical resources. After describing the information of the quantum state with classical data, some quantum problems can be solved by methods such as classical machine learning. And using this method, the cost of the quantum circuit running times in some existing variational quantum algorithms like variational quantum eigensolver (VQE) may be reduced so that an acceleration can be achieved."
]
},
{
"cell_type": "markdown",
"id": "remarkable-season",
"metadata": {},
"source": [
"## The Classical Shadow\n",
"\n",
"The intuition of the classic shadow comes from shadows in real life. If we use a beam of light to illuminate a polyhedron vertically, we get a shadow of it. Rotating this polyhedron, we can see its different shadows. After many rotations, this series of shadows naturally reflect some information about this polyhedron. Similarly, in the quantum world, if we perform a unitary transformation on a quantum state and then perform a measurement, can we also get a \"shadow\" of the quantum state? The construction of the classical shadow is analogous to this example. Its construction process is as follows:\n",
"\n",
"First, we apply a unitary transformation to an unknown quantum state $\\rho$ in a $n$ qubits system: $\\rho \\mapsto U \\rho U^{\\dagger}$, and then measure each qubit in the computational basis. For the measurement result, we use $|\\hat{b}\\rangle$ as an example. We perform the inverse transformation of the previous unitary on $|\\hat{b}\\rangle$ to get $U^{\\dagger}|\\hat{b}\\rangle\\langle\\hat{b}|U$. We know that the expectation value of measured quantum state $|\\hat{b}\\rangle\\langle\\hat{b}|$ is: \n",
"\n",
"$$\n",
"\\mathbb{E}(|\\hat{b}\\rangle\\langle\\hat{b}|) = \\sum_{b \\in \\{0,1\\}^{n}} \\operatorname{Pr}(|\\hat{b}\\rangle\\langle\\hat{b}| = |b\\rangle\\langle b|)\\cdot |b\\rangle \\langle b|= \\sum_{b \\in \\{0,1\\}^{n}}\\langle b|U\\rho U^{\\dagger} |b\\rangle |b\\rangle \\langle b| \\tag{1}\n",
"$$\n",
"\n",
"Then after the reverse operation, the expectation value of $U^{\\dagger}|\\hat{b}\\rangle\\langle\\hat{b}|U$ is $\\sum_{b \\in \\{0,1\\}^{n}}\\langle b|U\\rho U^{\\dagger} |b\\rangle U^{\\dagger}|b\\rangle \\langle b|U$. In this process, the unitary transformation $U$ is randomly selected from a fixed set. \n",
"When we repeat this process and average $U$, we can get:\n",
"\n",
"$$\n",
"\\mathbb{E}_{U \\sim \\mathcal{U}\\left(n\\right)}(\\mathbb{E}(U^{\\dagger}|\\hat{b}\\rangle\\langle\\hat{b}|U))=\\sum_{b \\in \\{0,1\\}^{n}}\\mathbb{E}_{U \\sim \\mathcal{U}\\left(n\\right)}(\\langle b|U\\rho U^{\\dagger} |b\\rangle U^{\\dagger}|b\\rangle \\langle b|U) \\tag{2}\n",
"$$ \n",
"where $\\mathcal{U}\\left(n\\right)$ is a given unitary transformation set on $n$ qubits.\n",
"\n",
"If this expectation value is recorded as $\\mathcal{M}(\\rho)$, then $\\mathcal{M}$ will be a map from $\\rho$ to $\\mathcal{M}(\\rho)$. When $\\mathcal{M}$ is linear and reversible [1], the initial quantum state $\\rho$ can be expressed as:\n",
"\n",
"$$\n",
"\\rho=\\mathcal{M}^{-1}(\\mathbb{E}_{U \\sim \\mathcal{U} \\left(n\\right)}(\\mathbb{E}(U^{\\dagger}|\\hat{b}\\rangle\\langle \\hat{b}|U))) = \\mathbb{E}_{U \\sim \\mathcal{U} \\left(n\\right)}(\\mathbb{E}(\\mathcal{M}^{-1} (U^{\\dagger}|\\hat{b}\\rangle\\langle \\hat{b}|U))) \\tag{3}\n",
"$$\n",
"\n",
"\n",
"With $\\mathcal{M}^{-1}$, every time $U$ is sampled, we compute $\\hat{\\rho} = \\mathcal{M }^{-1}(U^{\\dagger}|\\hat{b}\\rangle\\langle\\hat{b}|U)$ and name it a snapshot. After repeating this process $N$ times, we get $N$ collections of snapshots about $\\rho$:\n",
"\n",
"\n",
"$$\n",
"\\text{S}(\\rho ; N)=\\{\\hat{\\rho}_{1}=\\mathcal{M}^{-1}(U_{1}^{\\dagger}|\\hat{b}_{1}\\rangle\\langle\\hat{b}_{1}| U_{1}), \\ldots, \\hat{\\rho}_{N}=\\mathcal{M}^{-1}(U_{N}^{\\dagger}|\\hat{b}_{N}\\rangle\\langle\\hat{b}_{N}| U_{N})\\} .\\tag{4}\n",
"$$\n",
"\n",
"We call $\\text{S}(\\rho; N)$ the classical shadow of $\\rho$. It is worth mentioning that what exactly $\\mathcal{M}$ is depends on the sampling set $\\mathcal{U}$ we select. For example, when we select the Clifford group as the sampling set $\\mathcal{U}$, we have:\n",
"\n",
"$$\n",
"\\mathcal{M}(\\rho)=\\mathbb{E}_{U \\sim \\operatorname{Cl} \\left(n\\right)}(\\mathbb{E}(U^{\\dagger}|\\hat{b}\\rangle\\langle \\hat{b}|U)) = \\frac{1}{2^{n}+1}\\rho+\\frac{1}{2^{n}+1}I. \\tag{5}\n",
"$$\n",
"\n",
"Readers may refer to [1] for details about why $\\mathcal{M}$ is as (5). It follows:\n",
"\n",
"$$\\mathcal{M}^{-1}(\\frac{1}{2^{n}+1}\\rho+\\frac{1}{2^{n}+1}I)=\\rho \\Rightarrow \\mathcal{M}^{-1}(\\rho) = (2^{n}+1)\\rho-I \\tag{6}$$\n",
"\n",
"After constructing the classical shadow, how does it help us to effectively estimate the properties of the quantum state? Some linear properties of quantum state $\\rho$ are very suitable to be estimated by the classical shadow, for example, the expectation value of $\\rho$ of an observable $\\mathcal{O}$: $o =\\operatorname{tr}\\left(\\mathcal{O} \\rho\\right)$ [1]. Let $\\hat{o}=\\operatorname{tr}\\left(\\mathcal{O} \\hat{\\rho}\\right)$, then according to (3), there is $\\mathbb{E}[\\hat{o}]=\\operatorname{tr}\\left(\\mathcal{O} \\rho\\right)$. We provide detailed applications and implementations in our following tutorial: [Estimation of Quantum State Properties Based on the Classical Shadow](./ClassicalShadow_Application_EN.ipynb)."
]
},
{
"cell_type": "markdown",
"id": "metropolitan-subdivision",
"metadata": {},
"source": [
"Next, we will show the process of constructing the classical shadow for a randomly selected quantum state $\\rho$ in Paddle Quantum to help readers understand how the classical shadow works. And we will use the Clifford group as the sampling set of the unitary transformation (specifically about the properties of the Clifford group and how to sample uniformly distributed Clifford operators from it randomly, readers can refer to [1], [3], and the `Clifford` class in Paddle Quantum)."
]
},
{
"cell_type": "markdown",
"id": "regional-binary",
"metadata": {},
"source": [
"## Paddle Quantum Implementation"
]
},
{
"cell_type": "markdown",
"id": "military-project",
"metadata": {},
"source": [
"First, we need to import all the dependencies:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "stopped-kennedy",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import paddle\n",
"import matplotlib.pyplot as plt\n",
"from paddle_quantum.clifford import Clifford\n",
"from paddle_quantum.state import vec_random\n",
"from paddle_quantum.circuit import UAnsatz\n",
"from paddle_quantum.utils import trace_distance"
]
},
{
"cell_type": "markdown",
"id": "activated-found",
"metadata": {},
"source": [
"Next, we randomly generate a quantum state $\\rho$."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "union-freedom",
"metadata": {},
"outputs": [],
"source": [
"# Number of qubits\n",
"n_qubit = 2\n",
"\n",
"# Randomly generate a state vector\n",
"phi_random = vec_random(n_qubit) \n",
"# Its density matrix form\n",
"rho_random = np.outer(phi_random, phi_random.conj())\n",
"\n",
"# Define |0> and |1>\n",
"ket_0 = np.array([[1,0]]).T\n",
"ket_1 = np.array([[0,1]]).T\n",
"I = np.identity(1<"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"# Print out the result\n",
"fig,ax = plt.subplots(figsize=(10, 10))\n",
" \n",
"plt.xlabel('number of samples')\n",
"plt.ylabel('trace distance')\n",
"j = range(len(tracedistance)) \n",
"plt.plot(j, tracedistance, 'r', label=\"trace_distance\")\n",
"\"\"\"open the grid\"\"\"\n",
"plt.grid(True)\n",
"plt.legend(bbox_to_anchor=(1.0, 1), loc=1, borderaxespad=0.)\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "ambient-actor",
"metadata": {},
"source": [
"## Conclusion\n",
"\n",
"This tutorial introduces some theoretical knowledge of the classical shadow. In this example, we constructed the classical shadow of a random 2-qubit quantum state in Paddle Quantum. We can intuitively understand that the classic shadow can make a good approximation to an unknown quantum state. In fact, [2] pointed out demanding full classical descriptions of quantum systems may be excessive for many concrete tasks. Instead, it is often sufficient to accurately predict certain properties of the quantum system. This is where the fundamental importance of the classical shadow lies. Another tutorial ([Estimation of Quantum State Properties Based on the Classical Shadow](./ClassicalShadow_Application_EN.ipynb)) will continue to introduce the applications of the classic shadow and how to use shadow function in Paddle Quantum."
]
},
{
"cell_type": "markdown",
"id": "through-order",
"metadata": {},
"source": [
"## References\n",
"[1] Huang, Hsin-Yuan, Richard Kueng, and John Preskill. \"Predicting many properties of a quantum system from very few measurements.\" [Nature Physics 16.10 (2020): 1050-1057.](https://authors.library.caltech.edu/102787/1/2002.08953.pdf) \n",
"\n",
"[2] Aaronson, Scott. \"Shadow tomography of quantum states.\" [SIAM Journal on Computing 49.5 (2019): STOC18-368.](https://dl.acm.org/doi/abs/10.1145/3188745.3188802) \n",
"\n",
"[3] Bravyi, Sergey, and Dmitri Maslov. \"Hadamard-free circuits expose the structure of the Clifford group.\" [IEEE Transactions on Information Theory 67.7 (2021): 4546-4563.](https://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=9435351)"
]
}
],
"metadata": {
"interpreter": {
"hash": "3b61f83e8397e1c9fcea57a3d9915794102e67724879b24295f8014f41a14d85"
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}