量子逻辑门¶
经典计算中,最基本的单元是比特,而最基本的控制模式是逻辑门。我们可以通过逻辑门的组合来达到我们控制电路的目的。类似地,处理量子比特的方式就是量子逻辑门。 使用量子逻辑门,我们有意识的使量子态发生演化。所以量子逻辑门是构成量子算法的基础。
量子逻辑门由酉矩阵表示。最常见的量子门在一个或两个量子位的空间上工作,就像常见的经典逻辑门在一个或两个位上操作一样。
常见量子逻辑门矩阵形式¶
单比特量子逻辑门:¶
|
\(\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}\quad\) |
|
|
\(\begin{bmatrix} 1/\sqrt {2} & 1/\sqrt {2} \\ 1/\sqrt {2} & -1/\sqrt {2} \end{bmatrix}\quad\) |
|
|
\(\begin{bmatrix} 1 & 0 \\ 0 & \exp(i\pi / 4) \end{bmatrix}\quad\) |
|
|
\(\begin{bmatrix} 1 & 0 \\ 0 & 1i \end{bmatrix}\quad\) |
|
|
\(\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}\quad\) |
|
|
\(\begin{bmatrix} 0 & -1i \\ 1i & 0 \end{bmatrix}\quad\) |
|
|
\(\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}\quad\) |
|
|
\(\begin{bmatrix} 1/\sqrt {2} & -1i/\sqrt {2} \\ -1i/\sqrt {2} & 1/\sqrt {2} \end{bmatrix}\quad\) |
|
|
\(\begin{bmatrix} 1/\sqrt {2} & -1/\sqrt {2} \\ 1/\sqrt {2} & 1/\sqrt {2} \end{bmatrix}\quad\) |
|
|
\(\begin{bmatrix} \exp(-i\pi/4) & 0 \\ 0 & \exp(i\pi/4) \end{bmatrix}\quad\) |
|
|
\(\begin{bmatrix} \cos(\theta/2) & -1i×\sin(\theta/2) \\ -1i×\sin(\theta/2) & \cos(\theta/2) \end{bmatrix}\quad\) |
|
|
\(\begin{bmatrix} \cos(\theta/2) & -\sin(\theta/2) \\ \sin(\theta/2) & \cos(\theta/2) \end{bmatrix}\quad\) |
|
|
\(\begin{bmatrix} \exp(-i\theta/2) & 0 \\ 0 & \exp(i\theta/2) \end{bmatrix}\quad\) |
|
|
\(\begin{bmatrix} 1 & 0 \\ 0 & \exp(i\theta) \end{bmatrix}\quad\) |
|
|
\(\begin{bmatrix} 1/\sqrt {2} & -\exp(i\lambda)/\sqrt {2} \\ \exp(i\phi)/\sqrt {2} & \exp(i\lambda+i\phi)/\sqrt {2} \end{bmatrix}\quad\) |
|
|
\(\begin{bmatrix} \cos(\theta/2) & -\exp(i\lambda)×\sin(\theta/2) \\ \exp(i\phi)×\sin(\theta/2) & \exp(i\lambda+i\phi)×\cos(\theta/2) \end{bmatrix}\quad\) |
|
|
\(\begin{bmatrix} u0 & u1 \\ u2 & u3 \end{bmatrix}\quad\) |
多比特量子逻辑门:¶
|
\(\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{bmatrix}\quad\) |
|
|
\(\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & \exp(i\theta) \end{bmatrix}\quad\) |
|
|
\(\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos(\theta) & i×\sin(\theta) & 0 \\ 0 & i×\sin(\theta) & \cos(\theta) & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}\quad\) |
|
|
\(\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}\quad\) |
|
|
\(\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & -1 \end{bmatrix}\quad\) |
|
|
\(\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & u0 & u1 \\ 0 & 0 & u2 & u3 \end{bmatrix}\quad\) |
|
|
\(\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\) |
|
|
\(\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\) |
|
|
\(\begin{bmatrix} \exp(-i\theta/2) & 0 & 0 & 0 \\ 0 & \exp(i\theta/2) & 0 & 0 \\ 0 & 0 & \exp(i\theta/2) & 0 \\ 0 & 0 & 0 & \exp(-i\theta/2) \end{bmatrix}\quad\) |
|
|
\(\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\) |
|
|
\(\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门,就可以通过如下方式获得:
QGate h = H(qubit);
可以看到,H函数只接收一个qubit,qubit如何申请会在 量子虚拟机 部分介绍。
再比如,您想要使用RX门,可以通过如下方式获得:
QGate rx = RX(qubit, PI);
如上所示,RX门接收两个参数,第一个是目标量子比特,第二个偏转角度。您也可以通过相同的方式使用RY,RZ门。
两比特量子逻辑门的使用和单比特量子逻辑门的用法相似,只不过是输入的参数不同,举个使用CNOT的例子:
QGate cnot = CNOT(control_qubit, target_qubit);
CNOT门接收两个参数,第一个是控制比特,第二个是目标比特。
接口介绍¶
在本章的开头介绍过,所有的量子逻辑门都是酉矩阵,那么您也可以对量子逻辑门做转置共轭操作。QGate类型有两个成员函数可以做转置共轭操作: dagger、setDagger。
setDagger的作用是根据输入参数更新当前量子逻辑门的dagger标记,在计算时计算后端会根据dagger判断当前量子逻辑门是否需要执行转置共轭操作。举个列子:
auto gate = H(qubit); gate.setDagger(true);
备注
setDagger有一个布尔类型参数,用来设置当前逻辑门是否需要转置共轭操作。
dagger的作用是复制一份当前的量子逻辑门,并更新复制的量子逻辑门的dagger标记。举个例子:
QGate rx_dagger = RX(qubit,PI).dagger();
除了转置共轭操作,您也可以为量子逻辑门添加控制比特,添加控制比特后,当前量子逻辑门是否执行需要根据控制比特的量子态决定。如果控制比特的量子态为|1>态时,当前量子逻辑门可被执行, 如果控制比特的量子态为|0>时,当前量子逻辑门不会被执行。QGate类型有两个成员函数帮助您添加控制比特:control、setControl。
setControl的作用是给当前的量子逻辑门添加控制比特,例如:
auto gate = RX(qubit,PI) gate.setControl(qvec);
control的作用是复制当前的量子逻辑门,并给复制的量子逻辑门添加控制比特,例如:
QGate rx_control = RX(qubit,PI).control(qvec);
备注
setControl、control都需要接收一个参数,参数类型为QVec,QVec是qubit的vector。
实例¶
以下实例主要是向您展现QGate类型接口的使用方式,我们先使用CPUQVM()来创建一个虚拟机,再调用init接口初始化,向量子虚拟机申请3个量子比特,并把q[0],q[1]存放在一个量子比特容器中。 接着,调用QProg构建一个量子程序prog,然后我们可以通过 << 操作符把量子逻辑门插入到prog中。最后调用probRunTupleList概率测量运行接口,就可以得到计算结果了。
#include "QPanda.h" using namespace QPanda; int main(void) { auto qvm = CPUQVM(); qvm.init(); auto q = qvm.qAllocMany(3); QVec qubits = {q[0],q[1]}; // 构建量子程序 auto prog = QProg(); prog << H(q[0]) << H(q[1]) << H(q[0]).dagger() << X(q[2]).control(qubits); // 概率测量,并返回目标量子比特的概率测量结果,下标为十进制 auto result = qvm.probRunTupleList(prog, q); // 打印测量结果 for(auto & aiter : result) { std::cout << aiter.first << " : " << aiter.second << std::endl; } return 0; }
计算结果如下:
0 : 0.5 2 : 0.5