SSVQE_EN.ipynb 15.4 KB
Newer Older
Q
Quleaf 已提交
1 2 3 4
{
 "cells": [
  {
   "cell_type": "markdown",
Q
Quleaf 已提交
5
   "metadata": {},
Q
Quleaf 已提交
6 7 8 9 10 11 12 13 14 15
   "source": [
    "# Subspace-search Variational Quantum Eigensolver\n",
    "\n",
    "<em> Copyright (c) 2021 Institute for Quantum Computing, Baidu Inc. All Rights Reserved. </em>\n",
    "\n",
    "## Overview\n",
    "\n",
    "- In this tutorial, we will show how to train a quantum neural network (QNN) through Paddle Quantum to find the entire energy spectrum of a quantum system.\n",
    "\n",
    "- First, import the following packages."
Q
Quleaf 已提交
16
   ]
Q
Quleaf 已提交
17 18 19
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
20
   "execution_count": 1,
Q
Quleaf 已提交
21 22 23 24 25
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-04-30T09:12:27.747028Z",
     "start_time": "2021-04-30T09:12:25.171248Z"
    }
Q
Quleaf 已提交
26 27 28 29 30 31 32 33 34 35 36 37
   },
   "outputs": [],
   "source": [
    "import numpy\n",
    "from numpy import pi as PI\n",
    "import paddle \n",
    "import paddle_quantum\n",
    "from paddle import matmul\n",
    "from paddle_quantum.ansatz import Circuit\n",
    "from paddle_quantum.qinfo import random_pauli_str_generator, pauli_str_to_matrix\n",
    "from paddle_quantum.linalg import dagger"
   ]
Q
Quleaf 已提交
38 39 40
  },
  {
   "cell_type": "markdown",
Q
Quleaf 已提交
41
   "metadata": {},
Q
Quleaf 已提交
42 43 44 45 46 47 48 49 50 51
   "source": [
    "## Background\n",
    "\n",
    "- Variational Quantum Eigensolver (VQE) [1-3] is one of the most promising applications for near-term quantum computing. One of the its powerful versions is SSVQE [4], which can be used to find the ground state and the **excited state** of a physical system's Hamiltonian. Mathematically, one can interpret it as solving the eigenvalues and eigenvectors of a Hermitian matrix. The set of eigenvalues of the Hamiltonian is called the energy spectrum.\n",
    "- Next, we will use a brief example to demonstrate how to solve this problem by training a QNN, that is, to solve the energy spectrum of a given Hamiltonian $H$.\n",
    "\n",
    "## Hamiltonian \n",
    "\n",
    "- For a specific molecule that needs to be analyzed, we need its geometry, charge, and spin multiplicity to obtain the Hamiltonian (in Pauli products form) describing the system. Specifically, through our built-in quantum chemistry toolkit, fermionic-to-qubit mapping technology can be used to output the qubit Hamiltonian.\n",
    "- As a simple demonstration of SSVQE, we provide a random 2-qubit Hamiltonian."
Q
Quleaf 已提交
52
   ]
Q
Quleaf 已提交
53 54 55
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
56
   "execution_count": 2,
Q
Quleaf 已提交
57 58
   "metadata": {
    "ExecuteTime": {
Q
Quleaf 已提交
59 60
     "end_time": "2021-04-30T09:12:27.773417Z",
     "start_time": "2021-04-30T09:12:27.752568Z"
Q
Quleaf 已提交
61
    }
Q
Quleaf 已提交
62 63 64 65 66 67
   },
   "outputs": [],
   "source": [
    "N = 2  # Number of qubits\n",
    "SEED = 14  # Fixed random seed"
   ]
Q
Quleaf 已提交
68 69 70
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
71 72 73 74 75 76 77
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-04-30T09:12:27.804177Z",
     "start_time": "2021-04-30T09:12:27.779339Z"
    }
   },
