量子计算的核心在于量子比特的操控,而量子门则是实现这种操控的基本单元。通过对单比特或多比特施加不同的量子门操作,可以实现任意量子态的演化,从而执行量子算法。事实上,X、Y、Z 门可实现单量子比特在 Bloch 球上的任意旋转,而 CNOT 门则实现了以控制比特为条件的目标比特操作。因此,基于这四个基础门可构建任意量子门。
基础量子门
反向门
由于量子门均为酉操作,满足:
U \cdot U^\dagger = I
因此,任何量子门的共轭操作(反向门)对应于其旋转操作的反向旋转。在 Qiskit 中可基于 inverse() 实现:
qc.inverse()单比特旋转门:Rx, Ry, Rz 门
Rx、Ry、Rz 门分别表示在 Bloch 球上绕 x、y、z 三个坐标轴的部分旋转操作。它们的作用是精确地改变量子比特的相位和概率振幅,是构建任意单比特操作的基础,有:
\begin{aligned}
R_X(\theta) &= e^{-i \frac{\theta}{2} \sigma_X}
= \begin{bmatrix}
\cos{\frac{\theta}{2}} & -i\sin{\frac{\theta}{2}} \\[2mm]
-i\sin{\frac{\theta}{2}} & \cos{\frac{\theta}{2}}
\end{bmatrix},\\[1mm]
R_Y(\theta) &= e^{-i \frac{\theta}{2} \sigma_Y}
= \begin{bmatrix}
\cos{\frac{\theta}{2}} & -\sin{\frac{\theta}{2}} \\[1mm]
\sin{\frac{\theta}{2}} & \cos{\frac{\theta}{2}}
\end{bmatrix},\\[1mm]
R_Z(\theta) &= e^{-i \frac{\theta}{2} \sigma_Z}
= \begin{bmatrix}
e^{-i\theta/2} & 0 \\[1mm]
0 & e^{i\theta/2}
\end{bmatrix}.
\end{aligned}在 Qiskit 中可使用 rx(theta, qubit)、ry(theta, qubit) 和 rz(theta, qubit) 实现):
qc.rx(theta/np.pi, qubit)
qc.ry(theta/np.pi, qubit)
qc.rz(theta/np.pi, qubit)H 门(Hadamard 门)
H 门在 Bloch 球上的操作为:绕 Z 轴旋转 180 度后,再绕 Y 轴旋转 90 度,即:
\begin{aligned}
H &= Y^{\frac{1}{2}} Z \\
&= \left(
\begin{bmatrix}
0 & -i \\
i & 0
\end{bmatrix}
\right)^{\frac{1}{2}}
\begin{bmatrix}
1 & 0 \\
0 & -1
\end{bmatrix} \\
&= \left(
i
\begin{bmatrix}
0 & -1 \\
1 & 0
\end{bmatrix}
\right)^{\frac{1}{2}}
\begin{bmatrix}
1 & 0 \\
0 & -1
\end{bmatrix} \\
&= \frac{1+i}{\sqrt{2}}
\left(
\frac{1}{\sqrt{2}}
\begin{bmatrix}
1 & -1 \\
1 & 1
\end{bmatrix}
\right)
\begin{bmatrix}
1 & 0 \\
0 & -1
\end{bmatrix} \\
&= \frac{1+i}{2}
\begin{bmatrix}
1 & -1 \\
1 & 1
\end{bmatrix}
\begin{bmatrix}
1 & 0 \\
0 & -1
\end{bmatrix} \\
&= \frac{1+i}{2}
\begin{bmatrix}
1 & 1 \\
1 & -1
\end{bmatrix}
\end{aligned}根据(本来写易得的):
\begin{aligned}
\frac{1+i}{2}
&= \frac{|1+i|}{2} \, e^{i\arg(1+i)} \\
&= \frac{\sqrt{(1+i)(1-i)}}{2} \, e^{i\arg(1+i)} \\
&= \frac{\sqrt{2}}{2} \, e^{i\arg(1+i)} \\
&= \frac{1}{\sqrt{2}} \, e^{i\arg(1+i)} \\
&= \frac{1}{\sqrt{2}} \, e^{i\tan^{-1}\!\left(\frac{1}{1}\right)} \\
&= \frac{1}{\sqrt{2}} \, e^{i\pi/4}
\end{aligned}为保证 H 是酉矩阵且自逆,其本质部分应有:
H^2 =1
因此,设置全局相位为:
e^{i\pi/4}则有:
\begin{aligned}
H &= \frac{1+i}{2}
\begin{bmatrix}
1 & 1 \\
1 & -1
\end{bmatrix} \\
&\sim \frac{1}{\sqrt{2}}
\begin{bmatrix}
1 & 1 \\
1 & -1
\end{bmatrix} \\
&=\ket{+}\bra{0}+\ket{-}\bra{1}\\
\end{aligned}在 Qiskit 中可写为 h(qubit),有:
qc.rz(np.pi, qubit)
qc.ry(np.pi/2, qubit)相位门
S 门
S 门为绕 Z 轴旋转 90 度的门:
S =
\begin{bmatrix}
1 & 0 \\
0 & i
\end{bmatrix}
= Z^{1/2}
= e^{i \pi/4} \, R_Z\left(\frac{\pi}{2}\right)
\sim R_Z\left(\frac{\pi}{2}\right)在 Qiskit 中可使用 s(qubit),有:
qc.rz(np.pi/2, qubit)T 门
T 门为绕 Z 轴旋转 45 度的门:
T = \begin{bmatrix}
1 & 0 \\
0 & e^{i\pi/4}
\end{bmatrix}
= Z^{1/4}
= e^{i \pi/8} \, R_Z\left(\frac{\pi}{4}\right)
\sim R_Z\left(\frac{\pi}{4}\right)在 Qiskit 中可使用 t(qubit),有:
qc.rz(np.pi/4, qubit)P 门
P 门也是相位门,作用是 0 不变,1 增加相位 ϕ:
P(\phi) =
\begin{bmatrix}
1 & 0 \\
0 & e^{i\phi}
\end{bmatrix}
= Z^{\phi/\pi}
= e^{i \phi/2} \, R_Z(\phi)
\sim R_Z(\phi)在 Qiskit 中可使用 p(phi, qubit),有:
qc.rz(phi, qubit)通用单比特门
U3 门
U3(θ, φ, λ) 是一个通用单比特门,它可以实现任意单比特的旋转:
\begin{aligned}
U3(\theta, \phi, \lambda)
&= R_z(\phi) \, R_y(\theta) \, R_z(\lambda) \\
&=
\begin{bmatrix} e^{-i\phi/2} & 0 \\ 0 & e^{i\phi/2} \end{bmatrix}
\begin{bmatrix} \cos(\theta/2) & -\sin(\theta/2) \\ \sin(\theta/2) & \cos(\theta/2) \end{bmatrix}
\begin{bmatrix} e^{-i\lambda/2} & 0 \\ 0 & e^{i\lambda/2} \end{bmatrix} \\
&=
\begin{bmatrix} e^{-i\phi/2}\cos(\theta/2) & - e^{-i\phi/2}\sin(\theta/2) \\ e^{i\phi/2}\sin(\theta/2) & e^{i\phi/2}\cos(\theta/2) \end{bmatrix}
\begin{bmatrix} e^{-i\lambda/2} & 0 \\ 0 & e^{i\lambda/2} \end{bmatrix} \\
&=
\begin{bmatrix}
e^{-i(\phi+\lambda)/2}\cos(\theta/2) & - e^{i(\lambda-\phi)/2}\sin(\theta/2) \\
e^{i(\phi+\lambda)/2}\sin(\theta/2) & e^{i(\phi+\lambda)/2}\cos(\theta/2)
\end{bmatrix} \\
&\sim
\begin{bmatrix}
\cos(\theta/2) & - e^{i\lambda}\sin(\theta/2) \\
e^{i\phi}\sin(\theta/2) & e^{i(\phi+\lambda)}\cos(\theta/2)
\end{bmatrix}
\end{aligned}其中,θ 为绕 Y 轴的旋转角度,φ 和 λ 为绕 Z 轴的旋转角度,这里忽略了不可测量的整体相位 (φ + λ)/2。
在 Qiskit 中可使用 U3(theta, phi, lamb, qubit),有:
qc.rz(phi, qubit)
qc.ry(theta, qubit)
qc.rz(lamb, qubit)U2 门
U2 门是 U3 门的特例,其作用是改变量子比特的相位和振幅,但不产生 θ 方向的振幅缩放:
U2(\phi, \lambda) = U3\Big(\frac{\pi}{2}, \phi, \lambda\Big)
= \frac{1}{\sqrt{2}}
\begin{bmatrix}
1 & -e^{i\lambda} \\
e^{i\phi} & e^{i(\phi+\lambda)}
\end{bmatrix}此外,易证:
H = U2(0, \pi)
在 Qiskit 中可以用 U2(phi, lamb, qubit) 实现,有:
qc.U3(np.pi/2, phi, lamb, qubit)U1 门
U1 门是 U3 门的另一个特例,其作用是只改变量子比特的相位,但不改变振幅,即:
U1(\lambda) = U3(0, 0, \lambda) = \begin{bmatrix} 1 & 0 \\ 0 & e^{i\lambda} \end{bmatrix} = e^{i\lambda/2} R_z(\lambda) \propto R_z(\lambda)即 U1 门与 R_z 门仅相差一个整体相位 θ/2,该相位对测量无影响,因此可以直接使用 Rz 门。事实上,U1 门在 Qiskit 中已被标记为“过时”,推荐直接使用 Rz 门。
受控门
CX 门(CNOT)
CX 门的功能是,当控制比特为 1 时,对目标比特执行 X 门,即:
CX = CNOT = \ket{0}\bra{0} \otimes I + \ket{1}\bra{1} \otimes X =\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\
0 & 0 & 1 & 0 \end{bmatrix}在 Qiskit 中可使用 cx(control_qubit, target_qubit)。
CZ 门(受控 Z)
CZ 门的功能是,当控制比特为 1 时,对目标比特执行 Z 门,即:
\begin{aligned}
CZ &= \ket{0}\bra{0} \otimes I + \ket{1}\bra{1} \otimes Z \\
&=(I \otimes H) \cdot \text{CNOT} \cdot (I \otimes H) \\
&= \left(\frac{1}{\sqrt{2}}\begin{bmatrix}1 & 0 \\ 0 & 1\end{bmatrix} \otimes \begin{bmatrix}1 & 1 \\ 1 & -1\end{bmatrix}\right)
\cdot \text{CNOT} \cdot
\left(\frac{1}{\sqrt{2}}\begin{bmatrix}1 & 0 \\ 0 & 1\end{bmatrix} \otimes \begin{bmatrix}1 & 1 \\ 1 & -1\end{bmatrix}\right) \\
&= \frac{1}{2}\begin{bmatrix}1 & 1 & 0 & 0 \\ 1 & -1 & 0 & 0 \\ 0 & 0 & 1 & 1 \\ 0 & 0 & 1 & -1\end{bmatrix}
\cdot
\begin{bmatrix}1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0\end{bmatrix}
\cdot
\begin{bmatrix}1 & 1 & 0 & 0 \\ 1 & -1 & 0 & 0 \\ 0 & 0 & 1 & 1 \\ 0 & 0 & 1 & -1\end{bmatrix} \\
&= \frac{1}{2}
\begin{bmatrix}1 & 1 & 0 & 0 \\ 1 & -1 & 0 & 0 \\ 0 & 0 & 1 & 1 \\ 0 & 0 & -1 & 1\end{bmatrix}
\cdot
\begin{bmatrix}1 & 1 & 0 & 0 \\ 1 & -1 & 0 & 0 \\ 0 & 0 & 1 & 1 \\ 0 & 0 & 1 & -1\end{bmatrix} \\
&= \begin{bmatrix}1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0\\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & -1\end{bmatrix}
\end{aligned}在 Qiskit 中可使用 cz(control_qubit, target_qubit),有:
qc.h(target_qubit)
qc.cx(control_qubit, target_qubit)
qc.h(target_qubit)CY 门(受控 Y)
CY 门的功能是,当控制比特为 1 时,对目标比特执行 Y 门,即:
\begin{aligned}
CY &= \ket{0}\bra{0} \otimes I + \ket{1}\bra{1} \otimes Y \\
&= (I \otimes S) \cdot CX \cdot (I \otimes S^\dagger) \\
&=
\begin{bmatrix}
1 & 0 & 0 & 0 \\
0 & i & 0 & 0 \\
0 & 0 & 1 & 0 \\
0 & 0 & 0 & i
\end{bmatrix}
\cdot
\begin{bmatrix}
1 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 \\
0 & 0 & 0 & 1 \\
0 & 0 & 1 & 0
\end{bmatrix}
\cdot
\begin{bmatrix}
1 & 0 & 0 & 0 \\
0 & -i & 0 & 0 \\
0 & 0 & 1 & 0 \\
0 & 0 & 0 & -i
\end{bmatrix} \\
&=
\begin{bmatrix}
1 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 \\
0 & 0 & 0 & -i \\
0 & 0 & i & 0
\end{bmatrix}
\end{aligned}在 Qiskit 中可使用 cy(control_qubit, target_qubit),有:
qc.s(target_qubit)
qc.cx(control_qubit, target_qubit)
qc.sdg(target_qubit) #对 target_qubit 的 dagger 做 s 门操作CH 门
CH 门的功能为,当控制比特为 1 时,对目标施加 H;否则保持不变:
\begin{aligned}
\mathit{CH} &= \ket{0}\bra{0} \otimes I+\ket{1}\bra{1} \otimes H \\
&= (I \otimes R_Y(\frac{\pi}{4})) \cdot CZ \cdot (I \otimes R_Y(-\frac{\pi}{4}))\\
&=
\begin{bmatrix}
1 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 \\
0 & 0 & \frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}} \\
0 & 0 & \frac{1}{\sqrt{2}} & -\frac{1}{\sqrt{2}}
\end{bmatrix}
\end{aligned}在 Qiskit 中可使用 ch(control_qubit, target_qubit),有:
qc.ry(np.pi/4, target_qubit)
qc.cz(control_qubit, target_qubit)
qc.ry(-np.pi/4, target_qubit)交换门
SWAP 门
SWAP 门用于交换两个量子比特的状态,即:
\begin{aligned}
\mathit{SWAP} &= \ket{0}\bra{0} \otimes \ket{0}\bra{0} +\ket{0}\bra{1} \otimes \ket{1}\bra{0} +\ket{1}\bra{0} \otimes \ket{0}\bra{1} +\ket{1}\bra{1} \otimes \ket{1}\bra{1}\\
&={CX}_{12}\, {CX}_{21}\, {CX}_{12} \\
&=
\begin{bmatrix}
1 & 0 & 0 & 0\\
0 & 0 & 1 & 0\\
0 & 1 & 0 & 0\\
0 & 0 & 0 & 1
\end{bmatrix}
\end{aligned}在 Qiskit 中可使用 swap(qubit_1, qubit_2),有:
qc.cx(qubit_1, qubit_2)
qc.cx(qubit_2, qubit_1)
qc.cx(qubit_1, qubit_2)iSWAP 门
iSWAP 门用于交换两个比特的状态,并在交换过程中累积 i 相位,即:
\begin{aligned}
\mathit{iSWAP}
&= \ket{0}\bra{0} \otimes \ket{0}\bra{0} + \ket{1}\bra{1} \otimes \ket{1}\bra{1} + i\ket{0}\bra{1} \otimes \ket{1}\bra{0} + i\ket{1}\bra{0} \otimes \ket{0}\bra{1}\\
&=(S \otimes S) \cdot CZ \cdot \mathit{SWAP} \\
&=
\begin{bmatrix}
1 & 0 & 0 & 0\\
0 & 0 & i & 0\\
0 & i & 0 & 0\\
0 & 0 & 0 & 1
\end{bmatrix}
\end{aligned}在 Qiskit 中可使用 iswap(qubit_1, qubit_2)。
受控旋转门
CRX 门
CRX 门是受控的 X 轴旋转门,即根据控制比特状态决定是否对目标比特做 Rx 操作:
\begin{aligned}
\mathit{CR}_X(\theta)
&= \ket{0}\bra{0} I + \ket{1}\bra{1} R_X(\theta)\\
&=
\begin{bmatrix}
1 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 \\
0 & 0 & \cos(\theta/2) & -i \sin(\theta/2) \\
0 & 0 & -i \sin(\theta/2) & \cos(\theta/2)
\end{bmatrix}
\end{aligned}在 Qiskit 中可使用 crx(theta, control_qubit, target_qubit)。
CRY 门
CRY 门则是受控的 Y 轴旋转门,即根据控制比特状态决定是否对目标比特做 Ry 操作:
\begin{aligned}
\mathit{CR}_Y(\theta)
&= \ket{0}\bra{0} I + \ket{1}\bra{1} R_Y(\theta)\\
&=
\begin{bmatrix}
1 & 0 & 0 & 0\\
0 & 1 & 0 & 0\\
0 & 0 & \cos(\theta/2) & -\sin(\theta/2)\\
0 & 0 & \sin(\theta/2) & \cos(\theta/2)
\end{bmatrix}
\end{aligned}在 Qiskit 中可使用 cry(theta, control_qubit, target_qubit)。
CRZ 门
CRZ 门是受控的 Z 轴旋转门,即根据控制比特状态决定是否对目标比特做 Rz 操作:
\begin{aligned}
\mathit{CR}_Z(\theta)
&= \ket{0}\bra{0} I + \ket{1}\bra{1} R_Z(\theta)\\
&=
\begin{bmatrix}
1 & 0 & 0 & 0\\
0 & 1 & 0 & 0\\
0 & 0 & e^{-i\theta/2} & 0\\
0 & 0 & 0 & e^{i\theta/2}
\end{bmatrix}
\end{aligned}在 Qiskit 中可使用 crz(theta, control_qubit, target_qubit)。
CS 门
CS 门为受控 S 门:
\begin{aligned}
CS &= \ket{0}\bra{0} \otimes I + \ket{1}\bra{1} \otimes S \\
&=
\begin{bmatrix}
1 & 0 & 0 & 0\\
0 & 1 & 0 & 0\\
0 & 0 & 1 & 0\\
0 & 0 & 0 & i
\end{bmatrix}
\end{aligned}在 Qiskit 中可使用 cs(control_qubit, target_qubit)。
CP 门
CP 门为受控 P 门:
\begin{aligned}
CP(\theta)
&= \ket{0}\bra{0} \otimes I + \ket{1}\bra{1} \otimes P(\theta) \\
&=
\begin{bmatrix}
1 & 0 & 0 & 0\\
0 & 1 & 0 & 0\\
0 & 0 & 1 & 0\\
0 & 0 & 0 & e^{i\theta}
\end{bmatrix}
\end{aligned}在 Qiskit 中可使用 cp(theta, control_qubit, target_qubit)。
CT 门
CT 门为受控 T 门:
\begin{aligned}
CT &= \ket{0}\bra{0} \otimes I + \ket{1}\bra{1} \otimes T \\
&=
\begin{bmatrix}
1 & 0 & 0 & 0\\
0 & 1 & 0 & 0\\
0 & 0 & 1 & 0\\
0 & 0 & 0 & e^{i\pi/4}
\end{bmatrix}
\end{aligned}在 Qiskit 中没有 CT 门方法,可以使用 cp(np.pi/4, control_qubit, target_qubit) 实现。
三比特门
Toffoli 门(CCX)
Toffoli 门为三量子门。其中,a 和 b 为控制比特,c 为目标比特。当且仅当 a 和 b 均为 1 时,翻转 c:
\begin{aligned}
CCX
&=I_{ab}\otimes I_c + \ket{11}\bra{11}_{ab}\otimes (X - I)_c \\
&=
\begin{bmatrix}
1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\
0 & 0 & 0 & 0 & 0 & 0 & 1 & 0
\end{bmatrix}
\end{aligned}在 Qiskit 中可使用 ccx(control_qubit_1, control_qubit_2, target_qubit)。
Fredkin 门
Fredkin 门为三量子门。其中,a 为控制比特,b 和 c 为目标比特。当 a 为 1 时,交换目标比特 b 和 c:
\begin{aligned}
\mathit{Fredkin}
&= |0\rangle \langle 0| \otimes I \otimes I + |1\rangle \langle 1| \otimes \mathit{SWAP} \\
&= \begin{bmatrix}
1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 1 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 1 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\
0 & 0 & 0 & 0 & 0 & 1 & 0 & 0\\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 1
\end{bmatrix}
\end{aligned}在 Qiskit 中可使用 fredkin(control_qubit, target_qubit_1, target_qubit_2)。

发表回复