"Requirement already satisfied: tqdm in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from wenxin-api) (4.64.1)\n",
"Requirement already satisfied: requests>=2.20 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from wenxin-api) (2.24.0)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests>=2.20->wenxin-api) (2019.9.11)\n",
"Requirement already satisfied: chardet<4,>=3.0.2 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests>=2.20->wenxin-api) (3.0.4)\n",
"Requirement already satisfied: idna<3,>=2.5 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests>=2.20->wenxin-api) (2.8)\n",
"Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests>=2.20->wenxin-api) (1.25.6)\n",
"Installing collected packages: wenxin-api\n",
"Successfully installed wenxin-api-0.1.0\n",
"\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip available: \u001b[0m\u001b[31;49m22.1.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m22.3.1\u001b[0m\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n"
"Looking in indexes: https://mirrors.bfsu.edu.cn/pypi/web/simple\n",
"Requirement already satisfied: paddle-quantum in /Users/wangzihe/temp/baidu/QPlatform/PaddleQu (2.2.1)\n",
"Requirement already satisfied: paddlepaddle<=2.3.0 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from paddle-quantum) (2.3.0)\n",
"Requirement already satisfied: scipy in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from paddle-quantum) (1.7.1)\n",
"Collecting protobuf<=3.20.1\n",
" Using cached https://mirrors.bfsu.edu.cn/pypi/web/packages/92/0e/b8a60441178c8725fb3afa648e80c312a77feab31e7831d69c672b3c18cc/protobuf-3.20.1-cp37-cp37m-macosx_10_9_x86_64.whl (961 kB)\n",
"Requirement already satisfied: networkx>=2.5 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from paddle-quantum) (2.6.3)\n",
"Requirement already satisfied: qcompute in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from paddle-quantum) (3.0.0)\n",
"Requirement already satisfied: matplotlib>=3.3.0 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from paddle-quantum) (3.5.2)\n",
"Requirement already satisfied: tqdm in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from paddle-quantum) (4.64.0)\n",
"Requirement already satisfied: openfermion in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from paddle-quantum) (1.5.1)\n",
"Requirement already satisfied: opencv-python in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from paddle-quantum) (4.6.0.66)\n",
"Requirement already satisfied: scikit-learn in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from paddle-quantum) (1.0.2)\n",
"Requirement already satisfied: fastdtw in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from paddle-quantum) (0.3.4)\n",
"Requirement already satisfied: cvxpy in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from paddle-quantum) (1.2.2)\n",
"Requirement already satisfied: rich in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from paddle-quantum) (12.0.1)\n",
"Requirement already satisfied: pyscf in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from paddle-quantum) (2.1.1)\n",
"Requirement already satisfied: pillow>=6.2.0 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from matplotlib>=3.3.0->paddle-quantum) (9.3.0)\n",
"Requirement already satisfied: pyparsing>=2.2.1 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from matplotlib>=3.3.0->paddle-quantum) (3.0.9)\n",
"Requirement already satisfied: python-dateutil>=2.7 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from matplotlib>=3.3.0->paddle-quantum) (2.8.2)\n",
"Requirement already satisfied: packaging>=20.0 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from matplotlib>=3.3.0->paddle-quantum) (21.3)\n",
"Requirement already satisfied: cycler>=0.10 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from matplotlib>=3.3.0->paddle-quantum) (0.11.0)\n",
"Requirement already satisfied: numpy>=1.17 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from matplotlib>=3.3.0->paddle-quantum) (1.21.3)\n",
"Requirement already satisfied: fonttools>=4.22.0 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from matplotlib>=3.3.0->paddle-quantum) (4.38.0)\n",
"Requirement already satisfied: kiwisolver>=1.0.1 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from matplotlib>=3.3.0->paddle-quantum) (1.4.4)\n",
"Requirement already satisfied: paddle-bfloat==0.1.2 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from paddlepaddle<=2.3.0->paddle-quantum) (0.1.2)\n",
"Requirement already satisfied: astor in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from paddlepaddle<=2.3.0->paddle-quantum) (0.8.1)\n",
"Requirement already satisfied: requests>=2.20.0 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from paddlepaddle<=2.3.0->paddle-quantum) (2.28.0)\n",
"Requirement already satisfied: six in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from paddlepaddle<=2.3.0->paddle-quantum) (1.16.0)\n",
"Requirement already satisfied: decorator in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from paddlepaddle<=2.3.0->paddle-quantum) (5.1.1)\n",
"Requirement already satisfied: opt-einsum==3.3.0 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from paddlepaddle<=2.3.0->paddle-quantum) (3.3.0)\n",
"Requirement already satisfied: scs>=1.1.6 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from cvxpy->paddle-quantum) (3.2.2)\n",
"Requirement already satisfied: osqp>=0.4.1 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from cvxpy->paddle-quantum) (0.6.2.post5)\n",
"Requirement already satisfied: ecos>=2 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from cvxpy->paddle-quantum) (2.0.10)\n",
"Requirement already satisfied: cirq-google>=0.15.0 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from openfermion->paddle-quantum) (1.0.0)\n",
"Requirement already satisfied: sympy in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from openfermion->paddle-quantum) (1.10.1)\n",
"Requirement already satisfied: deprecation in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from openfermion->paddle-quantum) (2.1.0)\n",
"Requirement already satisfied: cirq-core>=0.15.0 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from openfermion->paddle-quantum) (1.0.0)\n",
"Requirement already satisfied: pubchempy in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from openfermion->paddle-quantum) (1.0.4)\n",
"Requirement already satisfied: h5py>=2.8 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from openfermion->paddle-quantum) (3.7.0)\n",
"Collecting scipy\n",
" Using cached https://mirrors.bfsu.edu.cn/pypi/web/packages/4c/4a/440cc9703938bbc86636ff6b9e17810f3d0f06e9b41891c5433dc4cd9091/scipy-1.1.0-cp37-cp37m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (16.7 MB)\n",
"Collecting qcompute\n",
" Using cached https://mirrors.bfsu.edu.cn/pypi/web/packages/f6/57/823a3f3e3fd6e327453c4d028751fee7292784e1f2447ae6b7c0f3cc6565/qcompute-2.0.6-py3-none-any.whl (172 kB)\n",
" Using cached https://mirrors.bfsu.edu.cn/pypi/web/packages/8e/03/0a64ec2b7e6395fa53688ad3e489163218c45f36f0c96b448e0279391538/qcompute-2.0.4-py3-none-any.whl (96 kB)\n",
"Requirement already satisfied: bidict in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from qcompute->paddle-quantum) (0.22.0)\n",
"Requirement already satisfied: pyprimes in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from qcompute->paddle-quantum) (0.1)\n",
"Requirement already satisfied: bce-python-sdk in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from qcompute->paddle-quantum) (0.8.73)\n",
"Requirement already satisfied: commonmark<0.10.0,>=0.9.0 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from rich->paddle-quantum) (0.9.1)\n",
"Requirement already satisfied: pygments<3.0.0,>=2.6.0 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from rich->paddle-quantum) (2.13.0)\n",
"Requirement already satisfied: typing-extensions<5.0,>=3.7.4 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from rich->paddle-quantum) (4.3.0)\n",
"Requirement already satisfied: joblib>=0.11 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from scikit-learn->paddle-quantum) (1.2.0)\n",
"Requirement already satisfied: threadpoolctl>=2.0.0 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from scikit-learn->paddle-quantum) (3.1.0)\n",
"Requirement already satisfied: backports.cached-property~=1.0.1 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from cirq-core>=0.15.0->openfermion->paddle-quantum) (1.0.2)\n",
"Requirement already satisfied: duet~=0.2.7 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from cirq-core>=0.15.0->openfermion->paddle-quantum) (0.2.7)\n",
"Requirement already satisfied: pandas in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from cirq-core>=0.15.0->openfermion->paddle-quantum) (1.3.5)\n",
"Requirement already satisfied: sortedcontainers~=2.0 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from cirq-core>=0.15.0->openfermion->paddle-quantum) (2.4.0)\n",
"Requirement already satisfied: google-api-core[grpc]<2.0.0dev,>=1.14.0 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from cirq-google>=0.15.0->openfermion->paddle-quantum) (1.33.2)\n",
"Requirement already satisfied: proto-plus>=1.20.0 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from cirq-google>=0.15.0->openfermion->paddle-quantum) (1.22.1)\n",
"Requirement already satisfied: qdldl in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from osqp>=0.4.1->cvxpy->paddle-quantum) (0.1.5.post2)\n",
"Requirement already satisfied: charset-normalizer~=2.0.0 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from requests>=2.20.0->paddlepaddle<=2.3.0->paddle-quantum) (2.0.12)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from requests>=2.20.0->paddlepaddle<=2.3.0->paddle-quantum) (2022.9.24)\n",
"Requirement already satisfied: idna<4,>=2.5 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from requests>=2.20.0->paddlepaddle<=2.3.0->paddle-quantum) (3.4)\n",
"Requirement already satisfied: urllib3<1.27,>=1.21.1 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from requests>=2.20.0->paddlepaddle<=2.3.0->paddle-quantum) (1.26.12)\n",
"Requirement already satisfied: future>=0.6.0 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from bce-python-sdk->qcompute->paddle-quantum) (0.18.2)\n",
"Requirement already satisfied: pycryptodome>=3.8.0 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from bce-python-sdk->qcompute->paddle-quantum) (3.15.0)\n",
"Requirement already satisfied: mpmath>=0.19 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from sympy->openfermion->paddle-quantum) (1.2.1)\n",
"Collecting typing-extensions<5.0,>=3.7.4\n",
" Using cached https://mirrors.bfsu.edu.cn/pypi/web/packages/2e/35/6c4fff5ab443b57116cb1aad46421fb719bed2825664e8fe77d66d99bcbc/typing_extensions-3.10.0.0-py3-none-any.whl (26 kB)\n",
"Collecting protobuf<=3.20.1\n",
" Using cached https://mirrors.bfsu.edu.cn/pypi/web/packages/ea/fe/82cf68917308b208731487f986db209e56903c30e324499b6bf0cc6a6203/protobuf-3.19.6-cp37-cp37m-macosx_10_9_x86_64.whl (979 kB)\n",
"Requirement already satisfied: google-auth<3.0dev,>=1.25.0 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from google-api-core[grpc]<2.0.0dev,>=1.14.0->cirq-google>=0.15.0->openfermion->paddle-quantum) (2.14.1)\n",
"Requirement already satisfied: googleapis-common-protos<2.0dev,>=1.56.2 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from google-api-core[grpc]<2.0.0dev,>=1.14.0->cirq-google>=0.15.0->openfermion->paddle-quantum) (1.57.0)\n",
"Requirement already satisfied: grpcio<2.0dev,>=1.33.2 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from google-api-core[grpc]<2.0.0dev,>=1.14.0->cirq-google>=0.15.0->openfermion->paddle-quantum) (1.50.0)\n",
"Requirement already satisfied: grpcio-status<2.0dev,>=1.33.2 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from google-api-core[grpc]<2.0.0dev,>=1.14.0->cirq-google>=0.15.0->openfermion->paddle-quantum) (1.48.2)\n",
"Requirement already satisfied: pytz>=2017.3 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from pandas->cirq-core>=0.15.0->openfermion->paddle-quantum) (2022.6)\n",
"Requirement already satisfied: pyasn1-modules>=0.2.1 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from google-auth<3.0dev,>=1.25.0->google-api-core[grpc]<2.0.0dev,>=1.14.0->cirq-google>=0.15.0->openfermion->paddle-quantum) (0.2.8)\n",
"Requirement already satisfied: cachetools<6.0,>=2.0.0 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from google-auth<3.0dev,>=1.25.0->google-api-core[grpc]<2.0.0dev,>=1.14.0->cirq-google>=0.15.0->openfermion->paddle-quantum) (5.2.0)\n",
"Requirement already satisfied: rsa<5,>=3.1.4 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from google-auth<3.0dev,>=1.25.0->google-api-core[grpc]<2.0.0dev,>=1.14.0->cirq-google>=0.15.0->openfermion->paddle-quantum) (4.9)\n",
"Requirement already satisfied: pyasn1<0.5.0,>=0.4.6 in /Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages (from pyasn1-modules>=0.2.1->google-auth<3.0dev,>=1.25.0->google-api-core[grpc]<2.0.0dev,>=1.14.0->cirq-google>=0.15.0->openfermion->paddle-quantum) (0.4.8)\n",
"/Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages/scipy/linalg/__init__.py:212: DeprecationWarning: The module numpy.dual is deprecated. Instead of using dual, use the functions directly from numpy or scipy.\n",
" from numpy.dual import register_func\n",
"/Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages/scipy/sparse/sputils.py:16: DeprecationWarning: `np.typeDict` is a deprecated alias for `np.sctypeDict`.\n",
" supported_dtypes = [np.typeDict[x] for x in supported_dtypes]\n",
"/Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages/scipy/special/orthogonal.py:81: DeprecationWarning: `np.int` is a deprecated alias for the builtin `int`. To silence this warning, use `int` by itself. Doing this will not modify any behavior and is safe. When replacing `np.int`, you may wish to use e.g. `np.int64` or `np.int32` to specify the precision. If you wish to review your current use, check the release note link for additional information.\n",
"Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
" from numpy import (exp, inf, pi, sqrt, floor, sin, cos, around, int,\n",
"/Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages/scipy/io/matlab/mio5.py:98: DeprecationWarning: `np.bool` is a deprecated alias for the builtin `bool`. To silence this warning, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.\n",
"Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
"/Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages/paddle/tensor/creation.py:125: DeprecationWarning: `np.object` is a deprecated alias for the builtin `object`. To silence this warning, use `object` by itself. Doing this will not modify any behavior and is safe. \n",
"Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
" if data.dtype == np.object:\n",
"/Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages/paddle/fluid/framework.py:1104: DeprecationWarning: `np.bool` is a deprecated alias for the builtin `bool`. To silence this warning, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.\n",
"Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
"Variational Shadow Quantum Learning (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."
]
},
{
"cell_type": "markdown",
"id": "99c07da5",
"metadata": {},
"source": [
"## 2. Introduction to the Model Principle\n",
"\n",
"The flow chart of VSQL is as follows.\n",
"\n",
"![pipeline](https://ai-studio-static-online.cdn.bcebos.com/2b806cc0405e425995df1786a5c5976196c5ca83697647d9ae70ac7cc0bf83c9 \"Flow chart of VSQL\")\n",
"<div style=\"text-align:center\">Flow chart of VSQL</div>\n",
"\n",
"The training steps are as below.\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.\n",
"\n",
"### 2.1 Introduction to local shadow circuits\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](https://ai-studio-static-online.cdn.bcebos.com/818b3c2bac5d4ef0b73e223c357b49d688649f036d7b4d798fb54838a555c6e6 \"The first circuit\")\n",
"<div style=\"text-align:center\">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](https://ai-studio-static-online.cdn.bcebos.com/b58591c4adfc4d18a657cf1b811f4fc0c6dced8fd6b54724a78fbe1c86bb32dc \"The second circuit\")\n",
"<div style=\"text-align:center\">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](https://ai-studio-static-online.cdn.bcebos.com/57089cc1c0dd412ba984a0b12e20f5d88585aaa4d293455aa9f6bb7869d7f771 \"The last circuit\")\n",
"<div style=\"text-align:center\">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 the follow figure.\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": "markdown",
"id": "bbec4432",
"metadata": {},
"source": [
"## 3. Model Performance\n",
"\n",
"The binary classification effect of VSQL on MNIST dataset is as follows.\n",
"As shown in the tables, VSQL can achieve higher classification accuracy with fewer parameters than other quantum neural networks. Compared with classical neural networks, VSQL can also achieve similar results in ten-class classification tasks with fewer parameters. And when the amount of data is small, VSQL can achieve better results than classical neural networks."
]
},
{
"cell_type": "markdown",
"id": "0af2eccc",
"metadata": {},
"source": [
"## 4. How to Use the Model\n",
"\n",
"Configure the environment according to the following code."
"/Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages/scipy/linalg/__init__.py:212: DeprecationWarning: The module numpy.dual is deprecated. Instead of using dual, use the functions directly from numpy or scipy.\n",
" from numpy.dual import register_func\n",
"/Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages/scipy/sparse/sputils.py:16: DeprecationWarning: `np.typeDict` is a deprecated alias for `np.sctypeDict`.\n",
" supported_dtypes = [np.typeDict[x] for x in supported_dtypes]\n",
"/Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages/scipy/special/orthogonal.py:81: DeprecationWarning: `np.int` is a deprecated alias for the builtin `int`. To silence this warning, use `int` by itself. Doing this will not modify any behavior and is safe. When replacing `np.int`, you may wish to use e.g. `np.int64` or `np.int32` to specify the precision. If you wish to review your current use, check the release note link for additional information.\n",
"Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
" from numpy import (exp, inf, pi, sqrt, floor, sin, cos, around, int,\n",
"/Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages/scipy/io/matlab/mio5.py:98: DeprecationWarning: `np.bool` is a deprecated alias for the builtin `bool`. To silence this warning, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.\n",
"Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
"/Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages/paddle/tensor/creation.py:125: DeprecationWarning: `np.object` is a deprecated alias for the builtin `object`. To silence this warning, use `object` by itself. Doing this will not modify any behavior and is safe. \n",
"Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
" if data.dtype == np.object:\n",
"/Users/wangzihe/opt/anaconda3/envs/py37/lib/python3.7/site-packages/paddle/fluid/framework.py:1104: DeprecationWarning: `np.bool` is a deprecated alias for the builtin `bool`. To silence this warning, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.\n",
"Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
" elif dtype == np.bool:\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"For handwritten digits 0, the model has 89.22% confidence that it is 0 and 10.78% confidence that it is 1.\n",
"For handwritten digits 1, the model has 18.29% confidence that it is 0 and 81.71% confidence that it is 1.\n"
" f\"the model has {test_prob[0][0].item():3.2%} confidence that it is 0 \"\n",
" f\"and {test_prob[0][1].item():3.2%} confidence that it is 1.\"\n",
")\n",
"print(\n",
" f\"For handwritten digits 1, \"\n",
" f\"the model has {test_prob[1][0].item():3.2%} confidence that it is 0 \"\n",
" f\"and {test_prob[1][1].item():3.2%} confidence that it is 1.\"\n",
")"
]
},
{
"cell_type": "markdown",
"id": "3990efae",
"metadata": {},
"source": [
"## 5. Note\n",
"\n",
"The model we provide is a binary classification model that can only be used to distinguish handwritten digits 0 and 1. For other classification tasks, it needs to be retrained."
]
},
{
"cell_type": "markdown",
"id": "4fe699ea",
"metadata": {},
"source": [
"## 6. Related papers and citations\n",
"\n",
"```\n",
"@inproceedings{li2021vsql,\n",
" title={VSQL: Variational shadow quantum learning for classification},\n",
" author={Li, Guangxi and Song, Zhixin and Wang, Xin},\n",
" booktitle={Proceedings of the AAAI Conference on Artificial Intelligence},\n",