mindspore打卡01非参数们和参数门
import numpy as np # 导入numpy库并简写为np
from mindquantum.core.gates import X, Y, Z, H, RX, RY, RZ # 导入量子门H, X, Y, Z, RX, RY, RZ
/home/ma-user/anaconda3/envs/Mindquantum-0.9.0/lib/python3.9/site-packages/mindquantum/simulator/__init__.py:17: UserWarning: Disable mqvector gpu backend due to: Malloc GPU memory failed: cudaErrorInsufficientDriver, CUDA driver version is insufficient for CUDA runtime version
from .available_simulator import SUPPORTED_SIMULATOR
说明:
(1)numpy是一个功能强大的Python库,主要用于对多维数组执行计算,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库;
(2)mindquantum是量子-经典混合计算框架,支持多种量子神经网络的训练和推理;
(3)搭建的量子线路中所需执行的量子门需要从mindquantum.core模块中导入;
量子门
量子门(量子逻辑门)是对量子比特进行操作的基本逻辑单元,是量子线路的基础。对于经典电路来说,任意的逻辑电路都可以由一系列基本逻辑门构成,类似地,任意的量子线路也可以由一系列基本量子门构成,如单量子比特门和受控非门。常用的基本量子门有 X 门、Y门、Z门、Hadamard门(H
门)、CNOT门以及旋转门RX门、RY门和RZ门。
一般来说,量子门可以分为含参量子门和不含参量子门。例如,不含参的量子门有X 门、Y门、Z门、Hadamard门(H
门)、CNOT门,它们的矩阵形式分别如下:
X = ( 0 1 1 0 ) , Y = ( 0 − i i 0 ) , Z = ( 1 0 0 − 1 ) , H = 1 2 ( 1 1 1 − 1 ) , CNOT = ( 1 0 0 0 0 1 0 0 0 0 0 1 0 0 1 0 ) . \text{X}= \left( \begin{matrix} 0&1\\ 1&0 \end{matrix} \right), \text{Y}= \left( \begin{matrix} 0&-i\\ i&0 \end{matrix} \right), \text{Z}= \left( \begin{matrix} 1&0\\ 0&-1 \end{matrix} \right), \text{H}=\frac{1}{\sqrt{2}} \left( \begin{matrix} 1&1\\ 1&-1 \end{matrix} \right), \text{CNOT}= \left( \begin{matrix} 1&0&0&0\\ 0&1&0&0\\ 0&0&0&1\\ 0&0&1&0 \end{matrix} \right). X=(0110),Y=(0i−i0),Z=(100−1),H=21(111−1),CNOT= 1000010000010010 .
分别打印上述量子门的矩阵形式,可以得到:
print('Gate name:', X)
X.matrix()
Gate name: X
array([[0, 1],
[1, 0]])
print('Gate name:', Y)
Y.matrix()
Gate name: Y
array([[ 0.+0.j, -0.-1.j],
[ 0.+1.j, 0.+0.j]])
print('Gate name:', Z)
Z.matrix()
Gate name: Z
array([[ 1, 0],
[ 0, -1]])
print('Gate name:', H)
H.matrix()
Gate name: H
array([[ 0.70710678, 0.70710678],
[ 0.70710678, -0.70710678]])
对于CNOT门,其本质上是受控X门(Controlled-X
gate),因此在MindSpore Quantum中,如果我们需要执行CNOT门,只需设定X门的控制比特位和目标比特位即可(实际上,任意的量子门我们都可以设定控制比特位和所需执行量子门操作的目标比特位)。例如:
cnot = X.on(0, 1) # X门作用在第0位量子比特且受第1位量子比特控制
print(cnot)
X(0 <-: 1)
说明:
(1)X(0 <-: 1) ,表示第0位量子比特位为目标比特位,第1位量子比特位为控制比特位,第0位量子比特受第1位量子比特控制,若第1位量子比特为1,则对第0位量子比特执行X门操作,否则不作任何操作;
上面介绍了一些常用的不含参量子门,接下来,我们将介绍一些含参量子门(如旋转门RX门、RY门和RZ门),通过赋予旋转角度 θ \theta θ某些确定的值,可以得到作用不同的旋转门。另外,这些含参量子门是后续搭建量子神经网络的重要组成单元。
RX ( θ ) = e − i θ X 2 = cos ( θ 2 ) ⋅ I − i sin ( θ 2 ) ⋅ X = ( cos ( θ 2 ) − i sin ( θ 2 ) − i sin ( θ 2 ) cos ( θ 2 ) ) , \text{RX}(\theta)= e^{-\frac{i\theta X}{2}}=\cos\left(\frac{\theta}{2}\right)\cdot I-i\sin\left(\frac{\theta}{2}\right)\cdot X= \left( \begin{matrix} \cos\left(\frac{\theta}{2}\right)&-i\sin\left(\frac{\theta}{2}\right)\\ -i\sin\left(\frac{\theta}{2}\right)&\cos\left(\frac{\theta}{2}\right) \end{matrix} \right), RX(θ)=e−2iθX=cos(2θ)⋅I−isin(2θ)⋅X=(cos(2θ)−isin(2θ)−isin(2θ)cos(2θ)),
RY ( θ ) = e − i θ Y 2 = cos ( θ 2 ) ⋅ I − i sin ( θ 2 ) ⋅ Y = ( cos ( θ 2 ) − sin ( θ 2 ) sin ( θ 2 ) cos ( θ 2 ) ) , \text{RY}(\theta)= e^{-\frac{i\theta Y}{2}}=\cos\left(\frac{\theta}{2}\right)\cdot I-i\sin\left(\frac{\theta}{2}\right)\cdot Y= \left( \begin{matrix} \cos\left(\frac{\theta}{2}\right)&-\sin\left(\frac{\theta}{2}\right)\\ \sin\left(\frac{\theta}{2}\right)&\cos\left(\frac{\theta}{2}\right) \end{matrix} \right), RY(θ)=e−2iθY=cos(2θ)⋅I−isin(2θ)⋅Y=(cos(2θ)sin(2θ)−sin(2θ)cos(2θ)),
RZ ( θ ) = e − i θ Z 2 = cos ( θ 2 ) ⋅ I − i sin ( θ 2 ) ⋅ Z = ( e − i θ 2 0 0 e i θ 2 ) . \text{RZ}(\theta)= e^{-\frac{i\theta Z}{2}}=\cos\left(\frac{\theta}{2}\right)\cdot I-i\sin\left(\frac{\theta}{2}\right)\cdot Z= \left( \begin{matrix} e^{-\frac{i\theta}{2}}&0\\ 0&e^{\frac{i\theta}{2}} \end{matrix} \right). RZ(θ)=e−2iθZ=cos(2θ)⋅I−isin(2θ)⋅Z=(e−2iθ00e2iθ).
我们令 θ \theta θ分别为 0 0 0和 π \pi π,然后打印 RX ( 0 ) \text{RX}(0) RX(0)门、 RY ( π ) \text{RY}(\pi) RY(π)门和 RZ ( π ) \text{RZ}(\pi) RZ(π)门的矩阵形式,可以得到:
rx = RX('theta')
print('Gate name:', rx)
rx.matrix({'theta': 0}) # 赋予theta的值为0
Gate name: RX(theta)
array([[1.+0.j, 0.+0.j],
[0.+0.j, 1.+0.j]])
当 θ = 0 \theta=0 θ=0时,此时 RX ( 0 ) \text{RX}(0) RX(0)门就是我们熟悉的I门。
ry = RY('theta')
print('Gate name:', ry)
np.round(ry.matrix({'theta': np.pi})) # pi需要从np中导入,赋予theta的值为pi。
Gate name: RY(theta)
array([[ 0.+0.j, -1.+0.j],
[ 1.+0.j, 0.+0.j]])
当 θ = π \theta=\pi θ=π时,此时 RY ( π ) \text{RY}(\pi) RY(π)门就是我们熟悉的Y门。(相差一个全局相位 i i i)
rz = RZ('theta')
print('Gate name:', rz)
np.round(rz.matrix({'theta': np.pi})) # 赋予theta的值为pi,由于计算机中存在浮点数不精确的问题,因此通过函数np.round返回浮点数的四舍五入值。
Gate name: RZ(theta)
array([[0.-1.j, 0.+0.j],
[0.+0.j, 0.+1.j]])
当 θ = π \theta=\pi θ=π时,此时 RZ ( π ) \text{RZ}(\pi) RZ(π)门就是我们熟悉的Z门。(相差一个全局相位 − i -i −i)
量子线路
量子线路(也称量子逻辑电路)是最常用的通用量子计算模型,表示在抽象概念下,对于量子比特进行操作的线路。类比于经典线路,我们可以把一系列的量子逻辑门进行精心的设计组合,构成一个量子线路并完成一定的任务。例如,我们构建如下图所示的量子线路,该量子线路由三个量子门构成,分别是作用在 q 0 q_0 q0比特上的H门,作用在 q 0 q_0 q0和 q 1 q_1 q1比特上的CNOT门(即作用在 q 1 q_1 q1比特上且受 q 0 q_0 q0比特控制的X门)和作用在 q 2 q_2 q2比特上的 RY ( θ ) \text{RY}(\theta) RY(θ)门。
通过在量子线路中添加作用在不同量子比特位上的量子门即可快速完成对量子线路的搭建。
from mindquantum.core.circuit import Circuit # 导入Circuit模块,用于搭建量子线路
encoder = Circuit() # 初始化量子线路
encoder += H.on(0) # H门作用在第0位量子比特
encoder += X.on(1, 0) # X门作用在第1位量子比特且受第0位量子比特控制
encoder += RY('theta').on(2) # RY(theta)门作用在第2位量子比特
print(encoder) # 打印Encoder
encoder.summary() # 总结Encoder量子线路
┏━━━┓
q0: ──┨ H ┠───■───────
┗━━━┛ ┃
┏━┻━┓
q1: ────────┨╺╋╸┠─────
┗━━━┛
┏━━━━━━━━━━━┓
q2: ──┨ RY(theta) ┠───
┗━━━━━━━━━━━┛
Circuit Summary ╭──────────────────────┬───────╮ │ Info │ value │ ├──────────────────────┼───────┤ │ Number of qubit │ 3 │ ├──────────────────────┼───────┤ │ Total number of gate │ 3 │ │ Barrier │ 0 │ │ Noise Channel │ 0 │ │ Measurement │ 0 │ ├──────────────────────┼───────┤ │ Parameter gate │ 1 │ │ 1 ansatz parameter │ theta │ ╰──────────────────────┴───────╯
在Jupyter Notebook环境中,可以调用量子线路的.svg()接口绘制出量子线路的图片格式。调用量子线路的.svg().to_file(filename='circuit.svg')
接口则可将量子线路的svg格式图片保存到本地。
encoder.svg()

Software | Version |
---|---|
mindquantum | 0.9.11 |
scipy | 1.9.3 |
numpy | 1.24.2 |
System | Info |
Python | 3.9.11 |
OS | Linux x86_64 |
Memory | 261.99 GB |
CPU Max Thread | 64 |
Date | Tue Jul 2 16:18:55 2024 |