{ "cells": [ { "cell_type": "markdown", "id": "2b8d96ea", "metadata": {}, "source": [ "# Encoding Classical Data into Quantum States\n", "\n", " Copyright (c) 2021 Institute for Quantum Computing, Baidu Inc. All Rights Reserved. " ] }, { "cell_type": "code", "execution_count": 1, "id": "f160535c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from IPython.core.display import HTML\n", "display(HTML(\"\"))" ] }, { "cell_type": "markdown", "id": "2e2ccb8d", "metadata": {}, "source": [ "## Overview\n", "\n", "Quantum encoding is a process to transform classical information into quantum states. \n", "It plays a crucial role in using quantum algorithms to solve classical problems, especially in quantum machine learning tasks. Interested readers can find an example of using quantum encoding \n", "in our tutorial on [quantum classifier](./QClassifier_EN.ipynb), where we use quantum neural networks to accomplish a classical binary classification task.\n", "Quantum encoding can be seen as a quantum circuit that acts on $\\left| 0^n \\right>$ state (n is the number of qubits), with some parameters determined by the classical information.\n", "\n", "In this tutorial, we will discuss five typical encoding schemes, including **basis encoding** [1], **amplitude encoding** [1], **angle encoding** [1], **instantaneous quantum polynomial (IQP) style encoding** [2], and **Hamiltonian evolution ansatz encoding** [3]. In Paddle Quantum, we provide built-in methods for the first four encoding strategies.\n", "\n", "## Basis Encoding\n", "\n", "Basis encoding is the most intuitive way to encode classical information into a quantum state. It encodes an $n$-bit binary string $x$ to an $n$-qubit quantum state $\\left|x\\right> = \\left|i_x\\right>$, where $\\left|i_x\\right>$ is a computational basis state. For example, if $x=1011$, the corresponding quantum state after basis encoding is $\\left|1011\\right>$. Let's take a look at how to use Paddle Quantum to implement basis encoding:" ] }, { "cell_type": "code", "execution_count": 2, "id": "3b220934", "metadata": {}, "outputs": [], "source": [ "# Import necessary library\n", "import paddle\n", "from paddle_quantum.circuit import UAnsatz\n", "import numpy as np" ] }, { "cell_type": "markdown", "id": "9e653bdf", "metadata": {}, "source": [ "Start from $\\left| 0^n \\right>$, and we apply an $X$ gate if the corresponding classical bit is 1. We construct the circuit as follows: " ] }, { "cell_type": "code", "execution_count": 3, "id": "ff86f454", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--X--\n", " \n", "-----\n", " \n", "--X--\n", " \n", "--X--\n", " \n" ] } ], "source": [ "# Number of qubits = length of the classical binary string\n", "n = 4\n", "# Initialize the circuit\n", "basis_enc = UAnsatz(n)\n", "# X is the classical information\n", "x = '1011'\n", "# Add a Pauli X gate to the ith qubit if the ith classical bit is 1\n", "for i in range(len(x)):\n", " if x[i] == '1':\n", " basis_enc.x(i)\n", " \n", "print(basis_enc)" ] }, { "cell_type": "markdown", "id": "95559773", "metadata": {}, "source": [ "The corresponding quantum state after basis encoding is: " ] }, { "cell_type": "code", "execution_count": 4, "id": "9a5909db", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j\n", " 0.+0.j 1.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j]\n" ] } ], "source": [ "basis_quan_state = basis_enc.run_state_vector()\n", "print(basis_quan_state.numpy())" ] }, { "cell_type": "markdown", "id": "a6360824", "metadata": {}, "source": [ "which is the state $\\left|1011\\right>$ as we desired.\n", "\n", "In Paddle Quantum, we also provide a built-in method for basis encoding:" ] }, { "cell_type": "code", "execution_count": 5, "id": "a7594453", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j\n", " 0.+0.j 1.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j]\n" ] } ], "source": [ "# Built-in basis encoding\n", "built_in_basis_enc = UAnsatz(n)\n", "# Classical information x should be of type Tensor\n", "x = paddle.to_tensor([1, 0, 1, 1])\n", "built_in_basis_enc.basis_encoding(x)\n", "built_in_basis_enc_state = built_in_basis_enc.run_state_vector()\n", "print(built_in_basis_enc_state.numpy())" ] }, { "cell_type": "markdown", "id": "0630a047", "metadata": {}, "source": [ "## Amplitude Encoding\n", "\n", "Amplitude encoding encodes a vector $\\mathbf{x}$ of length $N$ into amplitudes of an $n$-qubit quantum state with $n = \\lceil\\log_2(N)\\rceil$:\n", "\n", "$$\n", "\\begin{align*} \\left|\\mathbf{x}\\right> = \\sum\\limits_{i}^{N}x_i\\left|i\\right>\\end{align*},\n", "$$\n", "\n", "where $\\left\\{\\left|i\\right>\\right\\}$ is the computational basis for the Hilbert space. Since the classical information forms the amplitudes of a quantum state, the input needs to satisfy the normalization condition: $\\left|\\mathbf{x}\\right|^{2} = 1$.\n", "\n", "For instance, if $\\mathbf{x} = \\begin{bmatrix} \\frac{1}{2}\\\\ \\frac{1}{2}\\\\ -\\frac{1}{2}\\\\ -\\frac{1}{2} \\end{bmatrix}$, the corresponding quantum state will be $\\left|\\mathbf{x}\\right> = \\frac{1}{2}\\left|00\\right> + \\frac{1}{2} \\left|01\\right> - \\frac{1}{2} \\left|10\\right> - \\frac{1}{2} \\left|11\\right>$. \n", "Here is another example with $N < 2^n$, if $\\mathbf{y} = \\begin{bmatrix} \\frac{1}{\\sqrt{3}}\\\\\\frac{1}{\\sqrt{3}}\\\\\\frac{1}{\\sqrt{3}} \\end{bmatrix}$, the corresponding quantum state will be $\\left|\\mathbf{y}\\right> = \\frac{1}{\\sqrt{3}}\\left|00\\right> + \\frac{1}{\\sqrt{3}}\\left|01\\right> + \\frac{1}{\\sqrt{3}}\\left|10\\right>$.\n", "\n", "You may have already noticed, amplitude encoding cannot be represented as a trivial quantum circuit. Instead, it can be implemented using arbitrary state preparation [1]. But don't worry. In Paddle Quantum, we provide a built-in method for amplitude encoding:" ] }, { "cell_type": "code", "execution_count": 6, "id": "adb251b5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.57735026+0.j 0.57735026+0.j 0.57735026+0.j 0. +0.j]\n" ] } ], "source": [ "# Built-in amplitude encoding\n", "# Number of qubits = 2\n", "n = 2\n", "# Initialize the circuit\n", "built_in_amplitude_enc = UAnsatz(n)\n", "# Classical information x should be of type Tensor\n", "x = paddle.to_tensor([0.5, 0.5, 0.5])\n", "state = built_in_amplitude_enc.amplitude_encoding(x, 'state_vector')\n", "print(state.numpy())" ] }, { "cell_type": "markdown", "id": "a90d9ca6", "metadata": {}, "source": [ "In Paddle Quantum, we will normalize the input classical vector by default. As you can see, \n", "the result is indeed $\\frac{1}{\\sqrt{3}}\\left|00\\right> + \\frac{1}{\\sqrt{3}}\\left|01\\right> + \\frac{1}{\\sqrt{3}}\\left|10\\right>$.\n", "\n", "## Angle Encoding\n", "\n", "Angle encoding makes use of rotation gates to encode classical information $\\mathbf{x}$. The classical information determines angles of rotation gates:\n", "\n", "$$\n", "\\left|\\mathbf{x}\\right> = \\bigotimes_{i}^{n} R(\\mathbf{x}_i) \\left| 0^n \\right>,\n", "$$\n", "\n", "where $R$ can be one of $R_x$, $R_y$, $R_z$. Usually, the number of qubits used for encoding is equal to the dimension of vector $\\mathbf{x}$. \n", "For example, when $\\mathbf{x} = \\begin{bmatrix} \\pi \\\\ \\pi\\\\ \\pi \\end{bmatrix}$, angle encoding rotates every qubit around Y-axis (if we choose $R_y$) for degree $\\pi$, so that the corresponding quantum state will be $\\left|111\\right>$.\n", "\n", "The circuit for angle encoding can be constructed as follows: " ] }, { "cell_type": "code", "execution_count": 7, "id": "5f9c513e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--Ry(3.142)--\n", " \n", "--Ry(3.142)--\n", " \n", "--Ry(3.142)--\n", " \n" ] } ], "source": [ "# Number of qubits = length of the classical information\n", "n = 3\n", "# Initialize the circuit\n", "angle_enc = UAnsatz(n)\n", "# X is the classical information\n", "x = paddle.to_tensor([np.pi, np.pi, np.pi], 'float64')\n", "# Add a layer of rotation y gates\n", "for i in range(len(x)):\n", " angle_enc.ry(x[i], i)\n", " \n", "print(angle_enc)" ] }, { "cell_type": "markdown", "id": "1e5a6e06", "metadata": {}, "source": [ "The corresponding quantum state after amplitude encoding is:" ] }, { "cell_type": "code", "execution_count": 8, "id": "295e7c3d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0j, 0j, 0j, 0j, 0j, 0j, 0j, (1+0j)]\n" ] } ], "source": [ "angle_quan_state = angle_enc.run_state_vector()\n", "print([np.round(i, 2) for i in angle_quan_state.numpy()])" ] }, { "cell_type": "markdown", "id": "c13e7bf8", "metadata": {}, "source": [ "The corresponding state is $\\left|111\\right>$ as we desired.\n", "\n", "In Paddle Quantum, we also provide a built-in method for angle encoding:" ] }, { "cell_type": "code", "execution_count": 9, "id": "af779e74", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0j, 0j, 0j, 0j, 0j, 0j, 0j, (1+0j)]\n" ] } ], "source": [ "# Built-in angle encoding\n", "# Number of qubits\n", "n = 3\n", "# Initialize the circuit\n", "built_in_angle_enc = UAnsatz(n)\n", "# Classical information x should be of type Tensor\n", "x = paddle.to_tensor([np.pi, np.pi, np.pi], 'float64')\n", "built_in_angle_enc.angle_encoding(x, \"ry\")\n", "state = built_in_angle_enc.run_state_vector()\n", "print([np.round(i, 2) for i in state.numpy()])" ] }, { "cell_type": "markdown", "id": "94d09e5f", "metadata": {}, "source": [ "## IQP Style Encoding\n", "\n", "IQP style encoding is a relatively complicated encoding strategy. We encode classical information $\\mathbf{x}$ to \n", "\n", "$$\n", "\\left|\\mathbf{x}\\right> = \\left(\\mathrm{U}_\\mathrm{Z}(\\mathbf{x})\\mathrm{H}^{\\otimes n}\\right)^{r}\\left|0^n\\right>,\n", "$$\n", "\n", "where $r$ is the depth of the circuit, indicating the repeating times of $\\mathrm{U}_\\mathrm{Z}(\\mathbf{x})\\mathrm{H}^{\\otimes n}$. $\\mathrm{H}^{\\otimes n}$ is a layer of Hadamard gates acting on all qubits. $\\mathrm{U}_\\mathrm{Z}(\\mathbf{x})$ is the key step in IQP encoding scheme:\n", "\n", "$$\n", "\\mathrm{U}_\\mathrm{Z}(\\mathbf{x})=\\prod\\limits_{[i,j]\\in S}R_{Z_iZ_j}(x_ix_j)\\bigotimes_{k=1}^{n} R_z(x_k),\n", "$$\n", "\n", "where $S$ is the set containing all pairs of qubits to be entangled using $R_{ZZ}$ gates.\n", "\n", "First, we consider a simple two-qubit gate: $R_{Z_1Z_2}(\\theta)$. Its mathematical form $e^{-i\\frac{\\theta}{2}Z_1\\otimes Z_2}$ can be seen as a two-qubit rotation gate around ZZ , which makes these two qubits entangled. \n", "One can implement this gate using Paddle Quantum as follows:" ] }, { "cell_type": "code", "execution_count": 10, "id": "bcde1189", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--*-----------------*--\n", " | | \n", "--X----Rz(2.000)----X--\n", " \n" ] } ], "source": [ "# Number of qubits\n", "n = 2\n", "# Initialize the circuit\n", "Rzz = UAnsatz(n)\n", "# Theta is the angle of Rzz gate\n", "theta = paddle.to_tensor([2], 'float64')\n", "# Implement Rzz gate\n", "Rzz.cnot([0, 1])\n", "Rzz.rz(theta, 1)\n", "Rzz.cnot([0, 1])\n", " \n", "print(Rzz)" ] }, { "cell_type": "markdown", "id": "89c92df4", "metadata": {}, "source": [ "In $\\mathrm{U}_\\mathrm{Z}$, an $R_{ZZ}$ gate needs to be added between every pair of qubits in set $S$. In our built-in IQP encoding method, users are allowed to define their customized set $S$.\n", "\n", "Now, let's take a look at how to implement IQP encoding using Paddle Quantum:" ] }, { "cell_type": "code", "execution_count": 11, "id": "38f5afac", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--H----Rz(-1.45)----*-----------------*------------------------------------------------\n", " | | \n", "--H----Rz(3.000)----X----Rz(-4.35)----X----*-----------------*-------------------------\n", " | | \n", "--H----Rz(2.000)---------------------------X----Rz(6.000)----X----*-----------------*--\n", " | | \n", "--H----Rz(-0.05)--------------------------------------------------X----Rz(-0.10)----X--\n", " \n" ] } ], "source": [ "# Number of qubits\n", "n = 4\n", "# Initialize the circuit\n", "iqp_enc = UAnsatz(n)\n", "# X is the classical information\n", "x = paddle.to_tensor([-1.45, 3, 2, -0.05], 'float64')\n", "# S is a list containing all the pairs to be entagled\n", "S = [[0, 1], [1, 2], [2, 3]]\n", "# r is the repeating times of U\n", "r = 1\n", "\n", "for i in range(r):\n", " # Add a layer of hadamard gates\n", " iqp_enc.superposition_layer()\n", " # Add a layer of rotation z gates\n", " for j in range(n):\n", " iqp_enc.rz(x[j] ,j)\n", " # Add a layer of ZZ gates\n", " for k in S:\n", " iqp_enc.cnot(k)\n", " iqp_enc.rz(x[k[0]] * x[k[1]], k[1])\n", " iqp_enc.cnot(k)\n", " \n", "print(iqp_enc)" ] }, { "cell_type": "markdown", "id": "a0e60e87", "metadata": {}, "source": [ "The corresponding quantum state after IQP style encoding is:" ] }, { "cell_type": "code", "execution_count": 12, "id": "187b7ec4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[(0.25+0j), (0.24719-0.03736j), (-0.0115+0.24974j), (-0.02397+0.24885j), (-0.01559-0.24951j), (-0.0527-0.24438j), (0.21313+0.13067j), (0.20633+0.14116j), (0.22138+0.11615j), (0.23625+0.08176j), (-0.12621+0.2158j), (-0.13684+0.20922j), (0.07483+0.23854j), (0.10964+0.22468j), (-0.2382-0.07589j), (-0.23411-0.0877j)]\n" ] } ], "source": [ "iqp_quan_state = iqp_enc.run_state_vector()\n", "print([np.round(i, 5) for i in iqp_quan_state.numpy()])" ] }, { "cell_type": "markdown", "id": "9aee1dcb", "metadata": {}, "source": [ "In Paddle Quantum, we provide a built-in IQP encoding method that exactly follows the way we explained above. However, this is just a particular case for IQP style encoding. IQP style encoding can refer \n", "to a more general class of encoding schemes. For example, you can replace the rotation Z gate with rotation X gate or rotation Y gate and replace $R_{ZZ}$ gate with $R_{XX}$ gate or $R_{YY}$ gate. Besides, you can think of three-qubit rotation gates, and add more layers with these three-qubit rotation gates." ] }, { "cell_type": "code", "execution_count": 13, "id": "a81b03cb", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[(0.25+0j), (0.24719-0.03736j), (-0.0115+0.24974j), (-0.02397+0.24885j), (-0.01559-0.24951j), (-0.0527-0.24438j), (0.21313+0.13067j), (0.20633+0.14116j), (0.22138+0.11615j), (0.23625+0.08176j), (-0.12621+0.2158j), (-0.13684+0.20922j), (0.07483+0.23854j), (0.10964+0.22468j), (-0.2382-0.07589j), (-0.23411-0.0877j)]\n" ] } ], "source": [ "# Built-in IQP style encoding\n", "# Number of qubits\n", "n = 4\n", "# Initialize the circuit\n", "built_in_iqp_enc = UAnsatz(n)\n", "# Classical information x should be of type Tensor\n", "x = paddle.to_tensor([-1.45, 3, 2, -0.05], 'float64')\n", "# S is a list containing all the pairs to be entagled\n", "S = [[0, 1], [1, 2], [2, 3]]\n", "# r is the repeating times of U\n", "r = 1\n", "built_in_iqp_enc.iqp_encoding(x, r, S)\n", "built_in_iqp_enc_state = built_in_iqp_enc.run_state_vector()\n", "print([np.round(i, 5) for i in built_in_iqp_enc_state.numpy()])" ] }, { "cell_type": "markdown", "id": "251975b7", "metadata": {}, "source": [ "## Hamiltonian Evolution Ansatz Encoding\n", "\n", "Hamiltonian evolution ansatz encoding, which uses a Trotter formula to approximate an evolution, has been explored in obtaining the ground state of a Hubbard model [4].\n", "\n", "$$\n", "\\left|\\mathbf{x}\\right> = \\left(\\prod\\limits_{i=1}^{n}R_{Z_iZ_{i+1}}(\\frac{t}{T}x_{i})R_{Y_iY_{i+1}}(\\frac{t}{T}x_{i})R_{X_iX_{i+1}}(\\frac{t}{T}x_{i})\\right)^{T}\\bigotimes_{i=1}^{n+1}\\left|\\psi_{i}\\right>,\n", "$$\n", "\n", "where $R_{XX}, R_{YY}, R_{ZZ}$ are the same rotation gates described in IQP style encoding, T is the number of Trotter steps, and $\\left|\\psi_{i}\\right>$ is a Haar-random single-qubit quantum state.\n", "You can implement this encoding by applying T layers of $R_{XX}, R_{YY}, R_{ZZ}$ gates to the prepared Haar-random quantum state." ] }, { "cell_type": "markdown", "id": "b4dddb79", "metadata": {}, "source": [ "---\n", "\n", "## References\n", "\n", "[1] Schuld, Maria. \"Quantum machine learning models are kernel methods.\" [arXiv:2101.11020 (2021).](https://arxiv.org/abs/2101.11020)\n", "\n", "[2] Havlíček, Vojtěch, et al. \"Supervised learning with quantum-enhanced feature spaces.\" [Nature 567.7747 (2019): 209-212.](https://www.nature.com/articles/s41586-019-0980-2)\n", "\n", "[3] Huang, Hsin-Yuan, et al. \"Power of data in quantum machine learning.\" [Nature Communications 12.1 (2021): 1-9.](https://www.nature.com/articles/s41467-021-22539-9)\n", "\n", "[4] Cade, Chris, et al. \"Strategies for solving the Fermi-Hubbard model on near-term quantum computers.\" [Physical Review B 102.23 (2020): 235122.](https://journals.aps.org/prb/abstract/10.1103/PhysRevB.102.235122)" ] } ], "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.0" } }, "nbformat": 4, "nbformat_minor": 5 }