"
]
},
"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": [
"shots: 1000\n",
"Keys: q1 q0│0.00 0.128 0.256 0.384 0.512 0.64\n",
"───────────┼───────────┴───────────┴───────────┴───────────┴───────────┴\n",
" 00│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒\n",
" │\n",
" 11│▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓\n",
" │\n",
"{'00': 488, '11': 512}\n",
"
\n"
],
"text/plain": [
"shots: 1000\n",
"Keys: q1 q0│0.00 0.128 0.256 0.384 0.512 0.64\n",
"───────────┼───────────┴───────────┴───────────┴───────────┴───────────┴\n",
" 00│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒\n",
" │\n",
" 11│▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓\n",
" │\n",
"{'00': 488, '11': 512}"
]
},
"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": [
"MindQuantum还提供了采样结果绘制SVG图的功能:"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": "",
"text/plain": [
""
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result.svg() # 打印出测量结果的SVG格式"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"我们可以看到,采样1000中,'00'出现了488次,'11'出现了512次。我们搭建的线路实际上制备出了一个贝尔态$\\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": "",
"text/plain": [
""
]
},
"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)门作用在第2位量子比特\n",
"para_circ += Measure('0').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": "",
"text/plain": [
""
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sim.reset()\n",
"result = sim.sampling(para_circ, {'theta': 0},\n",
" shots=1000) # 将上面定义的线路参数'theta'赋值为0采样1000次\n",
"result.svg()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"我们可以看到,采样结果中'00'出现了513次,'11'出现了487次。事实上把RY门参数赋值为0,它即为我们熟悉的I门,相当于不对线路做任何操作,因此该采样线路与上面不含参线路本质是同一个,可以观察到二次采样结果几乎相同,符合预期结果。\n",
"\n",
"想进一步学习如何对量子线路做测量操作,想了解采样结果分布的理论解释,请点击:[量子测量教程](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.8/quantum_measurement.html)。\n",
"\n",
"若想查询更多关于MindQuantum的API,请点击:[https://mindspore.cn/mindquantum/](https://mindspore.cn/mindquantum/)。"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "MindSpore",
"language": "python",
"name": "mindspore"
},
"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.8"
}
},
"nbformat": 4,
"nbformat_minor": 4
}