paddle_quantum.circuit module

class paddle_quantum.circuit.UAnsatz(n)

基类:object

基于Paddle的动态图机制实现量子线路的 class

用户可以通过实例化该 class 来搭建自己的量子线路。

n

该线路的量子比特数

Type:int
run_state_vector(input_state=None, store_state=True)

运行当前的量子线路,输入输出的形式为态矢量。

参数:
  • input_state (ComplexVariable, optional) – 输入的态矢量,默认为 \(|00...0\rangle\)
  • store_state (Bool, optional) – 是否存储输出的态矢量,默认为 True ,即存储
返回:

量子线路输出的态矢量

返回类型:

ComplexVariable

代码示例:

import numpy as np
from paddle import fluid
from paddle_quantum.circuit import UAnsatz
n = 2
theta = np.ones(3)
input_state = np.ones(2**n)+0j
input_state = input_state / np.linalg.norm(input_state)
with fluid.dygraph.guard():

    input_state_var = fluid.dygraph.to_variable(input_state)
    theta = fluid.dygraph.to_variable(theta)
    cir = UAnsatz(n)
    cir.rx(theta[0], 0)
    cir.ry(theta[1], 1)
    cir.rz(theta[2], 1)
    vec = cir.run_state_vector(input_state_var).numpy()
    print(f"运行后的向量是 {vec}")
运行后的向量是 [0.17470783-0.09544332j 0.59544332+0.32529217j 0.17470783-0.09544332j 0.59544332+0.32529217j]
run_density_matrix(input_state=None, store_state=True)

运行当前的量子线路,输入输出的形式为密度矩阵。

参数:
  • input_state (ComplexVariable, optional) – 输入的密度矩阵,默认为 \(|00...0\rangle \langle00...0|\)
  • store_state (bool, optional) – 是否存储输出的密度矩阵,默认为 True ,即存储
返回:

量子线路输出的密度矩阵

返回类型:

ComplexVariable

代码示例:

import numpy as np
from paddle import fluid
from paddle_quantum.circuit import UAnsatz
n = 1
theta = np.ones(3)
input_state = np.diag(np.arange(2**n))+0j
input_state = input_state / np.trace(input_state)
with fluid.dygraph.guard():

    input_state_var = fluid.dygraph.to_variable(input_state)
    theta = fluid.dygraph.to_variable(theta)
    cir = UAnsatz(n)
    cir.rx(theta[0], 0)
    cir.ry(theta[1], 0)
    cir.rz(theta[2], 0)
    density = cir.run_density_matrix(input_state_var).numpy()
    print(f"密度矩阵是\n{density}")
密度矩阵是
[[ 0.35403671+0.j         -0.47686058-0.03603751j]
[-0.47686058+0.03603751j  0.64596329+0.j        ]]
U

量子线路的酉矩阵形式。

返回:当前线路的酉矩阵表示
返回类型:ComplexVariable

代码示例:

from paddle import fluid
from paddle_quantum.circuit import UAnsatz
n = 2
with fluid.dygraph.guard():
    cir = UAnsatz(2)
    cir.h(0)
    cir.cnot([0, 1])
    matrix = cir.U
    print("生成贝尔态电路的酉矩阵表示为\n",matrix.numpy())
生成贝尔态电路的酉矩阵表示为
[[ 0.70710678+0.j  0.        +0.j  0.70710678+0.j  0.        +0.j]
[ 0.        +0.j  0.70710678+0.j  0.        +0.j  0.70710678+0.j]
[ 0.        +0.j  0.70710678+0.j  0.        +0.j -0.70710678+0.j]
[ 0.70710678+0.j  0.        +0.j -0.70710678+0.j  0.        +0.j]]
rx(theta, which_qubit)

添加关于x轴的单量子比特旋转门。

其矩阵形式为:

\[\begin{split}\begin{bmatrix} \cos\frac{\theta}{2} & -i\sin\frac{\theta}{2} \\ -i\sin\frac{\theta}{2} & \cos\frac{\theta}{2} \end{bmatrix}\end{split}\]
参数:
  • theta (Variable) – 旋转角度
  • which_qubit (int) – 作用在的qubit的编号,其值应该在[0, n)范围内,n为该量子线路的量子比特数
