{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Quantum Classifier\n", "\n", " Copyright (c) 2021 Institute for Quantum Computing, Baidu Inc. All Rights Reserved. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Overview\n", "\n", "In this tutorial, we will discuss the workflow of Variational Quantum Classifiers (VQC) and how to use quantum neural networks (QNN) to accomplish a **binary classification** task. The main representatives of this approach include the [Quantum Circuit Learning (QCL)](https://arxiv.org/abs/1803.00745) [1] by Mitarai et al. (2018), Farhi & Neven (2018) [2] and [Circuit-Centric Quantum Classifiers](https://arxiv.org/abs/1804.00633) [3] by Schuld et al. (2018). Here, we mainly talk about classification in the language of supervised learning. Unlike classical methods, quantum classifiers require pre-processing to encode classical data into quantum data, and then train the parameters in the quantum neural network. Using different encoding methods, we can benchmark the optimal classification performance through test data. Finally, we demonstrate how to use built-in quantum datasets to accomplish quantum classification.\n", "\n", "### Background\n", "\n", "In the language of supervised learning, we need to enter a data set composed of $N$ pairs of labeled data points $D = \\{(x^k,y^k)\\}_{k=1}^{N}$ , Where $x^k\\in \\mathbb{R}^{m}$ is the data point, and $y^k \\in\\{0,1\\}$ is the label associated with the data point $x^k$. **The classification process is essentially a decision-making process, which determines the label attribution of a given data point**. For the quantum classifier framework, the realization of the classifier $\\mathcal{F}$ is a combination of a quantum neural network (or parameterized quantum circuit) with parameters $\\theta$, measurement, and data processing. An excellent classifier $\\mathcal{F}_\\theta$ should correctly map the data points in each data set to the corresponding labels as accurate as possible $\\mathcal{F}_\\theta(x^k ) \\rightarrow y^k$. Therefore, we use the cumulative distance between the predicted label $\\tilde{y}^{k} = \\mathcal{F}_\\theta(x^k)$ and the actual label $y^k$ as the loss function $\\mathcal {L}(\\theta)$ to be optimized. For binary classification tasks, we can choose the following loss function,\n", "\n", "$$\n", "\\mathcal{L}(\\theta) = \\sum_{k=1}^N 1/N \\cdot |\\tilde{y}^{k}-y^k|^2. \\tag{1}\n", "$$\n", "\n", "### Pipeline\n", "\n", "Here we give the whole pipeline to implement a quantum classifier under the framework of quantum circuit learning (QCL).\n", "\n", "1. Encode the classical data $x^k$ to quantum data $\\lvert \\psi_{\\rm in}\\rangle^k$. In this tutorial, we use Angle Encoding, see [encoding methods](./DataEncoding_EN.ipynb) for details. Readers can also try other encoding methods, e.g., Amplitude Encoding, and see the performance.\n", "2. Construct the parameterized quantum circuit (PQC), corresponds to the unitary gate $U(\\theta)$.\n", "3. Apply the parameterized circuit $U(\\theta)$ with the parameter $\\theta$ on input states $\\lvert \\psi_{\\rm in} \\rangle^k$, thereby obtaining the output state $\\lvert \\psi_{\\rm out} \\rangle^k = U(\\theta)\\lvert \\psi_{\\rm in} \\rangle^k$.\n", "4. Measure the quantum state $\\lvert \\psi_{\\rm out}\\rangle^k$ processed by the quantum neural network to get the estimated label $\\tilde{y}^{k}$.\n", "5. Repeat steps 3-4 until all data points in the data set have been processed. Then calculate the loss function $\\mathcal{L}(\\theta)$.\n", "6. Continuously adjust the parameter $\\theta$ through optimization methods such as gradient descent to minimize the loss function. Record the optimal parameters after optimization $\\theta^* $, and then we obtain the optimal classifier $\\mathcal{F}_{\\theta^*}$.\n", "\n", " \n", "