量子逻辑门
多比特量子逻辑门
量子计算机的空间随着量子比特的数量呈指数增长。对于 n 个量子比特,复向量空间的维数为 \(d=2^n\) 。为了描述多量子比特系统的状态,张量积被用来 "粘合 "算子和基向量。
让我们先考虑一个双量子比特系统。给定两个分别作用于一个量子比特的算子 A 和 B,那么作用于两个量子比特的联合算子 \(A\otimes B\) .
\[\begin{split}\begin{aligned}
A\otimes B=\begin{pmatrix}A_{00}\begin{pmatrix}B_{00}&B_{01}\\B_{10}&B_{11}\end{pmatrix}&A_{01}\begin{pmatrix}B_{00}&B_{01}\\B_{10}&B_{11}\end{pmatrix}\\A_{10}\begin{pmatrix}B_{00}&B_{01}\\B_{10}&B_{11}\end{pmatrix}&A_{11}\begin{pmatrix}B_{00}&B_{01}\\B_{10}&B_{11}\end{pmatrix}\end{pmatrix}
\end{aligned}\end{split}\]
其中, \(A_{jk} 和 B_{lm}\) 分别是A和B的矩阵元素。
与此类似,双量子比特系统的基向量也是通过单量子比特基向量的张量乘积形成的:
\[\begin{split}\begin{aligned}
|00\rangle=\begin{pmatrix}1\begin{pmatrix}1\\0\end{pmatrix}\\0\begin{pmatrix}1\\0\end{pmatrix}\end{pmatrix}=\begin{pmatrix}1\\0\\0\\0\end{pmatrix}\quad|01\rangle=\begin{pmatrix}1\begin{pmatrix}0\\1\end{pmatrix}\\0\begin{pmatrix}0\\1\end{pmatrix}\end{pmatrix}=\begin{pmatrix}0\\1\\0\\0\end{pmatrix}
\end{aligned}\end{split}\]
\[\begin{split}\begin{aligned}
|10\rangle=\begin{pmatrix}0\begin{pmatrix}1\\0\end{pmatrix}\\1\begin{pmatrix}1\\0\end{pmatrix}\end{pmatrix}=\begin{pmatrix}0\\0\\1\\0\end{pmatrix}\quad|11\rangle=\begin{pmatrix}0\begin{pmatrix}0\\1\end{pmatrix}\\1\begin{pmatrix}0\\1\end{pmatrix}\end{pmatrix}=\begin{pmatrix}0\\0\\0\\1\end{pmatrix}
\end{aligned}\end{split}\]
注意,我们为基向量的张量乘引入了一个简写 \(|0\rangle\otimes|0\rangle\) 记作 \(\left|00\right\rangle\) 。n 量子比特系统的状态可以用n维量子比特基向量的张量积来描述。请注意,2量子比特系统的基向量是4维的;如前所述,一般来说,n量子比特系统的基向量是 \(2^n\) 维的。
大多数双量子比特门都属于受控类型(SWAP 门是个例外)。一般来说,受控双量子比特门 \(C_U\) 的作用是,当第一个量子比特的状态处于 \(|1\rangle\) 时,将单量子比特单元应用于第二个量子比特U。假设有一个矩阵U表示
\[\begin{split}\begin{aligned}
U=\begin{pmatrix}u_{00}&u_{01}\\u_{10}&u_{11}\end{pmatrix}.
\end{aligned}\end{split}\]
我们可以计算出 \(C_U\) 的作用如下。回顾一下,双量子比特系统的基向量排序为 \(|00\rangle,|01\rangle,|10\rangle,|11\rangle\) 。假设控制量子比特是量子比特 \(q_0\) 。如果控制量子比特是 \(|1\rangle\) 的情况下,U则应作用于目标位。因此,在 \(C_U\) 的作用下,基向量会根据以下公式进行变换
\[\begin{aligned}
C_{U}: \underset{\text{qubit}~1}{\left|0\right\rangle}\otimes \underset{\text{qubit}~0}{\left|0\right\rangle} &\rightarrow \underset{\text{qubit}~1}{\left|0\right\rangle}\otimes \underset{\text{qubit}~0}{\left|0\right\rangle}
\end{aligned}\]
\[\begin{aligned}
C_{U}: \underset{\text{qubit}~1}{\left|0\right\rangle}\otimes \underset{\text{qubit}~0}{\left|1\right\rangle} &\rightarrow \underset{\text{qubit}~1}{\left|0\right\rangle}\otimes \underset{\text{qubit}~0}{\left|1\right\rangle}
\end{aligned}\]
\[\begin{aligned}
C_{U}: \underset{\text{qubit}~1}{\left|1\right\rangle}\otimes \underset{\text{qubit}~0}{\left|0\right\rangle} &\rightarrow \underset{\text{qubit}~1}{\left|1\right\rangle}\otimes \underset{\text{qubit}~0}{U\left|0\right\rangle}
\end{aligned}\]
\[\begin{aligned}
C_{U}: \underset{\text{qubit}~1}{\left|1\right\rangle}\otimes \underset{\text{qubit}~0}{\left|1\right\rangle} &\rightarrow \underset{\text{qubit}~1}{\left|1\right\rangle}\otimes \underset{\text{qubit}~0}{U\left|1\right\rangle}
\end{aligned}\]
\(C_U\) 的矩阵形式为:
\[\begin{split}\begin{aligned}
\begin{aligned}
C_U = \begin{pmatrix}
1 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 \\
0 & 0 & u_{00} & u_{01} \\
0 & 0 & u_{10} & u_{11}
\end{pmatrix}
\end{aligned}
\end{aligned}\end{split}\]
其中,右下角对应酉矩阵形式为上述U4门:
CU
\(\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & u_{00} & u_{01} \\ 0 & 0 & u_{10} & u_{11} \end{bmatrix}\quad\)
CNOT门
作用于两个量子比特的量子门,当第一个量子比特(控制位)为 |1⟩的时候,对第二个量子比特施加一个X门的效果。
所以CNOT在很多文献中也称为CX门。
CNOT
\(\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{bmatrix}\quad\)
CZ门
如果控制量子比特是 |1⟩的时候,受控 Z 门会翻转目标量子比特的相位。无论控制量子位是 MSB 还是 LSB,矩阵看起来都是一样的:
\[\begin{split}\begin{aligned}
C_Z =
\begin{pmatrix}
1 & 0 & 0 & 0\\
0 & 1 & 0 & 0\\
0 & 0 & 1 & 0\\
0 & 0 & 0 & -1
\end{pmatrix}
\end{aligned}\end{split}\]
CZ
\(\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & -1 \end{bmatrix}\quad\)
SWAP门
SWAP 门交换两个量子比特。它将基向量变换为
\[\begin{aligned}
\left|00\right\rangle \rightarrow \left|00\right\rangle~,~\left|01\right\rangle \rightarrow \left|10\right\rangle~,~\left|10\right\rangle \rightarrow \left|01\right\rangle~,~\left|11\right\rangle \rightarrow \left|11\right\rangle
\end{aligned}\]
SWAP
\(\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}\quad\)
受控相位旋转CR门
如果两个量子比特都处于 \(\left|11\right\rangle\) 状态,则进行相位旋转。无论控制比特是 MSB 还是 LSB,矩阵看起来都是一样的。
CR
\(\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & e^{i\theta} \end{bmatrix}\quad\)
iSWAP
\(\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & i & 0 \\ 0 & i & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}\quad\)
RXX
\(\begin{bmatrix} \cos(\theta/2) & 0 & 0 & -i\sin(\theta/2) \\ 0 & \cos(\theta/2) & -i\sin(\theta/2) & 0 \\ 0 & -i\sin(\theta/2) & \cos(\theta/2) & 0 \\ -i\sin(\theta/2) & 0 & 0 & \cos(\theta/2) \end{bmatrix}\quad\)
RYY
\(\begin{bmatrix} \cos(\theta/2) & 0 & 0 & i\sin(\theta/2) \\ 0 & \cos(\theta/2) & -i\sin(\theta/2) & 0 \\ 0 & -i\sin(\theta/2) & \cos(\theta/2) & 0 \\ i\sin(\theta/2) & 0 & 0 & \cos(\theta/2) \end{bmatrix}\quad\)
RZZ
\(\begin{bmatrix} e^{-i\theta/2} & 0 & 0 & 0 \\ 0 & e^{i\theta/2} & 0 & 0 \\ 0 & 0 & e^{i\theta/2} & 0 \\ 0 & 0 & 0 & e^{-i\theta/2} \end{bmatrix}\quad\)
RZX
\(\begin{bmatrix} \cos(\theta/2) & 0 & -i\sin(\theta/2) & 0 \\ 0 & \cos(\theta/2) & 0 & i\sin(\theta/2) \\ -i\sin(\theta/2) & 0 & \cos(\theta/2) & 0 \\ 0 & i\sin(\theta/2) & 0 & \cos(\theta/2) \end{bmatrix}\quad\)
Toffoli门
如果前两个量子位都是 \(\left|1\right\rangle\) ,托福利门就会翻转第三个量子位。
\[\begin{aligned}
|abc\rangle\to|bc\oplus a\rangle\otimes|b\rangle\otimes|c\rangle.
\end{aligned}\]
Toffoli
\(\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}\quad\)
QPanda 2把所有的量子逻辑门封装为API向用户提供使用,并可获得QGate类型的返回值。比如,您想要使用Hadamard门,就可以通过如下方式获得:
from pyqpanda import *
import numpy as np
qvm = CPUQVM()
qvm.init_qvm()
qubits = qvm.qAlloc_many(4)
h = H(qubits[0])
其中参数为目标比特,返回值为量子逻辑门
pyqpanda中支持的不含角度的单门有: I、 H、 T、 S、 X、 Y、 Z、 X1、 Y1、 Z1
qubit如何申请会在 GPU量子虚拟机 部分介绍。
单门带有一个旋转角的逻辑门,例如RX门:
rx = RX(qubits[0], np.pi/3)
第一个参数为目标比特,第二个参数为旋转角度
pyqpanda中支持的单门带有一个旋转角度的逻辑门有: RX、RY、RZ、U1、P
pyqpanda中还支持 U2、U3、U4 门,其用法如下:
# U2(qubit, phi, lambda) 有两个角度
u2 = U2(qubits[0], np.pi, np.pi/2)
# U3(qubit, theta, phi, lambda) 有三个角度
u3 = U3(qubits[0], np.pi, np.pi/2, np.pi/4)
# U4(qubit, alpha, beta, gamma, delta) 有四个角度
u4 = U4(qubits[0], np.pi, np.pi/2, np.pi/4, np.pi/2)
两比特量子逻辑门的使用和单比特量子逻辑门的用法相似,只不过是输入的参数不同,例如CNOT门:
cnot = CNOT(qubits[0], qubits[1])
第一个参数为控制比特
第二个参数为目标比特
注:两个比特不能相同
pyqpanda中支持的双门不含角度的逻辑门有: CNOT、CZ 、SWAP、iSWAp、SqiSWAP
双门带有旋转角度的门有:CR、RXX 、RYY、RZZ、RZX,例如CR门:
cr = CR(qubits[0], qubits[1], np.pi)
第一个参数为控制比特, 第二个参数为目标比特, 第三个参数为旋转角度
支持CU门,使用方法如下:
# CU(control, target, alpha, beta, gamma, delta) 有四个角度
cu = CU(qubits[0], qubits[1], np.pi, np.pi/2, np.pi/3, np.pi/4)
获得三量子逻辑门 Toffoli 的方式:
toffoli = Toffoli(qubits[0], qubits[1], qubits[2])
三比特量子逻辑门Toffoli实际上是CCNOT门,前两个参数是控制比特,最后一个参数是目标比特。
循循善友