QGAN_EN.ipynb 39.2 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 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
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Quantum Generative Adversarial Network"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<em> Copyright (c) 2021 Institute for Quantum Computing, Baidu Inc. All Rights Reserved. </em>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Classical Generative Adversarial Network"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Introduction to Generative Adversarial Network"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Generative Adversarial Network (GAN) is a generative model, which is a breakthrough in deep learning in recent years [1]. It contains two parts: the generator $G$ and the discriminator $D$. The generator accepts a random noise signal and uses it as input to generate the data we expect. The discriminator evaluates the received data $x$ and outputs a probability $P(x)$ that the data $x$ is real."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Nash Equilibrium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here we use the idea of Nash equilibrium to discuss the convergence problem of GAN.\n",
    "\n",
    "Nash equilibrium refers to a non-cooperative game involving two or more participants, assuming that each participant knows other participants' equilibrium strategies. No participant can change one's strategy to benefit. In the game theory, if each participant chooses his strategy, and no player can benefit by changing the strategy while other participants remain the same, then the current set of strategy choices and their corresponding results constitute Nash Equilibrium.\n",
    "\n",
    "We can regard the training process of GAN as a game process between generator and discriminator. No matter what the generator's strategy is, the best strategy of the discriminator is to try to distinguish the real data and the generated data. And regardless of the strategy of the discriminator, the best strategy for the generator is to make the discriminator unable to distinguish. This game is a zero-sum game, which is one of non-cooperative game. That is, one party gains while the other party must lose. Therefore, there exists the Nash equilibrium strategy in the game between the generator and the discriminator. When there are enough samples of real data and the learning ability of both parties is strong enough, a Nash equilibrium point will eventually be reached. **The generator has the ability to generate real data, and the discriminator can no longer distinguish between generated data and real data.** \n",
    "\n",
    "GAN adopts the idea of Nash equilibrium. In GAN, the generator and the discriminator are playing a non-cooperative game. No matter what strategies the generator adopts, the best strategy of the discriminator is to discriminate as much as possible; and no matter what strategies the discriminator adopts, the best strategy of the generator is to make the discriminator unable to discriminate as much as possible. Therefore, the two parties' strategic combination in the game and the corresponding results constitute the Nash equilibrium. **When the Nash equilibrium is reached, the generator can to generate real data, and the discriminator cannot distinguish between the generated data and the real data**."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Optimization goal"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In GAN, we want to get an excellent generator (but only an excellent discriminator can accurately determine whether the generator is excellent). Our training's ideal result is that the discriminator cannot identify whether the data comes from real data or generated data.\n",
    "\n",
    "Therefore, our objective function is as follows:\n",
    "\n",
    "$$\n",
    "\\min_{G}\\max_{D} V(G,D)= \\min_{G}\\max_{D}\\mathbb{E}_{x\\sim P_{data}}[\\log D(x)] +\\mathbb{E}_{z\\sim P_{z}}[\\log(1-D(G(z)))]. \\tag{1}\n",
    "$$\n",
    "\n",
    "Here, $G$ represents the parameters of the generator and $D$ represents the parameters of the discriminator. In the actual process, the alternate training method is usually adopted; that is, first fix $G$, train $D$, then fix $D$, train $G$, and repeat. When the two's performance is sufficient, the model will converge, and the two will reach the Nash equilibrium."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Advantages"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- Compared with other generative models, GAN generates better results.\n",
    "- Theoretically, as long as it is a differentiable function, it can be used to build generators and discriminators, so it can be combined with deep neural networks to make deep generative models.\n",
    "- Compared with other generative models, GAN does not rely on prior assumptions, and we do not need to assume the probability distribution and law of the data in advance.\n",
    "- The form of data generated by GAN is also very simple, just forward propagation through the generator."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Disadvantages"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- GAN does not require pre-modeling. Too much freedom makes training difficult to converge and unstable.\n",
    "- GAN has a vanishing gradient problem. In this case, there is no loss in the training of the discriminator, so there is no adequate gradient information to pass back to the generator to optimize itself.\n",
    "- There may be a problem of model collapse in the learning process of GAN. The generator degenerates, always generating the same sample points, and cannot continue learning. The discriminator also points to similar sample points in similar directions, and the model parameters are no longer updated, but the actual effect is feeble."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Quantum Generative Adversarial Network"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The quantum generative adversarial network is similar to the classical one, except that it is no longer used to generate classical data but generate quantum states [2-3]. In practice, if we have a quantum state, it will collapse to a certain eigenstate after observation and cannot be restored to the previous quantum state. Therefore, if we have a method that generates many identical (or similar) quantum states based on the existing target quantum state, it will be very convenient for our experiments.\n",
    "\n",
    "Assuming that our existing target quantum states all come from a mixed state, they belong to the same ensemble, and their density operator is $\\rho$. Then we need to have a generator $G$ whose input is noise data, denoted by the ensemble $\\rho_{z}=\\sum_{i}p_{i}|z_{i}\\rangle\\langle z_ {i}|$. Therefore, we take out a random noise sample $|z_{i}\\rangle$ every time and get the generated quantum state $|x\\rangle=G|z_{i}\\rangle$ after passing through the generator. We expect the generated $| x\\rangle$ that is close to the target quantum state $\\rho$.\n",
    "\n",
    "It is worth noting that for the ensemble of the target state and the ensemble of the noise data mentioned above, we think that there exists a physical device that can generate a quantum state from the ensemble. According to quantum physics, We can get a genuinely random quantum state every time. However, in computer programs, we need to simulate this process.\n",
    "\n",
    "We expect the discriminator to judge whether the quantum state we input is an existing target state or a generated quantum state. This process needs to be given by measurement.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## A simple example"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem Description"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For simplicity, we assume that the existing target quantum state is a pure state, and the input state for the generator is $|0\\rangle$.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The circuit to prepare the existing target quantum state:\n",
    "![QGAN-fig-target_state](figures/QGAN-fig-target_state.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The circuit of the generator is:\n",
    "![QGAN-fig-generator](figures/QGAN-fig-generator.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The circuit of the discriminator is:\n",
    "![QGAN-fig-discriminator](figures/QGAN-fig-discriminator.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By measuring the quantum state output by the discriminator, we can get the probability of judging the target state as the target state $P_{T}$ and the probability of judging the generated state as the target state $P_{G}$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Specific process"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Assuming that the existing target quantum state is $|\\psi\\rangle$, the quantum state generated by the generator is $|x\\rangle=G|00\\rangle$ (the generator uses a two-qubit circuit, of which the 0th qubit is a generated quantum state).\n",
    "\n",
    "The discriminator discriminates the data and obtains the quantum state $|\\phi\\rangle$ when the input is the target state, $|\\phi\\rangle=D(|\\psi\\rangle\\otimes |00\\rangle)$; When the input is generated state, $|\\phi\\rangle=D(G\\otimes I)|000\\rangle$.\n",
    "\n",
    "For the quantum state obtained by the discriminator, we also need to use the Pauli Z gate to measure the third qubit so as to obtain the judgment result of the input quantum state by the discriminator (that is, the probability that the discriminator thinks the input is the target state). First there is $M_{z}=I\\otimes I\\otimes\\sigma_{z}$, and the measurement result is $\\text{disc_output}=\\langle\\phi|M_{z}|\\phi\\rangle$, so the probability of the measurement that the result is the target state is $P=(\\text{disc_output}+1)/2$.\n",
    "\n",
    "We define the loss function of the discriminator as $\\mathcal{L}_{D}=P_{G}(\\text{gen_theta}, \\text{disc_phi})-P_{T}(\\text{disc_phi})$, The loss function of the generator is $\\mathcal{L}_{G}=-P_{G}(\\text{gen_theta}, \\text{disc_phi})$. Here, $P_{G}$ and $P_{T}$ are the expressions of $P=(\\text{disc_output}+1)/2$, when the input quantum state is the generated state and the target state, respectively. gen_theta and disc_phi are the parameters of the generator and discriminator circuits.\n",
    "\n",
    "So we only need to optimize the objective function $\\min_{\\text{disc_phi}}\\mathcal{L}_{D}$ and $\\min_{\\text{gen_theta}}\\mathcal{L}_{G}$ respectively. The discriminator and generator can be alternately trained."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Paddle Quantum Implementation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First import the relevant packages."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-09T10:49:41.308412Z",
     "start_time": "2021-01-09T10:49:38.115680Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import paddle\n",
    "from paddle_quantum.circuit import UAnsatz\n",
    "from paddle_quantum.utils import partial_trace, dagger, state_fidelity\n",