import numpy as np
from paddle import fluid
from paddle_quantum.circuit import UAnsatz
theta = np.array([np.pi], np.float64)
with fluid.dygraph.guard():
    theta = fluid.dygraph.to_variable(theta)
    num_qubits = 1
    cir = UAnsatz(num_qubits)
    which_qubit = 0
    cir.rx(theta[0], which_qubit)
ry(theta, which_qubit)

添加关于y轴的单量子比特旋转门。

其矩阵形式为:

\[\begin{split}\begin{bmatrix} \cos\frac{\theta}{2} & -\sin\frac{\theta}{2} \\ \sin\frac{\theta}{2} & \cos\frac{\theta}{2} \end{bmatrix}\end{split}\]
参数:
  • theta (Variable) – 旋转角度
  • which_qubit (int) – 作用在的qubit的编号,其值应该在[0, n)范围内,n为该量子线路的量子比特数
import numpy as np
from paddle import fluid
from paddle_quantum.circuit import UAnsatz
theta = np.array([np.pi], np.float64)
with fluid.dygraph.guard():
    theta = fluid.dygraph.to_variable(theta)
    num_qubits = 1
    cir = UAnsatz(num_qubits)
    which_qubit = 0
    cir.ry(theta[0], which_qubit)
rz(theta, which_qubit)

添加关于y轴的单量子比特旋转门。

其矩阵形式为:

\[\begin{split}\begin{bmatrix} e^{-\frac{i\theta}{2}} & 0 \\ 0 & e^{\frac{i\theta}{2}} \end{bmatrix}\end{split}\]
参数:
  • theta (Variable) – 旋转角度
  • which_qubit (int) – 作用在的qubit的编号,其值应该在[0, n)范围内,n为该量子线路的量子比特数
import numpy as np
from paddle import fluid
from paddle_quantum.circuit import UAnsatz
theta = np.array([np.pi], np.float64)
with fluid.dygraph.guard():
    theta = fluid.dygraph.to_variable(theta)
    num_qubits = 1
    cir = UAnsatz(num_qubits)
    which_qubit = 0
    cir.ry(theta[0], which_qubit)
cnot(control)

添加一个CNOT门。

对于2量子比特的量子线路,当control为 [0, 1] 时,其矩阵形式为:

\[\begin{split}\begin{align} CNOT &=|0\rangle \langle 0|\otimes I + |1 \rangle \langle 1|\otimes X\\ &=\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{bmatrix} \end{align}\end{split}\]
参数:control (list) – 作用在的qubit的编号,control[0] 为控制位,control[1] 为目标位,其值都应该在 \([0, n)\) 为该量子线路的量子比特数
import numpy as np
from paddle import fluid
from paddle_quantum.circuit import UAnsatz
num_qubits = 2
with fluid.dygraph.guard():
    cir = UAnsatz(num_qubits)
    cir.cnot([0, 1])
x(which_qubit)

添加单量子比特X门。

其矩阵形式为:

\[\begin{split}\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}\end{split}\]
参数:which_qubit (int) – 作用在的qubit的编号,其值应该在[0, n)范围内,n为该量子线路的量子比特数
from paddle import fluid
from paddle_quantum.circuit import UAnsatz
with fluid.dygraph.guard():
    num_qubits = 1
    cir = UAnsatz(num_qubits)
    which_qubit = 0
    cir.x(which_qubit)
y(which_qubit)

添加单量子比特Y门。

其矩阵形式为:

\[\begin{split}\begin{bmatrix} 0 & -i \\ i & 0 \end{bmatrix}\end{split}\]
参数:which_qubit (int) – 作用在的qubit的编号,其值应该在[0, n)范围内,n为该量子线路的量子比特数
from paddle import fluid
from paddle_quantum.circuit import UAnsatz
with fluid.dygraph.guard():
    num_qubits = 1
    cir = UAnsatz(num_qubits)
    which_qubit = 0
    cir.y(which_qubit)
z(which_qubit)

添加单量子比特Z门。

其矩阵形式为:

\[\begin{split}\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}\end{split}\]
参数:which_qubit (int) – 作用在的qubit的编号,其值应该在[0, n)范围内,n为该量子线路的量子比特数
from paddle import fluid
from paddle_quantum.circuit import UAnsatz
with fluid.dygraph.guard():
    num_qubits = 1
    cir = UAnsatz(num_qubits)
    which_qubit = 0
    cir.z(which_qubit)