Q
Quleaf 已提交
78 79
   "outputs": [
    {
Q
Quleaf 已提交
80
     "name": "stdout",
Q
Quleaf 已提交
81
     "output_type": "stream",
Q
Quleaf 已提交
82 83
     "text": [
      "Random Hamiltonian in Pauli string format = \n",
Q
Quleaf 已提交
84
      " [[0.9152074787317819, 'x1,y0'], [-0.2717604556798945, 'z0'], [0.3628495008719168, 'x0'], [-0.5050129214094752, 'x1'], [-0.6971554357833791, 'y0,x1'], [0.8651151857574237, 'x0,y1'], [0.7409989105435002, 'y0'], [-0.39981603921243236, 'y0'], [0.06862640764702, 'z0'], [-0.7647553733438246, 'y1']]\n"
Q
Quleaf 已提交
85 86 87
     ]
    }
   ],
Q
Quleaf 已提交
88 89 90 91 92 93 94 95 96 97
   "source": [
    "# Generate random Hamiltonian represented by Pauli string\n",
    "numpy.random.seed(SEED)\n",
    "hamiltonian = random_pauli_str_generator(N, terms=10)\n",
    "print(\"Random Hamiltonian in Pauli string format = \\n\", hamiltonian)\n",
    "\n",
    "# Generate matrix representation of Hamiltonian\n",
    "complex_dtype = paddle_quantum.get_dtype()\n",
    "H = pauli_str_to_matrix(hamiltonian, N).astype(complex_dtype)"
   ]
Q
Quleaf 已提交
98 99 100
  },
  {
   "cell_type": "markdown",
Q
Quleaf 已提交
101
   "metadata": {},
Q
Quleaf 已提交
102 103 104 105 106 107
   "source": [
    "## Building a quantum neural network\n",
    "\n",
    "- To implement SSVQE, we first need to design a QNN $U(\\theta)$ (parameterized quantum circuit). In this tutorial, we provide a predefined universal quantum circuit template suitable for 2 qubits. Theoretically, this template has enough expressibility to simulate arbitrary 2-qubit unitary operation [5]. The specific implementation requires 3 $CNOT$ gates plus 15 single-qubit rotation gates $\\in \\{R_y, R_z\\}$.\n",
    "\n",
    "- One can randomly initialize the QNN parameters ${\\bf{\\vec{\\theta }}}$ containing 15 parameters."
Q
Quleaf 已提交
108
   ]
Q
Quleaf 已提交
109 110 111
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
112 113 114 115 116 117 118 119
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-04-30T09:12:27.822250Z",
     "start_time": "2021-04-30T09:12:27.809696Z"
    }
   },
   "outputs": [],
Q
Quleaf 已提交
120
   "source": [
Q
Quleaf 已提交
121
    "def U_theta(num_qubits: int) -> Circuit:\n",
Q
Quleaf 已提交
122 123 124 125
    "    \"\"\"\n",
    "    U_theta\n",
    "    \"\"\"\n",
    "    # Initialize the quantum neural network according to the number of qubits/network width\n",
Q
Quleaf 已提交
126
    "    cir = Circuit(num_qubits)\n",
Q
Quleaf 已提交
127 128
    "    \n",
    "    # Call the built-in quantum neural network template\n",
Q
Quleaf 已提交
129
    "    cir.universal_two_qubits([0, 1])\n",
Q
Quleaf 已提交
130
    "\n",
Q
Quleaf 已提交
131 132
    "    # Return the circuit of the quantum neural network\n",
    "    return cir"
Q
Quleaf 已提交
133
   ]
