"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."
"Variational Shadow Quantum Learning (VSQL) is a hybrid 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."
"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."
"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.\n"
"After installing Paddle Quantum successfully, let's load the VSQL model and the images to be predicted."
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 2,
"execution_count": 2,
"id": "86d4405c",
"id": "7d88445e",
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [],
{
"name": "stderr",
"output_type": "stream",
"text": [
"/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",
"# The overall configuration file of the model.\n",
"# Enter the current task, which can be 'train' or 'test', representing training and prediction respectively. Here we use test, indicating that we want to make a prediction.\n",
"task = 'test'\n",
"# The file path of the image to be predicted.\n",
"image_path = 'data_0.png'\n",
"# Whether the image path above is a folder or not. For folder paths, we will predict all image files inside the folder. This way you can test multiple images at once.\n",
"is_dir = false\n",
"# The file path of the trained model parameter file.\n",
"model_path = 'vsql.pdparams'\n",
"# The number of qubits that the quantum circuit contains.\n",
"num_qubits = 10\n",
"# The number of qubits that the shadow circuit contains.\n",
"num_shadow = 2\n",
"# Circuit depth.\n",
"depth = 1\n",
"# The class to be predicted by the model. Here, 0 and 1 are classified.\n",
"classes = [0, 1]\n",
"\"\"\""
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "35514ce7",
"metadata": {},
"source": [
"Then, we use the VSQL model to make predictions."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "ca32fb07",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"For the input image, the model has 89.22% confidence that it is 0, and 10.78% confidence that it is 1.\n"
]
}
],
"source": [
"config = toml.loads(test_toml)\n",
"task = config.pop('task')\n",
"prediction, prob = inference(**config)\n",
"prob = prob[0]\n",
"msg = 'For the input image, the model has'\n",
"for idx, item in enumerate(prob):\n",
" if idx == len(prob) - 1:\n",
" msg += 'and'\n",
" label = config['classes'][idx]\n",
" msg += f' {item:3.2%} confidence that it is {label:d}'\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",
"name": "stdout",
"output_type": "stream",
"output_type": "stream",
"text": [
"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 the input image, the model has 18.29% confidence that it is 0, and 81.71% 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"
"# The overall configuration file of the model.\n",
"test_data = [np.pad(datum, pad_width=(0, 2 ** num_qubits - datum.size)) for datum in test_data]\n",
"# Enter the current task, which can be 'train' or 'test', representing training and prediction respectively. Here we use test, indicating that we want to make a prediction.\n",
"test_data = [paddle.to_tensor(datum / np.linalg.norm(datum), dtype=pq.get_dtype()) for datum in test_data]\n",
"task = 'test'\n",
"# Use the model to make predictions and get the corresponding probability\n",
"# The file path of the image to be predicted.\n",
"# Whether the image path above is a folder or not. For folder paths, we will predict all image files inside the folder. This way you can test multiple images at once.\n",
"print(\n",
"is_dir = false\n",
" f\"For handwritten digits 0, \"\n",
"# The file path of the trained model parameter file.\n",
" f\"the model has {test_prob[0][0].item():3.2%} confidence that it is 0 \"\n",
"model_path = 'vsql.pdparams'\n",
" f\"and {test_prob[0][1].item():3.2%} confidence that it is 1.\"\n",
"# The number of qubits that the quantum circuit contains.\n",
")\n",
"num_qubits = 10\n",
"print(\n",
"# The number of qubits that the shadow circuit contains.\n",
" f\"For handwritten digits 1, \"\n",
"num_shadow = 2\n",
" f\"the model has {test_prob[1][0].item():3.2%} confidence that it is 0 \"\n",
"# Circuit depth.\n",
" f\"and {test_prob[1][1].item():3.2%} confidence that it is 1.\"\n",
"depth = 1\n",
")"
"# The class to be predicted by the model. Here, 0 and 1 are classified.\n",
"classes = [0, 1]\n",
"\"\"\"\n",
"\n",
"config = toml.loads(test_toml)\n",
"task = config.pop('task')\n",
"prediction, prob = inference(**config)\n",
"prob = prob[0]\n",
"msg = 'For the input image, the model has'\n",
"for idx, item in enumerate(prob):\n",
" if idx == len(prob) - 1:\n",
" msg += 'and'\n",
" label = config['classes'][idx]\n",
" msg += f' {item:3.2%} confidence that it is {label:d}'\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."
"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.\n",
"\n",
"A more detailed description of the use can be found at https://github.com/PaddlePaddle/Quantum/blob/master/applications/handwritten_digits_classification/introduction_en.ipynb .\n",
"\n",
"A detailed description of the VSQL model can be found at https://github.com/PaddlePaddle/Quantum/blob/master/tutorials/machine_learning/VSQL_EN.ipynb ."
]
]
},
},
{
{
...
@@ -374,7 +413,7 @@
...
@@ -374,7 +413,7 @@
],
],
"metadata": {
"metadata": {
"kernelspec": {
"kernelspec": {
"display_name": "Python 3.7.15 ('py37')",
"display_name": "pq-dev",
"language": "python",
"language": "python",
"name": "python3"
"name": "python3"
},
},
...
@@ -388,7 +427,7 @@
...
@@ -388,7 +427,7 @@
"name": "python",
"name": "python",
"nbconvert_exporter": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"pygments_lexer": "ipython3",
"version": "3.7.15"
"version": "3.8.15 (default, Nov 10 2022, 13:17:42) \n[Clang 14.0.6 ]"