h(which_qubit)

添加一个单量子比特的Hadamard门。

具体形式为

\[\begin{split}H = \frac{1}{\sqrt{2}}\begin{bmatrix} 1&1\\1&-1 \end{bmatrix}\end{split}\]
参数:which_qubit (int) – 作用在的qubit的编号,其值应该在[0, n)范围内,n为该量子线路的量子比特数
s(which_qubit)

添加一个单量子比特的S门。

具体形式为

\[\begin{split}S = \begin{bmatrix} 1&0\\0&i \end{bmatrix}\end{split}\]
参数:which_qubit (int) – 作用在的qubit的编号,其值应该在[0, n)范围内,n为该量子线路的量子比特数
t(which_qubit)

添加一个单量子比特的T门。

具体形式为

\[\begin{split}T = \begin{bmatrix} 1&0\\0&e^\frac{i\pi}{4} \end{bmatrix}\end{split}\]
参数:which_qubit (int) – 作用在的qubit的编号,其值应该在[0, n)范围内,n为该量子线路的量子比特数
u3(theta, phi, lam, which_qubit)

添加一个单量子比特的旋转门。

具体形式为

\[\begin{split}\begin{align} U3(\theta, \phi, \lambda) = \begin{bmatrix} \cos\frac\theta2&-e^{i\lambda}\sin\frac\theta2\\ e^{i\phi}\sin\frac\theta2&e^{i(\phi+\lambda)}\cos\frac\theta2 \end{bmatrix} \end{align}\end{split}\]
参数:
  • theta (Variable) – 旋转角度 \(\theta\)
  • phi (Variable) – 旋转角度 \(\phi\)
  • lam (Variable) – 旋转角度 \(\lambda\)
  • which_qubit (int) – 作用在的qubit的编号,其值应该在[0, n)范围内,n为该量子线路的量子比特数
measure(which_qubits=None, shots=1024, plot=False)

对量子线路输出的量子态进行测量。

警告

当plot为True时,当前量子线路的量子比特数需要小于6,否则无法绘制图片,会抛出异常。

参数:
  • which_qubits (list, optional) – 要测量的qubit的编号,默认全都测量
  • shots (int, optional) – 该量子线路输出的量子态的测量次数,默认为1024次;若为0,则输出测量期望值的精确值
  • plot (bool, optional) – 是否绘制测量结果图,默认为 False ,即不绘制
返回:

测量的结果

返回类型:

dict

代码示例:

import paddle
from paddle_quantum.circuit import UAnsatz
with paddle.fluid.dygraph.guard():
    cir = UAnsatz(2)
    cir.h(0)
    cir.cnot([0,1])
    cir.run_state_vector()
    result = cir.measure(shots = 2048, which_qubits = [1])
    print(f"测量第1号量子比特2048次的结果是{result}")
测量第1号量子比特2048次的结果是{'0': 964, '1': 1084}
import paddle
from paddle_quantum.circuit import UAnsatz
with paddle.fluid.dygraph.guard():
    cir = UAnsatz(2)
    cir.h(0)
    cir.cnot([0,1])
    cir.run_state_vector()
    result = cir.measure(shots = 0, which_qubits = [1])
    print(f"测量第1号量子比特的概率结果是{result}")
测量第1号量子比特的概率结果是{'0': 0.4999999999999999, '1': 0.4999999999999999}
expecval(H)

量子线路输出的量子态关于可观测量H的期望值。

提示

如果想输入的可观测量的矩阵为 \(0.7Z\otimes X\otimes I+0.2I\otimes Z\otimes I\) 。则 H 应为 [[0.7, 'z0,x1'], [0.2, 'z1']]

参数:H (list) – 可观测量的相关信息
返回:量子线路输出的量子态关于H的期望值
返回类型:Variable

代码示例:

