def density_op_random(n, real_or_complex=2, rank=None): r"""随机生成一个密度矩阵的 numpy 形式。 Args: n (int): 量子比特数量 real_or_complex (int, optional): 默认为 2,即生成复数组,若为 1,则生成实数组 rank (int, optional): 矩阵的秩,默认为 :math:`2^n` (当 ``rank`` 为 ``None`` 时) Returns: numpy.ndarray: 一个形状为 ``(2**n, 2**n)`` 的 numpy 数组 """ assert n > 0, 'qubit number must be positive' rank = rank if rank is not None else 2**n assert 0 < rank <= 2**n, 'rank is an invalid number' if real_or_complex == 1: psi = np_random.randn(2**n, rank) else: psi = np_random.randn(2**n, rank) + 1j * np_random.randn(2**n, rank) psi_dagger = psi.conj().T rho = np_matmul(psi, psi_dagger) rho = rho / np_trace(rho) return rho.astype('complex128')
def purity(rho): r"""计算量子态的纯度。 .. math:: P = \text{tr}(\rho^2) Args: rho (numpy.ndarray): 量子态的密度矩阵形式 Returns: float: 输入的量子态的纯度 """ gamma = np_trace(np_matmul(rho, rho)) return gamma.real
def main(): # 生成用泡利字符串表示的特定的哈密顿量 H = [[-1.0, 'z0,z1'], [-1.0, 'z1,z2'], [-1.0, 'z0,z2']] # 生成哈密顿量的矩阵信息 N_SYS_B = 3 # 用于生成吉布斯态的子系统B的量子比特数 hamiltonian = pauli_str_to_matrix(H, N_SYS_B) # 生成理想情况下的目标吉布斯态 rho beta = 1.5 # 设置逆温度参数 beta rho_G = scipy.linalg.expm(-1 * beta * hamiltonian) / np_trace(scipy.linalg.expm(-1 * beta * hamiltonian)) # 设置成 Paddle quantum 所支持的数据类型 hamiltonian = hamiltonian.astype("complex128") rho_G = rho_G.astype("complex128") rho_B = Paddle_GIBBS(hamiltonian, rho_G) print(rho_B)
def main(): # Generate Pauli string representing a specific Hamiltonian H = [[-1.0, 'z0,z1'], [-1.0, 'z1,z2'], [-1.0, 'z0,z2']] # Generate the marix form of the Hamiltonian N_SYS_B = 3 # Number of qubits in subsystem B used to generate Gibbs state hamiltonian = pauli_str_to_matrix(H, N_SYS_B) # Generate the target Gibbs state rho beta = 1.5 # Set inverse temperature beta rho_G = scipy.linalg.expm(-1 * beta * hamiltonian) / np_trace( scipy.linalg.expm(-1 * beta * hamiltonian)) # Convert to the data type supported by Paddle Quantum hamiltonian = hamiltonian.astype("complex128") rho_G = rho_G.astype("complex128") rho_B = Paddle_GIBBS(hamiltonian, rho_G) print(rho_B)
def gate_fidelity(U, V): r"""计算两个量子门的保真度。 .. math:: F(U, V) = |\text{tr}(UV^\dagger)|/2^n :math:`U` 是一个 :math:`2^n\times 2^n` 的 Unitary 矩阵。 Args: U (numpy.ndarray): 量子门的酉矩阵形式 V (numpy.ndarray): 量子门的酉矩阵形式 Returns: float: 输入的量子门之间的保真度 """ assert U.shape == V.shape, 'The shape of two unitary matrices are different' dim = U.shape[0] fidelity = absolute(np_trace(np_matmul(U, V.conj().T))) / dim return fidelity
def H_generator(): """ Generate a Hamiltonian with trivial descriptions Returns: A Hamiltonian """ # Generate Pauli string representing a specific Hamiltonian H = [[-1.0, 'z0,z1'], [-1.0, 'z1,z2'], [-1.0, 'z0,z2']] # Generate the matrix form of the Hamiltonian N_SYS_B = 3 # Number of qubits in subsystem B used to generate Gibbs state hamiltonian = pauli_str_to_matrix(H, N_SYS_B) # Generate the target Gibbs state rho beta = 1.5 # Set inverse temperature beta rho_G = scipy.linalg.expm(-1 * beta * hamiltonian) / np_trace( scipy.linalg.expm(-1 * beta * hamiltonian)) # Convert to the data type supported by Paddle Quantum hamiltonian = hamiltonian.astype("complex128") rho_G = rho_G.astype("complex128") return hamiltonian, rho_G
def H_generator(): """ Generate a Hamiltonian with trivial descriptions Returns: A Hamiltonian """ # 生成用泡利字符串表示的特定的哈密顿量 H = [[-1.0, 'z0,z1'], [-1.0, 'z1,z2'], [-1.0, 'z0,z2']] # 生成哈密顿量的矩阵信息 N_SYS_B = 3 # 用于生成吉布斯态的子系统B的量子比特数 hamiltonian = pauli_str_to_matrix(H, N_SYS_B) # 生成理想情况下的目标吉布斯态 rho beta = 1.5 # 设置逆温度参数 beta rho_G = scipy.linalg.expm(-1 * beta * hamiltonian) / np_trace( scipy.linalg.expm(-1 * beta * hamiltonian)) # 设置成 Paddle quantum 所支持的数据类型 hamiltonian = hamiltonian.astype("complex128") rho_G = rho_G.astype("complex128") return hamiltonian, rho_G