Q
Quleaf 已提交
236
    "from tqdm import tqdm"
Q
Quleaf 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then define our network model QGAN."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-09T10:49:43.091496Z",
     "start_time": "2021-01-09T10:49:43.060879Z"
    }
   },
   "outputs": [],
   "source": [
Q
Quleaf 已提交
257
    "class QGAN(paddle.nn.Layer):\n",
Q
Quleaf 已提交
258 259 260 261 262 263
    "    def __init__(self):\n",
    "        super(QGAN, self).__init__()\n",
    "        \n",
    "        # The angle used to prepare the target quantum state\n",
    "        target_omega_0 = 0.9 * np.pi\n",
    "        target_omega_1 = 0.2 * np.pi\n",
Q
Quleaf 已提交
264 265
    "        self.target_omega = paddle.to_tensor(\n",
    "            np.array([target_omega_0, target_omega_1], np.float64))\n",
Q
Quleaf 已提交
266 267 268
    "        \n",
    "        # Generator and discriminator circuit parameters\n",
    "        self.gen_theta = self.create_parameter([9], \n",
Q
Quleaf 已提交
269 270
    "        dtype=\"float64\", default_initializer=paddle.nn.initializer.Uniform(\n",
    "                            low=0.0, high=np.pi))\n",
Q
Quleaf 已提交
271
    "        self.disc_phi = self.create_parameter([9], \n",
Q
Quleaf 已提交
272 273
    "        dtype=\"float64\", default_initializer=paddle.nn.initializer.Uniform(\n",
    "                            low=0.0, high=np.pi))\n",
Q
Quleaf 已提交
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
    "        \n",
    "        # Prepare target quantum state\n",
    "        cir = UAnsatz(3)\n",
    "        cir.ry(self.target_omega[0], 0)\n",
    "        cir.rz(self.target_omega[1], 0)\n",
    "        self.target_state = cir.run_state_vector()\n",
    "\n",
    "    def generator(self, theta):\n",
    "        \"\"\"\n",
    "        Generator quantum circuit\n",
    "        \"\"\"\n",
    "        cir = UAnsatz(3)\n",
    "        cir.u3(*theta[:3], 0)\n",
    "        cir.u3(*theta[3:6], 1)\n",
    "        cir.cnot([0, 1])\n",
    "        cir.u3(*theta[6:], 0)\n",
    "\n",
    "        return cir\n",
    "\n",
    "    def discriminator(self, phi):\n",
    "        \"\"\"\n",
    "        Quantum circuit of the discriminator\n",
    "        \"\"\"\n",
    "        cir = UAnsatz(3)\n",
    "        cir.u3(*phi[:3], 0)\n",
    "        cir.u3(*phi[3:6], 2)\n",
    "        cir.cnot([0, 2])\n",
    "        cir.u3(*phi[6:], 0)\n",
    "\n",
    "        return cir\n",
    "\n",
    "    def disc_target_as_target(self):\n",
Q
Quleaf 已提交
306 307 308 309 310 311
    "        \"\"\"\n",
    "        The probability that the discriminator judges the target state as the target state\n",
    "        \"\"\"\n",
    "        # Discriminator circuit\n",
    "        cir = self.discriminator(self.disc_phi)\n",
    "        cir.run_state_vector(self.target_state)\n",
Q
Quleaf 已提交
312
    "\n",
Q
Quleaf 已提交
313 314 315 316 317
    "        # The judgment result of the discriminator on the target state\n",
    "        target_disc_output = cir.expecval([[1.0,'z2']])\n",
    "        prob_as_target = (target_disc_output + 1) / 2\n",
    "\n",
    "        return prob_as_target\n",
Q
Quleaf 已提交
318 319
    "\n",
    "    def disc_gen_as_target(self):\n",
Q
Quleaf 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333
    "        \"\"\"\n",
    "        The probability that the discriminator judges the generated state as the target state\n",
    "        \"\"\"\n",
    "        # Get the quantum state generated by the generator\n",
    "        gen_state = self.generator(\n",
    "                 self.gen_theta).run_state_vector()\n",
    "        # Discriminator circuit\n",
    "        cir = self.discriminator(self.disc_phi)\n",
    "        cir.run_state_vector(gen_state)\n",
    "        # The judgment result of the discriminator on the generated state\n",
    "        gen_disc_output = cir.expecval([[1.0,'z2']])\n",
    "        prob_as_target = (gen_disc_output + 1) / 2\n",
    "\n",
    "        return prob_as_target\n",
Q
Quleaf 已提交
334 335
    "\n",
    "    def forward(self, model_name):\n",
Q
Quleaf 已提交
336 337 338 339 340 341 342 343
    "        if model_name =='gen':\n",
    "         # Calculate the loss function of the generator, the interval of the loss value is [-1, 0],\n",
    "         # 0 means extremely poor generation effect, -1 means excellent generation effect\n",
    "            loss = -1 * self.disc_gen_as_target()\n",
    "        else:\n",
    "         # Calculate the loss function of the discriminator, the loss value range is [-1, 1],\n",
    "         # -1 means perfect distinction, 0 means indistinguishable, 1 means inverted distinction\n",
    "            loss = self.disc_gen_as_target() - self.disc_target_as_target()\n",
Q
Quleaf 已提交
344
    "\n",
Q
Quleaf 已提交
345
    "        return loss\n",
Q
Quleaf 已提交
346 347
    "\n",
    "    def get_target_state(self):\n",
Q
Quleaf 已提交
348 349 350 351 352 353 354
    "        \"\"\"\n",
    "        Get the density matrix representation of the target state\n",
    "        \"\"\"\n",
    "        state = self.target_state\n",
    "        state = paddle.reshape(state, [1] + state.shape)\n",
    "        density_matrix = paddle.matmul(dagger(state), state)\n",
    "        state = partial_trace(density_matrix, 2, 4, 2)\n",
Q
Quleaf 已提交
355
    "\n",
Q
Quleaf 已提交
356
    "        return state.numpy()\n",
Q
Quleaf 已提交
357 358
    "\n",
    "    def get_generated_state(self):\n",
Q
Quleaf 已提交
359 360 361 362 363 364 365 366
    "        \"\"\"\n",
    "        Get the density matrix representation of the generated state\n",
    "        \"\"\"\n",
    "        state = self.generator(\n",
    "             self.gen_theta).run_state_vector()\n",
    "        state = paddle.reshape(state, [1] + state.shape)\n",
    "        density_matrix = paddle.matmul(dagger(state), state)\n",
    "        state = partial_trace(density_matrix, 2, 4, 2)\n",
Q
Quleaf 已提交
367
    "\n",
Q
Quleaf 已提交
368
    "        return state.numpy()"
Q
Quleaf 已提交
369 370 371 372 373 374
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Q
Quleaf 已提交
375
    "Next, we use PaddlePaddle to train our model."
Q
Quleaf 已提交
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-01-09T10:52:15.221809Z",
     "start_time": "2021-01-09T10:49:55.308722Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
Q
Quleaf 已提交
392
      "Training: 100%|#################################################| 1050/1050 [01:31<00:00, 11.51it/s]\n"
Q
Quleaf 已提交
393 394 395 396 397 398 399
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the density matrix of the target state:\n",
Q
Quleaf 已提交
400 401
      "[[0.02447174+0.00000000e+00j 0.125     +9.08178160e-02j]\n",
      " [0.125     -9.08178160e-02j 0.97552826+5.16498656e-18j]] \n",
Q
Quleaf 已提交
402 403
      "\n",
      "the density matrix of the generated state:\n",
Q
Quleaf 已提交
404 405
      "[[0.0244643 -5.29696618e-19j 0.12657544+8.85689120e-02j]\n",
      " [0.12657544-8.85689120e-02j 0.9755357 -2.82739625e-19j]] \n",
Q
Quleaf 已提交
406
      "\n",
Q
Quleaf 已提交
407
      "the distance between these two quantum states is 1.5079277656078345e-05 \n",
Q
Quleaf 已提交
408
      "\n",
Q
Quleaf 已提交
409
      "the fidelity between these two quantum states is 0.9999962306522913\n"
Q
Quleaf 已提交
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
     ]
    }
   ],
   "source": [
    "# Learning rate\n",
    "LR = 0.1\n",
    "# Total number of iterations\n",
    "ITR = 15\n",
    "# In each iteration, the number of iterations of the discriminator\n",
    "ITR1 = 20\n",
    "# In each iteration, the number of generator iterations\n",
    "ITR2 = 50\n",
    "\n",
    "# Used to record the change of loss value\n",
    "loss_history = list()\n",
Q
Quleaf 已提交
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
    "paddle.seed(18)\n",
    "gan_demo = QGAN()\n",
    "optimizer = paddle.optimizer.SGD(learning_rate=LR, parameters=gan_demo.parameters())\n",
    "pbar = tqdm(desc=\"Training: \", total=ITR * (ITR1 + ITR2), ncols=100, ascii=True)\n",
    "for itr0 in range(ITR):\n",
    "\n",
    "    # Record the change in the loss value of the discriminator\n",
    "    loss_disc_history = list()\n",
    "\n",
    "    # Train the discriminator\n",
    "    for itr1 in range(ITR1):\n",
    "        pbar.update(1)\n",
    "        loss_disc = gan_demo('disc')\n",
    "        loss_disc.backward()\n",
    "        optimizer.minimize(loss_disc, parameters\n",
    "                           =[gan_demo.disc_phi],\n",
    "                           no_grad_set=[gan_demo.gen_theta])\n",
    "        gan_demo.clear_gradients()\n",
    "        loss_disc_history.append(loss_disc.numpy()[0])\n",
    "\n",
    "    # Record the change of the generator loss value\n",
    "    loss_gen_history = list()\n",
    "\n",
    "    # Training generator\n",
    "    for itr2 in range(ITR2):\n",
    "        pbar.update(1)\n",
    "        loss_gen = gan_demo('gen')\n",
    "        loss_gen.backward()\n",
    "        optimizer.minimize(loss_gen, parameters\n",
    "                           =[gan_demo.gen_theta],\n",
    "                           no_grad_set=[gan_demo.disc_phi])\n",
    "        optimizer.clear_grad()\n",
    "        loss_gen_history.append(loss_gen.numpy()[0])\n",
    "\n",
    "    loss_history.append((loss_disc_history, loss_gen_history))\n",
    "pbar.close()\n",
    "\n",
    "# Get the target quantum state\n",
    "target_state = gan_demo.get_target_state()\n",
    "\n",
    "# Get the final quantum state generated by the generator\n",
    "gen_state = gan_demo.get_generated_state()\n",
    "print(\"the density matrix of the target state:\")\n",
    "print(target_state, \"\\n\")\n",
    "print(\"the density matrix of the generated state:\")\n",
    "print(gen_state, \"\\n\")\n",
    "\n",
    "# Calculate the distance between two quantum states,\n",
    "# The distance here is defined as tr[(target_state-gen_state)^2]\n",
    "distance = np.trace(np.matmul(target_state-gen_state,\n",
    "                              target_state-gen_state)).real\n",
    "# Calculate the fidelity of two quantum states\n",
    "fidelity = state_fidelity(target_state, gen_state)\n",
    "print(\"the distance between these two quantum states is\", distance, \"\\n\")\n",
    "print(\"the fidelity between these two quantum states is\", fidelity)"
Q
Quleaf 已提交
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We compare the target quantum state's density matrix $\\rho_\\text{target}$ and  the generated quantum state's density matrix $\\rho_\\text{gen}$ and calculate the distance between them $\\text{tr}[(\\rho_\\text{target}-\\rho_\\text{gen})^2]$ and fidelity. We can know that our generator generates a quantum state very close to the target state."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Visualization of the training process"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, let's observe the change of the discriminator and generator's loss curve during the training process.\n",
    "\n",
    "First install the required packages."
   ]
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
507
   "execution_count": 4,
