VSQL_EN.ipynb 26.6 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
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "adjacent-printing",
   "metadata": {},
   "source": [
    "# Variational Shadow Quantum Learning\n",
    "\n",
    "<em> Copyright (c) 2021 Institute for Quantum Computing, Baidu Inc. All Rights Reserved. </em>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "rising-daisy",
   "metadata": {},
   "source": [
    "## Overview\n",
    "\n",
    "In this tutorial, we will discuss the workflow of Variational Shadow Quantum Learning (VSQL) [1] and accomplish a **binary classification** task using VSQL. VSQL is a hybird quantum-classical framework for supervised quantum learning, which utilizes parameterized quantum circuits and classical shadows. Unlike commonly used variational quantum algorithms, the VSQL method extracts \"local\" features from the subspace instead of the whole Hilbert space.\n",
    "\n",
    "### Background\n",
    "\n",
    "We consider a $k$-label classification problem. The input is a set containing $N$ labeled data points $D=\\left\\{(\\mathbf{x}^i, \\mathbf{y}^i)\\right\\}_{i=1}^{N}$, where $\\mathbf{x}^i\\in\\mathbb{R}^{m}$ is the data point and $\\mathbf{y}^i$ is a one-hot vector with length $k$ indicating which category the corresponding data point belongs to. Representing the labels as one-hot vectors is a common choice within the machine learning community. For example, for $k=3$, $\\mathbf{y}^{a}=[1, 0, 0]^\\text{T}, \\mathbf{y}^{b}=[0, 1, 0]^\\text{T}$, and $\\mathbf{y}^{c}=[0, 0, 1]^\\text{T}$ indicate that the $a^{\\text{th}}$, the $b^{\\text{th}}$, and the $c^{\\text{th}}$ data points belong to class 0, class 1, and class 2, respectively. **The learning process aims to train a model $\\mathcal{F}$ to predict the label of every data point as accurately as possible.**\n",
    "\n",
    "The realization of $\\mathcal{F}$ in VSQL is a combination of parameterized local quantum circuits, known as **shadow circuits**, and a classical fully-connected neural network (FCNN). VSQL requires preprocessing to encode classical information into quantum states. After encoding the data, we convolutionally apply a parameterized local quantum circuit $U(\\mathbf{\\theta})$ to qubits in each encoded quantum state, where $\\mathbf{\\theta}$ is the vector of parameters in the circuit. Then, expectation values are obtained via measuring local observables on these qubits. After the measurement, there is an additional classical FCNN for postprocessing.\n",
    "\n",
    "We can write the output of $\\mathcal{F}$, which is obtained from VSQL, as $\\tilde{\\mathbf{y}}^i = \\mathcal{F}(\\mathbf{x}^i)$. Here $\\tilde{\\mathbf{y}}^i$ is a probability distribution, where $\\tilde{y}^i_j$ is the probability of the $i^{\\text{th}}$ data point belonging to the $j^{\\text{th}}$ class. In order to predict the actual label, we calculate the cumulative distance between $\\tilde{\\mathbf{y}}^i$ and $\\mathbf{y}^i$ as the loss function $\\mathcal{L}$ to be optimized:\n",
    "\n",
    "$$\n",
    "\\mathcal{L}(\\mathbf{\\theta}, \\mathbf{W}, \\mathbf{b}) = -\\frac{1}{N}\\sum\\limits_{i=1}^{N}\\sum\\limits_{j=1}^{k}y^i_j\\log{\\tilde{y}^i_j}, \\tag{1}\n",
    "$$\n",
    "\n",
    "where $\\mathbf{W}$ and $\\mathbf{b}$ are the weights and the bias of the one layer FCNN. Note that this loss function is derived from cross-entropy [2].\n",
    "\n",
    "### Pipeline\n",
    "\n",
    "![pipeline](./figures/vsql-fig-pipeline.png \"Figure 1: Flow chart of VSQL\")\n",
    "<div style=\"text-align:center\">Figure 1: Flow chart of VSQL </div>\n",
    "\n",
    "Here we give the whole pipeline to implement VSQL.\n",
    "\n",
    "1. Encode a classical data point $\\mathbf{x}^i$ into a quantum state $\\left|\\mathbf{x}^i\\right>$.\n",
    "2. Prepare a parameterized local quantum circuit $U(\\mathbf{\\theta})$ and initialize its parameters $\\mathbf{\\theta}$.\n",
    "3. Apply $U(\\mathbf{\\theta})$ on the first few qubits. Then, obtain a shadow feature via measuring a local observable, for instance, $X\\otimes X\\cdots \\otimes X$, on these qubits.\n",
    "4. Sliding down $U(\\mathbf{\\theta})$ one qubit each time, repeat step 3 until the last qubit has been covered.\n",
    "5. Feed all shadow features obtained from steps 3-4 to an FCNN and get the predicted label $\\tilde{\\mathbf{y}}^i$ through an activation function. For multi-label classification problems, we use the softmax activation function.\n",
    "5. Repeat steps 3-5 until all data points in the data set have been processed. Then calculate the loss function $\\mathcal{L}(\\mathbf{\\theta}, \\mathbf{W}, \\mathbf{b})$.\n",
    "6. Adjust the parameters $\\mathbf{\\theta}$, $\\mathbf{W}$, and $\\mathbf{b}$ through optimization methods such as gradient descent to minimize the loss function. Then we get the optimized model $\\mathcal{F}$.\n",
    "\n",
    "Since VSQL only extracts local shadow features, it can be easily implemented on quantum devices with topological connectivity limits. Besides, since the $U(\\mathbf{\\theta})$ used in circuits are identical, the number of parameters involved is significantly smaller than other commonly used variational quantum classifiers."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "after-shadow",
   "metadata": {},
   "source": [
    "## Paddle Quantum Implementation\n",
    "\n",
    "We will apply VSQL to classify handwritten digits taken from the MNIST dataset [3], a public benchmark dataset containing ten different classes labeled from '0' to '9'. Here, we consider a binary classification problem for the prupose of demonstration, in which only data labeled as '0' or '1' are used.\n",
    "\n",
    "First, we import the required packages."
   ]
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
68
   "execution_count": 2,
Q
Quleaf 已提交
69 70 71 72 73 74 75 76
   "id": "auburn-compound",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "import numpy as np\n",
    "import paddle\n",
    "import paddle.nn.functional as F\n",
Q
Quleaf 已提交
77 78 79
    "from paddle.vision.datasets import MNIST\n",
    "import paddle_quantum\n",
    "from paddle_quantum.ansatz import Circuit"
Q
Quleaf 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93
   ]
  },
  {
   "cell_type": "markdown",
   "id": "violent-procurement",
   "metadata": {},
   "source": [
    "### Data preprocessing\n",
    "\n",
    "Each image of a handwritten digit consists of $28\\times 28$ grayscale pixels valued in $[0, 255]$. We first flatten the $28\\times 28$ 2D matrix into a 1D vector $\\mathbf{x}^i$, and use amplitude encoding to encode every $\\mathbf{x}^i$ into a 10-qubit quantum state $\\left|\\mathbf{x}^i\\right>$. To do amplitude encoding, we first normalize each vector and pad it with zeros at the end."
   ]
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
94
   "execution_count": 3,
Q
Quleaf 已提交
95 96 97 98 99 100 101
   "id": "valuable-criterion",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Normalize the vector and do zero-padding\n",
    "def norm_img(images):\n",
    "    new_images = [np.pad(np.array(i).flatten(), (0, 240), constant_values=(0, 0)) for i in images]\n",
Q
Quleaf 已提交
102 103
    "    new_images = [paddle.to_tensor(i / np.linalg.norm(i), dtype='complex64') for i in new_images]\n",
    "\n",
Q
Quleaf 已提交
104 105 106 107 108
    "    return new_images"
   ]
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
109
   "execution_count": 4,
