{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 变分量子线路\n",
    "\n",
    "[![下载Notebook](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r2.5.0/resource/_static/logo_notebook.svg)](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/r2.5.0/mindquantum/zh_cn/beginner/mindspore_parameterized_quantum_circuit.ipynb) \n",
    "[![下载样例代码](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r2.5.0/resource/_static/logo_download_code.svg)](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/r2.5.0/mindquantum/zh_cn/beginner/mindspore_parameterized_quantum_circuit.py) \n",
    "[![查看源文件](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r2.5.0/resource/_static/logo_source.svg)](https://gitee.com/mindspore/docs/blob/r2.5.0/docs/mindquantum/docs/source_zh_cn/beginner/parameterized_quantum_circuit.ipynb)\n",
    "\n",
    "## 概述\n",
    "\n",
    "变分量子线路(Parameterized Quantum Circuit, PQC)即由含参数的量子门组成的量子线路,是进行量子机器学习的途径之一。在很多情况下,为了能与经典机器学习中神经网络进行类比,我们也经常会把变分量子线路称为量子神经网络。量子-经典混合计算架构MindSpore Quantum能够处理此类变分量子线路,并利用量子神经网络的可逆性对该线路进行自动微分,最后通过测量得到的观测值,即可计算出观测值对于各参数的导数。\n",
    "\n",
    "构建PQC并用PQC模拟器算子对量子线路进行演化的大致流程如下:\n",
    "\n",
    "- Step 1 初始化量子线路;\n",
    "\n",
    "- Step 2 在量子线路中加入所需的含参量子门或者不含参量子门;\n",
    "\n",
    "- Step 3 利用PQC模拟器算子进行态演化或者梯度求解。\n",
    "\n",
    "## 环境准备\n",
    "\n",
    "导入本教程所依赖的模块。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np                                          # 导入numpy库并简写为np\n",
    "from mindquantum.core.gates import X, Y, Z, H, RX, RY, RZ   # 导入量子门H, X, Y, Z, RX, RY, RZ"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "说明:\n",
    "\n",
    "(1)numpy是一个功能强大的Python库,主要用于对多维数组执行计算,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库;\n",
    "\n",
    "(2)mindquantum是量子-经典混合计算框架,支持多种量子神经网络的训练和推理;\n",
    "\n",
    "(3)搭建的量子线路中所需执行的量子门需要从mindquantum.core模块中导入;\n",
    "\n",
    "## 量子门\n",
    "\n",
    "量子门(量子逻辑门)是对量子比特进行操作的基本逻辑单元,是量子线路的基础。对于经典电路来说,任意的逻辑电路都可以由一系列基本逻辑门构成,类似地,任意的量子线路也可以由一系列基本量子门构成,如单量子比特门和受控非门。常用的基本量子门有 [X](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.XGate.html) 门、[Y](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.YGate.html)门、[Z](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.ZGate.html)门、[Hadamard](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.HGate.html)门(`H`门)、[CNOT](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.CNOTGate.html)门以及旋转门[RX](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.RX.html)门、[RY](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.RY.html)门和[RZ](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.RZ.html)门。\n",
    "\n",
    "一般来说,量子门可以分为含参量子门和不含参量子门。例如,不含参的量子门有[X](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.XGate.html) 门、[Y](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.YGate.html)门、[Z](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.ZGate.html)门、[Hadamard](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.HGate.html)门(`H`门)、[CNOT](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.CNOTGate.html)门,它们的矩阵形式分别如下:\n",
    "\n",
    "$$\n",
    "\\text{X}=\n",
    "\\left(\n",
    "    \\begin{matrix}\n",
    "        0&1\\\\\n",
    "        1&0\n",
    "    \\end{matrix}\n",
    "\\right),\n",
    "\\text{Y}=\n",
    "\\left(\n",
    "    \\begin{matrix}\n",
    "        0&-i\\\\\n",
    "        i&0\n",
    "    \\end{matrix}\n",
    "\\right),\n",
    "\\text{Z}=\n",
    "\\left(\n",
    "    \\begin{matrix}\n",
    "        1&0\\\\\n",
    "        0&-1\n",
    "    \\end{matrix}\n",
    "\\right),\n",
    "\\text{H}=\\frac{1}{\\sqrt{2}}\n",
    "\\left(\n",
    "    \\begin{matrix}\n",
    "        1&1\\\\\n",
    "        1&-1\n",
    "    \\end{matrix}\n",
    "\\right),\n",
    "\\text{CNOT}=\n",
    "\\left(\n",
    "    \\begin{matrix}\n",
    "        1&0&0&0\\\\\n",
    "        0&1&0&0\\\\\n",
    "        0&0&0&1\\\\\n",
    "        0&0&1&0\n",
    "    \\end{matrix}\n",
    "\\right).$$\n",
    "\n",
    "分别打印上述量子门的矩阵形式,可以得到:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gate name: X\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[0, 1],\n",
       "       [1, 0]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('Gate name:', X)\n",
    "X.matrix()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gate name: Y\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 0.+0.j, -0.-1.j],\n",
       "       [ 0.+1.j,  0.+0.j]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('Gate name:', Y)\n",
    "Y.matrix()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gate name: Z\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 1,  0],\n",
       "       [ 0, -1]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('Gate name:', Z)\n",
    "Z.matrix()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gate name: H\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 0.70710678,  0.70710678],\n",
       "       [ 0.70710678, -0.70710678]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('Gate name:', H)\n",
    "H.matrix()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对于[CNOT](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.CNOTGate.html)门,其本质上是受控[X](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.XGate.html)门(`Controlled-X` gate),因此在MindSpore Quantum中,如果我们需要执行[CNOT](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.CNOTGate.html)门,只需设定[X](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.XGate.html)门的控制比特位和目标比特位即可(实际上,任意的量子门我们都可以设定控制比特位和所需执行量子门操作的目标比特位)。例如:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "X(0 <-: 1)\n"
     ]
    }
   ],
   "source": [
    "cnot = X.on(0, 1)   # X门作用在第0位量子比特且受第1位量子比特控制\n",
    "print(cnot)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "说明:\n",
    "\n",
    "(1)X(0 <-: 1) ,表示第0位量子比特位为目标比特位,第1位量子比特位为控制比特位,第0位量子比特受第1位量子比特控制,若第1位量子比特为1,则对第0位量子比特执行[X](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.XGate.html)门操作,否则不作任何操作;\n",
    "\n",
    "上面介绍了一些常用的不含参量子门,接下来,我们将介绍一些含参量子门(如旋转门[RX](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.RX.html)门、[RY](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.RY.html)门和[RZ](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.RZ.html)门),通过赋予旋转角度$\\theta$某些确定的值,可以得到作用不同的旋转门。另外,这些含参量子门是后续搭建量子神经网络的重要组成单元。\n",
    "\n",
    "例如,[RX](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.RX.html)门、[RY](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.RY.html)门和[RZ](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.RZ.html)门的矩阵形式如下:\n",
    "\n",
    "$$\\text{RX}(\\theta)= e^{-\\frac{i\\theta X}{2}}=\\cos\\left(\\frac{\\theta}{2}\\right)\\cdot I-i\\sin\\left(\\frac{\\theta}{2}\\right)\\cdot X=\n",
    "\\left(\n",
    "    \\begin{matrix}\n",
    "        \\cos\\left(\\frac{\\theta}{2}\\right)&-i\\sin\\left(\\frac{\\theta}{2}\\right)\\\\\n",
    "        -i\\sin\\left(\\frac{\\theta}{2}\\right)&\\cos\\left(\\frac{\\theta}{2}\\right)\n",
    "    \\end{matrix}\n",
    "\\right),$$\n",
    "\n",
    "$$\\text{RY}(\\theta)= e^{-\\frac{i\\theta Y}{2}}=\\cos\\left(\\frac{\\theta}{2}\\right)\\cdot I-i\\sin\\left(\\frac{\\theta}{2}\\right)\\cdot Y=\n",
    "\\left(\n",
    "    \\begin{matrix}\n",
    "        \\cos\\left(\\frac{\\theta}{2}\\right)&-\\sin\\left(\\frac{\\theta}{2}\\right)\\\\\n",
    "        \\sin\\left(\\frac{\\theta}{2}\\right)&\\cos\\left(\\frac{\\theta}{2}\\right)\n",
    "    \\end{matrix}\n",
    "\\right),$$\n",
    "\n",
    "$$\\text{RZ}(\\theta)= e^{-\\frac{i\\theta Z}{2}}=\\cos\\left(\\frac{\\theta}{2}\\right)\\cdot I-i\\sin\\left(\\frac{\\theta}{2}\\right)\\cdot Z=\n",
    "\\left(\n",
    "    \\begin{matrix}\n",
    "        e^{-\\frac{i\\theta}{2}}&0\\\\\n",
    "        0&e^{\\frac{i\\theta}{2}}\n",
    "    \\end{matrix}\n",
    "\\right).$$\n",
    "\n",
    "我们令$\\theta$分别为$0$和$\\pi$,然后打印$\\text{RX}(0)$门、$\\text{RY}(\\pi)$门和$\\text{RZ}(\\pi)$门的矩阵形式,可以得到:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gate name: RX(theta)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[1.+0.j, 0.+0.j],\n",
       "       [0.+0.j, 1.+0.j]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rx = RX('theta')\n",
    "print('Gate name:', rx)\n",
    "rx.matrix({'theta': 0})   # 赋予theta的值为0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当$\\theta=0$时,此时$\\text{RX}(0)$门就是我们熟悉的[I](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.IGate.html)门。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gate name: RY(theta)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 0.+0.j, -1.+0.j],\n",
       "       [ 1.+0.j,  0.+0.j]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ry = RY('theta')\n",
    "print('Gate name:', ry)\n",
    "np.round(ry.matrix({'theta': np.pi}))   # pi需要从np中导入,赋予theta的值为pi。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当$\\theta=\\pi$时,此时$\\text{RY}(\\pi)$门就是我们熟悉的[Y](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.YGate.html)门。(相差一个全局相位$i$)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gate name: RZ(theta)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[0.-1.j, 0.+0.j],\n",
       "       [0.+0.j, 0.+1.j]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rz = RZ('theta')\n",
    "print('Gate name:', rz)\n",
    "np.round(rz.matrix({'theta': np.pi}))   # 赋予theta的值为pi,由于计算机中存在浮点数不精确的问题,因此通过函数np.round返回浮点数的四舍五入值。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当$\\theta=\\pi$时,此时$\\text{RZ}(\\pi)$门就是我们熟悉的[Z](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.ZGate.html)门。(相差一个全局相位$-i$)\n",
    "\n",
    "## 量子线路\n",
    "\n",
    "量子线路(也称量子逻辑电路)是最常用的通用量子计算模型,表示在抽象概念下,对于量子比特进行操作的线路。类比于经典线路,我们可以把一系列的量子逻辑门进行精心的设计组合,构成一个量子线路并完成一定的任务。例如,我们构建如下图所示的量子线路,该量子线路由三个量子门构成,分别是作用在$q_0$比特上的[H](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.HGate.html)门,作用在$q_0$和$q_1$比特上的[CNOT](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.CNOTGate.html)门(即作用在$q_1$比特上且受$q_0$比特控制的[X](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.XGate.html)门)和作用在$q_2$比特上的$\\text{RY}(\\theta)$门。\n",
    "\n",
    "![quantum circuit](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r2.5.0/docs/mindquantum/docs/source_zh_cn/images/quantum_circuit.png)\n",
    "\n",
    "通过在量子线路中添加作用在不同量子比特位上的量子门即可快速完成对量子线路的搭建。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      ┏━━━┓           \n",
      "q0: ──┨ H ┠───■───────\n",
      "      ┗━━━┛   ┃       \n",
      "            ┏━┻━┓     \n",
      "q1: ────────┨╺╋╸┠─────\n",
      "            ┗━━━┛     \n",
      "      ┏━━━━━━━━━━━┓   \n",
      "q2: ──┨ RY(theta) ┠───\n",
      "      ┗━━━━━━━━━━━┛   \n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #ff0000; text-decoration-color: #ff0000; font-weight: bold\">        Circuit Summary         </span>\n",
       "╭──────────────────────┬───────╮\n",
       "│<span style=\"font-weight: bold\"> </span><span style=\"color: #3b3b95; text-decoration-color: #3b3b95; font-weight: bold\">Info</span><span style=\"font-weight: bold\">                 </span>│<span style=\"font-weight: bold\"> </span><span style=\"color: #3b3b95; text-decoration-color: #3b3b95; font-weight: bold\">value</span><span style=\"font-weight: bold\"> </span>│\n",
       "├──────────────────────┼───────┤\n",
       "│ <span style=\"font-weight: bold\">Number of qubit</span>      │ 3     │\n",
       "├──────────────────────┼───────┤\n",
       "│ <span style=\"font-weight: bold\">Total number of gate</span> │ 3     │\n",
       "│ Barrier              │ 0     │\n",
       "│ Noise Channel        │ 0     │\n",
       "│ Measurement          │ 0     │\n",
       "├──────────────────────┼───────┤\n",
       "│ <span style=\"font-weight: bold\">Parameter gate</span>       │ 1     │\n",
       "│ 1 ansatz parameter   │ <span style=\"color: #48c9b0; text-decoration-color: #48c9b0\">theta</span> │\n",
       "╰──────────────────────┴───────╯\n",
       "</pre>\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│\n",
       "├──────────────────────┼───────┤\n",
       "│ \u001b[1mNumber of qubit\u001b[0m      │ 3     │\n",
       "├──────────────────────┼───────┤\n",
       "│ \u001b[1mTotal number of gate\u001b[0m │ 3     │\n",
       "│ Barrier              │ 0     │\n",
       "│ Noise Channel        │ 0     │\n",
       "│ Measurement          │ 0     │\n",
       "├──────────────────────┼───────┤\n",
       "│ \u001b[1mParameter gate\u001b[0m       │ 1     │\n",
       "│ 1 ansatz parameter   │ \u001b[38;2;72;201;176mtheta\u001b[0m │\n",
       "╰──────────────────────┴───────╯\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from mindquantum.core.circuit import Circuit     # 导入Circuit模块,用于搭建量子线路\n",
    "\n",
    "encoder = Circuit()                              # 初始化量子线路\n",
    "encoder += H.on(0)                               # H门作用在第0位量子比特\n",
    "encoder += X.on(1, 0)                            # X门作用在第1位量子比特且受第0位量子比特控制\n",
    "encoder += RY('theta').on(2)                     # RY(theta)门作用在第2位量子比特\n",
    "\n",
    "print(encoder)                                   # 打印Encoder\n",
    "encoder.summary()                                # 总结Encoder量子线路"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在Jupyter Notebook环境中,可以调用量子线路的[.svg()](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/circuit/mindquantum.core.circuit.Circuit.html#mindquantum.core.circuit.Circuit.svg)接口绘制出[量子线路](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/circuit/mindquantum.core.circuit.Circuit.html)的图片格式。调用量子线路的`.svg().to_file(filename='circuit.svg')`接口则可将量子线路的svg格式图片保存到本地。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"216.8\" height=\"200.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"216.8\" height=\"200.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q2: </text><line x1=\"48.8\" x2=\"196.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"196.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"196.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"132.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"138.8\" x2=\"166.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"72.8\" y=\"140.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"112.8\" y=\"156.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RY </text><text x=\"112.8\" y=\"172.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >theta </text></svg>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7faa3c1128b0>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "encoder.svg()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "从对Encoder的Summary中可以看到,该量子线路由3个量子门组成,其中有1个含参量子门且参数为theta,该量子线路调控的量子比特数为3。\n",
    "\n",
    "因此,我们可以根据自身所需求解的问题,搭建对应的量子线路。赶紧动手搭建属于你的第一个[量子线路](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/circuit/mindquantum.core.circuit.Circuit.html)吧!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<table border=\"1\">\n",
       "  <tr>\n",
       "    <th>Software</th>\n",
       "    <th>Version</th>\n",
       "  </tr>\n",
       "<tr><td>mindquantum</td><td>0.9.11</td></tr>\n",
       "<tr><td>scipy</td><td>1.10.1</td></tr>\n",
       "<tr><td>numpy</td><td>1.23.5</td></tr>\n",
       "<tr>\n",
       "    <th>System</th>\n",
       "    <th>Info</th>\n",
       "</tr>\n",
       "<tr><td>Python</td><td>3.9.16</td></tr><tr><td>OS</td><td>Linux x86_64</td></tr><tr><td>Memory</td><td>8.3 GB</td></tr><tr><td>CPU Max Thread</td><td>8</td></tr><tr><td>Date</td><td>Sun Dec 31 23:36:39 2023</td></tr>\n",
       "</table>\n"
      ],
      "text/plain": [
       "<mindquantum.utils.show_info.InfoTable at 0x7faa3c24ec70>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from mindquantum.utils.show_info import InfoTable\n",
    "\n",
    "InfoTable('mindquantum', 'scipy', 'numpy')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "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.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}