import numpy as np
from paddle import fluid
from paddle_quantum.circuit import UAnsatz
n = 5
H_info = [[0.1, 'x1'], [0.2, 'y0,z4']]
theta = np.ones(3)
input_state = np.ones(2**n)+0j
input_state = input_state / np.linalg.norm(input_state)
with fluid.dygraph.guard():
    input_state_var = fluid.dygraph.to_variable(input_state)
    theta = fluid.dygraph.to_variable(theta)
    cir = UAnsatz(n)
    cir.rx(theta[0], 0)
    cir.rz(theta[1], 1)
    cir.rx(theta[2], 2)
    cir.run_state_vector(input_state_var)
    expect_value = cir.expecval(H_info).numpy()
    print(f'计算得到的{H_info}期望值是{expect_value}')
计算得到的[[0.1, 'x1'], [0.2, 'y0,z4']]期望值是[0.05403023]
import numpy as np
from paddle import fluid
from paddle_quantum.circuit import UAnsatz
n = 5
H_info = [[0.1, 'x1'], [0.2, 'y0,z4']]
theta = np.ones(3)
input_state = np.diag(np.arange(2**n))+0j
input_state = input_state / np.trace(input_state)
with fluid.dygraph.guard():
    input_state_var = fluid.dygraph.to_variable(input_state)
    theta = fluid.dygraph.to_variable(theta)
    cir = UAnsatz(n)
    cir.rx(theta[0], 0)
    cir.ry(theta[1], 1)
    cir.rz(theta[2], 2)
    cir.run_density_matrix(input_state_var)
    expect_value = cir.expecval(H_info).numpy()
    print(f'计算得到的{H_info}期望值是{expect_value}')
计算得到的[[0.1, 'x1'], [0.2, 'y0,z4']]期望值是[-0.02171538]
superposition_layer()

添加一层Hadamard门。

代码示例:

import paddle
from paddle_quantum.circuit import UAnsatz
with paddle.fluid.dygraph.guard():
    cir = UAnsatz(2)
    cir.superposition_layer()
    cir.run_state_vector()
    result = cir.measure(shots = 0)
    print(f"测量全部量子比特的结果是{result}")
测量全部量子比特结果是{'00': 0.2499999999999999, '01': 0.2499999999999999, '10': 0.2499999999999999, '11': 0.2499999999999999}
weak_superposition_layer()

添加一层Hadamard的平方根门,即 \(\sqrt{H}\) 门。

代码示例:

import paddle
from paddle_quantum.circuit import UAnsatz
with paddle.fluid.dygraph.guard():
    cir = UAnsatz(2)
    cir.weak_superposition_layer()
    cir.run_state_vector()
    result = cir.measure(shots = 0)
    print(f"测量全部量子比特的结果是{result}")
测量全部量子比特的结果是{'00': 0.7285533905932737, '01': 0.12500000000000003, '10': 0.12500000000000003, '11': 0.021446609406726238}
real_entangled_layer(theta, depth)

添加一层包含Ry门的强纠缠层。

注解

这一层量子门的数学表示形式为实数酉矩阵。

注意

theta 的维度为 (depth, n, 1)

参数:
  • theta (Variable) – Ry门的旋转角度
  • depth (int) – 纠缠层的深度

代码示例:

import numpy as np
from paddle import fluid
from paddle_quantum.circuit import UAnsatz
n = 2
DEPTH = 3
theta = np.ones([DEPTH, n, 1])
with fluid.dygraph.guard():
    theta = fluid.dygraph.to_variable(theta)
    cir = UAnsatz(n)
    cir.real_entangled_layer(fluid.dygraph.to_variable(theta), DEPTH)
    cir.run_state_vector()
    print(cir.measure(shots = 0))
{'00': 2.52129874867343e-05, '01': 0.295456784923382, '10': 0.7045028818254718, '11': 1.5120263659845063e-05}
complex_entangled_layer(theta, depth)

添加一层包含U3门的强纠缠层。

注解

这一层量子门的数学表示形式为复数酉矩阵。

注意

theta 的维度为 (depth, n, 3) ,最低维内容为对应的 u3 的参数 (theta, phi, lam)

参数:
  • theta (Variable) – U3门的旋转角度
  • depth (int) – 纠缠层的深度

代码示例:

