{
  "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_quantum_simulator.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_quantum_simulator.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/quantum_simulator.ipynb)\n",
        "\n",
        "## 概述\n",
        "\n",
        "搭建出量子线路后,我们需要指定一个后端来运行量子线路,在MindSpore Quantum中,我们可以利用量子模拟器 [Simulator](https://mindspore.cn/mindquantum/docs/zh-CN/r0.10/simulator/mindquantum.simulator.Simulator.html#mindquantum.simulator.Simulator) 来对量子线路进行模拟运行。在本教程中我们声明一个两比特的`mqvector`模拟器,并以此来简介模拟器的关键功能。\n",
        "\n",
        "## 环境准备\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "导入本教程所依赖的模块。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np                             # 导入numpy库并简写为np\n",
        "from mindquantum.simulator import Simulator    # 从mindquantum.simulator中导入Simulator类\n",
        "from mindquantum.core.gates import X, H, RY    # 导入量子门H, X, RY"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "说明:\n",
        "\n",
        "(1)numpy是一个功能强大的Python库,主要用于对多维数组执行计算,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库;\n",
        "\n",
        "(2)mindquantum是量子-经典混合计算框架,支持多种量子神经网络的训练和推理;\n",
        "\n",
        "(3)搭建的量子线路中所需执行的量子门需要从mindquantum.core模块中导入;"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "mqvector simulator with 2 qubits (little endian), dtype: mindquantum.complex128.\n",
              "Current quantum state:\n",
              "1¦00⟩"
            ]
          },
          "execution_count": 2,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "sim = Simulator('mqvector', 2)   #声明一个两比特的mqvector模拟器\n",
        "sim                              #展示模拟器状态"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "在MindSpore Quantum中,我们可以在`mindquantum.simulator`模块导入模拟器。[Simulator](https://mindspore.cn/mindquantum/docs/zh-CN/r0.10/simulator/mindquantum.simulator.Simulator.html#mindquantum.simulator.Simulator) 类可以接受四个参数:\n",
        "\n",
        "- `backend`:所用到的模拟器名称,目前`mindquantum`支持`mqvector`、`mqvector_gpu`、`mqmatrix` 和 [NoiseBackend](https://mindspore.cn/mindquantum/docs/zh-CN/r0.10/simulator/mindquantum.simulator.NoiseBackend.html#mindquantum.simulator.NoiseBackend) 作为后端进行模拟。\n",
        "- `n_qubits`:模拟器所用到的比特数,也就是这里的2。\n",
        "- `seed`:模拟器在运行随机性相关算法时的随机种子,默认为一个随机数,可以不用提供。\n",
        "- `dtype`: 模拟器模拟时用到的数据类型。由于量子态为复数,因此模拟器当前支持 [mindquantum.complex64](https://mindspore.cn/mindquantum/docs/zh-CN/r0.10/mindquantum.dtype.html) 的单精度模拟和 [mindquantum.complex128](https://mindspore.cn/mindquantum/docs/zh-CN/r0.10/mindquantum.dtype.html) 的双精度模拟,默认值为[mindquantum.complex128](https://mindspore.cn/mindquantum/docs/zh-CN/r0.10/mindquantum.dtype.html)。\n",
        "\n",
        "通过模拟器的输出结果我们可以发现,这是一个`mqvector`的2比特模拟器,并且是little endian的。这里little endian的意思是,整个模拟器中,我们都是将比特序号小的比特放在量子态矢量的右边。接下来,输出还说明了模拟器当前所处的量子态是多少,且在模拟器初始化后,当前的量子态默认处于零态。注意,量子模拟器始终会维护一个内部的量子态,当我们作用量子门或者量子线路到模拟器上时,这个量子态会随即发生改变,而当我们只是想获取关于这个量子态的一些信息时,这个量子态则不会改变。这里就涉及到对量子模拟器的两类操作:\n",
        "\n",
        "- 会改变量子态的操作,通常以`apply`开头,主要有如下几个\n",
        "    - [apply_gate](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/simulator/mindquantum.simulator.Simulator.html#mindquantum.simulator.Simulator.apply_gate): 作用一个量子门到模拟器上\n",
        "    - [apply_circuit](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/simulator/mindquantum.simulator.Simulator.html#mindquantum.simulator.Simulator.apply_circuit): 作用一个量子线路到模拟器上\n",
        "    - [apply_hamiltonian](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/simulator/mindquantum.simulator.Simulator.html#mindquantum.simulator.Simulator.apply_hamiltonian): 将一个哈密顿量作用到模拟器上,注意,此后模拟器的量子态将不再是一个真的量子态\n",
        "    - [set_qs](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/simulator/mindquantum.simulator.Simulator.html#mindquantum.simulator.Simulator.set_qs): 直接设置模拟器的当前量子态\n",
        "    - [reset](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/simulator/mindquantum.simulator.Simulator.html#mindquantum.simulator.Simulator.reset): 重置模拟器的状态为|0⟩态\n",
        "- 不会改变量子态的操作,通常以`get`开头,主要有如下几个\n",
        "    - [get_qs](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/simulator/mindquantum.simulator.Simulator.html#mindquantum.simulator.Simulator.get_qs): 获取模拟器的当前量子态\n",
        "    - [get_expectation](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/simulator/mindquantum.simulator.Simulator.html#mindquantum.simulator.Simulator.get_expectation): 计算模拟器当前量子态关于某个观察量的期望值\n",
        "    - [get_expectation_with_grad](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/simulator/mindquantum.simulator.Simulator.html#mindquantum.simulator.Simulator.get_expectation_with_grad): 跟上一个接口类似,只不过这个方法还会计算期望值关于参数化量子线路的梯度\n",
        "    - [sampling](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/simulator/mindquantum.simulator.Simulator.html#mindquantum.simulator.Simulator.sampling): 在当前量子态下,对给定的量子线路进行采样\n",
        "\n",
        "下面我们简单学习模拟器的基本操作。\n",
        "\n",
        "## 作用量子门和量子线路"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "mqvector simulator with 2 qubits (little endian), dtype: mindquantum.complex128.\n",
              "Current quantum state:\n",
              "√2/2¦00⟩\n",
              "√2/2¦01⟩"
            ]
          },
          "execution_count": 3,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "sim = Simulator('mqvector', 2)    #声明一个2比特的mqvector模拟器\n",
        "sim.apply_gate(H.on(0))           #作用一个Hadamard门到0号比特上\n",
        "sim                               #输出量子模拟器的信息"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "上面我们在量子模拟器的初态上作用了一个 [Hadamard](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.HGate.html) 门,并输出了演化过后的量子态。接下来我们生成一个参数化[量子线路](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/circuit/mindquantum.core.circuit.Circuit.html),并将其作用到当前的量子态上。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "image/svg+xml": [
              "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"156.8\" height=\"140.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"156.8\" height=\"140.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><line x1=\"48.8\" x2=\"136.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"136.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"80.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=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RY </text><text x=\"92.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >a </text></svg>"
            ],
            "text/plain": [
              "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f495766df10>"
            ]
          },
          "execution_count": 4,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "from mindquantum.core.circuit import Circuit   # 导入Circuit模块,用于搭建量子线路\n",
        "\n",
        "circ = Circuit()                               #声明一个空的量子线路\n",
        "circ += H.on(1)                                #向其中添加一个hadamard门,并作用到1号比特上\n",
        "circ += RY('a').on(0)                          #向其中添加一个参数化的RY门,并作用到0号比特上\n",
        "circ.svg()                                     #绘制SVG格式的量子线路图片"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "mqvector simulator with 2 qubits (little endian), dtype: mindquantum.complex128.\n",
              "Current quantum state:\n",
              "0.11851349145283663¦00⟩\n",
              "0.6971044056263442¦01⟩\n",
              "0.11851349145283663¦10⟩\n",
              "0.6971044056263442¦11⟩"
            ]
          },
          "execution_count": 5,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "sim.apply_circuit(circ, pr={'a': 1.234})  #作用一个量子线路,当线路是一个参数化量子线路时,我们还需要提供参数值。\n",
        "sim"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "在上面的代码中,我们先生成了一个参数化量子线路`circ`,随后我们将其作用到量子模拟器上,并通过传入字典的方式,将参数`a`设置为`1.234`。最后输出量子模拟器演化出来的量子态。"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 设置并获取模拟器状态\n",
        "\n",
        "我们使用 [get_qs(ket=False)](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/simulator/mindquantum.simulator.Simulator.html#mindquantum.simulator.Simulator.get_qs) 查看当前模拟器的状态,\n",
        "参数`ket`是一个`bool`类型的数,它决定了当前模拟器的状态是否以ket字符串的形式返回,`ket=False`时是以`numpy.ndarray`形式,`ket=True`时是以ket字符串形式。默认`ket=False`。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[0.11851349+0.j 0.69710441+0.j 0.11851349+0.j 0.69710441+0.j]\n"
          ]
        }
      ],
      "source": [
        "print(sim.get_qs())  #查看模拟器状态,以numpy.ndarray形式返回结果"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "0.11851349145283663¦00⟩\n",
            "0.6971044056263442¦01⟩\n",
            "0.11851349145283663¦10⟩\n",
            "0.6971044056263442¦11⟩\n"
          ]
        }
      ],
      "source": [
        "print(sim.get_qs(True))  #查看模拟器状态,以ket形式返回结果"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "在实际写代码过程中,我们常常需要将模拟器指定一个初始态开始演化,这个操作可以使用 [set_qs()](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/simulator/mindquantum.simulator.Simulator.html#mindquantum.simulator.Simulator.set_qs) 实现。\n",
        "\n",
        "例如,我们希望模拟器状态为\n",
        "\n",
        "$$\n",
        "\\frac{\\sqrt{3}}{3}|00⟩+\\frac{\\sqrt{6}}{3}|11⟩\n",
        "$$\n",
        "\n",
        "第一步:我们计算出目标状态的向量形式:\n",
        "\n",
        "$$\n",
        "\\frac{\\sqrt{3}}{3}|00⟩+\\frac{\\sqrt{6}}{3}|11⟩ =\\frac{\\sqrt{3}}{3}\\times\n",
        "\\left(\n",
        "\\begin{array}{l}\n",
        "1\\\\\n",
        "0\n",
        "\\end{array}\n",
        "\\right)\n",
        "\\otimes\n",
        "\\left(\n",
        "\\begin{array}{l}\n",
        "1\\\\\n",
        "0\n",
        "\\end{array}\n",
        "\\right)+\n",
        "\\frac{\\sqrt{6}}{3}\\times\n",
        "\\left(\n",
        "\\begin{array}{l}\n",
        "0\\\\\n",
        "1\n",
        "\\end{array}\n",
        "\\right)\\otimes\n",
        "\\left(\n",
        "\\begin{array}{l}\n",
        "0\\\\\n",
        "1\n",
        "\\end{array}\n",
        "\\right)= \\frac{\\sqrt{3}}{3}\\times\n",
        "\\left(\n",
        "\\begin{array}{l}\n",
        "1\\\\\n",
        "0\\\\\n",
        "0\\\\\n",
        "0\n",
        "\\end{array}\n",
        "\\right)+\n",
        "\\frac{\\sqrt{6}}{3}\\times\n",
        "\\left(\n",
        "\\begin{array}{l}\n",
        "0\\\\\n",
        "0\\\\\n",
        "0\\\\\n",
        "1\n",
        "\\end{array}\n",
        "\\right)=\n",
        "\\left(\n",
        "\\begin{array}{l}\n",
        "\\frac{\\sqrt{3}}{3}\\\\\n",
        "0\\\\\n",
        "0\\\\\n",
        "\\frac{\\sqrt{6}}{3}\n",
        "\\end{array}\n",
        "\\right)\n",
        "$$\n",
        "\n",
        "第二步:我们将这个向量使用 [set_qs()](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/simulator/mindquantum.simulator.Simulator.html#mindquantum.simulator.Simulator.set_qs) 赋值给模拟器,让其作为模拟器的状态:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "0.5773502691896258¦00⟩\n",
            "0.816496580927726¦11⟩\n"
          ]
        }
      ],
      "source": [
        "sim.set_qs(np.array([3**0.5, 0, 0, 6**0.5]))        #设置模拟器状态,无需归一化\n",
        "print(sim.get_qs(True))                             #查看模拟器状态"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "通过 [get_qs()](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/simulator/mindquantum.simulator.Simulator.html#mindquantum.simulator.Simulator.get_qs) 查看模拟器状态可以发现,当前模拟器状态即为我们希望设置的$\\frac{\\sqrt{3}}{3}|00⟩+\\frac{\\sqrt{6}}{3}|11⟩$。\n",
        "\n",
        "在实际编程过程中,我们常常需要多次模拟电路,通过多开模拟器的方式会导致内存占用非常大,我们可以通过现有模拟器复位的方式来复用模拟器,从而减少内存消耗。\n",
        "\n",
        "我们使用 [reset()](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/simulator/mindquantum.simulator.Simulator.html#mindquantum.simulator.Simulator.reset) 来复位模拟器:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[1.+0.j 0.+0.j 0.+0.j 0.+0.j]\n"
          ]
        }
      ],
      "source": [
        "sim.reset()          #复位模拟器\n",
        "print(sim.get_qs())  #查看模拟器状态"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "可以发现,当前模拟器被复位成了初始的$1|00⟩$态,相当于一个全新的模拟器。\n",
        "\n",
        "因此,我们可以根据自身所需的量子初态,设置对应的量子模拟器,并运行自定义的量子线路。赶紧动手运行你构造出的的第一个量子线路吧!\n",
        "\n",
        "## [量子线路采样](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/simulator/mindquantum.simulator.Simulator.html#mindquantum.simulator.Simulator.sampling)\n",
        "\n",
        "线路采样是指对量子线路执行多次模拟测量,统计测量出各种结果出现的频次。**采样不会改变量子线路中的状态**。\n",
        "\n",
        "[sampling(circuit, pr=None, shots=1, seed=None)](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/simulator/mindquantum.simulator.Simulator.html#mindquantum.simulator.Simulator.sampling) 是`MindSpore Quantum`中提供的对模拟器进行线路采样方法,它接受四个参数:\n",
        "\n",
        "- `circuit (Circuit)`:希望进行采样的[量子线路](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/circuit/mindquantum.core.circuit.Circuit.html),注意,该线路中必须包含至少一个测量操作(即采样点)。\n",
        "- `pr (Union[None, dict, ParameterResolver])`:[parameter resolver](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/parameterresolver/mindquantum.core.parameterresolver.ParameterResolver.html),当 `circuit`是含参线路时,需要给出参数的值。\n",
        "- `shots (int)`:采样的次数,默认为1。\n",
        "- `seed`:采样时的随机种子,默认为一个随机数,可以不用提供。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "image/svg+xml": [
              "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"276.8\" height=\"140.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"276.8\" height=\"140.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><line x1=\"48.8\" x2=\"256.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"256.8\" y1=\"100.0\" y2=\"100.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=\"192.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"212.8\" cy=\"50.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 200.0 50.4 A 12.8 12.8 0 0 1 225.60000000000002 50.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 216.90184831748593 33.93539030917347 L 225.21569219381655 29.135390309173467 L 225.21569219381655 38.73539030917347 L 221.8901546432843 36.815390309173466 L 214.04707658144957 50.4 L 212.38430780618347 49.44 L 220.2273858680182 35.85539030917347 Z\" fill=\"#ffffff\" /><rect x=\"192.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"212.8\" cy=\"110.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 200.0 110.4 A 12.8 12.8 0 0 1 225.60000000000002 110.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 216.90184831748593 93.93539030917347 L 225.21569219381655 89.13539030917347 L 225.21569219381655 98.73539030917347 L 221.8901546432843 96.81539030917347 L 214.04707658144957 110.4 L 212.38430780618347 109.44 L 220.2273858680182 95.85539030917347 Z\" fill=\"#ffffff\" /></svg>"
            ],
            "text/plain": [
              "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f495766dee0>"
            ]
          },
          "execution_count": 10,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# 不含参线路采样:\n",
        "from mindquantum.core.gates import Measure  # 引入测量门\n",
        "\n",
        "circ = Circuit()                            # 初始化量子线路\n",
        "circ += H.on(0)                             # H门作用在第0位量子比特\n",
        "circ += X.on(1, 0)                          # X门作用在第1位量子比特且受第0位量子比特控制\n",
        "circ += Measure('q0').on(0)                 # 在0号量子比特作用一个测量,并将该测量命名为'q0'\n",
        "circ += Measure('q1').on(1)                 # 在1号量子比特作用一个测量,并将该测量命名为'q1'\n",
        "circ.svg()                                  # 绘制SVG格式的量子线路图片"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/html": [
              "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"></pre>\n"
            ],
            "text/plain": []
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "text/html": [
              "<pre style=\"white-space: pre;\"><span style=\"color: #808000; text-decoration-color: #808000\">shots: </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1000</span>\n",
              "<span style=\"color: #808000; text-decoration-color: #808000\">Keys: q1 q0│</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.00</span><span style=\"color: #808000; text-decoration-color: #808000\">   </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.127</span><span style=\"color: #808000; text-decoration-color: #808000\">       </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.254</span><span style=\"color: #808000; text-decoration-color: #808000\">       </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.381</span><span style=\"color: #808000; text-decoration-color: #808000\">       </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.508</span><span style=\"color: #808000; text-decoration-color: #808000\">       </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0.635</span>\n",
              "<span style=\"color: #808000; text-decoration-color: #808000\">───────────┼───────────┴───────────┴───────────┴───────────┴───────────┴</span>\n",
              "<span style=\"color: #808000; text-decoration-color: #808000\">         </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">00</span><span style=\"color: #808000; text-decoration-color: #808000\">│▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓</span>\n",
              "<span style=\"color: #808000; text-decoration-color: #808000\">           │</span>\n",
              "<span style=\"color: #808000; text-decoration-color: #808000\">         </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">11</span><span style=\"color: #808000; text-decoration-color: #808000\">│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒</span>\n",
              "<span style=\"color: #808000; text-decoration-color: #808000\">           │</span>\n",
              "<span style=\"color: #808000; text-decoration-color: #808000; font-weight: bold\">{</span><span style=\"color: #008000; text-decoration-color: #008000\">'00'</span><span style=\"color: #808000; text-decoration-color: #808000\">: </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">508</span><span style=\"color: #808000; text-decoration-color: #808000\">, </span><span style=\"color: #008000; text-decoration-color: #008000\">'11'</span><span style=\"color: #808000; text-decoration-color: #808000\">: </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">492</span><span style=\"color: #808000; text-decoration-color: #808000; font-weight: bold\">}</span>\n",
              "</pre>\n"
            ],
            "text/plain": [
              "shots: 1000\n",
              "Keys: q1 q0│0.00   0.127       0.254       0.381       0.508       0.635\n",
              "───────────┼───────────┴───────────┴───────────┴───────────┴───────────┴\n",
              "         00│▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓\n",
              "           │\n",
              "         11│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒\n",
              "           │\n",
              "{'00': 508, '11': 492}"
            ]
          },
          "execution_count": 11,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "sim.reset()\n",
        "result = sim.sampling(circ, shots=1000)  # 对上面定义的线路采样1000次\n",
        "result"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "MindSpore Quantum还提供了采样结果绘制SVG图的功能:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "image/svg+xml": [
              "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"408.4\" height=\"147.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0\" width=\"408.4\" height=\"147.0\" fill=\"#ffffff\" /><text x=\"10\" y=\"17.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >Shots:\n",
              " 1000 </text><text x=\"10\" y=\"31.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >Keys: q1 q0 </text><line x1=\"38.4\" x2=\"398.4\" y1=\"62.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"38.4\" x2=\"38.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><text x=\"40.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >0.0 </text><line x1=\"38.4\" x2=\"38.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"98.4\" x2=\"98.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><text x=\"100.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >0.102 </text><line x1=\"98.4\" x2=\"98.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" /><line x1=\"158.4\" x2=\"158.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><text x=\"160.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >0.203 </text><line x1=\"158.4\" x2=\"158.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" /><line x1=\"218.4\" x2=\"218.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><text x=\"220.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >0.305 </text><line x1=\"218.4\" x2=\"218.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" /><line x1=\"278.4\" x2=\"278.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><text x=\"280.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >0.406 </text><line x1=\"278.4\" x2=\"278.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" /><line x1=\"338.4\" x2=\"338.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><text x=\"340.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >0.508 </text><line x1=\"338.4\" x2=\"338.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" /><text x=\"29.4\" y=\"85.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >00 </text><line x1=\"31.4\" x2=\"38.4\" y1=\"85.0\" y2=\"85.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"38.4\" y=\"73.0\" width=\"300.0\" height=\"24\" id=\"bar_0_1704185495836093268\" fill=\"#5e7ce0\" /><text x=\"348.4\" y=\"85.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_0_1704185495836114011\" fill-opacity=\"0\" >508 </text><text x=\"29.4\" y=\"115.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >11 </text><line x1=\"31.4\" x2=\"38.4\" y1=\"115.0\" y2=\"115.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"38.4\" y=\"103.0\" width=\"290.5511811023622\" height=\"24\" id=\"bar_1_1704185495836127910\" fill=\"#16acff\" /><text x=\"338.95118110236217\" y=\"115.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_1_1704185495836141049\" fill-opacity=\"0\" >492 </text><animate xlink:href=\"#bar_0_1704185495836093268\" attributeName=\"width\" from=\"0\" to=\"300.0\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 300.0\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" /><animate xlink:href=\"#bar_1_1704185495836127910\" attributeName=\"width\" from=\"0\" to=\"290.5511811023622\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 290.5511811023622\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" /><animate xlink:href=\"#bar_0_1704185495836093268\" attributeName=\"fill\" from=\"#5e7ce0\" to=\"#fac209\" dur=\"0.15s\" calcMode=\"spline\" values=\"#5e7ce0; #fac209\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" /><animate xlink:href=\"#bar_text_0_1704185495836114011\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" /><animate xlink:href=\"#bar_text_1_1704185495836141049\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" /><text x=\"206.7\" y=\"41.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"middle\" >probability </text></svg>"
            ],
            "text/plain": [
              "<mindquantum.io.display.measure_res_svg_drawer.SVGMeasure at 0x7f4957631730>"
            ]
          },
          "execution_count": 12,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "result.svg()  # 打印出测量结果的SVG格式"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "我们可以看到,采样1000中,'00'出现了508次,'11'出现了492次(在你运行时,结果可能会不同)。我们搭建的线路实际上制备出了一个贝尔态$\\frac{\\sqrt{2}}{2}|00⟩+\\frac{\\sqrt{2}}{2}|11⟩$。直观上,我们可以看到对该状态进行测量得到'00'的概率为$\\frac{1}{2}$,得到'11'的概率为$\\frac{1}{2}$,采样结果符合概率,细微的误差是由模拟器噪声导致。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "image/svg+xml": [
              "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"376.8\" height=\"140.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"376.8\" height=\"140.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><line x1=\"48.8\" x2=\"356.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"356.8\" y1=\"100.0\" y2=\"100.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=\"192.8\" y=\"80.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"232.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RY </text><text x=\"232.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >theta </text><rect x=\"192.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"212.8\" cy=\"50.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 200.0 50.4 A 12.8 12.8 0 0 1 225.60000000000002 50.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 216.90184831748593 33.93539030917347 L 225.21569219381655 29.135390309173467 L 225.21569219381655 38.73539030917347 L 221.8901546432843 36.815390309173466 L 214.04707658144957 50.4 L 212.38430780618347 49.44 L 220.2273858680182 35.85539030917347 Z\" fill=\"#ffffff\" /><rect x=\"292.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" /><circle cx=\"312.8\" cy=\"110.4\" r=\"1.6\" fill=\"#ffffff\" /><path d=\"M 300.0 110.4 A 12.8 12.8 0 0 1 325.6 110.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" /><path d=\"M 316.9018483174859 93.93539030917347 L 325.21569219381655 89.13539030917347 L 325.21569219381655 98.73539030917347 L 321.8901546432843 96.81539030917347 L 314.0470765814496 110.4 L 312.3843078061835 109.44 L 320.2273858680182 95.85539030917347 Z\" fill=\"#ffffff\" /></svg>"
            ],
            "text/plain": [
              "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f49575cb760>"
            ]
          },
          "execution_count": 13,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# 含参线路采样:\n",
        "para_circ = Circuit()             # 初始化量子线路\n",
        "para_circ += H.on(0)              # H门作用在第0位量子比特\n",
        "para_circ += X.on(1, 0)           # X门作用在第1位量子比特且受第0位量子比特控制\n",
        "para_circ += RY('theta').on(1)    # RY(theta)门作用在第1位量子比特\n",
        "para_circ += Measure('q0').on(0)   # 在0号量子比特作用一个测量,并将该测量命名为'q0'\n",
        "para_circ += Measure('q1').on(1)  # 在1号量子比特作用一个测量,并将该测量命名为'q1'\n",
        "para_circ.svg()                   # 绘制SVG格式的量子线路图片"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 14,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "image/svg+xml": [
              "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"408.4\" height=\"147.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0\" width=\"408.4\" height=\"147.0\" fill=\"#ffffff\" /><text x=\"10\" y=\"17.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >Shots:\n",
              " 1000 </text><text x=\"10\" y=\"31.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >Keys: q1 0 </text><line x1=\"38.4\" x2=\"398.4\" y1=\"62.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"38.4\" x2=\"38.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><text x=\"40.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >0.0 </text><line x1=\"38.4\" x2=\"38.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"98.4\" x2=\"98.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><text x=\"100.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >0.103 </text><line x1=\"98.4\" x2=\"98.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" /><line x1=\"158.4\" x2=\"158.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><text x=\"160.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >0.207 </text><line x1=\"158.4\" x2=\"158.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" /><line x1=\"218.4\" x2=\"218.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><text x=\"220.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >0.31 </text><line x1=\"218.4\" x2=\"218.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" /><line x1=\"278.4\" x2=\"278.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><text x=\"280.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >0.414 </text><line x1=\"278.4\" x2=\"278.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" /><line x1=\"338.4\" x2=\"338.4\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><text x=\"340.4\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >0.517 </text><line x1=\"338.4\" x2=\"338.4\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" /><text x=\"29.4\" y=\"85.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >00 </text><line x1=\"31.4\" x2=\"38.4\" y1=\"85.0\" y2=\"85.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"38.4\" y=\"73.0\" width=\"300.0\" height=\"24\" id=\"bar_0_1704185495862088833\" fill=\"#5e7ce0\" /><text x=\"348.4\" y=\"85.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_0_1704185495862112515\" fill-opacity=\"0\" >517 </text><text x=\"29.4\" y=\"115.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >11 </text><line x1=\"31.4\" x2=\"38.4\" y1=\"115.0\" y2=\"115.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"38.4\" y=\"103.0\" width=\"280.27079303675043\" height=\"24\" id=\"bar_1_1704185495862147173\" fill=\"#16acff\" /><text x=\"328.6707930367504\" y=\"115.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_1_1704185495862157410\" fill-opacity=\"0\" >483 </text><animate xlink:href=\"#bar_0_1704185495862088833\" attributeName=\"width\" from=\"0\" to=\"300.0\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 300.0\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" /><animate xlink:href=\"#bar_1_1704185495862147173\" attributeName=\"width\" from=\"0\" to=\"280.27079303675043\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 280.27079303675043\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" /><animate xlink:href=\"#bar_0_1704185495862088833\" attributeName=\"fill\" from=\"#5e7ce0\" to=\"#fac209\" dur=\"0.15s\" calcMode=\"spline\" values=\"#5e7ce0; #fac209\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" /><animate xlink:href=\"#bar_text_0_1704185495862112515\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" /><animate xlink:href=\"#bar_text_1_1704185495862157410\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" /><text x=\"206.7\" y=\"41.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"middle\" >probability </text></svg>"
            ],
            "text/plain": [
              "<mindquantum.io.display.measure_res_svg_drawer.SVGMeasure at 0x7f49575f52e0>"
            ]
          },
          "execution_count": 14,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "sim.reset()\n",
        "result = sim.sampling(para_circ, {'theta': 0}, shots=1000)  # 将上面定义的线路参数'theta'赋值为0采样1000次\n",
        "result.svg()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "我们可以看到,采样结果中'00'出现了517次,'11'出现了483次(在你运行时,结果可能会不同)。事实上把RY门参数赋值为0,它即为我们熟悉的I门,相当于不对线路做任何操作,因此该采样线路与上面不含参线路本质是同一个,可以观察到二次采样结果几乎相同,符合预期结果。\n",
        "\n",
        "## 模拟器支持情况\n",
        "\n",
        "当前 MindSpore Quantum 支持多种模拟器,具体情况如下:\n",
        "\n",
        "|模拟器名称|特性|是否支持GPU|单精度支持|多精度支持|\n",
        "|--|--|--|--|--|\n",
        "|`mqvector`|全振幅模拟器|❌|✅|✅|\n",
        "|`mqvector_gpu`|GPU版全振幅模拟器|✅|✅|✅|\n",
        "|`mqmatrix`|密度矩阵模拟器|❌|✅|✅|\n",
        "|[NoiseBackend](https://mindspore.cn/mindquantum/docs/zh-CN/r0.10/simulator/mindquantum.simulator.NoiseBackend.html#mindquantum.simulator.NoiseBackend)|噪声模拟器,教程请参考:[噪声模拟器](https://mindspore.cn/mindquantum/docs/zh-CN/r0.10/middle_level/noise_simulator.html)|✅|✅|✅|\n",
        "\n",
        "> 请注意,`mqvector_gpu`后端仅支持在CUDA 11及以上版本的环境中运行。\n",
        "\n",
        "## 模拟器的选择\n",
        "\n",
        "我们知道,量子态的维度是随着比特数的增多而指数增加的,因此模拟大比特量子系统时,需要的物理内存将急剧增加。下面,我们给出不同比特下,存储一个全振幅量子态所需要的内存空间:\n",
        "\n",
        "|比特数|mindquantum.complex128|mindquantum.complex64|\n",
        "|--|--|--|\n",
        "|6|1kB|0.5kB|\n",
        "|16|1MB|0.5MB|\n",
        "|26|1GB|0.5GB|\n",
        "|30|16GB|8GB|\n",
        "|36|1TB|0.5TB|\n",
        "|40|16TB|8TB|\n",
        "|46|1PB|0.5PB|\n",
        "\n",
        "由此可见,在模拟大比特系统时,我们可以考虑使用单精度类型来减小内存占用量。在 MindSpore Quantum 中,我们可以通过 `dtype` 来方便的修改模拟器的数据类型:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "mqvector simulator with 2 qubits (little endian), dtype: mindquantum.complex64.\n",
            "Current quantum state:\n",
            "1¦00⟩\n"
          ]
        }
      ],
      "source": [
        "import mindquantum as mq\n",
        "from mindquantum.simulator import Simulator\n",
        "\n",
        "sim = Simulator('mqvector', 2, dtype=mq.complex64)\n",
        "print(sim)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "下面我们给出一些经验规则,帮助大家来合理的选择不同的模拟器:\n",
        "\n",
        "- 量子系统小于8比特时:`mqvector`。在小比特时,CPU反而会比GPU运行快速。\n",
        "- 量子系统大于8比特时:`mqvector_gpu`。在大比特时,GPU将会发挥其并行运算优势。\n",
        "- 混态模拟时:`mqmatrix`。`mqmatrix`是密度矩阵模拟器,因此支持混态系统的模拟。\n",
        "- 量子系统含噪声时:[NoiseBackend](https://mindspore.cn/mindquantum/docs/zh-CN/r0.10/simulator/mindquantum.simulator.NoiseBackend.html#mindquantum.simulator.NoiseBackend)。通过该模拟器,我们可以方便的往线路中添加不同的量子信道,达到对噪声系统的模拟。\n",
        "- 当进行量子化学模拟时:`mindquantum.complex128`。量子化学模拟需要模拟基态能量达到化学精度,因此建议用双精度类型。\n",
        "- 当进行量子机器学习时:`mindquantum.complex64`。机器学习类任务对精度要求不敏感,因此建议用单精度类型。\n",
        "\n",
        "想进一步学习如何对量子线路做测量操作,想了解采样结果分布的理论解释,请点击:[量子测量教程](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/beginner/quantum_measurement.html)。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 16,
      "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.24.4</td></tr>\n",
              "<tr>\n",
              "    <th>System</th>\n",
              "    <th>Info</th>\n",
              "</tr>\n",
              "<tr><td>Python</td><td>3.8.17</td></tr><tr><td>OS</td><td>Linux x86_64</td></tr><tr><td>Memory</td><td>16.62 GB</td></tr><tr><td>CPU Max Thread</td><td>16</td></tr><tr><td>Date</td><td>Tue Jan  2 16:51:35 2024</td></tr>\n",
              "</table>\n"
            ],
            "text/plain": [
              "<mindquantum.utils.show_info.InfoTable at 0x7f49575faaf0>"
            ]
          },
          "execution_count": 16,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "from mindquantum.utils.show_info import InfoTable\n",
        "\n",
        "InfoTable('mindquantum', 'scipy', 'numpy')"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "MindSpore",
      "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.8.17"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 4
}