{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Initial Experience of Quantum Neural Network\n", "\n", "Translator: [unseenme](https://gitee.com/unseenme)\n", "\n", "[](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/r2.7.0rc1/mindquantum/en/advanced/mindspore_initial_experience_of_quantum_neural_network.ipynb) \n", "[](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/r2.7.0rc1/mindquantum/en/advanced/mindspore_initial_experience_of_quantum_neural_network.py) \n", "[](https://gitee.com/mindspore/docs/blob/r2.7.0rc1/docs/mindquantum/docs/source_en/advanced/initial_experience_of_quantum_neural_network.ipynb)\n", "\n", "## Structure of Quantum Neural Network\n", "\n", "In MindSpore Quantum, the structure of the Quantum Neural Network (QNN) is shown in the figure below, which usually consists of three parts:\n", "\n", "(1) One (or more) encoding circuit(s) for encoding classical data to quantum data (usually called Encoder);\n", "\n", "(2) One (or more) training circuit(s) for training parameters in a quantum gate with parameters (usually called Ansatz);\n", "\n", "(3) One (or more) measurement(s) for detecting whether the measurement value (for example, the measurement in the Z direction is the projection of the quantum state of a qubit on the Z axis, and the measurement obtains the expected value of the quantum state with respect to the Pauli Z operator (not limited to the Pauli Z operator, it can be replaced by other operators)) is close to the target expected value.\n", "\n", "\n", "\n", "Below, we use a simple example to experience how to use MindSpore Quantum.\n", "\n", "## Simple Example\n", "\n", "\n", "\n", "We build a quantum neural network as shown in the figure above, where the Encoder consists of an [H](https://www.mindspore.cn/mindquantum/docs/en/r0.11/core/gates/mindquantum.core.gates.HGate.html) gate, an [RX](https://www.mindspore.cn/mindquantum/docs/en/r0.11/core/gates/mindquantum.core.gates.RX.html) gate, an [RY](https://www.mindspore.cn/mindquantum/docs/en/r0.11/core/gates/mindquantum.core.gates.RY.html) gate and an [RZ](https://www.mindspore.cn/mindquantum/docs/en/r0.11/core/gates/mindquantum.core.gates.RZ.html) gate, Ansatz consists of an [RX](https://www.mindspore.cn/mindquantum/docs/en/r0.11/core/gates/mindquantum.core.gates.RX.html) gate and an [RY](https://www.mindspore.cn/mindquantum/docs/en/r0.11/core/gates/mindquantum.core.gates.RY.html) gate, and the measurement is the Pauli `Z` operator acting on the 0th qubit.\n", "\n", "Problem description: We regard the Encoder as the error effect of the system on the initial quantum state (the parameters $\\alpha_0, \\alpha_1$ and $\\alpha_2$ are a fixed value obtained by preprocessing the original classical data (optional), that is, the known value, which is set to 0.2, 0.3 and 0.4 here respectively). We need to train an Ansatz to cancel out this error, so that the final quantum state is still in the $|0\\rangle$ state.\n", "\n", "Idea: Perform the Pauli `Z` operator measurement in the final state, and the measurement value at this time is the expected value of the quantum state at this time about the Pauli `Z` operator. Since $|0\\rangle$ is the eigenstate of operator `Z`, and the eigenvalue is 1, it is easy to know.\n", "\n", "$$\n", "\\langle 0|Z|0\\rangle=1.\n", "$$\n", "\n", "That is, the target expectation is 1. Whether the state at this time is $|0\\rangle$ can be verified by measuring the expected value.\n", "\n", "Solution: By training the parameters in the Ansatz, we hope that the measurement value is close to the target expected value. In other words, we only need to make the measurement value as close as possible to the expected value corresponding to the $|0\\rangle$ state with respect to the Pauli `Z` operator, then the state is $|0\\rangle$, that is, the Ansatz cancels the error generated by the Encoder on the initial quantum state.\n", "\n", "## Environment Preparation\n", "\n", "Import the modules that this tutorial depends on." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np # Import the numpy library and abbreviated as np\n", "from mindquantum.core.circuit import Circuit # Import the Circuit module for building quantum circuits\n", "from mindquantum.core.gates import H, RX, RY, RZ # Import quantum gates H, RX, RY, RZ\n", "from mindquantum.core.parameterresolver import PRGenerator # import a parameter generator" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Building Encoder\n", "\n", "According to the quantum circuit diagram shown, we can build the Encoder in MindSpore Quantum." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Circuit Summary \n", "╭──────────────────────┬────────────────────────╮\n", "│ Info │ value │\n", "├──────────────────────┼────────────────────────┤\n", "│ Number of qubit │ 1 │\n", "├──────────────────────┼────────────────────────┤\n", "│ Total number of gate │ 4 │\n", "│ Barrier │ 0 │\n", "│ Noise Channel │ 0 │\n", "│ Measurement │ 0 │\n", "├──────────────────────┼────────────────────────┤\n", "│ Parameter gate │ 3 │\n", "│ 3 encoder parameters │ alpha0, alpha1, alpha2 │\n", "╰──────────────────────┴────────────────────────╯\n", "\n" ], "text/plain": [ "\u001b[1;38;2;255;0;0m Circuit Summary \u001b[0m\n", "╭──────────────────────┬────────────────────────╮\n", "│\u001b[1m \u001b[0m\u001b[1;38;2;59;59;149mInfo\u001b[0m\u001b[1m \u001b[0m\u001b[1m \u001b[0m│\u001b[1m \u001b[0m\u001b[1;38;2;59;59;149mvalue\u001b[0m\u001b[1m \u001b[0m\u001b[1m \u001b[0m│\n", "├──────────────────────┼────────────────────────┤\n", "│ \u001b[1mNumber of qubit\u001b[0m │ 1 │\n", "├──────────────────────┼────────────────────────┤\n", "│ \u001b[1mTotal number of gate\u001b[0m │ 4 │\n", "│ Barrier │ 0 │\n", "│ Noise Channel │ 0 │\n", "│ Measurement │ 0 │\n", "├──────────────────────┼────────────────────────┤\n", "│ \u001b[1mParameter gate\u001b[0m │ 3 │\n", "│ 3 encoder parameters │ \u001b[38;2;255;200;87malpha0, alpha1, alpha2\u001b[0m │\n", "╰──────────────────────┴────────────────────────╯\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "" ], "text/plain": [ "
Circuit Summary \n", "╭──────────────────────┬────────────────────────╮\n", "│ Info │ value │\n", "├──────────────────────┼────────────────────────┤\n", "│ Number of qubit │ 1 │\n", "├──────────────────────┼────────────────────────┤\n", "│ Total number of gate │ 6 │\n", "│ Barrier │ 0 │\n", "│ Noise Channel │ 0 │\n", "│ Measurement │ 0 │\n", "├──────────────────────┼────────────────────────┤\n", "│ Parameter gate │ 5 │\n", "│ 3 encoder parameters │ alpha0, alpha1, alpha2 │\n", "│ 2 ansatz parameters │ theta0, theta1 │\n", "╰──────────────────────┴────────────────────────╯\n", "\n" ], "text/plain": [ "\u001b[1;38;2;255;0;0m Circuit Summary \u001b[0m\n", "╭──────────────────────┬────────────────────────╮\n", "│\u001b[1m \u001b[0m\u001b[1;38;2;59;59;149mInfo\u001b[0m\u001b[1m \u001b[0m\u001b[1m \u001b[0m│\u001b[1m \u001b[0m\u001b[1;38;2;59;59;149mvalue\u001b[0m\u001b[1m \u001b[0m\u001b[1m \u001b[0m│\n", "├──────────────────────┼────────────────────────┤\n", "│ \u001b[1mNumber of qubit\u001b[0m │ 1 │\n", "├──────────────────────┼────────────────────────┤\n", "│ \u001b[1mTotal number of gate\u001b[0m │ 6 │\n", "│ Barrier │ 0 │\n", "│ Noise Channel │ 0 │\n", "│ Measurement │ 0 │\n", "├──────────────────────┼────────────────────────┤\n", "│ \u001b[1mParameter gate\u001b[0m │ 5 │\n", "│ 3 encoder parameters │ \u001b[38;2;255;200;87malpha0, alpha1, alpha2\u001b[0m │\n", "│ 2 ansatz parameters │ \u001b[38;2;72;201;176mtheta0, theta1\u001b[0m │\n", "╰──────────────────────┴────────────────────────╯\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "" ], "text/plain": [ "
Software | \n", "Version | \n", "
---|---|
mindquantum | 0.9.11 |
scipy | 1.10.1 |
numpy | 1.23.5 |
System | \n", "Info | \n", "
Python | 3.9.16 |
OS | Linux x86_64 |
Memory | 8.3 GB |
CPU Max Thread | 8 |
Date | Sun Dec 31 21:30:32 2023 |