def run_density_matrix(self, input_state=None, store_state=True): r"""运行当前的量子线路,输入输出的形式为密度矩阵。 Args: input_state (ComplexVariable, optional): 输入的密度矩阵,默认为 :math:`|00...0\rangle \langle00...0|` store_state (bool, optional): 是否存储输出的密度矩阵,默认为 ``True`` ,即存储 Returns: ComplexVariable: 量子线路输出的密度矩阵 代码示例: .. code-block:: python 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 ]] """ state = dygraph.to_variable(density_op( self.n)) if input_state is None else input_state assert state.real.shape == [2**self.n, 2**self.n], "The dimension is not right" state = matmul(self.U, matmul(state, dagger(self.U))) if store_state: self.__state = state # Add info about which function user called self.__run_state = 'density_matrix' return state
def __init__(self, N, shape, param_attr=paddle.nn.initializer.Uniform(low=0.0, high=2 * PI), dtype='float64'): super(Net, self).__init__() # Initialize theta by sampling from a uniform distribution [0, 2*pi] self.theta = self.create_parameter(shape=shape, attr=param_attr, dtype=dtype, is_bias=False) # Set the initial state as rho = |0..0><0..0| self.initial_state = paddle.to_tensor(density_op(N))
def run_density_matrix(self, input_state=None, store_state=True): r"""运行当前的量子线路,输入输出的形式为密度矩阵。 Args: input_state (Tensor, optional): 输入的密度矩阵,默认为 :math:`|00...0\rangle \langle00...0|` store_state (bool, optional): 是否存储输出的密度矩阵,默认为 ``True`` ,即存储 Returns: Tensor: 量子线路输出的密度矩阵 代码示例: .. code-block:: python import numpy as np import paddle from paddle_quantum.circuit import UAnsatz from paddle_quantum.state import density_op n = 1 theta = np.ones(3) input_state = paddle.to_tensor(density_op(n)) theta = paddle.to_tensor(theta) cir = UAnsatz(n) cir.rx(theta[0], 0) cir.ry(theta[1], 0) cir.rz(theta[2], 0) density_matrix = cir.run_density_matrix(input_state).numpy() print(f"The output density matrix is\n{density_matrix}") :: The output density matrix is [[0.64596329+0.j 0.47686058+0.03603751j] [0.47686058-0.03603751j 0.35403671+0.j ]] """ state = paddle.to_tensor(density_op( self.n)) if input_state is None else input_state assert paddle.real(state).shape == [2**self.n, 2**self.n ], "The dimension is not right" state = matmul(self.U, matmul(state, dagger(self.U))) if store_state: self.__state = state # Add info about which function user called self.__run_state = 'density_matrix' return state
def __init__(self, N, shape, param_attr=fluid.initializer.Uniform(low=0.0, high=2 * PI, seed=SEED), dtype='float64'): super(Net, self).__init__() # 初始化 theta 参数列表,并用 [0, 2*pi] 的均匀分布来填充初始值 self.theta = self.create_parameter(shape=shape, attr=param_attr, dtype=dtype, is_bias=False) # 初始化 rho = |0..0><0..0| 的密度矩阵 self.initial_state = fluid.dygraph.to_variable(density_op(N))