# PaddlePaddle / Quantum 大约 1 小时 前同步成功

 Q Quleaf 已提交 8月 20, 2021 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 { "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 }