Q
Quleaf 已提交
134 135 136
  },
  {
   "cell_type": "markdown",
Q
Quleaf 已提交
137
   "metadata": {},
Q
Quleaf 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151
   "source": [
    "## Training model and loss function\n",
    "\n",
    "- After setting up the Hamiltonian and the quantum neural network architecture, we will further define the parameters to be trained, the loss function and optimization methods. For a detailed inspection of the theory of SSVQE, please refer to the original paper [4].\n",
    "\n",
    "- By acting the quantum neural network $U(\\theta)$ on a set of orthogonal initial states (one can take the computational basis $\\{|00\\rangle, |01\\rangle, |10\\rangle, |11 \\rangle \\}$), we will get the output states $\\{\\left| {\\psi_1 \\left( {\\bf{\\theta }} \\right)} \\right\\rangle, \\left| {\\psi_2 \\left( {\\bf{\\theta }} \\right)} \\right\\rangle, \\left| {\\psi_3 \\left( {\\bf{\\theta }} \\right)} \\right\\rangle, \\left| {\\psi_4 \\left( {\\bf{\\theta }} \\right)} \\right\\rangle \\}$.\n",
    "\n",
    "- Further, the loss function in the SSVQE model generally consists of expectation value of each output quantum state $\\left| {\\psi_k \\left( {\\bf{\\theta }} \\right)} \\right\\rangle$ given the Hamiltonian $H$. More specifically, it's the weighted summation of the energy expectation value. In this example, the default weight vector is $\\vec{w} = [4, 3, 2, 1]$.\n",
    "\n",
    "- The loss function is defined as:\n",
    "\n",
    "$$\n",
    "\\mathcal{L}(\\boldsymbol{\\theta}) = \\sum_{k=1}^{2^n}w_k*\\left\\langle {\\psi_k \\left( {\\bf{\\theta }} \\right)} \\right|H\\left| {\\psi_k \\left( {\\bf{\\theta }} \\right)} \\right\\rangle. \\tag{1}\n",
    "$$"
Q
Quleaf 已提交
152
   ]
Q
Quleaf 已提交
153 154 155
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
156 157 158 159 160 161 162 163
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-04-30T09:12:28.432737Z",
     "start_time": "2021-04-30T09:12:28.423798Z"
    }
   },
   "outputs": [],
Q
Quleaf 已提交
164
   "source": [
Q
Quleaf 已提交
165
    "class Net(paddle.nn.Layer):\n",
Q
Quleaf 已提交
166
    "    def __init__(self, num_qubits: int):\n",
Q
Quleaf 已提交
167 168
    "        super(Net, self).__init__()\n",
    "        \n",
Q
Quleaf 已提交
169 170 171
    "        # Build quantum neural network\n",
    "        self.cir = U_theta(num_qubits)\n",
    "\n",
Q
Quleaf 已提交
172 173 174 175
    "    # Define loss function and forward propagation mechanism\n",
    "    def forward(self, H, N):\n",
    "        \n",
    "        # Calculate the loss function\n",
Q
Quleaf 已提交
176
    "        U = self.cir.unitary_matrix()\n",
Q
Quleaf 已提交
177
    "        loss_struct = paddle.real(matmul(matmul(dagger(U), H), U))\n",
Q
Quleaf 已提交
178 179 180
    "\n",
    "        # Enter the computational basis to calculate the expected value \n",
    "        # which is equivalent to taking the diagonal element of U^dagger*H*U\n",
Q
Quleaf 已提交
181 182 183
    "        loss_components = []\n",
    "        for i in range(len(loss_struct)):\n",
    "            loss_components.append(loss_struct[i][i])\n",
Q
Quleaf 已提交
184 185
    "        \n",
    "        # Weighted summation of loss function\n",
Q
Quleaf 已提交
186 187 188 189
    "        loss = 0\n",
    "        for i in range(len(loss_components)):\n",
    "            weight = 4 - i\n",
    "            loss += weight * loss_components[i]\n",
Q
Quleaf 已提交
190
    "        \n",
Q
Quleaf 已提交
191 192
    "        return loss, loss_components, self.cir"
   ]
