GibbsState_EN.ipynb 15.5 KB
Newer Older
Q
Quleaf 已提交
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
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Gibbs State Preparation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<em> Copyright (c) 2021 Institute for Quantum Computing, Baidu Inc. All Rights Reserved. </em>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Overview"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Q
Quleaf 已提交
28
    "This tutorial will show how to train a quantum neural network (QNN) through Paddle Quantum to prepare a quantum Gibbs state."
Q
Quleaf 已提交
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
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Background"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The frontiers of quantum computing include quantum machine learning and quantum optimization. In these two areas, the preparation of specific quantum states is a fundamental problem. In particular, the preparation of Gibbs state is a necessary step to realize many quantum algorithms and is widely used in:\n",
    "- Learning of restricted Boltzmann machines in quantum machine learning [1]\n",
    "- Solving optimization problems such as convex optimization and positive semidefinite programming [2]\n",
    "- Combinatorial optimization problem [3]\n",
    "\n",
    "The Gibbs state is defined as follows: Given the Hamiltonian $H$ of an $n$-qubit system (generally this is a Hermitian matrix of $2^n\\times2^n$), the Gibbs state at temperature $T$ is\n",
    "$$\n",
    "\\rho_G = \\frac{{{e^{-\\beta H}}}}{{\\text{tr}({e^{-\\beta H}})}},\n",
    "\\tag{1}\n",
    "$$\n",
    "where ${e^{-\\beta H}}$ is the matrix exponential of matrix $-\\beta H$. $\\beta = \\frac{1}{{kT}}$ is the inverse temperature parameter of the system, where $T $ Is the temperature parameter and $k$ is Boltzmann's constant (in this tutorial, we take $k = 1$)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Paddle Quantum Implementation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First, let us import the necessary libraries and packages through the following code."
   ]
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
71
   "execution_count": 1,
Q
Quleaf 已提交
72 73
   "metadata": {
    "ExecuteTime": {
Q
Quleaf 已提交
74 75
     "end_time": "2021-04-30T08:56:40.078886Z",
     "start_time": "2021-04-30T08:56:36.868403Z"
Q
Quleaf 已提交
76 77 78 79 80
    }
   },
   "outputs": [],
   "source": [
    "import scipy\n",
Q
Quleaf 已提交
81
    "import paddle\n",
Q
Quleaf 已提交
82
    "from numpy import trace as np_trace\n",
Q
Quleaf 已提交
83
    "import paddle_quantum as pq\n",
Q
Quleaf 已提交
84
    "from paddle_quantum.ansatz import Circuit\n",
Q
Quleaf 已提交
85
    "from paddle_quantum.state import zero_state\n",
Q
Quleaf 已提交
86
    "from paddle_quantum.qinfo import state_fidelity, partial_trace, pauli_str_to_matrix"
Q
Quleaf 已提交
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
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As a hands-on example, here we consider a 3-qubit Hamiltonian and its Gibbs state:\n",
    "\n",
    "$$\n",
    "H = -Z \\otimes Z \\otimes I - I \\otimes Z \\otimes Z - Z \\otimes I \\otimes Z, \\quad I=\\left [\n",
    "\\begin{matrix}\n",
    "1 & 0  \\\\\n",
    "0 & 1  \\\\\n",
    "\\end{matrix} \n",
    "\\right ], \\quad \n",
    "Z=\\left [\n",
    "\\begin{matrix}\n",
    "1 & 0  \\\\\n",
    "0 & -1  \\\\\n",
    "\\end{matrix} \n",
    "\\right ].\n",
    "\\tag{2}\n",
    "$$\n",
    "\n",
    "In this example, we set the inverse temperature parameter to $\\beta = 1.5$. Besides, to test the final results, we have generated the ideal Gibbs state $\\rho_G$ in advance according to the definition."
   ]
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
116
   "execution_count": 2,