Q
Quleaf 已提交
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import clear_output\n",
    "!pip install celluloid\n",
    "clear_output()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, we draw the change of the loss curve."
   ]
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
525
   "execution_count": 5,
Q
Quleaf 已提交
526 527 528 529
   "metadata": {},
   "outputs": [
    {
     "data": {
Q
Quleaf 已提交
530
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAZgAAAEXCAYAAACQ3VJYAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAAAmJklEQVR4nO3deZhdVZnv8e+PJASEMHMxJIRECUMEDFAGkRaRSS7QJgzKIBpsoJQW2haQ4dKXRoYrgrbKIw1EpoA0UxokAhKSACJzCgwhCWYgYCcxEOZRw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 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "from celluloid import Camera\n",
    "def draw_pic(loss_history):\n",
    "    fig, axes = plt.subplots(nrows=1, ncols=2)\n",
    "    camera = Camera(fig)\n",
    "    axes[0].set_title(\"discriminator\")\n",
    "    axes[0].set_xlabel(\"disc_iter\")\n",
    "    axes[0].set_ylabel(\"disc_loss\")\n",
    "    axes[0].set_xlim(0, 20)\n",
    "    axes[0].set_ylim(-1, 1)\n",
    "    axes[1].set_title(\"generator\")\n",
    "    axes[1].set_xlabel(\"gen_iter\")\n",
    "    axes[1].set_ylabel(\"gen_loss\")\n",
    "    axes[1].set_xlim(0, 50)\n",
    "    axes[1].set_ylim(-1, 0)\n",
    "    for loss in loss_history:\n",
    "        disc_data, gen_data = loss\n",
    "        disc_x_data = range(0, len(disc_data))\n",
    "        gen_x_data = range(0, len(gen_data))\n",
    "        axes[0].plot(disc_x_data, disc_data, color='red')\n",
    "        axes[1].plot(gen_x_data, gen_data, color='blue')\n",
    "        camera.snap()\n",
    "    animation = camera.animate(interval=600, \n",
    "                               repeat=True, repeat_delay=800)\n",
    "    animation.save(\"./figures/loss.gif\")\n",
    "draw_pic(loss_history)\n",
    "clear_output()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![QGAN-fig-loss](./figures/loss.gif)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this dynamic picture, each frame represents an iterative process. In an iteration, the red line on the left represents the loss curve of the discriminator, and the blue line on the right represents the loss curve of the generator. It can be seen that at the initial stage, the discriminator and generator can gradually learn from a poor discriminant ability and generation ability to a better discrimination ability and generation ability in the current situation. As the learning progresses, the generator’s generation ability is getting stronger and stronger, and the discriminator’s ability is getting stronger and stronger. However, the discriminator can not distinguish the real data and the generated data because the generator has generated data close to real data. At this time, the model has converged."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Q
Quleaf 已提交
589 590
    "_______\n",
    "\n",
Q
Quleaf 已提交
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
    "## References"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[1] Goodfellow, I. J. et al. Generative Adversarial Nets. [Proc. 27th Int. Conf. Neural Inf. Process. Syst. (2014).](https://papers.nips.cc/paper/5423-generative-adversarial-nets)\n",
    "\n",
    "[2] Lloyd, S. & Weedbrook, C. Quantum Generative Adversarial Learning. [Phys. Rev. Lett. 121, 040502 (2018).](https://journals.aps.org/prl/abstract/10.1103/PhysRevLett.121.040502)\n",
    "\n",
    "[3] Benedetti, M., Grant, E., Wossnig, L. & Severini, S. Adversarial quantum circuit learning for pure state approximation. [New J. Phys. 21, (2019).](https://iopscience.iop.org/article/10.1088/1367-2630/ab14b5)\n"
   ]
  }
 ],
 "metadata": {
  "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",
Q
Quleaf 已提交
622
   "version": "3.7.10"
Q
Quleaf 已提交
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
  },
  "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": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}