{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 量子相位估计算法\n", "\n", "[](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/r2.5.0/mindquantum/zh_cn/case_library/mindspore_quantum_phase_estimation.ipynb) \n", "[](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/r2.5.0/mindquantum/zh_cn/case_library/mindspore_quantum_phase_estimation.py) \n", "[](https://gitee.com/mindspore/docs/blob/r2.5.0/docs/mindquantum/docs/source_zh_cn/case_library/quantum_phase_estimation.ipynb)\n", "\n", "## 概述\n", "\n", "量子相位估计算法(Quantum Phase Estimation Algorithm,简称QPE),是很多量子算法的关键。假设一个幺正算符 $U$,这个幺正算符作用在其本征态 $|u\\rangle$ 上会出现一个相位 $e^{2\\pi i \\varphi}$,现在我们假设 $U$ 算符的本征值未知,也就是 $\\varphi$ 未知,但是 $U$ 算符和本征态 $|u\\rangle$ 已知,相位估计算法的作用就是对这个相位 $\\varphi$ 进行估计。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 算法解析\n", "\n", "量子相位估计算法的实现需要两个寄存器(register),第一寄存器包含$t$个初始在 $|0\\rangle$ 的量子比特,比特数和最后相位估计的结果的精度和算法的成功概率相关;第二个寄存器初始化在幺正算符 $U$ 的本征态 $|u\\rangle$ 上。相位估计算法主要分为三步:\n", "\n", "### 步骤一\n", "\n", "对第一寄存器的所有量子比特进行 [Hadamard](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.HGate.html) 门操作,对第二寄存器连续进行 `控制U` 门操作,其中 $U$ 门的幂次依次为 $2^0, 2^1,...,2^{t-1}$,控制比特依次为 $q_{t-1}, q_{t-2},..., q_{1}, q_{0}$。这时第一寄存器中的态就会变为\n", "\n", "$$\n", "|\\psi_1\\rangle=\\frac{1}{2^{t/2}}\\left(|0\\rangle+e^{i2\\pi 2^{t-1}\\varphi}|1\\rangle\\right)\\left(|0\\rangle+e^{i2\\pi2^{t-2}\\varphi}|1\\rangle\\right)...\\left(|0\\rangle+e^{i2\\pi 2^{0}\\varphi}|1\\rangle\\right) = \\frac{1}{2^{t/2}}\\sum_{k=0}^{2^t-1}e^{i2\\pi\\varphi k}|k\\rangle\n", "$$\n", "\n", "其中$k$为直积态的十进制表示,比如 $k=0$ 表示第一寄存器中t个比特全部在基态 $|00...00\\rangle$, $k=2$ 表示 $|00...10\\rangle$,以此类推。\n", "\n", "### 步骤二\n", "\n", "对第一寄存器的进行量子傅里叶变换的逆变换(Inverse Quantum Fourier Transform),在线路中表示成 $QFT^\\dagger$, 对 $|\\psi_1\\rangle$ 进行逆量子傅里叶变换可得 $|\\psi_2\\rangle$\n", "\n", "$$\n", "|\\psi_2\\rangle=QFT^\\dagger|\\psi_1\\rangle =\\frac{1}{2^t}\\sum_{x=0}^{2^t-1}a_x|x\\rangle\n", "$$\n", "\n", "其中\n", "\n", "$$\n", "a_x=\\sum_{k=0}^{2^t-1}e^{2\\pi i k(\\varphi-x/2^t)}\n", "$$\n", "\n", "为本征基矢 $|x\\rangle$ ($x=0.1,...,2^t$) 对应的概率幅。由上式可得,当 $2^t\\varphi$ 为整数,且满足 $x=2^t\\varphi$ 时,概率幅取最大值1,此时第一寄存器的末态可以精确反映 $\\varphi$;当 $2^t\\varphi$ 不是整数时,$x$ 为 $\\varphi$ 的估计,且$t$越大,估计精度越高。\n", "\n", "### 步骤三\n", "\n", "对第一寄存器的量子比特进行测量,得到第一寄存器的末态 $f=\\sum_{x}^{2^t-1}a_x|x\\rangle$, $x=0,1,...,2^t$,从中找到最大的振幅 $a_{max}$,其对应的本征基矢 $|x\\rangle$ 中的 $x$ 再除以 $2^t$ 即为相位的估计值。\n", "\n", "## QPE代码实现\n", "\n", "下面用一个实例来演示如何在MindSpore Quantum实现量子相位估计算法,选择 [T](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.TGate.html) 门作为进行估计的幺正算符,由定义\n", "\n", "$$\n", "T|1\\rangle=e^{i\\pi/4}|1\\rangle\n", "$$\n", "\n", "可知需要估计的相位角为 $\\varphi=\\frac{1}{8}$。\n", "\n", "现在假设我们不知道 [T](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.TGate.html) 门的相位信息,只知道幺正算符 $U$ 是 [T](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.TGate.html) 门且本征态为 $|1\\rangle$ ,接下来我们需要用量子相位估计算法求出其对应的本征值,即需要估计本征值指数上的相位角。\n", "\n", "首先导入相关依赖。" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from mindquantum.core.gates import T, H, X, Power, BARRIER\n", "from mindquantum.core.circuit import Circuit, UN\n", "from mindquantum.simulator import Simulator\n", "from mindquantum.algorithm.library import qft\n", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[UN](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/circuit/mindquantum.core.circuit.UN.html) 可以指定量子门,目标比特和控制比特,从而在线路中搭建门操作; [Power](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.Power.html) 可以得到指定量子门的指数形式。因为我们已知 [T](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.TGate.html) 门的本征态为 $|1\\rangle$,所以第二寄存器只需1个比特,而在第一寄存器中的比特数越多,得到的结果就越准确,在这里我们使用4个比特。\n", "\n", "因此我们需要搭建5比特线路, $q_0, q_1, q_2, q_3$ 比特用于估计,属于第一寄存器, $q_4$ 属于第二寄存器用于传入 $T$ 算符的本征态。\n", "\n", "利用 [UN](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/circuit/mindquantum.core.circuit.UN.html) 对 $q_0, q_1, q_2, q_3$ 进行 [Hadamard](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.HGate.html) 门操作,用 [X](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.XGate.html) 门对 $q_4$ 进行翻转,得到 [T](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.10/core/gates/mindquantum.core.gates.TGate.html) 门的本征态 $|1\\rangle$。" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"156.8\" height=\"320.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"156.8\" height=\"320.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><text x=\"20.0\" y=\"220.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q3: </text><text x=\"20.0\" y=\"280.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q4: </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\" /><line x1=\"48.8\" x2=\"136.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"136.8\" y1=\"220.0\" y2=\"220.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"136.8\" y1=\"280.0\" y2=\"280.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><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=\"140.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=\"160.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=\"200.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=\"220.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=\"260.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=\"280.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >X </text></svg>" ], "text/plain": [ "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fe694657dc0>" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# pylint: disable=W0104\n", "n = 4\n", "circ = Circuit()\n", "circ += UN(H, n) # 对前4个比特作用力H门\n", "circ += X.on(n) # 对q4作用X门\n", "circ.svg()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "以 $q_4$ 为目标比特,添加控制$T^{2^i}$门。" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"396.8\" height=\"320.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"396.8\" height=\"320.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><text x=\"20.0\" y=\"220.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q3: </text><text x=\"20.0\" y=\"280.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q4: </text><line x1=\"48.8\" x2=\"376.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"376.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"376.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"376.8\" y1=\"220.0\" y2=\"220.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"376.8\" y1=\"280.0\" y2=\"280.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><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=\"140.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=\"160.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=\"200.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=\"220.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=\"260.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=\"280.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >X </text><circle cx=\"152.8\" cy=\"220.0\" r=\"4\" fill=\"#5e7ce0\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"220.0\" y2=\"280.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" /><rect x=\"132.8\" y=\"260.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"152.8\" y=\"280.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >T^1 </text><circle cx=\"212.8\" cy=\"160.0\" r=\"4\" fill=\"#5e7ce0\" /><line x1=\"212.8\" x2=\"212.8\" y1=\"160.0\" y2=\"280.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" /><rect x=\"192.8\" y=\"260.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"212.8\" y=\"280.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >T^2 </text><circle cx=\"272.8\" cy=\"100.0\" r=\"4\" fill=\"#5e7ce0\" /><line x1=\"272.8\" x2=\"272.8\" y1=\"100.0\" y2=\"280.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" /><rect x=\"252.8\" y=\"260.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"272.8\" y=\"280.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >T^4 </text><circle cx=\"332.8\" cy=\"40.0\" r=\"4\" fill=\"#5e7ce0\" /><line x1=\"332.8\" x2=\"332.8\" y1=\"40.0\" y2=\"280.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" /><rect x=\"312.8\" y=\"260.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"332.8\" y=\"280.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >T^8 </text></svg>" ], "text/plain": [ "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fe69464b370>" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# pylint: disable=W0104\n", "for i in range(n):\n", " circ += Power(T, 2**i).on(n, n - i - 1) # 添加T^2^i门,其中q4为目标比特,n-i-1为控制比特\n", "circ.svg()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "对第一寄存器中的比特进行逆量子傅里叶变换。" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"1116.8\" height=\"320.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"1116.8\" height=\"320.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><text x=\"20.0\" y=\"220.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q3: </text><text x=\"20.0\" y=\"280.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q4: </text><line x1=\"48.8\" x2=\"1096.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"1096.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"1096.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"1096.8\" y1=\"220.0\" y2=\"220.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"1096.8\" y1=\"280.0\" y2=\"280.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><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=\"140.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=\"160.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=\"200.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=\"220.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=\"260.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=\"280.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >X </text><circle cx=\"152.8\" cy=\"220.0\" r=\"4\" fill=\"#5e7ce0\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"220.0\" y2=\"280.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" /><rect x=\"132.8\" y=\"260.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"152.8\" y=\"280.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >T^1 </text><circle cx=\"212.8\" cy=\"160.0\" r=\"4\" fill=\"#5e7ce0\" /><line x1=\"212.8\" x2=\"212.8\" y1=\"160.0\" y2=\"280.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" /><rect x=\"192.8\" y=\"260.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"212.8\" y=\"280.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >T^2 </text><circle cx=\"272.8\" cy=\"100.0\" r=\"4\" fill=\"#5e7ce0\" /><line x1=\"272.8\" x2=\"272.8\" y1=\"100.0\" y2=\"280.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" /><rect x=\"252.8\" y=\"260.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"272.8\" y=\"280.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >T^4 </text><circle cx=\"332.8\" cy=\"40.0\" r=\"4\" fill=\"#5e7ce0\" /><line x1=\"332.8\" x2=\"332.8\" y1=\"40.0\" y2=\"280.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" /><rect x=\"312.8\" y=\"260.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"332.8\" y=\"280.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >T^8 </text><rect x=\"372.8\" y=\"20.0\" width=\"0\" height=\"280\" fill=\"gray\" fill-opacity=\"0.8\" /><line x1=\"392.8\" x2=\"392.8\" y1=\"80.0\" y2=\"180.0\" stroke-width=\"3\" stroke=\"#16acff\" /><rect x=\"372.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" /><path d=\"M 383.2 96.31384387633061 L 388.0 88.0 L 392.8 96.31384387633061 L 389.44 96.31384387633061 L 389.44 112.0 L 386.56 112.0 L 386.56 96.31384387633061 Z\" fill=\"#ffffff\" /><path d=\"M 402.40000000000003 103.68615612366939 L 397.6 112.0 L 392.8 103.68615612366939 L 396.16 103.68615612366939 L 396.16 88.0 L 399.04 88.0 L 399.04 103.68615612366939 Z\" fill=\"#ffffff\" /><rect x=\"372.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" /><path d=\"M 383.2 156.31384387633062 L 388.0 148.0 L 392.8 156.31384387633062 L 389.44 156.31384387633062 L 389.44 172.0 L 386.56 172.0 L 386.56 156.31384387633062 Z\" fill=\"#ffffff\" /><path d=\"M 402.40000000000003 163.68615612366938 L 397.6 172.0 L 392.8 163.68615612366938 L 396.16 163.68615612366938 L 396.16 148.0 L 399.04 148.0 L 399.04 163.68615612366938 Z\" fill=\"#ffffff\" /><line x1=\"452.8\" x2=\"452.8\" y1=\"20.0\" y2=\"240.0\" stroke-width=\"3\" stroke=\"#16acff\" /><rect x=\"432.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" /><path d=\"M 443.2 36.31384387633061 L 448.0 28.0 L 452.8 36.31384387633061 L 449.44 36.31384387633061 L 449.44 52.0 L 446.56 52.0 L 446.56 36.31384387633061 Z\" fill=\"#ffffff\" /><path d=\"M 462.40000000000003 43.68615612366939 L 457.6 52.0 L 452.8 43.68615612366939 L 456.16 43.68615612366939 L 456.16 28.0 L 459.04 28.0 L 459.04 43.68615612366939 Z\" fill=\"#ffffff\" /><rect x=\"432.8\" y=\"200.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" /><path d=\"M 443.2 216.31384387633062 L 448.0 208.0 L 452.8 216.31384387633062 L 449.44 216.31384387633062 L 449.44 232.0 L 446.56 232.0 L 446.56 216.31384387633062 Z\" fill=\"#ffffff\" /><path d=\"M 462.40000000000003 223.68615612366938 L 457.6 232.0 L 452.8 223.68615612366938 L 456.16 223.68615612366938 L 456.16 208.0 L 459.04 208.0 L 459.04 223.68615612366938 Z\" fill=\"#ffffff\" /><rect x=\"492.8\" y=\"200.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"512.8\" y=\"220.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><circle cx=\"572.8\" cy=\"220.0\" r=\"4\" fill=\"#fac209\" /><line x1=\"572.8\" x2=\"572.8\" y1=\"160.0\" y2=\"220.0\" stroke=\"#fac209\" stroke-width=\"3\" /><rect x=\"552.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"572.8\" y=\"156.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >PS </text><text x=\"572.8\" y=\"172.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >-π/2 </text><rect x=\"612.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"632.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><circle cx=\"692.8\" cy=\"220.0\" r=\"4\" fill=\"#fac209\" /><line x1=\"692.8\" x2=\"692.8\" y1=\"100.0\" y2=\"220.0\" stroke=\"#fac209\" stroke-width=\"3\" /><rect x=\"672.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"692.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >PS </text><text x=\"692.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >-π/4 </text><circle cx=\"752.8\" cy=\"160.0\" r=\"4\" fill=\"#fac209\" /><line x1=\"752.8\" x2=\"752.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#fac209\" stroke-width=\"3\" /><rect x=\"732.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"752.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >PS </text><text x=\"752.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >-π/2 </text><rect x=\"792.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=\"812.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><circle cx=\"872.8\" cy=\"220.0\" r=\"4\" fill=\"#fac209\" /><line x1=\"872.8\" x2=\"872.8\" y1=\"40.0\" y2=\"220.0\" stroke=\"#fac209\" stroke-width=\"3\" /><rect x=\"852.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=\"872.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >PS </text><text x=\"872.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >-π/8 </text><circle cx=\"932.8\" cy=\"160.0\" r=\"4\" fill=\"#fac209\" /><line x1=\"932.8\" x2=\"932.8\" y1=\"40.0\" y2=\"160.0\" stroke=\"#fac209\" stroke-width=\"3\" /><rect x=\"912.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=\"932.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >PS </text><text x=\"932.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >-π/4 </text><circle cx=\"992.8\" cy=\"100.0\" r=\"4\" fill=\"#fac209\" /><line x1=\"992.8\" x2=\"992.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#fac209\" stroke-width=\"3\" /><rect x=\"972.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=\"992.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >PS </text><text x=\"992.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >-π/2 </text><rect x=\"1032.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=\"1052.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text></svg>" ], "text/plain": [ "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fe60baeac70>" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# pylint: disable=W0104\n", "circ += BARRIER\n", "circ += qft(range(n)).hermitian() # 对前4个比特作用量子傅立叶变换的逆变换\n", "circ.svg()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "选择后端、传入总比特数创建模拟器,对量子线路进行演化,得到末态。" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "tags": [] }, "outputs": [ { "data": { "image/svg+xml": [ "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"422.8\" height=\"117.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0\" width=\"422.8\" height=\"117.0\" fill=\"#ffffff\" /><text x=\"10\" y=\"17.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >Shots:\n", " 100 </text><text x=\"10\" y=\"31.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >Keys: q3 q2 q1 q0 </text><line x1=\"52.8\" x2=\"412.8\" y1=\"62.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"52.8\" x2=\"52.8\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><text x=\"54.8\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >0.0 </text><line x1=\"52.8\" x2=\"52.8\" y1=\"62.0\" y2=\"107.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"112.8\" x2=\"112.8\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><text x=\"114.8\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >0.2 </text><line x1=\"112.8\" x2=\"112.8\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" /><line x1=\"172.8\" x2=\"172.8\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><text x=\"174.8\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >0.4 </text><line x1=\"172.8\" x2=\"172.8\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" /><line x1=\"232.8\" x2=\"232.8\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><text x=\"234.8\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >0.6 </text><line x1=\"232.8\" x2=\"232.8\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" /><line x1=\"292.8\" x2=\"292.8\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><text x=\"294.8\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >0.8 </text><line x1=\"292.8\" x2=\"292.8\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" /><line x1=\"352.8\" x2=\"352.8\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><text x=\"354.8\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >1.0 </text><line x1=\"352.8\" x2=\"352.8\" y1=\"62.0\" y2=\"107.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" /><text x=\"43.8\" y=\"85.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >0100 </text><line x1=\"45.8\" x2=\"52.8\" y1=\"85.0\" y2=\"85.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"52.8\" y=\"73.0\" width=\"300.0\" height=\"24\" id=\"bar_0_1704038371467074865\" fill=\"#5e7ce0\" /><text x=\"362.8\" y=\"85.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_0_1704038371467096865\" fill-opacity=\"0\" >100 </text><animate xlink:href=\"#bar_0_1704038371467074865\" 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_0_1704038371467074865\" 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_1704038371467096865\" 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=\"213.9\" 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 0x7fe60bab1040>" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# pylint: disable=W0104\n", "from mindquantum.core.gates import Measure\n", "sim = Simulator('mqvector', circ.n_qubits) # 创建模拟器\n", "sim.apply_circuit(circ) # 用模拟器演化线路\n", "qs = sim.get_qs() # 获得演化得到的量子态\n", "res = sim.sampling(UN(Measure(), circ.n_qubits - 1), shots=100) # 在寄存器1中加入测量门并对线路进行100次采样,获得统计结果\n", "res.svg()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "需要注意的是,测量结果作为二进制串的读取顺序应为$|q_0q_1q_2q_3\\rangle$,因此我们得到寄存器1的测量结果为`0010`,概率幅为1,该末态可以精准地反映相位$\\varphi$。但`0010`是二进制结果,因此我们将它转回十进制后再除以$2^n$,就得到了我们最终的估计值:$\\varphi=\\frac{2}{2^4}=\\frac{1}{8}$。\n", "\n", "我们也可以通过线路演化得到的量子态 `qs` 找出第一寄存器中振幅最大值 $a_{max}$ 的位置,进而得到其对应的本征基矢 $|x\\rangle$ ,其中的 $x$ 再除以 $2^t$ 即为相位的估计值。" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10100\n" ] } ], "source": [ "index = np.argmax(np.abs(qs))\n", "print(bin(index)[2:])" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } }, "source": [ "需要注意的是,`qs` 对应的是整个量子线路的末态,因此得到的 ``index`` 也包含第二寄存器中的比特,不能直接得到第一寄存器末态中 $a_{max}$ 对应的 $|x\\rangle$ ,需要将 ``index`` 转成二进制后将 $q4$ 对应的比特位剔除,然后得到的才是第一寄存器的 $|x\\rangle$ 。" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0010\n" ] } ], "source": [ "bit_string = bin(index)[2:].zfill(circ.n_qubits)[1:] # 将index转换成01串并剔除q4\n", "bit_string = bit_string[::-1] # 将比特串顺序调整为q0q1q2q3\n", "print(bit_string)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "再将二进制转回十进制,得到我们最终的估计值。" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.125" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# pylint: disable=W0104\n", "theta_exp = int(bit_string, 2) / 2**n\n", "theta_exp" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "可见得到的估计相位和 $\\varphi$ 近似相等。" ] }, { "cell_type": "code", "execution_count": 9, "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:59:31 2023</td></tr>\n", "</table>\n" ], "text/plain": [ "<mindquantum.utils.show_info.InfoTable at 0x7fe60baa64f0>" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from mindquantum.utils.show_info import InfoTable\n", "\n", "InfoTable('mindquantum', 'scipy', 'numpy')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 参考文献\n", "\n", "[1] Michael A. Nielsen and Isaac L. Chuang. [Quantum computation and quantum information](www.cambridge.org/9781107002173)" ] } ], "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 }