Q
Quleaf 已提交
117 118
   "metadata": {
    "ExecuteTime": {
Q
Quleaf 已提交
119 120
     "end_time": "2021-04-30T08:56:40.099967Z",
     "start_time": "2021-04-30T08:56:40.082138Z"
Q
Quleaf 已提交
121 122 123 124
    }
   },
   "outputs": [],
   "source": [
Q
Quleaf 已提交
125 126 127 128 129 130
    "N = 4                               # The width of the QNN\n",
    "N_SYS_B = 3                         # The number of qubits of subsystem B used to generate the Gibbs state\n",
    "SEED = 16                           # Fixed random seed\n",
    "beta = 1.5                          # Set the inverse temperature parameter beta\n",
    "pq.set_backend('density_matrix')    # Set density matrix backend\n",
    "pq.set_dtype('complex128')          # Set calculation precision"
Q
Quleaf 已提交
131 132 133 134
   ]
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
135
   "execution_count": 3,
Q
Quleaf 已提交
136 137
   "metadata": {
    "ExecuteTime": {
Q
Quleaf 已提交
138 139
     "end_time": "2021-04-30T08:56:40.201757Z",
     "start_time": "2021-04-30T08:56:40.106315Z"
Q
Quleaf 已提交
140 141 142 143 144 145 146 147
    }
   },
   "outputs": [],
   "source": [
    "# Generate a specific Hamiltonian represented by a Pauli string\n",
    "H = [[-1.0,'z0,z1'], [-1.0,'z1,z2'], [-1.0,'z0,z2']]\n",
    "\n",
    "# Generate matrix information of Hamiltonian\n",
Q
Quleaf 已提交
148
    "hamiltonian = pauli_str_to_matrix(H, N_SYS_B).numpy()\n",
Q
Quleaf 已提交
149 150 151 152 153
    "\n",
    "# Generate the ideal target Gibbs state rho\n",
    "rho_G = scipy.linalg.expm(-1 * beta * hamiltonian) / np_trace(scipy.linalg.expm(-1 * beta * hamiltonian))\n",
    "\n",
    "# Set to the data type supported by Paddle Quantum\n",
Q
Quleaf 已提交
154 155
    "hamiltonian = hamiltonian.astype('complex128')\n",
    "rho_G = paddle.to_tensor(rho_G, dtype='complex128')"
Q
Quleaf 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Building a quantum neural network"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- In this example, we will prepare the Gibbs state by training the QNN (also can be understood as a parameterized quantum circuit). Here, we provide a simple 4-qubit quantum circuit as follows:\n",
    "\n",
    "  ![Ugibbs.jpg](https://release-data.cdn.bcebos.com/PIC%2FUgibbs.jpg)\n",
    "\n",
    "- We need to preset some circuit parameters. For example, the circuit has 4 qubits. The first qubit is the ancillary system, and the 2-4th qubits are the subsystems used to generate the Gibbs state.\n",
    "- Initialize the variable ${\\bf{\\theta }}$ that represents the vector of parameters in our QNN.\n",
    "         \n",
    "\n",
Q
Quleaf 已提交
177
    "Next, we use Paddle Quantum's `Circuit` class and the built-in `real_entangled_layer` circuit template to build a QNN based on the circuit design in the above figure."
Q
Quleaf 已提交
178 179 180 181
   ]
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
182
   "execution_count": 4,
