"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"circ = shift(circ, 1)\n",
"circ.svg() # 线路作用的量子比特从q0、q1变为q1、q2"
]
},
{
"cell_type": "markdown",
"id": "b5050bd9",
"metadata": {},
"source": [
"### 案例——鸢尾花分类:搭建Encoder\n",
"\n",
"以上高阶操作的示例显示了量子线路高阶操作的适用性及便捷性,如果我们进一步将它们组合起来用于构建量子线路,又会产生什么神奇魔力呢?\n",
"\n",
"接下来我们将以 通过量子神经网络对鸢尾花进行分类\n",
" 的案例中 搭建Encoder 部分为例,搭建下图所示的量子线路:\n",
"\n",
"![encoder classification of iris by qnn](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/docs/mindquantum/docs/source_zh_cn/images/encoder_classification_of_iris_by_qnn.png)\n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "89bbaf78",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" Circuit Summary \n",
"╭──────────────────────┬───────────────────────────────────────────────────────────────╮\n",
"│ Info │ value │\n",
"├──────────────────────┼───────────────────────────────────────────────────────────────┤\n",
"│ Number of qubit │ 4 │\n",
"├──────────────────────┼───────────────────────────────────────────────────────────────┤\n",
"│ Total number of gate │ 17 │\n",
"│ Barrier │ 0 │\n",
"│ Noise Channel │ 0 │\n",
"│ Measurement │ 0 │\n",
"├──────────────────────┼───────────────────────────────────────────────────────────────┤\n",
"│ Parameter gate │ 7 │\n",
"│ 7 ansatz parameters │ 0_alpha, 1_alpha, 2_alpha, 3_alpha, 4_alpha, 5_alpha, 6_alpha │\n",
"╰──────────────────────┴───────────────────────────────────────────────────────────────╯\n",
"
\n"
],
"text/plain": [
"\u001b[1;38;2;255;0;0m Circuit Summary \u001b[0m\n",
"╭──────────────────────┬───────────────────────────────────────────────────────────────╮\n",
"│\u001b[1m \u001b[0m\u001b[1;38;2;59;59;149mInfo\u001b[0m\u001b[1m \u001b[0m\u001b[1m \u001b[0m│\u001b[1m \u001b[0m\u001b[1;38;2;59;59;149mvalue\u001b[0m\u001b[1m \u001b[0m\u001b[1m \u001b[0m│\n",
"├──────────────────────┼───────────────────────────────────────────────────────────────┤\n",
"│ \u001b[1mNumber of qubit\u001b[0m │ 4 │\n",
"├──────────────────────┼───────────────────────────────────────────────────────────────┤\n",
"│ \u001b[1mTotal number of gate\u001b[0m │ 17 │\n",
"│ Barrier │ 0 │\n",
"│ Noise Channel │ 0 │\n",
"│ Measurement │ 0 │\n",
"├──────────────────────┼───────────────────────────────────────────────────────────────┤\n",
"│ \u001b[1mParameter gate\u001b[0m │ 7 │\n",
"│ 7 ansatz parameters │ \u001b[38;2;72;201;176m0_alpha, 1_alpha, 2_alpha, 3_alpha, 4_alpha, 5_alpha, 6_alpha\u001b[0m │\n",
"╰──────────────────────┴───────────────────────────────────────────────────────────────╯\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from mindquantum.core.circuit import shift, add_prefix, Circuit, UN\n",
"from mindquantum.core.gates import RZ, X, H\n",
"\n",
"template = Circuit([X.on(1, 0), RZ('alpha').on(1), X.on(1, 0)])\n",
"encoder = UN(H, 4) + (RZ(f'{i}_alpha').on(i) for i in range(4)) + sum(add_prefix(shift(template, i), f'{i+4}') for i in range(3))\n",
"encoder.summary()"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "1686faa1",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
""
],
"text/plain": [
""
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"encoder.svg()"
]
},
{
"cell_type": "markdown",
"id": "8dcb676b",
"metadata": {},
"source": [
"在上面的代码中,我们使用UN模块将不含参数的H门作用于目标量子比特$q_0$~$q_3$,然后将RZ(i_alpha)门作用于第i位量子比特。\n",
"接着,观察到线路中有三个一样的模块,它们都由一个受控X门、一个RZ门和一个受控X门构成,因此,我们构造对应的模板template,它由一个受$q_0$控制、作用在$q_1$的X门,一个作用在$q_1$的RZ门和一个受$q_0$控制、作用在$q_1$的X门构成。然后我们通过shift(template, i)来改变模板作用的量子比特范围,构建三个结构相同但作用的量子比特范围相差1的模块,其涉及的量子比特范围分别为$q_0$和$q_1$、$q_1$和$q_2$、$q_2$和$q_3$。\n",
"最后,我们使用add_prefix方法为通过shift构造的所有含参量子门的参数名加上一个数字前缀。\n",
"\n",
"至此,通过MindSpore Quantum提供的这些量子线路高阶操作,我们仅用两行代码就构建了所需的Encoder!"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "abfafaad",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
" \n",
" Software | \n",
" Version | \n",
"
\n",
"mindquantum | 0.9.11 |
\n",
"scipy | 1.10.1 |
\n",
"numpy | 1.24.4 |
\n",
"\n",
" System | \n",
" Info | \n",
"
\n",
"Python | 3.8.17 |
OS | Linux x86_64 |
Memory | 16.62 GB |
CPU Max Thread | 16 |
Date | Tue Jan 2 14:38:55 2024 |
\n",
"
\n"
],
"text/plain": [
""
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from mindquantum.utils.show_info import InfoTable\n",
"\n",
"InfoTable('mindquantum', 'scipy', 'numpy')\n"
]
}
],
"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": 5
}