第三章 量子线路 (Quantum Circuit)#
2.1 单量子比特门#
与经典逻辑门不同,量子电路对单个比特的操作要丰富得多。直觉地理解,经典比特对应 \(|0\rangle\) 和 \(|1\rangle\) 两个量子态,在Bloch球上分别是北极和南极,唯一能涉及的操作也就是在这两个点之间翻转(NOT门)。与之不同,单量子比特态却对应整个球面,我们可以通过变换连接球面上任意两个点。一些最重要的量子门包括泡利(Pauli)矩阵:
\[\begin{split}X = \left(
\begin{array}{cc}
0 & 1 \\
1 & 0 \\
\end{array}
\right), \quad
Y = \left(
\begin{array}{cc}
0 & -i \\
i & 0 \\
\end{array}
\right), \quad
Z = \left(
\begin{array}{cc}
1 & 0 \\
0 & -1 \\
\end{array}
\right)。\quad\end{split}\]
要注意\(X\),\(Y\)和\(Z\)这三个矩阵既是幺正的,又是厄米的。所以我们可以将他们看做量子门,也可以对他们进行测量。
另外三个在量子计算中极为重要的量子门包括:
\[\begin{split}H = \frac{1}{\sqrt{2}}\left(
\begin{array}{cc}
1 & 1 \\
1 & -1 \\
\end{array}
\right), \quad
S = \left(
\begin{array}{cc}
1 & 0 \\
0 & i \\
\end{array}
\right), \quad
T = \left(
\begin{array}{cc}
1 & 0 \\
0 & e^{i\pi/4} \\
\end{array}
\right)。\quad\end{split}\]
为了更深入地理解单量子比特门,我们可以用泡利矩阵定义如下三个幺正矩阵:
\[\begin{split}\begin{aligned}
R_x(\theta) &= \cos(\theta/2)I - i\sin(\theta/2) X = \left(
\begin{array}{cc}
\cos(\theta/2) & -i\sin(\theta/2) \\
-i\sin(\theta/2) & \cos(\theta/2) \\
\end{array}
\right),\\
R_y(\theta) &= \cos(\theta/2)I - i\sin(\theta/2) Y = \left(
\begin{array}{cc}
\cos(\theta/2) & -\sin(\theta/2) \\
\sin(\theta/2) & \cos(\theta/2) \\
\end{array}
\right),\\
R_z(\theta) &= \cos(\theta/2)I - i\sin(\theta/2) Z = \left(
\begin{array}{cc}
e^{-i\theta/2} & 0 \\
0 & e^{i\theta/2} \\
\end{array}
\right)。
\end{aligned}\end{split}\]
这三个矩阵分别称为绕Bloch球上的\(x,y,z\)轴顺时针旋转\(\theta\)角。我们以\(R_z\)为例:
\[R_z(\alpha)(\cos(\theta/2)|0\rangle + \sin(\theta/2)e^{i\varphi}|1\rangle) \cong \cos(\theta/2)|0\rangle + \sin(\theta/2)e^{i(\varphi+\theta)}|1\rangle\]
而这正是量子态在Bloch球上绕着\(Z\)轴顺时针旋转了\(\theta\)。\(R_x,R_y\)的验证要复杂些,我们这边不做展开。除了\(x,y,z\)轴的旋转外,我们也可以找到绕任意单位向量\(\hat{n}=(n_x,n_y,n_z)\)旋转的单量子比特门:
\[R_{\hat{n}}(\theta)=\cos(\theta/2)I + i \sin(\theta/2)(n_xX + n_y Y +n_z Z)。\]
事实上,任意单量子比特门都可以看成是绕某根特定轴\(\hat{n}\)的旋转,满足:
\[U_1 \cong R_{\hat{n}}(\theta),\]
其中\(\theta\),\(\hat{n}\)完全由\(U_1\)决定。
如果我们只能进行绕固定轴的旋转,我们仍然可以实现任意单量子比特门的幺正变化。这里我们有以下非常有用的定理:
定理:任意作用在单量子比特上的幺正操作 \(U_1\) ,都可以分解为绕任意2根固定的互相垂直的轴的旋转。以\(y\)轴和\(z\)轴为例,我们有:
\[U_1\cong R_z(\beta)R_y(\gamma)R_z(\delta),\]
其中\(\beta,\gamma,\delta\)由\(U_1\)决定。
[6]:
import tensorcircuit as tc
import tensorflow as tf
import math
import numpy as np
tc.set_backend("tensorflow")
X = tc.gates._x_matrix # same as tc.gates.xgate().tensor.numpy()
Y = tc.gates._y_matrix # same as tc.gates.ygate().tensor.numpy()
Z = tc.gates._z_matrix # same as tc.gates.zgate().tensor.numpy()
H = tc.gates._h_matrix # same as tc.gates.hgate().tensor.numpy()
S = tc.gates._s_matrix
T = tc.gates._t_matrix
print(f"{X=}\n")
print(f"{Y=}\n")
print(f"{Z=}\n")
print(f"{H=}\n")
print(f"{S=}\n")
print(f"{T=}\n")
theta = math.pi / 2
rx = tc.gates.rx_gate(theta).tensor.numpy()
ry = tc.gates.ry_gate(theta).tensor.numpy()
rz = tc.gates.rz_gate(theta).tensor.numpy()
# print(f"{rx=}\n")
# print(f"{ry=}\n")
# print(f"{rz=}\n")
rx_square = rx**2
ry_square = ry**2
rz_square = rz**2
print(f"{rx_square=}\n")
print(f"{ry_square=}\n")
print(f"{rz_square=}\n")
X=array([[0., 1.],
[1., 0.]])
Y=array([[ 0.+0.j, -0.-1.j],
[ 0.+1.j, 0.+0.j]])
Z=array([[ 1., 0.],
[ 0., -1.]])
H=array([[ 0.70710678, 0.70710678],
[ 0.70710678, -0.70710678]])
S=array([[1.+0.j, 0.+0.j],
[0.+0.j, 0.+1.j]])
T=array([[1. +0.j , 0. +0.j ],
[0. +0.j , 0.70710678+0.70710678j]])
rx_square=array([[ 0.49999997+0.j, -0.49999997-0.j],
[-0.49999997-0.j, 0.49999997+0.j]], dtype=complex64)
ry_square=array([[0.49999997+0.j, 0.49999997-0.j],
[0.49999997+0.j, 0.49999997+0.j]], dtype=complex64)
rz_square=array([[0.-0.99999994j, 0.+0.j ],
[0.+0.j , 0.+0.99999994j]], dtype=complex64)