Q
Quleaf 已提交
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
   "id": "ancient-philosophy",
   "metadata": {},
   "outputs": [],
   "source": [
    "def data_loading(n_train=1000, n_test=100):\n",
    "    # We use the MNIST provided by paddle\n",
    "    train_dataset = MNIST(mode='train')\n",
    "    test_dataset = MNIST(mode='test')\n",
    "    # Select data points from category 0 and 1\n",
    "    train_dataset = np.array([i for i in train_dataset if i[1][0] == 0 or i[1][0] == 1], dtype=object)\n",
    "    test_dataset = np.array([i for i in test_dataset if i[1][0] == 0 or i[1][0] == 1], dtype=object)\n",
    "    np.random.shuffle(train_dataset)\n",
    "    np.random.shuffle(test_dataset)\n",
    "    # Separate images and labels\n",
    "    train_images = train_dataset[:, 0][:n_train]\n",
    "    train_labels = train_dataset[:, 1][:n_train].astype('int64')\n",
    "    test_images = test_dataset[:, 0][:n_test]\n",
    "    test_labels = test_dataset[:, 1][:n_test].astype('int64')\n",
    "    # Normalize data and pad them with zeros\n",
    "    x_train = norm_img(train_images)\n",
    "    x_test = norm_img(test_images)\n",
    "    # Transform integer labels into one-hot vectors\n",
    "    train_targets = np.array(train_labels).reshape(-1)\n",
    "    y_train = paddle.to_tensor(np.eye(2)[train_targets])\n",
    "    test_targets = np.array(test_labels).reshape(-1)\n",
    "    y_test = paddle.to_tensor(np.eye(2)[test_targets])\n",
Q
Quleaf 已提交
136
    "\n",
Q
Quleaf 已提交
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
    "    return x_train, y_train, x_test, y_test"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "alive-spanish",
   "metadata": {},
   "source": [
    "### Building the shadow circuit\n",
    "\n",
    "Now, we are ready for the next step. Before diving into details of the circuit, we need to clarify several parameters:\n",
    "- $n$: the number of qubits encoding each data point.\n",
    "- $n_{qsc}$: the width of the quantum shadow circuit . We only apply $U(\\mathbf{\\theta})$ on consecutive $n_{qsc}$ qubits each time.\n",
    "- $D$: the depth of the circuit, indicating the repeating times of a layer in $U(\\mathbf{\\theta})$.\n",
    "\n",
    "Here, we give an example where $n = 4$ and $n_{qsc} = 2$.\n",
    "\n",
    "We first apply $U(\\mathbf{\\theta})$ to the first two qubits and obtain the shadow feature $O_1$.\n",
    "\n",
    "![qubit0](./figures/vsql-fig-qubit0.png \"Figure 2: The first circuit\")\n",
    "<div style=\"text-align:center\">Figure 2: The first circuit </div>\n",
    "\n",
    "Then, we prepare a copy of the same input state $\\left|\\mathbf{x}^i\\right>$, apply $U(\\mathbf{\\theta})$ to the two qubits in the middle, and obtain the shadow feature $O_2$.\n",
    "\n",
    "![qubit1](./figures/vsql-fig-qubit1.png \"Figure 3: The second circuit\")\n",
    "<div style=\"text-align:center\">Figure 3: The second circuit </div>\n",
    "\n",
    "Finally, we prepare another copy of the same input state, apply $U(\\mathbf{\\theta})$ to the last two qubits, and obtain the shadow feature $O_3$. Now we are done with this data point!\n",
    "\n",
    "![qubit2](./figures/vsql-fig-qubit2.png \"Figure 4: The last circuit\")\n",
    "<div style=\"text-align:center\">Figure 4: The last circuit </div>\n",
    "\n",
    "In general, we will need to repeat this process for $n - n_{qsc} + 1$ times for each data point. One thing to point out is that we only use one shadow circuit in the above example. When sliding the shadow circuit $U(\\mathbf{\\theta})$ through the $n$-qubit Hilbert space, the same parameters $\\mathbf{\\theta}$ are used. You can use more shadow circuits for complicated tasks, and different shadow circuits should have different parameters $\\mathbf{\\theta}$.\n",
    "\n",
    "Below, we will use a 2-local shadow circuit, i.e., $n_{qsc}=2$ for the MNIST classification task, and the circuit's structure is shown in Figure 5.\n",
    "\n",
    "![2-local](./figures/vsql-fig-2-local.png \"Figure 5: The 2-local shadow circuit design\")\n",
    "<div style=\"text-align:center\">Figure 5: The 2-local shadow circuit design </div>\n",
    "\n",
    "The circuit layer in the dashed box is repeated for $D$ times to increase the expressive power of the quantum circuit. The structure of the circuit is not unique. You can try to design your own circuit."
   ]
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
181
   "execution_count": 5,
Q
Quleaf 已提交
182 183 184 185 186
   "id": "derived-workstation",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Construct the shadow circuit U(theta)\n",
Q
Quleaf 已提交
187
    "def U_theta(n, n_qsc=2, depth=1):\n",
Q
Quleaf 已提交
188
    "    # Initialize the circuit\n",
Q
Quleaf 已提交
189
    "    cir = Circuit(n)\n",
Q
Quleaf 已提交
190 191
    "    # Add layers of rotation gates\n",
    "    for i in range(n_qsc):\n",
Q
Quleaf 已提交
192 193 194
    "        cir.rx(qubits_idx=i)\n",
    "        cir.ry(qubits_idx=i)\n",
    "        cir.rx(qubits_idx=i)\n",
Q
Quleaf 已提交
195 196 197
    "    # Add D layers of the dashed box\n",
    "    for repeat in range(1, depth + 1):\n",
    "        for i in range(n_qsc - 1):\n",
Q
Quleaf 已提交
198 199
    "            cir.cnot([i, i + 1])\n",
    "        cir.cnot([n_qsc - 1, 0])\n",
Q
Quleaf 已提交
200
    "        for i in range(n_qsc):\n",
Q
Quleaf 已提交
201
    "            cir.ry(qubits_idx=i)\n",
Q
Quleaf 已提交
202
    "\n",
Q
Quleaf 已提交
203 204 205 206 207 208 209 210
    "    return cir\n",
    "\n",
    "# Slide the circuit\n",
    "def slide_circuit(cir, distance):\n",
    "    for sublayer in cir.sublayers():\n",
    "        qubits_idx = np.array(sublayer.qubits_idx)\n",
    "        qubits_idx = qubits_idx + distance\n",
    "        sublayer.qubits_idx = qubits_idx.tolist()"
Q
Quleaf 已提交
211 212 213 214 215 216 217 218 219 220 221 222
   ]
  },
  {
   "cell_type": "markdown",
   "id": "constant-scottish",
   "metadata": {},
   "source": [
    "When $n_{qsc}$ is larger, the $n_{qsc}$-local shadow circuit can be constructed by extending this 2-local shadow circuit. Let's print a 4-local shadow circuit with $D=2$ to find out how it works."
   ]
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
223
   "execution_count": 6,
Q
Quleaf 已提交
224 225 226 227 228 229 230
   "id": "roman-radical",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
Q
Quleaf 已提交
231
      "--Rx(2.461)----Ry(5.857)----Rx(1.809)----*--------------x----Ry(4.381)----*--------------x----Ry(1.523)--\n",
Q
Quleaf 已提交
232
      "                                         |              |                 |              |               \n",
Q
Quleaf 已提交
233
      "--Rx(3.861)----Ry(5.536)----Rx(3.228)----x----*---------|----Ry(1.633)----x----*---------|----Ry(2.853)--\n",
Q
Quleaf 已提交
234
      "                                              |         |                      |         |               \n",
Q
Quleaf 已提交
235
      "--Rx(3.690)----Ry(5.288)----Rx(2.211)---------x----*----|----Ry(0.397)---------x----*----|----Ry(6.159)--\n",
Q
Quleaf 已提交
236
      "                                                   |    |                           |    |               \n",
Q
Quleaf 已提交
237
      "--Rx(3.030)----Ry(5.486)----Rx(3.769)--------------x----*----Ry(1.769)--------------x----*----Ry(2.564)--\n",
Q
Quleaf 已提交
238 239 240 241 242 243 244 245 246 247 248 249
      "                                                                                                         \n",
      "---------------------------------------------------------------------------------------------------------\n",
      "                                                                                                         \n",
      "---------------------------------------------------------------------------------------------------------\n",
      "                                                                                                         \n"
     ]
    }
   ],
   "source": [
    "N = 6\n",
    "NQSC = 4\n",
    "D = 2\n",
Q
Quleaf 已提交
250
    "cir = U_theta(N, n_qsc=NQSC, depth=D)\n",
Q
Quleaf 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
    "print(cir)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "extreme-abuse",
   "metadata": {},
   "source": [
    "### Shadow features\n",
    "\n",
    "We've talked a lot about shadow features, but what is a shadow feature? It can be seen as a projection of a state from Hilbert space to classical space. There are various projections that can be used as a shadow feature. Here, we choose the expectation value of the Pauli $X\\otimes X\\otimes \\cdots \\otimes X$ observable on selected $n_{qsc}$ qubits as the shadow feature. In our previous example, $O_1 = \\left<X\\otimes X\\right>$ on the first two qubits is the first shadow feature we extracted with the shadow circuit."
   ]
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
266
   "execution_count": 7,
Q
Quleaf 已提交
267 268 269 270 271 272 273
   "id": "equipped-begin",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Construct the observable for extracting shadow features\n",
    "def observable(n_start, n_qsc=2):\n",
    "    pauli_str = ','.join('x' + str(i) for i in range(n_start, n_start + n_qsc))\n",
Q
Quleaf 已提交
274 275 276
    "    hamiltonian = paddle_quantum.Hamiltonian([[1.0, pauli_str]])\n",
    "\n",
    "    return hamiltonian"
Q
Quleaf 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
   ]
  },
  {
   "cell_type": "markdown",
   "id": "exempt-brooks",
   "metadata": {},
   "source": [
    "### Classical postprocessing with FCNN\n",
    "\n",
    "After obtaining all shadow features, we feed them into a classical FCNN. We use the softmax activation function so that the output from the FCNN will be a probability distribution. The $i^{\\text{th}}$ element of the output is the probability of this data point belonging to the $i^{\\text{th}}$ category, and we predict this data point belongs to the category with the highest probability. In order to predict the actual label, we calculate the cumulative distance between the predicted label and the actual label as the loss function to be optimized:\n",
    "\n",
    "$$\n",
    "\\mathcal{L}(\\mathbf{\\theta}, \\mathbf{W}, \\mathbf{b}) = -\\frac{1}{N}\\sum\\limits_{i=1}^{N}\\sum\\limits_{j=1}^{k}y^i_j\\log{\\tilde{y}^i_j}. \\tag{2}\n",
    "$$   "
   ]
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
295
   "execution_count": 8,
Q
Quleaf 已提交
296 297 298 299 300 301 302 303
   "id": "upper-petite",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Net(paddle.nn.Layer):\n",
    "    def __init__(self,\n",
    "                 n, # Number of qubits: n     \n",
    "                 n_qsc, # Number of local qubits in a shadow\n",
Q
Quleaf 已提交
304
    "                 depth=1 # Circuit depth\n",
Q
Quleaf 已提交
305 306 307 308 309
    "                ):\n",
    "        super(Net, self).__init__()\n",
    "        self.n = n\n",
    "        self.n_qsc = n_qsc\n",
    "        self.depth = depth\n",
Q
Quleaf 已提交
310
    "        self.cir = U_theta(self.n, n_qsc=self.n_qsc, depth=self.depth)\n",
Q
Quleaf 已提交
311 312 313 314
    "        # FCNN, initialize the weights and the bias with a Gaussian distribution\n",
    "        self.fc = paddle.nn.Linear(n - n_qsc + 1, 2,\n",
    "                                   weight_attr=paddle.ParamAttr(initializer=paddle.nn.initializer.Normal()),\n",
    "                                   bias_attr=paddle.ParamAttr(initializer=paddle.nn.initializer.Normal()))\n",
Q
Quleaf 已提交
315
    "\n",
Q
Quleaf 已提交
316 317 318 319 320 321
    "    # Define forward propagation mechanism, and then calculate loss function and cross-validation accuracy\n",
    "    def forward(self, batch_in, label):\n",
    "        # Quantum part\n",
    "        dim = len(batch_in)\n",
    "        features = []\n",
    "        for state in batch_in:\n",
Q
Quleaf 已提交
322
    "            _state = paddle_quantum.State(state)\n",
Q
Quleaf 已提交
323 324 325
    "            f_i = []\n",
    "            for st in range(self.n - self.n_qsc + 1):\n",
    "                ob = observable(st, n_qsc=self.n_qsc)\n",
Q
Quleaf 已提交
326 327 328 329
    "                # Slide the circuit to act on different qubits\n",
    "                slide_circuit(self.cir, 1 if st != 0 else 0)\n",
    "                expecval = paddle_quantum.loss.ExpecVal(ob)\n",
    "                out_state = self.cir(_state)\n",
Q
Quleaf 已提交
330
    "                # Calculate the expectation value\n",
Q
Quleaf 已提交
331
    "                f_ij = expecval(out_state)\n",
Q
Quleaf 已提交
332
    "                f_i.append(f_ij)\n",
Q
Quleaf 已提交
333 334
    "            # Slide the circuit back to the initial position\n",
    "            slide_circuit(self.cir, -st)\n",
Q
Quleaf 已提交
335 336 337 338 339 340 341 342 343 344 345 346 347
    "            f_i = paddle.concat(f_i)\n",
    "            features.append(f_i)\n",
    "        features = paddle.stack(features)\n",
    "        # Classical part\n",
    "        outputs = self.fc(features)\n",
    "        outputs = F.log_softmax(outputs)\n",
    "        # Calculate loss and accuracy\n",
    "        loss = -paddle.mean(paddle.sum(outputs * label)) \n",
    "        is_correct = 0\n",
    "        for i in range(dim):\n",
    "            if paddle.argmax(label[i], axis=-1) == paddle.argmax(outputs[i], axis=-1):\n",
    "                is_correct = is_correct + 1\n",
    "        acc = is_correct / dim\n",
Q
Quleaf 已提交
348
    "\n",
Q
Quleaf 已提交
349 350 351 352 353
    "        return loss, acc"
   ]
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
354
   "execution_count": 9,