Q
Quleaf 已提交
183 184
   "metadata": {
    "ExecuteTime": {
Q
Quleaf 已提交
185 186
     "end_time": "2021-04-30T08:56:40.213503Z",
     "start_time": "2021-04-30T08:56:40.205311Z"
Q
Quleaf 已提交
187 188 189 190
    }
   },
   "outputs": [],
   "source": [
Q
Quleaf 已提交
191
    "def U_theta(num_qubits: int, depth: int) -> Circuit:\n",
Q
Quleaf 已提交
192 193 194
    "    \"\"\"\n",
    "    Quantum Neural Network\n",
    "    \"\"\"\n",
Q
Quleaf 已提交
195 196
    "    # Initialize the QNN according to the number of qubits\n",
    "    cir = Circuit(num_qubits)\n",
Q
Quleaf 已提交
197 198
    "    \n",
    "    # Built-in {R_y + CNOT} circuit template\n",
Q
Quleaf 已提交
199
    "    cir.real_entangled_layer(depth=depth)\n",
Q
Quleaf 已提交
200 201
    "    \n",
    "    # The QNN acts on a given initial state\n",
Q
Quleaf 已提交
202 203 204
    "    cir.ry()\n",
    "    \n",
    "    return cir"
Q
Quleaf 已提交
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
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Configuring the training model: loss function"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- Now that we have the data and QNN architecture, we also need to define appropriate training parameters, models, and loss functions to achieve our goals.\n",
    "\n",
    "- Specifically, we refer to the method in the paper [4]. The core idea is to use the Gibbs state to achieve the minimum free energy.\n",
    "\n",
    "- By applying the QNN $U(\\theta)$ on the initial state, we can get the output state $\\left| {\\psi \\left( {\\bf{\\theta }} \\right)} \\right\\rangle $. Its state in the 2-4th qubits is recorded as $\\rho_B(\\theta)$.\n",
    "\n",
    "- Set the loss function in the training model. In Gibbs state learning, we use the truncation of the von Neumann entropy function to estimate the free energy, and the corresponding loss function, as in reference [4], can be set as $loss = {L_1} + {L_2} + {L_3} $, where\n",
    "\n",
    "$$\n",
    "{L_1}= \\text{tr}(H\\rho_B), \\quad {L_2} = 2{\\beta^{-1}}{\\text{tr}}(\\rho_B^2), \\quad L_3 = - {\\beta ^{ - 1}}\\big(\\text{tr}(\\rho_B^3) + 3\\big)/2.\n",
    "\\tag{3}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
234
   "execution_count": 5,
Q
Quleaf 已提交
235 236
   "metadata": {
    "ExecuteTime": {
Q
Quleaf 已提交
237 238
     "end_time": "2021-04-30T08:56:40.238960Z",
     "start_time": "2021-04-30T08:56:40.228348Z"
Q
Quleaf 已提交
239 240 241 242
    }
   },
   "outputs": [],
   "source": [
Q
Quleaf 已提交
243 244 245 246 247 248
    " # define loss function\n",
    "def loss_func(cir: Circuit, Hamiltonian: paddle.Tensor, N_SYS_B: int) -> paddle.Tensor:\n",
    "    # Apply QNN\n",
    "    rho_AB = cir(zero_state(N))\n",
    "    \n",
    "    # Calculate partial trace to obtain the quantum state rho_B of subsystem B\n",
Q
Quleaf 已提交
249
    "    rho_B = partial_trace(rho_AB.data, 2 ** (N - N_SYS_B), 2 ** (N_SYS_B), 1)\n",
Q
Quleaf 已提交
250 251 252 253 254 255 256 257 258 259 260
    "    \n",
    "    # Calculate the three parts of the loss function\n",
    "    rho_B_squre = rho_B @ rho_B\n",
    "    loss1 = paddle.real(paddle.trace(rho_B @ Hamiltonian))\n",
    "    loss2 = paddle.real(paddle.trace(rho_B_squre)) * 2 / beta\n",
    "    loss3 = -(paddle.real(paddle.trace(rho_B_squre @ rho_B)) + 3) / (2 * beta)\n",
    "    \n",
    "    # 最终的损失函数\n",
    "    loss = loss1 + loss2 + loss3  \n",
    "    \n",
    "    return loss, rho_B"
Q
Quleaf 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Configure training model: model parameters"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Before training the QNN, we also need to set some training hyperparameters, mainly the learning rate (LR), the number of iterations (ITR), and the depth (D) of the QNN. Here we set the learning rate to 0.5 and the number of iterations to 50. Readers may wish to adjust by themselves to explore the influence of hyperparameter adjustment on the training effect."
   ]
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
279
   "execution_count": 6,
Q
Quleaf 已提交
280 281
   "metadata": {
    "ExecuteTime": {
Q
Quleaf 已提交
282 283
     "end_time": "2021-04-30T08:56:40.966455Z",
     "start_time": "2021-04-30T08:56:40.959537Z"
Q
Quleaf 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
    }
   },
   "outputs": [],
   "source": [
    "ITR = 50 # Set the total number of iterations of training\n",
    "LR = 0.5 # Set the learning rate\n",
    "D = 1 # Set the depth of the QNN"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Training"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Q
Quleaf 已提交
304
    "- After all the training model parameters are set, we convert the data into Tensor in PaddlePaddle and then train the QNN.\n",
Q
Quleaf 已提交
305
    "- During training, we use [Adam Optimizer](https://www.paddlepaddle.org.cn/documentation/docs/en/api/paddle/optimizer/adam/Adam_en.html). Other optimizers are also provided in PaddlePaddle.\n",
Q
Quleaf 已提交
306 307 308 309 310 311
    "- We output the results of the training process in turn.\n",
    "- In particular, we sequentially output the fidelity of the quantum state $\\rho_B(\\theta)$ and Gibbs state $\\rho_G$ we learned. The higher the fidelity, the closer the QNN output state is to Gibbs state."
   ]
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
312
   "execution_count": 8,
Q
Quleaf 已提交
313 314
   "metadata": {
    "ExecuteTime": {
Q
Quleaf 已提交
315 316
     "end_time": "2021-04-30T08:56:50.509486Z",
     "start_time": "2021-04-30T08:56:47.508586Z"
Q
Quleaf 已提交
317 318 319 320 321 322 323
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
Q
Quleaf 已提交
324
      "iter: 10 loss: -3.1085 fid: 0.9241\n",
Q
Quleaf 已提交
325 326 327
      "iter: 20 loss: -3.3375 fid: 0.9799\n",
      "iter: 30 loss: -3.3692 fid: 0.9897\n",
      "iter: 40 loss: -3.3990 fid: 0.9929\n",
Q
Quleaf 已提交
328 329
      "iter: 50 loss: -3.4133 fid: 0.9959\n",
      "\n",
Q
Quleaf 已提交
330
      "The trained circuit:  \n",
Q
Quleaf 已提交
331
      "--Ry(6.290)----*--------------x----Ry(0.747)--\n",
Q
Quleaf 已提交
332
      "               |              |               \n",
Q
Quleaf 已提交
333
      "--Ry(4.745)----x----*---------|----Ry(6.249)--\n",
Q
Quleaf 已提交
334
      "                    |         |               \n",
Q
Quleaf 已提交
335
      "--Ry(-0.01)---------x----*----|----Ry(-0.05)--\n",
Q
Quleaf 已提交
336
      "                         |    |               \n",
Q
Quleaf 已提交
337
      "--Ry(0.017)--------------x----*----Ry(6.310)--\n",
Q
Quleaf 已提交
338
      "                                              \n"
Q
Quleaf 已提交
339 340 341 342
     ]
    }
   ],
   "source": [
Q
Quleaf 已提交
343
    "paddle.seed(SEED)\n",
Q
Quleaf 已提交
344
    "    \n",
Q
Quleaf 已提交
345 346
    "# Convert Numpy array to Tensor supported in PaddlePaddle\n",
    "H = paddle.to_tensor(hamiltonian)\n",
Q
Quleaf 已提交
347
    "\n",
Q
Quleaf 已提交
348
    "# Determine the parameter dimension of the network\n",
Q
Quleaf 已提交
349
    "circuit = U_theta(N, D)\n",
Q
Quleaf 已提交
350
    "\n",
Q
Quleaf 已提交
351 352
    "# Generally speaking, we use Adam optimizer to get relatively good convergence\n",
    "# Of course, it can be changed to SGD or RMS prop.\n",
Q
Quleaf 已提交
353
    "opt = paddle.optimizer.Adam(learning_rate=LR, parameters=circuit.parameters())\n",
Q
Quleaf 已提交
354
    "\n",
Q
Quleaf 已提交
355 356
    "# Optimization loops\n",
    "for itr in range(1, ITR + 1):\n",
Q
Quleaf 已提交
357
    "        \n",
Q
Quleaf 已提交
358
    "    # Run forward propagation to calculate the loss function and return the generated quantum state rho_B\n",
Q
Quleaf 已提交
359
    "    loss, rho_B = loss_func(circuit, H, N_SYS_B)\n",
Q
Quleaf 已提交
360
    "        \n",
Q
Quleaf 已提交
361 362 363 364
    "    # Run back propagation to minimize the loss function\n",
    "    loss.backward()\n",
    "    opt.minimize(loss)\n",
    "    opt.clear_grad()\n",
Q
Quleaf 已提交
365
    "\n",
Q
Quleaf 已提交
366
    "    # Convert to Numpy array to calculate the fidelity of the quantum state F(rho_B, rho_G)\n",
Q
Quleaf 已提交
367
    "    fid = state_fidelity(rho_B, rho_G)\n",
Q
Quleaf 已提交
368
    "        \n",
Q
Quleaf 已提交
369
    "    # Print training results\n",
Q
Quleaf 已提交
370 371
    "    if itr % 10 == 0:\n",
    "        print('iter:', itr, 'loss:', '%.4f' % loss.numpy(), 'fid:', '%.4f' % fid.numpy())\n",
Q
Quleaf 已提交
372
    "    if itr == ITR:\n",
Q
Quleaf 已提交
373 374
    "        print(\"\\nThe trained circuit:  \")\n",
    "        print(circuit)"
Q
Quleaf 已提交
375 376 377 378 379 380
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Q
Quleaf 已提交
381 382 383 384
    "## Conclusion\n",
    "\n",
    "According to the results obtained from the above training, after about 50 iterations, we can achieve a high-precision Gibbs state with a fidelity higher than 99.5% and complete the preparation of the Gibbs state efficiently and accurately. We can output the QNN's learned parameters and its output state through the print function.\n",
    "\n",
Q
Quleaf 已提交
385 386
    "_______\n",
    "\n",
Q
Quleaf 已提交
387 388
    "## References\n",
    "\n",
Q
Quleaf 已提交
389 390 391 392 393 394
    "[1] Kieferová, M. & Wiebe, N. Tomography and generative training with quantum Boltzmann machines. [Phys. Rev. A 96, 062327 (2017).](https://journals.aps.org/pra/abstract/10.1103/PhysRevA.96.062327)\n",
    "\n",
    "[2] Brandao, F. G. S. L. & Svore, K. M. Quantum Speed-Ups for Solving Semidefinite Programs. [in 2017 IEEE 58th Annual Symposium on Foundations of Computer Science (FOCS) 415–426 (IEEE, 2017). ](https://ieeexplore.ieee.org/abstract/document/8104077)\n",
    "\n",
    "[3] Somma, R. D., Boixo, S., Barnum, H. & Knill, E. Quantum Simulations of Classical Annealing Processes. [Phys. Rev. Lett. 101, 130504 (2008).](https://journals.aps.org/prl/abstract/10.1103/PhysRevLett.101.130504)\n",
    "\n",
Q
Quleaf 已提交
395
    "[4] Wang, Y., Li, G. & Wang, X. Variational quantum Gibbs state preparation with a truncated Taylor series. [Phys. Rev. A 16, 054035 (2021).](https://journals.aps.org/prapplied/abstract/10.1103/PhysRevApplied.16.054035)"
Q
Quleaf 已提交
396 397 398 399 400
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
Q
Quleaf 已提交
401
   "display_name": "Python 3.7.13 ('py3.7_pq2.2.1')",
Q
Quleaf 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414
   "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",
Q
Quleaf 已提交
415
   "version": "3.7.13"
Q
Quleaf 已提交
416 417 418 419 420 421 422 423 424 425 426 427
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
Q
Quleaf 已提交
428
   "toc_window_display": false
Q
Quleaf 已提交
429 430 431 432 433
  },
  "vscode": {
   "interpreter": {
    "hash": "4e4e2eb86ad73936e915e7c7629a18a8ca06348106cf3e66676b9578cb1a47dd"
   }
Q
Quleaf 已提交
434 435 436 437 438
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}