Q
Quleaf 已提交
193 194 195
  },
  {
   "cell_type": "markdown",
Q
Quleaf 已提交
196
   "metadata": {},
Q
Quleaf 已提交
197 198 199
   "source": [
    "## Hyper-parameters\n",
    "\n",
Q
Quleaf 已提交
200
    "Before training the quantum neural network, we also need to set up several hyper-parameters, mainly the learning rate LR, the number of iterations ITR. Here we set the learning rate to be LR = 0.3 and the number of iterations ITR = 100. One can adjust these hyper-parameters accordingly and check how they influence the training performance."
Q
Quleaf 已提交
201
   ]
Q
Quleaf 已提交
202 203 204
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
205
   "execution_count": 8,
Q
Quleaf 已提交
206 207
   "metadata": {
    "ExecuteTime": {
Q
Quleaf 已提交
208 209
     "end_time": "2021-04-30T09:12:29.579180Z",
     "start_time": "2021-04-30T09:12:29.575632Z"
Q
Quleaf 已提交
210
    }
Q
Quleaf 已提交
211 212 213 214 215 216
   },
   "outputs": [],
   "source": [
    "ITR = 100  # Set the total number of iterations of training\n",
    "LR = 0.3  # Set the learning rate"
   ]
Q
Quleaf 已提交
217 218 219
  },
  {
   "cell_type": "markdown",
Q
Quleaf 已提交
220
   "metadata": {},
Q
Quleaf 已提交
221 222 223
   "source": [
    "## Training process\n",
    "\n",
Q
Quleaf 已提交
224
    "- After setting all the parameters of SSVQE model, we need to convert all the data into Tensor in the PaddlePaddle, and then train the quantum neural network.\n",
Q
Quleaf 已提交
225
    "- We use Adam Optimizer in training, and one can also call other optimizers provided in PaddlePaddle."
Q
Quleaf 已提交
226
   ]
Q
Quleaf 已提交
227 228 229
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
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
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-04-30T09:12:44.010556Z",
     "start_time": "2021-04-30T09:12:41.952650Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "iter: 10 loss: -4.5668\n",
      "iter: 20 loss: -5.3998\n",
      "iter: 30 loss: -5.6210\n",
      "iter: 40 loss: -5.8872\n",
      "iter: 50 loss: -5.9246\n",
      "iter: 60 loss: -5.9471\n",
      "iter: 70 loss: -5.9739\n",
      "iter: 80 loss: -5.9833\n",
      "iter: 90 loss: -5.9846\n",
      "iter: 100 loss: -5.9848\n"
     ]
    }
   ],
Q
Quleaf 已提交
255
   "source": [
Q
Quleaf 已提交
256
    "paddle.seed(SEED)\n",
Q
Quleaf 已提交
257
    "\n",
Q
Quleaf 已提交
258 259
    "# We need to convert numpy.ndarray to Tensor supported in Paddle\n",
    "hamiltonian = paddle.to_tensor(H)\n",
Q
Quleaf 已提交
260
    "\n",
Q
Quleaf 已提交
261
    "# Determine the parameter dimension of the network\n",
Q
Quleaf 已提交
262
    "net = Net(N)\n",
Q
Quleaf 已提交
263
    "\n",
Q
Quleaf 已提交
264 265 266 267 268 269 270 271
    "# We use Adam optimizer for better performance\n",
    "# One can change it to SGD or RMSprop.\n",
    "opt = paddle.optimizer.Adam(learning_rate=LR, parameters=net.parameters())\n",
    "\n",
    "# Optimization loop\n",
    "for itr in range(1, ITR + 1):\n",
    "\n",
    "    # Forward propagation calculates the loss function and returns the estimated energy spectrum\n",
Q
Quleaf 已提交
272
    "    loss, loss_components, cir = net(hamiltonian, N)\n",
Q
Quleaf 已提交
273 274 275 276 277 278 279 280
    "\n",
    "    # Under the dynamic graph mechanism, use back propagation to minimize the loss function\n",
    "    loss.backward()\n",
    "    opt.minimize(loss)\n",
    "    opt.clear_grad()\n",
    "\n",
    "    # Print training results\n",
    "    if itr% 10 == 0:\n",
Q
Quleaf 已提交
281 282
    "        print('iter:', itr,'loss:','%.4f'% loss.numpy()[0])"
   ]
Q
Quleaf 已提交
283 284 285
  },
  {
   "cell_type": "markdown",
Q
Quleaf 已提交
286
   "metadata": {},
Q
Quleaf 已提交
287 288 289 290 291 292 293
   "source": [
    "## Benchmarking\n",
    "\n",
    "We have now completed the training of the quantum neural network, and we will verify the results by comparing them with theoretical values.\n",
    "- The theoretical Hamiltonian eigenvalues are solved by the linear algebra package in NumPy;\n",
    "- We compare the energy of each energy level obtained by training QNN with the theoretical value.\n",
    "- It can be seen that the training output is very close to the exact value."
Q
Quleaf 已提交
294
   ]
Q
Quleaf 已提交
295 296 297
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-04-30T09:12:45.991342Z",
     "start_time": "2021-04-30T09:12:45.976287Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The estimated ground state energy is:  [-2.1876235]\n",
      "The theoretical ground state energy is:  -2.187902\n",
      "The estimated 1st excited state energy is: [-0.13721023]\n",
      "The theoretical 1st excited state energy is: -0.13704127073287964\n",
      "The estimated 2nd excited state energy is: [0.85251486]\n",
      "The theoretical 2nd excited state energy is: 0.8523274064064026\n",
      "The estimated 3rd excited state energy is: [1.4723194]\n",
      "The theoretical 3rd excited state energy is: 1.4726158380508423\n"
     ]
    }
   ],
