\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": [
"In the above code, we use the [UN](https://www.mindspore.cn/mindquantum/docs/en/r0.9/core/circuit/mindquantum.core.circuit.UN.html) module to apply the parameter-free [H](https://www.mindspore.cn/mindquantum/docs/en/r0.9/core/gates/mindquantum.core.gates.HGate.html) gate to the target qubits $q_0$~$q_3$, and then apply the [RZ](https://www.mindspore.cn/mindquantum/docs/en/r0.9/core/gates/mindquantum.core.gates.RZ.html)(i_alpha) gate to the i-th qubit.\n",
"Next, it is observed that there are three identical modules in the circuit, and they are all composed of a controlled [X](https://www.mindspore.cn/mindquantum/docs/en/r0.9/core/gates/mindquantum.core.gates.XGate.html) gate, a [RZ](https://www.mindspore.cn/mindquantum/docs/en/r0.9/core/gates/mindquantum.core.gates.RZ.html) gate and a controlled [X](https://www.mindspore.cn/mindquantum/docs/en/r0.9/core/gates/mindquantum.core.gates.XGate.html) gate. Therefore, we construct the corresponding template template, which is controlled by $q_0$, an [X](https://www.mindspore.cn/mindquantum/docs/en/r0.9/core/gates/mindquantum.core.gates.XGate.html) gate acting on $q_1$, an [RZ](https://www.mindspore.cn/mindquantum/docs/en/r0.9/core/gates/mindquantum.core.gates.RZ.html) gate acting on $q_1$, and an [X](https://www.mindspore.cn/mindquantum/docs/en/r0.9/core/gates/mindquantum.core.gates.XGate.html) gate controlled by $q_0$ acting on $q_1$ are formed. Then we use [shift](https://www.mindspore.cn/mindquantum/docs/en/r0.9/core/circuit/mindquantum.core.circuit.shift.html)(template, i) to change the qubit range that the template acts on, and build three modules with the same structure but the qubit range differs by 1. The qubit ranges involved are $q_0$ and $q_1$, $q_1$ and $q_2$, $q_2$ and $q_3$.\n",
"Finally, we use the [add_prefix](https://www.mindspore.cn/mindquantum/docs/en/r0.9/core/circuit/mindquantum.core.circuit.add_prefix.html) method to prefix the parameter names of all parameter quantum gates constructed by shift with a number.\n",
"\n",
"So far, through these advanced operations of quantum circuit provided by MindSpore Quantum, we have built the required Encoder with only two lines of code!"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "038c6317",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"