{ "cells": [ { "cell_type": "markdown", "id": "adjacent-printing", "metadata": {}, "source": [ "# Variational Shadow Quantum Learning\n", "\n", " Copyright (c) 2021 Institute for Quantum Computing, Baidu Inc. All Rights Reserved. " ] }, { "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", "
Figure 1: Flow chart of VSQL
\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", "execution_count": 1, "id": "auburn-compound", "metadata": {}, "outputs": [], "source": [ "import time\n", "import numpy as np\n", "import paddle\n", "from paddle_quantum.circuit import UAnsatz\n", "from paddle.vision.datasets import MNIST\n", "import paddle.nn.functional as F\n", "\n", "paddle.set_default_dtype(\"float64\")" ] }, { "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", "execution_count": 2, "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", " new_images = [paddle.to_tensor(i / np.linalg.norm(i), dtype='complex128') for i in new_images]\n", " \n", " return new_images" ] }, { "cell_type": "code", "execution_count": 3, "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", " \n", " 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", "
Figure 2: The first circuit
\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", "
Figure 3: The second circuit
\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", "
Figure 4: The last circuit
\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", "
Figure 5: The 2-local shadow circuit design
\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", "execution_count": 4, "id": "derived-workstation", "metadata": {}, "outputs": [], "source": [ "# Construct the shadow circuit U(theta)\n", "def U_theta(theta, n, n_start, n_qsc=2, depth=1):\n", " # Initialize the circuit\n", " cir = UAnsatz(n)\n", " # Add layers of rotation gates\n", " for i in range(n_qsc):\n", " cir.rx(theta[0][0][i], n_start + i)\n", " cir.ry(theta[0][1][i], n_start + i)\n", " cir.rx(theta[0][2][i], n_start + i)\n", " # Add D layers of the dashed box\n", " for repeat in range(1, depth + 1):\n", " for i in range(n_qsc - 1):\n", " cir.cnot([n_start + i, n_start + i + 1])\n", " cir.cnot([n_start + n_qsc - 1, n_start])\n", " for i in range(n_qsc):\n", " cir.ry(theta[repeat][1][i], n_start + i)\n", "\n", " return cir" ] }, { "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", "execution_count": 5, "id": "roman-radical", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--Rx(2.435)----Ry(5.433)----Rx(5.893)----*--------------X----Ry(0.430)----*--------------X----Ry(3.016)--\n", " | | | | \n", "--Rx(4.804)----Ry(2.413)----Rx(4.271)----X----*---------|----Ry(3.770)----X----*---------|----Ry(4.303)--\n", " | | | | \n", "--Rx(1.397)----Ry(0.021)----Rx(2.358)---------X----*----|----Ry(5.194)---------X----*----|----Ry(0.938)--\n", " | | | | \n", "--Rx(2.457)----Ry(0.133)----Rx(0.743)--------------X----*----Ry(2.042)--------------X----*----Ry(3.704)--\n", " \n", "---------------------------------------------------------------------------------------------------------\n", " \n", "---------------------------------------------------------------------------------------------------------\n", " \n" ] } ], "source": [ "N = 6\n", "NSTART = 0\n", "NQSC = 4\n", "D = 2\n", "theta = paddle.uniform([D + 1, 3, NQSC], min=0.0, max=2 * np.pi)\n", "cir = U_theta(theta, N, NSTART, n_qsc=NQSC, depth=D)\n", "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$ on the first two qubits is the first shadow feature we extracted with the shadow circuit." ] }, { "cell_type": "code", "execution_count": 6, "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", " \n", " return [[1.0, pauli_str]]" ] }, { "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", "execution_count": 7, "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", " depth=1, # Circuit depth\n", " seed=3, # Random seed\n", " ):\n", " super(Net, self).__init__()\n", "\n", " self.n = n\n", " self.n_qsc = n_qsc\n", " self.depth = depth\n", " # Initialize the parameters theta with a uniform distribution of [0, 2 * pi]\n", " self.theta = self.create_parameter(shape=[depth + 1, 3, n_qsc],\n", " default_initializer=paddle.nn.initializer.Uniform(low=0.0, high=2 * np.pi))\n", " # 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", " \n", " # 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", " f_i = []\n", " for st in range(self.n - self.n_qsc + 1):\n", " ob = observable(st, n_qsc=self.n_qsc)\n", " cir = U_theta(self.theta, self.n, st, n_qsc=self.n_qsc)\n", " cir.run_state_vector(state)\n", " # Calculate the expectation value\n", " f_ij = cir.expecval(ob)\n", " f_i.append(f_ij)\n", " 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", " \n", " return loss, acc" ] }, { "cell_type": "code", "execution_count": 8, "id": "handed-study", "metadata": {}, "outputs": [], "source": [ "def ShadowClassifier(N=4, n_qsc=2, D=1, EPOCH=4, LR=0.1, BATCH=1, seed=1, N_train=1000, N_test=100):\n", " # 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", " net = Net(N, n_qsc, depth=D, seed=seed)\n", " # 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", "execution_count": 9, "id": "unique-indie", "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "epoch: 0 iter: 0 loss: 18.4158 batch acc: 0.2500 test acc: 0.4800\n", "epoch: 0 iter: 10 loss: 10.6799 batch acc: 0.8000 test acc: 0.9500\n", "epoch: 0 iter: 20 loss: 10.1750 batch acc: 0.8500 test acc: 0.9400\n", "epoch: 1 iter: 0 loss: 8.5254 batch acc: 0.9000 test acc: 0.9600\n", "epoch: 1 iter: 10 loss: 7.0151 batch acc: 0.9000 test acc: 0.9700\n", "epoch: 1 iter: 20 loss: 7.5365 batch acc: 0.9000 test acc: 0.9700\n", "epoch: 2 iter: 0 loss: 6.5155 batch acc: 0.8500 test acc: 0.9700\n", "epoch: 2 iter: 10 loss: 5.5648 batch acc: 0.9500 test acc: 0.9800\n", "epoch: 2 iter: 20 loss: 6.3797 batch acc: 0.9500 test acc: 0.9700\n", "time used: 548.4337043762207\n" ] } ], "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", " seed=1024, # Random seed\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", "[1] Li, Guangxi, et al. \"VSQL: Variational Shadow Quantum Learning for Classification.\" [arXiv:2012.08288 (2020).](https://arxiv.org/abs/2012.08288)\n", "\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": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.10" }, "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 }