Q
Quleaf 已提交
355 356 357 358
   "id": "handed-study",
   "metadata": {},
   "outputs": [],
   "source": [
Q
Quleaf 已提交
359
    "def ShadowClassifier(N=4, n_qsc=2, D=1, EPOCH=4, LR=0.1, BATCH=1, N_train=1000, N_test=100):\n",
Q
Quleaf 已提交
360 361 362
    "    # Load data\n",
    "    x_train, y_train, x_test, y_test = data_loading(n_train=N_train, n_test=N_test)\n",
    "    # Initialize the neural network\n",
Q
Quleaf 已提交
363
    "    net = Net(N, n_qsc, depth=D)\n",
Q
Quleaf 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
    "    # Generally speaking, we use Adam optimizer to obtain relatively good convergence,\n",
    "    # You can change it to SGD or RMS prop.\n",
    "    opt = paddle.optimizer.Adam(learning_rate=LR, parameters=net.parameters())\n",
    "\n",
    "    # Optimization loop\n",
    "    for ep in range(EPOCH):\n",
    "        for itr in range(N_train // BATCH):\n",
    "            # Forward propagation to calculate loss and accuracy\n",
    "            loss, batch_acc = net(x_train[itr * BATCH:(itr + 1) * BATCH],\n",
    "                                  y_train[itr * BATCH:(itr + 1) * BATCH])\n",
    "            # Use back propagation to minimize the loss function\n",
    "            loss.backward()\n",
    "            opt.minimize(loss)\n",
    "            opt.clear_grad()\n",
    "            # Evaluation\n",
    "            if itr % 10 == 0:\n",
    "                # Compute test accuracy and loss\n",
    "                loss_useless, test_acc = net(x_test[0:N_test],\n",
    "                                             y_test[0:N_test])\n",
    "                # Print test results\n",
    "                print(\"epoch:%3d\" % ep, \"  iter:%3d\" % itr,\n",
    "                      \"  loss: %.4f\" % loss.numpy(),\n",
    "                      \"  batch acc: %.4f\" % batch_acc,\n",
    "                      \"  test acc: %.4f\" % test_acc)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "diverse-publisher",
   "metadata": {},
   "source": [
    "Let's take a look at the actual training process, which takes about eight minutes: "
   ]
  },
  {
   "cell_type": "code",
Q
Quleaf 已提交
400
   "execution_count": 11,
Q
Quleaf 已提交
401 402 403 404 405
   "id": "unique-indie",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
Q
Quleaf 已提交
406 407 408 409 410 411 412 413 414 415
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\v_zhaoxuanqiang\\AppData\\Local\\Temp\\ipykernel_9684\\2285585707.py:6: FutureWarning: The input object of type 'Image' is an array-like implementing one of the corresponding protocols (`__array__`, `__array_interface__` or `__array_struct__`); but not a sequence (or 0-D). In the future, this object will be coerced as if it was first converted using `np.array(obj)`. To retain the old behaviour, you have to either modify the type 'Image', or assign to an empty array created with `np.empty(correct_shape, dtype=object)`.\n",
      "  train_dataset = np.array([i for i in train_dataset if i[1][0] == 0 or i[1][0] == 1], dtype=object)\n",
      "C:\\Users\\v_zhaoxuanqiang\\AppData\\Local\\Temp\\ipykernel_9684\\2285585707.py:7: FutureWarning: The input object of type 'Image' is an array-like implementing one of the corresponding protocols (`__array__`, `__array_interface__` or `__array_struct__`); but not a sequence (or 0-D). In the future, this object will be coerced as if it was first converted using `np.array(obj)`. To retain the old behaviour, you have to either modify the type 'Image', or assign to an empty array created with `np.empty(correct_shape, dtype=object)`.\n",
      "  test_dataset = np.array([i for i in test_dataset if i[1][0] == 0 or i[1][0] == 1], dtype=object)\n"
     ]
    },