import numpy as np
from paddle import fluid
from paddle_quantum.circuit import UAnsatz
n = 2
DEPTH = 3
theta = np.ones([DEPTH, n, 1])
with fluid.dygraph.guard():
    theta = fluid.dygraph.to_variable(theta)
    cir = UAnsatz(n)
    cir.complex_entangled_layer(fluid.dygraph.to_variable(theta), DEPTH)
    cir.run_state_vector()
    print(cir.measure(shots = 0))
{'00': 0.15032627279218896, '01': 0.564191201239618, '10': 0.03285998070292556, '11': 0.25262254526526823}
universal_2_qubit_gate(theta)

添加2-qubit通用门,这个通用门需要15个参数。

注意

只适用于量子比特数为2的量子线路。

参数:theta (Variable) – 2-qubit通用门的参数,其维度为 (15, )

代码示例:

import numpy as np
from paddle import fluid
from paddle_quantum.circuit import UAnsatz
n = 2
theta = np.ones(15)
with fluid.dygraph.guard():
    theta = fluid.dygraph.to_variable(theta)
    cir = UAnsatz(n)
    cir.universal_2_qubit_gate(fluid.dygraph.to_variable(theta))
    cir.run_state_vector()
    print(cir.measure(shots = 0))
{'00': 0.4306256106527819, '01': 0.07994547866706268, '10': 0.07994547866706264, '11': 0.40948343201309334}
real_block_layer(theta, depth)

添加一层包含Ry门的弱纠缠层。

注解

这一层量子门的数学表示形式为实数酉矩阵。

注意

theta 的维度为 (depth, n-1, 4)

参数:
  • theta (Variable) – Ry门的旋转角度
  • depth (int) – 纠缠层的深度

代码示例:

n = 4
DEPTH = 3
theta = np.ones([DEPTH, n-1, 4])
with fluid.dygraph.guard():
    theta = fluid.dygraph.to_variable(theta)
    cir = UAnsatz(n)
    cir.real_block_layer(fluid.dygraph.to_variable(theta), DEPTH)
    cir.run_density_matrix()
    print(cir.measure(shots = 0, which_qubits = [0]))
{'0': 0.9646724056906162, '1': 0.035327594309385896}
complex_block_layer(theta, depth)

添加一层包含U3门的弱纠缠层。

注解

这一层量子门的数学表示形式为复数酉矩阵。

注意

theta 的维度为 (depth, n-1, 12)

参数:
  • theta (Variable) – U3门的角度信息
  • depth (int) – 纠缠层的深度

代码示例:

n = 4
DEPTH = 3
theta = np.ones([DEPTH, n-1, 12])
with fluid.dygraph.guard():
    theta = fluid.dygraph.to_variable(theta)
    cir = UAnsatz(n)
    cir.complex_block_layer(fluid.dygraph.to_variable(theta), DEPTH)
    cir.run_density_matrix()
    print(cir.measure(shots = 0, which_qubits = [0]))
{'0': 0.5271554811768046, '1': 0.4728445188231988}
paddle_quantum.circuit.H_prob(cir, H, shots=1024)

构造Pauli测量电路并测量关于H的期望值。

参数:
  • cir (UAnsatz) – UAnsatz的一个实例化对象
  • H (list) – 记录哈密顿量信息的列表
  • shots (int, optional) – 默认为1024,表示测量次数;若为0,则表示返回测量期望值的精确值,即测量无穷次后的期望值
返回:

测量得到的H的期望值

返回类型:

float

代码示例:

import numpy as np
from paddle import fluid
from paddle_quantum.circuit import UAnsatz, H_prob
n = 4
theta = np.ones(3)
experiment_shots = 2**10
H_info = [[0.1, 'x2'], [0.3, 'y1,z3']]
input_state = np.ones(2**n)+0j
input_state = input_state / np.linalg.norm(input_state)
with fluid.dygraph.guard():
    theta = fluid.dygraph.to_variable(theta)
    cir = UAnsatz(n)
    cir.rx(theta[0], 0)
    cir.ry(theta[1], 1)
    cir.rz(theta[2], 1)
    result_1 = H_prob(cir, H_info, shots = experiment_shots)
    result_2 = H_prob(cir, H_info, shots = 0)
    print(f'消耗 {experiment_shots} 次测量后的期望值实验值是 {result_1}')
    print(f'H期望值精确值是 {result_2}')
消耗 1024 次测量后的期望值实验值是 0.2326171875
H期望值精确值是 0.21242202548207134