Q
Quleaf 已提交
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
   "source": [
    "def output_ordinalvalue(num):\n",
    "    r\"\"\"\n",
    "    Convert to ordinal value\n",
    "\n",
    "    Args:\n",
    "        num (int): input number\n",
    "\n",
    "    Return:\n",
    "        (str): output ordinal value\n",
    "    \"\"\"\n",
    "    if num == 1:\n",
    "        return str(num) + \"st\"\n",
    "    elif num == 2:\n",
    "        return str(num) + \"nd\"\n",
    "    elif num == 3:\n",
    "        return str(num) + \"rd\"\n",
    "    else:\n",
    "        return str(num) + 'th'\n",
    "\n",
    "for i in range(len(loss_components)):\n",
    "    if i == 0:\n",
    "        print('The estimated ground state energy is: ', loss_components[i].numpy())\n",
    "        print('The theoretical ground state energy is: ', numpy.linalg.eigh(H)[0][i])\n",
    "    else:\n",
    "        print('The estimated {} excited state energy is: {}'.format(\n",
    "            output_ordinalvalue(i), loss_components[i].numpy())\n",
    "        )\n",
    "        print('The theoretical {} excited state energy is: {}'.format(\n",
    "            output_ordinalvalue(i), numpy.linalg.eigh(H)[0][i])\n",
    "        )"
Q
Quleaf 已提交
352
   ]
Q
Quleaf 已提交
353 354 355
  },
  {
   "cell_type": "markdown",
Q
Quleaf 已提交
356
   "metadata": {},
Q
Quleaf 已提交
357 358 359 360 361 362 363 364 365 366 367 368 369 370
   "source": [
    "_______\n",
    "\n",
    "## References\n",
    "\n",
    "[1] Peruzzo, A. et al. A variational eigenvalue solver on a photonic quantum processor. [Nat. Commun. 5, 4213 (2014).](https://www.nature.com/articles/ncomms5213)\n",
    "\n",
    "[2] McArdle, S., Endo, S., Aspuru-Guzik, A., Benjamin, S. C. & Yuan, X. Quantum computational chemistry. [Rev. Mod. Phys. 92, 015003 (2020).](https://journals.aps.org/rmp/abstract/10.1103/RevModPhys.92.015003)\n",
    "\n",
    "[3] Cao, Y. et al. Quantum chemistry in the age of quantum computing. [Chem. Rev. 119, 10856–10915 (2019).](https://pubs.acs.org/doi/abs/10.1021/acs.chemrev.8b00803)\n",
    "\n",
    "[4] Nakanishi, K. M., Mitarai, K. & Fujii, K. Subspace-search variational quantum eigensolver for excited states. [Phys. Rev. Res. 1, 033062 (2019).](https://journals.aps.org/prresearch/pdf/10.1103/PhysRevResearch.1.033062)\n",
    "\n",
    "[5] Vatan, F. & Williams, C. Optimal quantum circuits for general two-qubit gates. [Phys. Rev. A 69, 032315 (2004).](https://journals.aps.org/pra/abstract/10.1103/PhysRevA.69.032315)"
Q
Quleaf 已提交
371
   ]
Q
Quleaf 已提交
372 373 374
  }
 ],
 "metadata": {
Q
Quleaf 已提交
375 376 377
  "interpreter": {
   "hash": "1ba3360425d54dc61cc146cb8ddc529b6d51be6719655a3ca16cefddffc9595a"
  },
Q
Quleaf 已提交
378
  "kernelspec": {
Q
Quleaf 已提交
379 380
   "display_name": "Python 3.8.10 64-bit ('paddle_quantum_test': conda)",
   "name": "python3"
Q
Quleaf 已提交
381 382 383 384 385 386 387 388 389 390 391
  },
  "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 已提交
392
   "version": "3.8.13"
Q
Quleaf 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
  },
  "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,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
Q
Quleaf 已提交
410
}