Q
Quleaf 已提交
416 417 418 419
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
Q
Quleaf 已提交
420 421 422 423 424 425 426 427 428 429
      "epoch:  0   iter:  0   loss: 15.8133   batch acc: 0.4500   test acc: 0.4700\n",
      "epoch:  0   iter: 10   loss: 12.3796   batch acc: 0.7500   test acc: 0.8800\n",
      "epoch:  0   iter: 20   loss: 11.3694   batch acc: 0.8500   test acc: 0.9600\n",
      "epoch:  1   iter:  0   loss: 9.0114   batch acc: 0.9000   test acc: 0.9700\n",
      "epoch:  1   iter: 10   loss: 8.0621   batch acc: 0.9500   test acc: 0.9700\n",
      "epoch:  1   iter: 20   loss: 8.1941   batch acc: 0.9000   test acc: 0.9800\n",
      "epoch:  2   iter:  0   loss: 6.5335   batch acc: 0.9000   test acc: 0.9900\n",
      "epoch:  2   iter: 10   loss: 6.2066   batch acc: 0.9000   test acc: 0.9900\n",
      "epoch:  2   iter: 20   loss: 6.6691   batch acc: 0.9000   test acc: 0.9900\n",
      "time used: 499.0781090259552\n"
Q
Quleaf 已提交
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
     ]
    }
   ],
   "source": [
    "time_st = time.time()\n",
    "ShadowClassifier(\n",
    "    N=10,         # Number of qubits: n\n",
    "    n_qsc=2,      # Number of local qubits in a shadow: n_qsc\n",
    "    D=1,          # Circuit depth\n",
    "    EPOCH=3,      # Number of training epochs\n",
    "    LR=0.02,      # Learning rate\n",
    "    BATCH=20,     # Batch size\n",
    "    N_train=500,  # Number of training data\n",
    "    N_test=100    # Number of test data\n",
    ")\n",
    "print(\"time used:\", time.time() - time_st)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "elegant-ability",
   "metadata": {},
   "source": [
    "## Conclusion\n",
    "\n",
    "VSQL is a hybrid quantum-classical algorithm based on classical shadows, which extracts local features in a convolution way. Combing parameterized circuit $U(\\mathbf{\\theta})$ with a classical FCNN, VSQL demonstrates a good performance in binary classification tasks. \n",
    "In the [quantum classifier](./QClassifier_EN.ipynb) tutorial, we have introduced a commonly used classifier using a parameterized quantum circuit. In that framework, the parameterized circuit is applied to all qubits, and the optimization process searches through the whole Hilbert space to find the optimized $\\mathbf{\\theta}$. Unlike that method, in VSQL, the parameterized circuit $U(\\mathbf{\\theta})$ is only applied to a few selected qubits each time. The number of parameters of VSQL for $k$-label classification is the sum of parameters in $U(\\mathbf{\\theta})$ and parameters in FCNN, which is in total $n_{qsc}D + [(n-n_{qsc}+1)+1]k$. Compared to commonly used variational quantum classifiers that need $nD$ parameters in the parameterized quantum circuit, VSQL only has $n_{qsc}D$ parameters in the parameterized local quantum circuit. As a result, the amount of quantum resources (the number of quantum gates) required has been significantly reduced."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "suitable-earthquake",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "## References\n",
    "\n",
Q
Quleaf 已提交
468
    "[1] Li, Guangxi, Zhixin Song, and Xin Wang. \"VSQL: Variational Shadow Quantum Learning for Classification.\" [Proceedings of the AAAI Conference on Artificial Intelligence. Vol. 35. No. 9. 2021.](https://ojs.aaai.org/index.php/AAAI/article/view/17016)\n",
Q
Quleaf 已提交
469 470 471 472 473 474 475 476 477
    "\n",
    "[2] Goodfellow, Ian, et al. Deep learning. Vol. 1. No. 2. Cambridge: MIT press, 2016.\n",
    "\n",
    "[3] LeCun, Yann. \"The MNIST database of handwritten digits.\" http://yann.lecun.com/exdb/mnist/ (1998)."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
Q
Quleaf 已提交
478
   "display_name": "Python 3 (ipykernel)",
Q
Quleaf 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491
   "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 已提交
492
   "version": "3.8.13"
Q
Quleaf 已提交
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
  },
  "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": 5
}