utils.py 55.7 KB
Newer Older
Q
Quleaf 已提交
1
# Copyright (c) 2021 Institute for Quantum Computing, Baidu Inc. All Rights Reserved.
Q
Quleaf 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from functools import reduce
Q
Quleaf 已提交
16
from math import log2
Q
Quleaf 已提交
17 18 19 20
from math import sqrt
import os.path
import copy
import re
Q
Quleaf 已提交
21
import numpy as np
Q
Quleaf 已提交
22 23 24
from scipy.linalg import logm, sqrtm
from matplotlib import colors as mplcolors
import matplotlib.pyplot as plt
Q
Quleaf 已提交
25
import paddle
Q
Quleaf 已提交
26
from paddle import add, to_tensor
Q
Quleaf 已提交
27
from paddle import kron as kron
Q
Quleaf 已提交
28
from paddle import matmul
Q
Quleaf 已提交
29 30 31
from paddle import transpose
from paddle import concat, ones
from paddle import zeros
Q
Quleaf 已提交
32 33 34 35
from scipy import sparse
import matplotlib as mpl
from paddle_quantum import simulator
import matplotlib.animation as animation
Y
yangguohao 已提交
36
import matplotlib.image
Q
Quleaf 已提交
37

Q
Quleaf 已提交
38 39
__all__ = [
    "partial_trace",
Q
Quleaf 已提交
40
    "state_fidelity",
Q
Quleaf 已提交
41
    "trace_distance",
Q
Quleaf 已提交
42 43 44 45
    "gate_fidelity",
    "purity",
    "von_neumann_entropy",
    "relative_entropy",
Q
Quleaf 已提交
46
    "NKron",
Q
Quleaf 已提交
47
    "dagger",
Q
Quleaf 已提交
48
    "random_pauli_str_generator",
Q
Quleaf 已提交
49 50 51 52 53
    "pauli_str_to_matrix",
    "partial_transpose_2",
    "partial_transpose",
    "negativity",
    "logarithmic_negativity",
Q
Quleaf 已提交
54
    "is_ppt",
Q
Quleaf 已提交
55 56 57 58
    "haar_orthogonal",
    "haar_unitary",
    "haar_state_vector",
    "haar_density_operator",
Q
Quleaf 已提交
59 60 61
    "Hamiltonian",
    "plot_state_in_bloch_sphere",
    "plot_rotation_in_bloch_sphere",
Y
yangguohao 已提交
62
    "img_to_density_matrix",
Q
Quleaf 已提交
63 64 65
]


Q
Quleaf 已提交
66 67 68 69
def partial_trace(rho_AB, dim1, dim2, A_or_B):
    r"""计算量子态的偏迹。

    Args:
Q
Quleaf 已提交
70
        rho_AB (Tensor): 输入的量子态
Q
Quleaf 已提交
71 72
        dim1 (int): 系统A的维数
        dim2 (int): 系统B的维数
Q
Quleaf 已提交
73
        A_or_B (int): 1或者2,1表示计算系统A上的偏迹,2表示计算系统B上的偏迹
Q
Quleaf 已提交
74 75

    Returns:
Q
Quleaf 已提交
76
        Tensor: 输入的量子态的偏迹
Q
Quleaf 已提交
77

Q
Quleaf 已提交
78
    """
Q
Quleaf 已提交
79 80 81
    if A_or_B == 2:
        dim1, dim2 = dim2, dim1

Q
Quleaf 已提交
82
    idty_np = np.identity(dim2).astype("complex128")
Q
Quleaf 已提交
83
    idty_B = to_tensor(idty_np)
Q
Quleaf 已提交
84

Q
Quleaf 已提交
85
    zero_np = np.zeros([dim2, dim2], "complex128")
Q
Quleaf 已提交
86
    res = to_tensor(zero_np)
Q
Quleaf 已提交
87 88

    for dim_j in range(dim1):
Q
Quleaf 已提交
89
        row_top = zeros([1, dim_j], dtype="float64")
Q
Quleaf 已提交
90
        row_mid = ones([1, 1], dtype="float64")
Q
Quleaf 已提交
91
        row_bot = zeros([1, dim1 - dim_j - 1], dtype="float64")
Q
Quleaf 已提交
92 93
        bra_j = concat([row_top, row_mid, row_bot], axis=1)
        bra_j = paddle.cast(bra_j, 'complex128')
Q
Quleaf 已提交
94

Q
Quleaf 已提交
95
        if A_or_B == 1:
Q
Quleaf 已提交
96
            row_tmp = kron(bra_j, idty_B)
Q
Quleaf 已提交
97
            row_tmp_conj = paddle.conj(row_tmp)
Q
Quleaf 已提交
98
            res = add(res, matmul(matmul(row_tmp, rho_AB), transpose(row_tmp_conj, perm=[1, 0]), ), )
Q
Quleaf 已提交
99 100

        if A_or_B == 2:
Q
Quleaf 已提交
101
            row_tmp = kron(idty_B, bra_j)
Q
Quleaf 已提交
102
            row_tmp_conj = paddle.conj(row_tmp)
Q
Quleaf 已提交
103
            res = add(res, matmul(matmul(row_tmp, rho_AB), transpose(row_tmp_conj, perm=[1, 0]), ), )
Q
Quleaf 已提交
104 105 106 107

    return res


Q
Quleaf 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
def partial_trace_discontiguous(rho, preserve_qubits=None):
    r"""计算量子态的偏迹,可选取任意子系统。

    Args:
        rho (Tensor): 输入的量子态
        preserve_qubits (list): 要保留的量子比特,默认为 None,表示全保留
    """
    if preserve_qubits is None:
        return rho
    else:
        n = int(log2(rho.size) // 2)
        num_preserve = len(preserve_qubits)

        shape = paddle.ones((n + 1,))
        shape = 2 * shape
Q
Quleaf 已提交
123
        shape[n] = 2 ** n
Q
Quleaf 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
        shape = paddle.cast(shape, "int32")
        identity = paddle.eye(2 ** n)
        identity = paddle.reshape(identity, shape=shape)
        discard = list()
        for idx in range(0, n):
            if idx not in preserve_qubits:
                discard.append(idx)
        addition = [n]
        preserve_qubits.sort()

        preserve_qubits = paddle.to_tensor(preserve_qubits)
        discard = paddle.to_tensor(discard)
        addition = paddle.to_tensor(addition)
        permute = paddle.concat([discard, preserve_qubits, addition])

        identity = paddle.transpose(identity, perm=permute)
Q
Quleaf 已提交
140
        identity = paddle.reshape(identity, (2 ** n, 2 ** n))
Q
Quleaf 已提交
141 142 143 144 145 146 147 148 149 150 151

        result = np.zeros((2 ** num_preserve, 2 ** num_preserve), dtype="complex64")
        result = paddle.to_tensor(result)

        for i in range(0, 2 ** num_preserve):
            bra = identity[i * 2 ** num_preserve:(i + 1) * 2 ** num_preserve, :]
            result = result + matmul(matmul(bra, rho), transpose(bra, perm=[1, 0]))

        return result


Q
Quleaf 已提交
152 153
def state_fidelity(rho, sigma):
    r"""计算两个量子态的保真度。
Q
Quleaf 已提交
154

Q
Quleaf 已提交
155 156
    .. math::
        F(\rho, \sigma) = \text{tr}(\sqrt{\sqrt{\rho}\sigma\sqrt{\rho}})
Q
Quleaf 已提交
157

Q
Quleaf 已提交
158 159 160
    Args:
        rho (numpy.ndarray): 量子态的密度矩阵形式
        sigma (numpy.ndarray): 量子态的密度矩阵形式
Q
Quleaf 已提交
161

Q
Quleaf 已提交
162 163
    Returns:
        float: 输入的量子态之间的保真度
Q
Quleaf 已提交
164
    """
Q
Quleaf 已提交
165
    assert rho.shape == sigma.shape, 'The shape of two quantum states are different'
Q
Quleaf 已提交
166
    fidelity = np.trace(sqrtm(sqrtm(rho) @ sigma @ sqrtm(rho))).real
Q
Quleaf 已提交
167 168 169 170

    return fidelity


Q
Quleaf 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
def trace_distance(rho, sigma):
    r"""计算两个量子态的迹距离。

    .. math::
        D(\rho, \sigma) = 1 / 2 * \text{tr}|\rho-\sigma|

    Args:
        rho (numpy.ndarray): 量子态的密度矩阵形式
        sigma (numpy.ndarray): 量子态的密度矩阵形式

    Returns:
        float: 输入的量子态之间的迹距离
    """
    assert rho.shape == sigma.shape, 'The shape of two quantum states are different'
    A = rho - sigma
    distance = 1 / 2 * np.sum(np.abs(np.linalg.eigvals(A)))

    return distance


Q
Quleaf 已提交
191 192 193 194 195 196 197 198 199 200
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:
Q
Quleaf 已提交
201 202
        U (numpy.ndarray): 量子门 :math:`U` 的酉矩阵形式
        V (numpy.ndarray): 量子门 :math:`V` 的酉矩阵形式
Q
Quleaf 已提交
203 204 205

    Returns:
        float: 输入的量子门之间的保真度
Q
Quleaf 已提交
206
    """
Q
Quleaf 已提交
207 208
    assert U.shape == V.shape, 'The shape of two unitary matrices are different'
    dim = U.shape[0]
Q
Quleaf 已提交
209 210
    fidelity = np.absolute(np.trace(np.matmul(U, V.conj().T))) / dim

Q
Quleaf 已提交
211
    return fidelity
Q
Quleaf 已提交
212 213


Q
Quleaf 已提交
214 215
def purity(rho):
    r"""计算量子态的纯度。
Q
Quleaf 已提交
216

Q
Quleaf 已提交
217 218 219 220 221 222 223 224 225
    .. math::

        P = \text{tr}(\rho^2)

    Args:
        rho (numpy.ndarray): 量子态的密度矩阵形式

    Returns:
        float: 输入的量子态的纯度
Q
Quleaf 已提交
226
    """
Q
Quleaf 已提交
227
    gamma = np.trace(np.matmul(rho, rho))
Q
Quleaf 已提交
228

Q
Quleaf 已提交
229
    return gamma.real
Q
Quleaf 已提交
230

Q
Quleaf 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243

def von_neumann_entropy(rho):
    r"""计算量子态的冯诺依曼熵。

    .. math::

        S = -\text{tr}(\rho \log(\rho))

    Args:
        rho(numpy.ndarray): 量子态的密度矩阵形式

    Returns:
        float: 输入的量子态的冯诺依曼熵
Q
Quleaf 已提交
244
    """
Q
Quleaf 已提交
245
    rho_eigenvalue, _ = np.linalg.eig(rho)
Q
Quleaf 已提交
246 247
    entropy = -np.sum(rho_eigenvalue * np.log(rho_eigenvalue))

Q
Quleaf 已提交
248
    return entropy.real
Q
Quleaf 已提交
249 250


Q
Quleaf 已提交
251 252
def relative_entropy(rho, sig):
    r"""计算两个量子态的相对熵。
Q
Quleaf 已提交
253

Q
Quleaf 已提交
254
    .. math::
Q
Quleaf 已提交
255

Q
Quleaf 已提交
256 257 258 259 260 261 262 263
        S(\rho \| \sigma)=\text{tr} \rho(\log \rho-\log \sigma)

    Args:
        rho (numpy.ndarray): 量子态的密度矩阵形式
        sig (numpy.ndarray): 量子态的密度矩阵形式

    Returns:
        float: 输入的量子态之间的相对熵
Q
Quleaf 已提交
264
    """
Q
Quleaf 已提交
265
    assert rho.shape == sig.shape, 'The shape of two quantum states are different'
Q
Quleaf 已提交
266
    res = np.trace(rho @ logm(rho) - rho @ logm(sig))
Q
Quleaf 已提交
267 268 269 270 271 272 273 274 275 276 277 278
    return res.real


def NKron(matrix_A, matrix_B, *args):
    r"""计算两个及以上的矩阵的Kronecker积。

    Args:
        matrix_A (numpy.ndarray): 一个矩阵
        matrix_B (numpy.ndarray): 一个矩阵
        *args (numpy.ndarray): 其余矩阵

    Returns:
Q
Quleaf 已提交
279
        Tensor: 输入矩阵的Kronecker积
Q
Quleaf 已提交
280 281

    .. code-block:: python
Q
Quleaf 已提交
282
  
Q
Quleaf 已提交
283 284 285 286 287 288 289
        from paddle_quantum.state import density_op_random
        from paddle_quantum.utils import NKron
        A = density_op_random(2)
        B = density_op_random(2)
        C = density_op_random(2)
        result = NKron(A, B, C)

Q
Quleaf 已提交
290
    ``result`` 应为 :math:`A \otimes B \otimes C`
Q
Quleaf 已提交
291
    """
Q
Quleaf 已提交
292
    return reduce(lambda result, index: np.kron(result, index), args, np.kron(matrix_A, matrix_B), )
Q
Quleaf 已提交
293 294


Q
Quleaf 已提交
295
def dagger(matrix):
Q
Quleaf 已提交
296
    r"""计算矩阵的埃尔米特转置,即Hermitian transpose。
Q
Quleaf 已提交
297

Q
Quleaf 已提交
298
    Args:
Q
Quleaf 已提交
299
        matrix (Tensor): 需要埃尔米特转置的矩阵
Q
Quleaf 已提交
300

Q
Quleaf 已提交
301
    Returns:
Q
Quleaf 已提交
302
        Tensor: 输入矩阵的埃尔米特转置
Q
Quleaf 已提交
303

Q
Quleaf 已提交
304
    代码示例:
Q
Quleaf 已提交
305

Q
Quleaf 已提交
306
    .. code-block:: python
Q
Quleaf 已提交
307
  
Q
Quleaf 已提交
308
        from paddle_quantum.utils import dagger
Q
Quleaf 已提交
309
        import numpy as np
Q
Quleaf 已提交
310 311
        rho = paddle.to_tensor(np.array([[1+1j, 2+2j], [3+3j, 4+4j]]))
        print(dagger(rho).numpy())
Q
Quleaf 已提交
312

Q
Quleaf 已提交
313
    ::
Q
Quleaf 已提交
314

Q
Quleaf 已提交
315 316
        [[1.-1.j 3.-3.j]
        [2.-2.j 4.-4.j]]
Q
Quleaf 已提交
317
    """
Q
Quleaf 已提交
318
    matrix_conj = paddle.conj(matrix)
Q
Quleaf 已提交
319
    matrix_dagger = transpose(matrix_conj, perm=[1, 0])
Q
Quleaf 已提交
320 321

    return matrix_dagger
Q
Quleaf 已提交
322 323


Q
Quleaf 已提交
324 325
def random_pauli_str_generator(n, terms=3):
    r"""随机生成一个可观测量(observable)的列表( ``list`` )形式。
Q
Quleaf 已提交
326

Q
Quleaf 已提交
327 328 329
    一个可观测量 :math:`O=0.3X\otimes I\otimes I+0.5Y\otimes I\otimes Z` 的
    列表形式为 ``[[0.3, 'x0'], [0.5, 'y0,z2']]`` 。这样一个可观测量是由
    调用 ``random_pauli_str_generator(3, terms=2)`` 生成的。
Q
Quleaf 已提交
330

Q
Quleaf 已提交
331 332 333 334 335 336
    Args:
        n (int): 量子比特数量
        terms (int, optional): 可观测量的项数

    Returns:
        list: 随机生成的可观测量的列表形式
Q
Quleaf 已提交
337
    """
Q
Quleaf 已提交
338
    pauli_str = []
Q
Quleaf 已提交
339
    for sublen in np.random.randint(1, high=n + 1, size=terms):
Q
Quleaf 已提交
340
        # Tips: -1 <= coeff < 1
Q
Quleaf 已提交
341
        coeff = np.random.rand() * 2 - 1
Q
Quleaf 已提交
342 343
        ops = np.random.choice(['x', 'y', 'z'], size=sublen)
        pos = np.random.choice(range(n), size=sublen, replace=False)
Q
Quleaf 已提交
344
        op_list = [ops[i] + str(pos[i]) for i in range(sublen)]
Q
Quleaf 已提交
345
        pauli_str.append([coeff, ','.join(op_list)])
Q
Quleaf 已提交
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
    return pauli_str


def pauli_str_to_matrix(pauli_str, n):
    r"""将输入的可观测量(observable)的列表( ``list`` )形式转换为其矩阵形式。

    如输入的 ``pauli_str`` 为 ``[[0.7, 'z0,x1'], [0.2, 'z1']]`` 且 ``n=3`` ,
    则此函数返回可观测量 :math:`0.7Z\otimes X\otimes I+0.2I\otimes Z\otimes I` 的
    矩阵形式。

    Args:
        pauli_str (list): 一个可观测量的列表形式
        n (int): 量子比特数量

    Returns:
        numpy.ndarray: 输入列表对应的可观测量的矩阵形式
Q
Quleaf 已提交
362
    """
Q
Quleaf 已提交
363 364
    pauli_dict = {'i': np.eye(2) + 0j, 'x': np.array([[0, 1], [1, 0]]) + 0j,
                  'y': np.array([[0, -1j], [1j, 0]]), 'z': np.array([[1, 0], [0, -1]]) + 0j}
Q
Quleaf 已提交
365 366 367 368

    # Parse pauli_str; 'x0,z1,y4' to 'xziiy'
    new_pauli_str = []
    for coeff, op_str in pauli_str:
Q
Quleaf 已提交
369 370
        init = list('i' * n)
        op_list = re.split(r',\s*', op_str.lower())
Q
Quleaf 已提交
371
        for op in op_list:
Q
Quleaf 已提交
372 373 374 375 376 377
            if len(op) > 1:
                pos = int(op[1:])
                assert pos < n, 'n is too small'
                init[pos] = op[0]
            elif op.lower() != 'i':
                raise ValueError('Only Pauli operator "I" can be accepted without specifying its position')
Q
Quleaf 已提交
378 379 380 381 382 383 384
        new_pauli_str.append([coeff, ''.join(init)])

    # Convert new_pauli_str to matrix; 'xziiy' to NKron(x, z, i, i, y)
    matrices = []
    for coeff, op_str in new_pauli_str:
        sub_matrices = []
        for op in op_str:
Q
Quleaf 已提交
385
            sub_matrices.append(pauli_dict[op.lower()])
Q
Quleaf 已提交
386 387 388 389 390 391
        if len(op_str) == 1:
            matrices.append(coeff * sub_matrices[0])
        else:
            matrices.append(coeff * NKron(sub_matrices[0], sub_matrices[1], *sub_matrices[2:]))

    return sum(matrices)
Q
Quleaf 已提交
392

Q
Quleaf 已提交
393

Q
Quleaf 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
def partial_transpose_2(density_op, sub_system=None):
    r"""计算输入量子态的 partial transpose :math:`\rho^{T_A}`

    Args:
        density_op (numpy.ndarray): 量子态的密度矩阵形式
        sub_system (int): 1或2,表示关于哪个子系统进行 partial transpose,默认为第二个

    Returns:
        float: 输入的量子态的 partial transpose

    代码示例:

    .. code-block:: python

        from paddle_quantum.utils import partial_transpose_2
        rho_test = np.arange(1,17).reshape(4,4)
        partial_transpose_2(rho_test, sub_system=1)

    ::

       [[ 1,  2,  9, 10],
        [ 5,  6, 13, 14],
        [ 3,  4, 11, 12],
        [ 7,  8, 15, 16]]
    """
    sys_idx = 2 if sub_system is None else 1

    # Copy the density matrix and not corrupt the original one
Q
Quleaf 已提交
422
    transposed_density_op = np.copy(density_op)
Q
Quleaf 已提交
423 424 425
    if sys_idx == 2:
        for j in [0, 2]:
            for i in [0, 2]:
Q
Quleaf 已提交
426
                transposed_density_op[i:i + 2, j:j + 2] = density_op[i:i + 2, j:j + 2].transpose()
Q
Quleaf 已提交
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
    else:
        transposed_density_op[2:4, 0:2] = density_op[0:2, 2:4]
        transposed_density_op[0:2, 2:4] = density_op[2:4, 0:2]

    return transposed_density_op


def partial_transpose(density_op, n):
    r"""计算输入量子态的 partial transpose :math:`\rho^{T_A}`。

    Args:
        density_op (numpy.ndarray): 量子态的密度矩阵形式

    Returns:
        float: 输入的量子态的 partial transpose
    """

    # Copy the density matrix and not corrupt the original one
Q
Quleaf 已提交
445
    transposed_density_op = np.copy(density_op)
Q
Quleaf 已提交
446 447 448
    for j in range(0, 2 ** n, 2):
        for i in range(0, 2 ** n, 2):
            transposed_density_op[i:i + 2, j:j + 2] = density_op[i:i + 2, j:j + 2].transpose()
Q
Quleaf 已提交
449 450 451

    return transposed_density_op

Q
Quleaf 已提交
452

Q
Quleaf 已提交
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
def negativity(density_op):
    r"""计算输入量子态的 Negativity :math:`N = ||\frac{\rho^{T_A}-1}{2}||`。

    Args:
        density_op (numpy.ndarray): 量子态的密度矩阵形式

    Returns:
        float: 输入的量子态的 Negativity

    代码示例:

    .. code-block:: python

        from paddle_quantum.utils import negativity
        from paddle_quantum.state import bell_state
        rho = bell_state(2)
        print("Negativity of the Bell state is:", negativity(rho))

    ::

        Negativity of the Bell state is: 0.5
    """
    # Implement the partial transpose
    density_op_T = partial_transpose_2(density_op)

    # Calculate through the equivalent expression N = sum(abs(\lambda_i)) when \lambda_i<0
    n = 0
Q
Quleaf 已提交
480
    eigen_val, _ = np.linalg.eig(density_op_T)
Q
Quleaf 已提交
481 482
    for val in eigen_val:
        if val < 0:
Q
Quleaf 已提交
483
            n = n + np.abs(val)
Q
Quleaf 已提交
484 485
    return n

Q
Quleaf 已提交
486

Q
Quleaf 已提交
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
def logarithmic_negativity(density_op):
    r"""计算输入量子态的 Logarithmic Negativity :math:`E_N = ||\rho^{T_A}||`。

    Args:
        density_op (numpy.ndarray): 量子态的密度矩阵形式

    Returns:
        float: 输入的量子态的 Logarithmic Negativity

    代码示例:

    .. code-block:: python

        from paddle_quantum.utils import logarithmic_negativity
        from paddle_quantum.state import bell_state
        rho = bell_state(2)
        print("Logarithmic negativity of the Bell state is:", logarithmic_negativity(rho))

    ::

        Logarithmic negativity of the Bell state is: 1.0
    """
    # Calculate the negativity
    n = negativity(density_op)

    # Calculate through the equivalent expression
Q
Quleaf 已提交
513
    log2_n = np.log2(2 * n + 1)
Q
Quleaf 已提交
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
    return log2_n


def is_ppt(density_op):
    r"""计算输入量子态是否满足 PPT 条件。

    Args:
        density_op (numpy.ndarray): 量子态的密度矩阵形式

    Returns:
        bool: 输入的量子态是否满足 PPT 条件

    代码示例:

    .. code-block:: python

        from paddle_quantum.utils import is_ppt
        from paddle_quantum.state import bell_state
        rho = bell_state(2)
        print("Whether the Bell state satisfies PPT condition:", is_ppt(rho))

    ::

        Whether the Bell state satisfies PPT condition: False
    """
    # By default the PPT condition is satisfied
    ppt = True

    # Detect negative eigenvalues from the partial transposed density_op
    if negativity(density_op) > 0:
        ppt = False
Q
Quleaf 已提交
545 546 547
    return ppt


Q
Quleaf 已提交
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
def haar_orthogonal(n):
    r"""生成一个服从 Haar random 的正交矩阵。采样算法参考文献:arXiv:math-ph/0609050v2

        Args:
            n (int): 正交矩阵对应的量子比特数

        Returns:
            numpy.ndarray: 一个形状为 ``(2**n, 2**n)`` 随机正交矩阵
    """
    # Matrix dimension
    d = 2 ** n
    # Step 1: sample from Ginibre ensemble
    g = (np.random.randn(d, d))
    # Step 2: perform QR decomposition of G
    q, r = np.linalg.qr(g)
    # Step 3: make the decomposition unique
    lam = np.diag(r) / abs(np.diag(r))
    u = q @ np.diag(lam)

    return u


def haar_unitary(n):
    r"""生成一个服从 Haar random 的酉矩阵。采样算法参考文献:arXiv:math-ph/0609050v2

        Args:
            n (int): 酉矩阵对应的量子比特数

        Returns:
            numpy.ndarray: 一个形状为 ``(2**n, 2**n)`` 随机酉矩阵
    """
    # Matrix dimension
    d = 2 ** n
    # Step 1: sample from Ginibre ensemble
    g = (np.random.randn(d, d) + 1j * np.random.randn(d, d)) / np.sqrt(2)
    # Step 2: perform QR decomposition of G
    q, r = np.linalg.qr(g)
    # Step 3: make the decomposition unique
    lam = np.diag(r) / abs(np.diag(r))
    u = q @ np.diag(lam)

    return u


def haar_state_vector(n, real=False):
    r"""生成一个服从 Haar random 的态矢量。

        Args:
            n (int): 量子态的量子比特数
            real (bool): 生成的态矢量是否为实态矢量,默认为 ``False``

        Returns:
            numpy.ndarray: 一个形状为 ``(2**n, 1)`` 随机态矢量
    """
    # Vector dimension
    d = 2 ** n
    if real:
        # Generate a Haar random orthogonal matrix
        o = haar_orthogonal(n)
        # Perform u onto |0>, i.e., the first column of o
        phi = o[:, 0]
    else:
        # Generate a Haar random unitary
        u = haar_unitary(n)
        # Perform u onto |0>, i.e., the first column of u
        phi = u[:, 0]

    return phi


def haar_density_operator(n, k=None, real=False):
    r"""生成一个服从 Haar random 的密度矩阵。

        Args:
            n (int): 量子态的量子比特数
            k (int): 密度矩阵的秩,默认为 ``None``,表示满秩
            real (bool): 生成的密度矩阵是否为实矩阵,默认为 ``False``

        Returns:
            numpy.ndarray: 一个形状为 ``(2**n, 2**n)`` 随机密度矩阵
    """
    d = 2 ** n
    k = k if k is not None else d
    assert 0 < k <= d, 'rank is an invalid number'
    if real:
        ginibre_matrix = np.random.randn(d, k)
        rho = ginibre_matrix @ ginibre_matrix.T
    else:
        ginibre_matrix = np.random.randn(d, k) + 1j * np.random.randn(d, k)
        rho = ginibre_matrix @ ginibre_matrix.conj().T

    return rho / np.trace(rho)


Q
Quleaf 已提交
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
class Hamiltonian:
    r""" Paddle Quantum 中的 Hamiltonian ``class``。

    用户可以通过一个 Pauli string 来实例化该 ``class``。
    """
    def __init__(self, pauli_str, compress=True):
        r""" 创建一个 Hamiltonian 类。

        Args:
            pauli_str (list): 用列表定义的 Hamiltonian,如 ``[(1, 'Z0, Z1'), (2, 'I')]``
            compress (bool): 是否对输入的 list 进行自动合并(例如 ``(1, 'Z0, Z1')`` 和 ``(2, 'Z1, Z0')`` 这两项将被自动合并),默认为 ``True``

        Note:
            如果设置 ``compress=False``,则不会对输入的合法性进行检验。
        """
        self.__coefficients = None
        self.__terms = None
        self.__pauli_words_r = []
        self.__pauli_words = []
        self.__sites = []
        self.__nqubits = None
        # when internally updating the __pauli_str, be sure to set __update_flag to True
        self.__pauli_str = pauli_str
        self.__update_flag = True
        self.__decompose()
        if compress:
            self.__compress()

    def __getitem__(self, indices):
        new_pauli_str = []
        if isinstance(indices, int):
            indices = [indices]
        elif isinstance(indices, slice):
            indices = list(range(self.n_terms)[indices])
        elif isinstance(indices, tuple):
            indices = list(indices)

        for index in indices:
            new_pauli_str.append([self.coefficients[index], ','.join(self.terms[index])])
        return Hamiltonian(new_pauli_str, compress=False)

    def __add__(self, h_2):
        new_pauli_str = self.pauli_str.copy()
        if isinstance(h_2, float) or isinstance(h_2, int):
            new_pauli_str.extend([[float(h_2), 'I']])
        else:
            new_pauli_str.extend(h_2.pauli_str)
        return Hamiltonian(new_pauli_str)

    def __mul__(self, other):
        new_pauli_str = copy.deepcopy(self.pauli_str)
        for i in range(len(new_pauli_str)):
            new_pauli_str[i][0] *= other
        return Hamiltonian(new_pauli_str, compress=False)

    def __sub__(self, other):
        return self.__add__(other.__mul__(-1))

    def __str__(self):
        str_out = ''
        for idx in range(self.n_terms):
            str_out += '{} '.format(self.coefficients[idx])
            for _ in range(len(self.terms[idx])):
                str_out += self.terms[idx][_]
                if _ != len(self.terms[idx]) - 1:
                    str_out += ', '
            if idx != self.n_terms - 1:
                str_out += '\n'
        return str_out

    def __repr__(self):
        return 'paddle_quantum.Hamiltonian object: \n' + self.__str__()

    @property
    def n_terms(self):
        r"""返回该哈密顿量的项数

        Returns:
            int :哈密顿量的项数
        """
        return len(self.__pauli_str)

    @property
    def pauli_str(self):
        r"""返回哈密顿量对应的 Pauli string

        Returns:
            list : 哈密顿量对应的 Pauli string
        """
        return self.__pauli_str

    @property
    def terms(self):
        r"""返回哈密顿量中的每一项构成的列表

        Returns:
            list :哈密顿量中的每一项,i.e. ``[['Z0, Z1'], ['I']]``
        """
        if self.__update_flag:
            self.__decompose()
            return self.__terms
        else:
            return self.__terms

    @property
    def coefficients(self):
        r""" 返回哈密顿量中的每一项对应的系数构成的列表

        Returns:
Q
Quleaf 已提交
751
            list :哈密顿量中每一项的系数,i.e. ``[1.0, 2.0]``
Q
Quleaf 已提交
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
        """
        if self.__update_flag:
            self.__decompose()
            return self.__coefficients
        else:
            return self.__coefficients

    @property
    def pauli_words(self):
        r"""返回哈密顿量中每一项对应的 Pauli word 构成的列表

        Returns:
            list :每一项对应的 Pauli word,i.e. ``['ZIZ', 'IIX']``
        """
        if self.__update_flag:
            self.__decompose()
            return self.__pauli_words
        else:
            return self.__pauli_words

    @property
    def pauli_words_r(self):
        r"""返回哈密顿量中每一项对应的简化(不包含 I) Pauli word 组成的列表

        Returns:
            list :不包含 "I" 的 Pauli word 构成的列表,i.e. ``['ZXZZ', 'Z', 'X']``
        """
        if self.__update_flag:
            self.__decompose()
            return self.__pauli_words_r
        else:
            return self.__pauli_words_r

    @property
    def sites(self):
        r"""返回该哈密顿量中的每一项对应的量子比特编号组成的列表

        Returns:
            list :哈密顿量中每一项所对应的量子比特编号构成的列表,i.e. ``[[1, 2], [0]]``
        """
        if self.__update_flag:
            self.__decompose()
            return self.__sites
        else:
            return self.__sites

    @property
    def n_qubits(self):
        r"""返回该哈密顿量对应的量子比特数

        Returns:
            int :量子比特数
        """
        if self.__update_flag:
            self.__decompose()
            return self.__nqubits
        else:
            return self.__nqubits

    def __decompose(self):
        r"""将哈密顿量分解为不同的形式

        Notes:
            这是一个内部函数,你不需要直接使用它
            这是一个比较基础的函数,它负责将输入的 Pauli string 拆分为不同的形式并存储在内部变量中
        """
        self.__pauli_words = []
        self.__pauli_words_r = []
        self.__sites = []
        self.__terms = []
        self.__coefficients = []
        self.__nqubits = 1
        new_pauli_str = []
        for coefficient, pauli_term in self.__pauli_str:
            pauli_word_r = ''
            site = []
            single_pauli_terms = re.split(r',\s*', pauli_term.upper())
            self.__coefficients.append(float(coefficient))
            self.__terms.append(single_pauli_terms)
            for single_pauli_term in single_pauli_terms:
                match_I = re.match(r'I', single_pauli_term, flags=re.I)
                if match_I:
                    assert single_pauli_term[0].upper() == 'I', \
                        'The offset is defined with a sole letter "I", i.e. (3.0, "I")'
                    pauli_word_r += 'I'
                    site.append('')
                else:
                    match = re.match(r'([XYZ])([0-9]+)', single_pauli_term, flags=re.I)
                    if match:
                        pauli_word_r += match.group(1).upper()
                        assert int(match.group(2)) not in site, 'each Pauli operator should act on different qubit'
                        site.append(int(match.group(2)))
                    else:
                        raise Exception(
                            'Operators should be defined with a string composed of Pauli operators followed' +
                            'by qubit index on which it act, separated with ",". i.e. "Z0, X1"')
                    self.__nqubits = max(self.__nqubits, int(match.group(2)) + 1)
            self.__pauli_words_r.append(pauli_word_r)
            self.__sites.append(site)
            new_pauli_str.append([float(coefficient), pauli_term.upper()])

        for term_index in range(len(self.__pauli_str)):
            pauli_word = ['I' for _ in range(self.__nqubits)]
            site = self.__sites[term_index]
            for site_index in range(len(site)):
                if type(site[site_index]) == int:
                    pauli_word[site[site_index]] = self.__pauli_words_r[term_index][site_index]
            self.__pauli_words.append(''.join(pauli_word))
            self.__pauli_str = new_pauli_str
            self.__update_flag = False

    def __compress(self):
        r""" 对同类项进行合并。

        Notes:
            这是一个内部函数,你不需要直接使用它
        """
        if self.__update_flag:
            self.__decompose()
        else:
            pass
        new_pauli_str = []
        flag_merged = [False for _ in range(self.n_terms)]
        for term_idx_1 in range(self.n_terms):
            if not flag_merged[term_idx_1]:
                for term_idx_2 in range(term_idx_1 + 1, self.n_terms):
                    if not flag_merged[term_idx_2]:
                        if self.pauli_words[term_idx_1] == self.pauli_words[term_idx_2]:
                            self.__coefficients[term_idx_1] += self.__coefficients[term_idx_2]
                            flag_merged[term_idx_2] = True
                    else:
                        pass
                if self.__coefficients[term_idx_1] != 0:
                    new_pauli_str.append([self.__coefficients[term_idx_1], ','.join(self.__terms[term_idx_1])])
        self.__pauli_str = new_pauli_str
        self.__update_flag = True

    def decompose_with_sites(self):
Q
Quleaf 已提交
890
        r"""将 pauli_str 分解为系数、泡利字符串的简化形式以及它们分别作用的量子比特下标。
Q
Quleaf 已提交
891 892

        Returns:
Q
Quleaf 已提交
893 894 895 896 897 898
            tuple: 包含如下元素的 tuple:

                 - coefficients (list): 元素为每一项的系数
                 - pauli_words_r (list): 元素为每一项的泡利字符串的简化形式,例如 'Z0, Z1, X3' 这一项的泡利字符串为 'ZZX'
                 - sites (list): 元素为每一项作用的量子比特下标,例如 'Z0, Z1, X3' 这一项的 site 为 [0, 1, 3]

Q
Quleaf 已提交
899 900 901 902 903 904
        """
        if self.__update_flag:
            self.__decompose()
        return self.coefficients, self.__pauli_words_r, self.__sites

    def decompose_pauli_words(self):
Q
Quleaf 已提交
905
        r"""将 pauli_str 分解为系数和泡利字符串。
Q
Quleaf 已提交
906 907

        Returns:
Q
Quleaf 已提交
908 909 910 911
            tuple: 包含如下元素的 tuple:

                - coefficients(list): 元素为每一项的系数
                - pauli_words(list): 元素为每一项的泡利字符串,例如 'Z0, Z1, X3' 这一项的泡利字符串为 'ZZIX'
Q
Quleaf 已提交
912 913 914 915 916 917 918
        """
        if self.__update_flag:
            self.__decompose()
        else:
            pass
        return self.coefficients, self.__pauli_words

Y
yangguohao 已提交
919
    def construct_h_matrix(self):
Q
Quleaf 已提交
920
        r"""构建 Hamiltonian 在 Z 基底下的矩阵。
Q
Quleaf 已提交
921 922 923 924 925

        Returns:
            np.ndarray: Z 基底下的哈密顿量矩阵形式
        """
        coefs, pauli_words, sites = self.decompose_with_sites()
Y
yangguohao 已提交
926 927 928 929
        n_qubit = 1
        for site in sites:
            if type(site[0]) is int:
                n_qubit = max(n_qubit, max(site) + 1)
Q
Quleaf 已提交
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
        h_matrix = np.zeros([2 ** n_qubit, 2 ** n_qubit], dtype='complex64')
        spin_ops = SpinOps(n_qubit, use_sparse=True)
        for idx in range(len(coefs)):
            op = coefs[idx] * sparse.eye(2 ** n_qubit, dtype='complex64')
            for site_idx in range(len(sites[idx])):
                if re.match(r'X', pauli_words[idx][site_idx], re.I):
                    op = op.dot(spin_ops.sigx_p[sites[idx][site_idx]])
                elif re.match(r'Y', pauli_words[idx][site_idx], re.I):
                    op = op.dot(spin_ops.sigy_p[sites[idx][site_idx]])
                elif re.match(r'Z', pauli_words[idx][site_idx], re.I):
                    op = op.dot(spin_ops.sigz_p[sites[idx][site_idx]])
            h_matrix += op
        return h_matrix


class SpinOps:
Q
Quleaf 已提交
946
    r"""矩阵表示下的自旋算符,可以用来构建哈密顿量矩阵或者自旋可观测量。
Q
Quleaf 已提交
947 948 949

    """
    def __init__(self, size: int, use_sparse=False):
Q
Quleaf 已提交
950
        r"""SpinOps 的构造函数,用于实例化一个 SpinOps 对象。
Q
Quleaf 已提交
951 952 953

        Args:
            size (int): 系统的大小(有几个量子比特)
Q
Quleaf 已提交
954
            use_sparse (bool): 是否使用 sparse matrix 计算,默认为 ``False``
Q
Quleaf 已提交
955 956 957
        """
        self.size = size
        self.id = sparse.eye(2, dtype='complex128')
Q
Quleaf 已提交
958 959 960 961 962 963
        self.__sigz = sparse.bsr.bsr_matrix([[1, 0], [0, -1]], dtype='complex64')
        self.__sigy = sparse.bsr.bsr_matrix([[0, -1j], [1j, 0]], dtype='complex64')
        self.__sigx = sparse.bsr.bsr_matrix([[0, 1], [1, 0]], dtype='complex64')
        self.__sigz_p = []
        self.__sigy_p = []
        self.__sigx_p = []
Q
Quleaf 已提交
964 965
        self.__sparse = use_sparse
        for i in range(self.size):
Q
Quleaf 已提交
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
            self.__sigz_p.append(self.__direct_prod_op(spin_op=self.__sigz, spin_index=i))
            self.__sigy_p.append(self.__direct_prod_op(spin_op=self.__sigy, spin_index=i))
            self.__sigx_p.append(self.__direct_prod_op(spin_op=self.__sigx, spin_index=i))

    @property
    def sigz_p(self):
        r""" :math:`Z` 基底下的 :math:`S^z_i` 算符。

        Returns:
            list : :math:`S^z_i` 算符组成的列表,其中每一项对应不同的 :math:`i`
        """
        return self.__sigz_p

    @property
    def sigy_p(self):
        r""" :math:`Z` 基底下的 :math:`S^y_i` 算符。

        Returns:
            list : :math:`S^y_i` 算符组成的列表,其中每一项对应不同的 :math:`i`
        """
        return self.__sigy_p

    @property
    def sigx_p(self):
        r""" :math:`Z` 基底下的 :math:`S^x_i` 算符。

        Returns:
            list : :math:`S^x_i` 算符组成的列表,其中每一项对应不同的 :math:`i`
        """
        return self.__sigx_p
Q
Quleaf 已提交
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029

    def __direct_prod_op(self, spin_op, spin_index):
        r"""直积,得到第 n 个自旋(量子比特)上的自旋算符

        Args:
            spin_op: 单体自旋算符
            spin_index: 标记第 n 个自旋(量子比特)

        Returns:
            scipy.sparse or np.ndarray: 直积后的自旋算符,其数据类型取决于 self.__use_sparse
        """
        s_p = copy.copy(spin_op)
        for i in range(self.size):
            if i < spin_index:
                s_p = sparse.kron(self.id, s_p)
            elif i > spin_index:
                s_p = sparse.kron(s_p, self.id)
        if self.__sparse:
            return s_p
        else:
            return s_p.toarray()


def __input_args_dtype_check(
        show_arrow,
        save_gif,
        filename,
        view_angle,
        view_dist
):
    r"""
    该函数实现对输入默认参数的数据类型检查,保证输入函数中的参数为所允许的数据类型

    Args:
Q
Quleaf 已提交
1030 1031 1032 1033 1034 1035
        show_arrow (bool): 是否展示向量的箭头,默认为 False
        save_gif (bool): 是否存储 gif 动图
        filename (str): 存储的 gif 动图的名字
        view_angle (list or tuple): 视图的角度,
            第一个元素为关于xy平面的夹角[0-360],第二个元素为关于xz平面的夹角[0-360], 默认为 (30, 45)
        view_dist (int): 视图的距离,默认为 7
Q
Quleaf 已提交
1036 1037 1038 1039
    """

    if show_arrow is not None:
        assert type(show_arrow) == bool, \
Q
Quleaf 已提交
1040
            'the type of "show_arrow" should be "bool".'
Q
Quleaf 已提交
1041 1042
    if save_gif is not None:
        assert type(save_gif) == bool, \
Q
Quleaf 已提交
1043
            'the type of "save_gif" should be "bool".'
Q
Quleaf 已提交
1044 1045 1046
    if save_gif:
        if filename is not None:
            assert type(filename) == str, \
Q
Quleaf 已提交
1047
                'the type of "filename" should be "str".'
Q
Quleaf 已提交
1048
            other, ext = os.path.splitext(filename)
Q
Quleaf 已提交
1049 1050
            assert ext == '.gif', 'The suffix of the file name must be "gif".'
            # If it does not exist, create a folder
Q
Quleaf 已提交
1051 1052 1053 1054 1055
            path, file = os.path.split(filename)
            if not os.path.exists(path):
                os.makedirs(path)
    if view_angle is not None:
        assert type(view_angle) == list or type(view_angle) == tuple, \
Q
Quleaf 已提交
1056
            'the type of "view_angle" should be "list" or "tuple".'
Q
Quleaf 已提交
1057 1058
        for i in range(2):
            assert type(view_angle[i]) == int, \
Q
Quleaf 已提交
1059
                'the type of "view_angle[0]" and "view_angle[1]" should be "int".'
Q
Quleaf 已提交
1060 1061
    if view_dist is not None:
        assert type(view_dist) == int, \
Q
Quleaf 已提交
1062
            'the type of "view_dist" should be "int".'
Q
Quleaf 已提交
1063 1064 1065 1066 1067 1068 1069


def __density_matrix_convert_to_bloch_vector(density_matrix):
    r"""该函数将密度矩阵转化为bloch球面上的坐标

    Args:
        density_matrix (numpy.ndarray): 输入的密度矩阵
Q
Quleaf 已提交
1070

Q
Quleaf 已提交
1071
    Returns:
Q
Quleaf 已提交
1072
        bloch_vector (numpy.ndarray): 存储bloch向量的 x,y,z 坐标,向量的模长,向量的颜色
Q
Quleaf 已提交
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
    """

    # Pauli Matrix
    pauli_x = np.array([[0, 1], [1, 0]])
    pauli_y = np.array([[0, -1j], [1j, 0]])
    pauli_z = np.array([[1, 0], [0, -1]])

    # Convert a density matrix to a Bloch vector.
    ax = np.trace(np.dot(density_matrix, pauli_x)).real
    ay = np.trace(np.dot(density_matrix, pauli_y)).real
    az = np.trace(np.dot(density_matrix, pauli_z)).real

    # Calc the length of bloch vector
    length = ax ** 2 + ay ** 2 + az ** 2
    length = sqrt(length)
    if length > 1.0:
        length = 1.0

    # Calc the color of bloch vector, the value of the color is proportional to the length
    color = length

    bloch_vector = [ax, ay, az, length, color]

    # You must use an array, which is followed by slicing and taking a column
    bloch_vector = np.array(bloch_vector)

    return bloch_vector


def __plot_bloch_sphere(
        ax,
        bloch_vectors=None,
        show_arrow=False,
        clear_plt=True,
        rotating_angle_list=None,
        view_angle=None,
Q
Quleaf 已提交
1109 1110
        view_dist=None,
        set_color=None
Q
Quleaf 已提交
1111 1112 1113 1114 1115
):
    r"""将 Bloch 向量展示在 Bloch 球面上

    Args:
        ax (Axes3D(fig)): 画布的句柄
Q
Quleaf 已提交
1116 1117 1118
        bloch_vectors (numpy.ndarray): 存储bloch向量的 x,y,z 坐标,向量的模长,向量的颜色
        show_arrow (bool): 是否展示向量的箭头,默认为 False
        clear_plt (bool): 是否要清空画布,默认为 True,每次画图的时候清空画布再画图
Q
Quleaf 已提交
1119
        rotating_angle_list (list): 旋转角度的列表,用于展示旋转轨迹
Q
Quleaf 已提交
1120 1121 1122 1123
        view_angle (list): 视图的角度,
            第一个元素为关于xy平面的夹角[0-360],第二个元素为关于xz平面的夹角[0-360], 默认为 (30, 45)
        view_dist (int): 视图的距离,默认为 7
        set_color (str): 设置指定的颜色,请查阅cmap表,默认为 "红-黑-根据向量的模长渐变" 颜色方案
Q
Quleaf 已提交
1124 1125 1126
    """
    # Assign a value to an empty variable
    if view_angle is None:
Q
Quleaf 已提交
1127
        view_angle = (30, 45)
Q
Quleaf 已提交
1128 1129 1130
    if view_dist is None:
        view_dist = 7
    # Define my_color
Q
Quleaf 已提交
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
    if set_color is None:
        color = 'rainbow'
        black_code = '#000000'
        red_code = '#F24A29'
        if bloch_vectors is not None:
            black_to_red = mplcolors.LinearSegmentedColormap.from_list(
                'my_color',
                [(0, black_code), (1, red_code)],
                N=len(bloch_vectors[:, 4])
            )
            map_vir = plt.get_cmap(black_to_red)
            color = map_vir(bloch_vectors[:, 4])
    else:
        color = set_color
Q
Quleaf 已提交
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254

    # Set the view angle and view distance
    ax.view_init(view_angle[0], view_angle[1])
    ax.dist = view_dist

    # Draw the general frame
    def draw_general_frame():

        # Do not show the grid and original axes
        ax.grid(False)
        ax.set_axis_off()
        ax.view_init(view_angle[0], view_angle[1])
        ax.dist = view_dist

        # Set the lower limit and upper limit of each axis
        # To make the bloch_ball look less flat, the default is relatively flat
        ax.set_xlim3d(xmin=-1.5, xmax=1.5)
        ax.set_ylim3d(ymin=-1.5, ymax=1.5)
        ax.set_zlim3d(zmin=-1, zmax=1.3)

        # Draw a new axes
        coordinate_start_x, coordinate_start_y, coordinate_start_z = \
            np.array([[-1.5, 0, 0], [0, -1.5, 0], [0, 0, -1.5]])
        coordinate_end_x, coordinate_end_y, coordinate_end_z = \
            np.array([[3, 0, 0], [0, 3, 0], [0, 0, 3]])
        ax.quiver(
            coordinate_start_x, coordinate_start_y, coordinate_start_z,
            coordinate_end_x, coordinate_end_y, coordinate_end_z,
            arrow_length_ratio=0.03, color="black", linewidth=0.5
        )
        ax.text(0, 0, 1.7, r"|0⟩", color="black", fontsize=16)
        ax.text(0, 0, -1.9, r"|1⟩", color="black", fontsize=16)
        ax.text(1.9, 0, 0, r"|+⟩", color="black", fontsize=16)
        ax.text(-1.7, 0, 0, r"|–⟩", color="black", fontsize=16)
        ax.text(0, 1.7, 0, r"|i+⟩", color="black", fontsize=16)
        ax.text(0, -1.9, 0, r"|i–⟩", color="black", fontsize=16)

        # Draw a surface
        horizontal_angle = np.linspace(0, 2 * np.pi, 80)
        vertical_angle = np.linspace(0, np.pi, 80)
        surface_point_x = np.outer(np.cos(horizontal_angle), np.sin(vertical_angle))
        surface_point_y = np.outer(np.sin(horizontal_angle), np.sin(vertical_angle))
        surface_point_z = np.outer(np.ones(np.size(horizontal_angle)), np.cos(vertical_angle))
        ax.plot_surface(
            surface_point_x, surface_point_y, surface_point_z, rstride=1, cstride=1,
            color="black", linewidth=0.05, alpha=0.03
        )

        # Draw circle
        def draw_circle(circle_horizon_angle, circle_vertical_angle, linewidth=0.5, alpha=0.2):
            r = 1
            circle_point_x = r * np.cos(circle_vertical_angle) * np.cos(circle_horizon_angle)
            circle_point_y = r * np.cos(circle_vertical_angle) * np.sin(circle_horizon_angle)
            circle_point_z = r * np.sin(circle_vertical_angle)
            ax.plot(
                circle_point_x, circle_point_y, circle_point_z,
                color="black", linewidth=linewidth, alpha=alpha
            )

        # draw longitude and latitude
        def draw_longitude_and_latitude():
            # Draw longitude
            num = 3
            theta = np.linspace(0, 0, 100)
            psi = np.linspace(0, 2 * np.pi, 100)
            for i in range(num):
                theta = theta + np.pi / num
                draw_circle(theta, psi)

            # Draw latitude
            num = 6
            theta = np.linspace(0, 2 * np.pi, 100)
            psi = np.linspace(-np.pi / 2, -np.pi / 2, 100)
            for i in range(num):
                psi = psi + np.pi / num
                draw_circle(theta, psi)

            # Draw equator
            theta = np.linspace(0, 2 * np.pi, 100)
            psi = np.linspace(0, 0, 100)
            draw_circle(theta, psi, linewidth=0.5, alpha=0.2)

            # Draw prime meridian
            theta = np.linspace(0, 0, 100)
            psi = np.linspace(0, 2 * np.pi, 100)
            draw_circle(theta, psi, linewidth=0.5, alpha=0.2)

        # If the number of data points exceeds 20, no longitude and latitude lines will be drawn.
        if bloch_vectors is not None and len(bloch_vectors) < 52:
            draw_longitude_and_latitude()
        elif bloch_vectors is None:
            draw_longitude_and_latitude()

        # Draw three invisible points
        invisible_points = np.array([[0.03440399, 0.30279721, 0.95243384],
                                     [0.70776026, 0.57712403, 0.40743499],
                                     [0.46991358, -0.63717908, 0.61088792]])
        ax.scatter(
            invisible_points[:, 0], invisible_points[:, 1], invisible_points[:, 2],
            c='w', alpha=0.01
        )

    # clean plt
    if clear_plt:
        ax.cla()
        draw_general_frame()

    # Draw the data points
    if bloch_vectors is not None:
        ax.scatter(
Q
Quleaf 已提交
1255
            bloch_vectors[:, 0], bloch_vectors[:, 1], bloch_vectors[:, 2], c=color, alpha=1.0
Q
Quleaf 已提交
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
        )

    # if show the rotating angle
    if rotating_angle_list is not None:
        bloch_num = len(bloch_vectors)
        rotating_angle_theta, rotating_angle_phi, rotating_angle_lam = rotating_angle_list[bloch_num - 1]
        rotating_angle_theta = round(rotating_angle_theta, 6)
        rotating_angle_phi = round(rotating_angle_phi, 6)
        rotating_angle_lam = round(rotating_angle_lam, 6)

        # Shown at the top right of the perspective
        display_text_angle = [-(view_angle[0] - 10), (view_angle[1] + 10)]
        text_point_x = 2 * np.cos(display_text_angle[0]) * np.cos(display_text_angle[1])
        text_point_y = 2 * np.cos(display_text_angle[0]) * np.sin(-display_text_angle[1])
        text_point_z = 2 * np.sin(-display_text_angle[0])
        ax.text(text_point_x, text_point_y, text_point_z, r'$\theta=' + str(rotating_angle_theta) + r'$',
                color="black", fontsize=14)
        ax.text(text_point_x, text_point_y, text_point_z - 0.1, r'$\phi=' + str(rotating_angle_phi) + r'$',
                color="black", fontsize=14)
        ax.text(text_point_x, text_point_y, text_point_z - 0.2, r'$\lambda=' + str(rotating_angle_lam) + r'$',
                color="black", fontsize=14)

    # If show the bloch_vector
    if show_arrow:
        ax.quiver(
            0, 0, 0, bloch_vectors[:, 0], bloch_vectors[:, 1], bloch_vectors[:, 2],
Q
Quleaf 已提交
1282
            arrow_length_ratio=0.05, color=color, alpha=1.0
Q
Quleaf 已提交
1283 1284 1285 1286 1287 1288 1289 1290 1291
        )


def plot_state_in_bloch_sphere(
        state,
        show_arrow=False,
        save_gif=False,
        filename=None,
        view_angle=None,
Q
Quleaf 已提交
1292 1293
        view_dist=None,
        set_color=None
Q
Quleaf 已提交
1294 1295 1296 1297 1298 1299 1300 1301
):
    r"""将输入的量子态展示在 Bloch 球面上

    Args:
        state (list(numpy.ndarray or paddle.Tensor)): 输入的量子态列表,可以支持态矢量和密度矩阵
        show_arrow (bool): 是否展示向量的箭头,默认为 ``False``
        save_gif (bool): 是否存储 gif 动图,默认为 ``False``
        filename (str): 存储的 gif 动图的名字
Q
Quleaf 已提交
1302 1303
        view_angle (list or tuple): 视图的角度,
            第一个元素为关于 xy 平面的夹角 [0-360],第二个元素为关于 xz 平面的夹角 [0-360], 默认为 ``(30, 45)``
Q
Quleaf 已提交
1304
        view_dist (int): 视图的距离,默认为 7
Q
Quleaf 已提交
1305
        set_color (str): 若要设置指定的颜色,请查阅 ``cmap`` 表。默认为红色到黑色的渐变颜色
Q
Quleaf 已提交
1306 1307 1308 1309 1310
    """
    # Check input data
    __input_args_dtype_check(show_arrow, save_gif, filename, view_angle, view_dist)

    assert type(state) == list or type(state) == paddle.Tensor or type(state) == np.ndarray, \
Q
Quleaf 已提交
1311
        'the type of "state" must be "list" or "paddle.Tensor" or "np.ndarray".'
Q
Quleaf 已提交
1312 1313 1314
    if type(state) == paddle.Tensor or type(state) == np.ndarray:
        state = [state]
    state_len = len(state)
Q
Quleaf 已提交
1315
    assert state_len >= 1, '"state" is NULL.'
Q
Quleaf 已提交
1316 1317
    for i in range(state_len):
        assert type(state[i]) == paddle.Tensor or type(state[i]) == np.ndarray, \
Q
Quleaf 已提交
1318 1319 1320 1321
            'the type of "state[i]" should be "paddle.Tensor" or "numpy.ndarray".'
    if set_color is not None:
        assert type(set_color) == str, \
            'the type of "set_color" should be "str".'
Q
Quleaf 已提交
1322 1323 1324 1325 1326

    # Assign a value to an empty variable
    if filename is None:
        filename = 'state_in_bloch_sphere.gif'
    if view_angle is None:
Q
Quleaf 已提交
1327
        view_angle = (30, 45)
Q
Quleaf 已提交
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
    if view_dist is None:
        view_dist = 7

    # Convert Tensor to numpy
    for i in range(state_len):
        if type(state[i]) == paddle.Tensor:
            state[i] = state[i].numpy()

    # Convert state_vector to density_matrix
    for i in range(state_len):
        if state[i].size == 2:
            state_vector = state[i]
            state[i] = np.outer(state_vector, np.conj(state_vector))

    # Calc the bloch_vectors
    bloch_vector_list = []
    for i in range(state_len):
        bloch_vector_tmp = __density_matrix_convert_to_bloch_vector(state[i])
        bloch_vector_list.append(bloch_vector_tmp)

    # List must be converted to array for slicing.
    bloch_vectors = np.array(bloch_vector_list)

    # A update function for animation class
    def update(frame):
        view_rotating_angle = 5
Q
Quleaf 已提交
1354 1355 1356 1357 1358
        new_view_angle = [view_angle[0], view_angle[1] + view_rotating_angle * frame]
        __plot_bloch_sphere(
            ax, bloch_vectors, show_arrow, clear_plt=True,
            view_angle=new_view_angle, view_dist=view_dist, set_color=set_color
        )
Q
Quleaf 已提交
1359 1360 1361

    # Dynamic update and save
    if save_gif:
Q
Quleaf 已提交
1362 1363 1364 1365 1366 1367
        # Helper function to plot vectors on a sphere.
        fig = plt.figure(figsize=(8, 8), dpi=100)
        fig.subplots_adjust(left=0, right=1, bottom=0, top=1)
        ax = fig.add_subplot(111, projection='3d')

        frames_num = 7
Q
Quleaf 已提交
1368
        anim = animation.FuncAnimation(fig, update, frames=frames_num, interval=600, repeat=False)
Q
Quleaf 已提交
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
        anim.save(filename, dpi=100, writer='pillow')
        # close the plt
        plt.close(fig)

    # Helper function to plot vectors on a sphere.
    fig = plt.figure(figsize=(8, 8), dpi=100)
    fig.subplots_adjust(left=0, right=1, bottom=0, top=1)
    ax = fig.add_subplot(111, projection='3d')

    __plot_bloch_sphere(
        ax, bloch_vectors, show_arrow, clear_plt=True,
        view_angle=view_angle, view_dist=view_dist, set_color=set_color
    )
Q
Quleaf 已提交
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392

    plt.show()


def plot_rotation_in_bloch_sphere(
        init_state,
        rotating_angle,
        show_arrow=False,
        save_gif=False,
        filename=None,
        view_angle=None,
Q
Quleaf 已提交
1393 1394
        view_dist=None,
        color_scheme=None,
Q
Quleaf 已提交
1395 1396 1397 1398 1399 1400 1401 1402 1403
):
    r"""在 Bloch 球面上刻画从初始量子态开始的旋转轨迹

    Args:
        init_state (numpy.ndarray or paddle.Tensor): 输入的初始量子态,可以支持态矢量和密度矩阵
        rotating_angle (list(float)): 旋转角度 ``[theta, phi, lam]``
        show_arrow (bool): 是否展示向量的箭头,默认为 ``False``
        save_gif (bool): 是否存储 gif 动图,默认为 ``False``
        filename (str): 存储的 gif 动图的名字
Q
Quleaf 已提交
1404 1405
        view_angle (list or tuple): 视图的角度,
            第一个元素为关于 xy 平面的夹角 [0-360],第二个元素为关于 xz 平面的夹角 [0-360], 默认为 ``(30, 45)``
Q
Quleaf 已提交
1406
        view_dist (int): 视图的距离,默认为 7
Q
Quleaf 已提交
1407
        color_scheme (list(str,str,str)): 分别是初始颜色,轨迹颜色,结束颜色。若要设置指定的颜色,请查阅 ``cmap`` 表。默认为红色
Q
Quleaf 已提交
1408 1409 1410 1411 1412 1413 1414
    """
    # Check input data
    __input_args_dtype_check(show_arrow, save_gif, filename, view_angle, view_dist)

    assert type(init_state) == paddle.Tensor or type(init_state) == np.ndarray, \
        'the type of input data should be "paddle.Tensor" or "numpy.ndarray".'
    assert type(rotating_angle) == tuple or type(rotating_angle) == list, \
Q
Quleaf 已提交
1415
        'the type of rotating_angle should be "tuple" or "list".'
Q
Quleaf 已提交
1416 1417 1418 1419 1420
    assert len(rotating_angle) == 3, \
        'the rotating_angle must include [theta=paddle.Tensor, phi=paddle.Tensor, lam=paddle.Tensor].'
    for i in range(3):
        assert type(rotating_angle[i]) == paddle.Tensor or type(rotating_angle[i]) == float, \
            'the rotating_angle must include [theta=paddle.Tensor, phi=paddle.Tensor, lam=paddle.Tensor].'
Q
Quleaf 已提交
1421 1422 1423 1424 1425 1426 1427
    if color_scheme is not None:
        assert type(color_scheme) == list and len(color_scheme) <= 3, \
            'the type of "color_scheme" should be "list" and ' \
            'the length of "color_scheme" should be less than or equal to "3".'
        for i in range(len(color_scheme)):
            assert type(color_scheme[i]) == str, \
                'the type of "color_scheme[i] should be "str".'
Q
Quleaf 已提交
1428 1429 1430 1431

    # Assign a value to an empty variable
    if filename is None:
        filename = 'rotation_in_bloch_sphere.gif'
Q
Quleaf 已提交
1432 1433 1434 1435 1436 1437 1438

    # Assign colors to bloch vectors
    color_list = ['orangered', 'lightsalmon', 'darkred']
    if color_scheme is not None:
        for i in range(len(color_scheme)):
            color_list[i] = color_scheme[i]
    set_init_color, set_trac_color, set_end_color = color_list
Q
Quleaf 已提交
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476

    theta, phi, lam = rotating_angle

    # Convert Tensor to numpy
    if type(init_state) == paddle.Tensor:
        init_state = init_state.numpy()

    # Convert state_vector to density_matrix
    if init_state.size == 2:
        state_vector = init_state
        init_state = np.outer(state_vector, np.conj(state_vector))

    # Rotating angle
    def rotating_operation(rotating_angle_each):
        gate_matrix = simulator.u_gate_matrix(rotating_angle_each)
        return np.matmul(np.matmul(gate_matrix, init_state), gate_matrix.conj().T)

    # Rotating angle division
    rotating_frame = 50
    rotating_angle_list = []
    state = []
    for i in range(rotating_frame + 1):
        angle_each = [theta / rotating_frame * i, phi / rotating_frame * i, lam / rotating_frame * i]
        rotating_angle_list.append(angle_each)
        state.append(rotating_operation(angle_each))

    state_len = len(state)
    # Calc the bloch_vectors
    bloch_vector_list = []
    for i in range(state_len):
        bloch_vector_tmp = __density_matrix_convert_to_bloch_vector(state[i])
        bloch_vector_list.append(bloch_vector_tmp)

    # List must be converted to array for slicing.
    bloch_vectors = np.array(bloch_vector_list)

    # A update function for animation class
    def update(frame):
Q
Quleaf 已提交
1477 1478
        frame = frame + 2
        if frame <= len(bloch_vectors) - 1:
Q
Quleaf 已提交
1479
            __plot_bloch_sphere(
Q
Quleaf 已提交
1480
                ax, bloch_vectors[1:frame], show_arrow=show_arrow, clear_plt=True,
Q
Quleaf 已提交
1481
                rotating_angle_list=rotating_angle_list,
Q
Quleaf 已提交
1482
                view_angle=view_angle, view_dist=view_dist, set_color=set_trac_color
Q
Quleaf 已提交
1483 1484 1485 1486 1487
            )

            # The starting and ending bloch vector has to be shown
            # show starting vector
            __plot_bloch_sphere(
Q
Quleaf 已提交
1488 1489 1490 1491 1492 1493 1494 1495 1496
                ax, bloch_vectors[:1],  show_arrow=True, clear_plt=False,
                view_angle=view_angle, view_dist=view_dist, set_color=set_init_color
            )

        # Show ending vector
        if frame == len(bloch_vectors):
            __plot_bloch_sphere(
                ax, bloch_vectors[frame - 1:frame], show_arrow=True, clear_plt=False,
                view_angle=view_angle, view_dist=view_dist, set_color=set_end_color
Q
Quleaf 已提交
1497 1498 1499
            )

    if save_gif:
Q
Quleaf 已提交
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
        # Helper function to plot vectors on a sphere.
        fig = plt.figure(figsize=(8, 8), dpi=100)
        fig.subplots_adjust(left=0, right=1, bottom=0, top=1)
        ax = fig.add_subplot(111, projection='3d')

        # Dynamic update and save
        stop_frames = 15
        frames_num = len(bloch_vectors) - 2 + stop_frames
        anim = animation.FuncAnimation(fig, update, frames=frames_num, interval=100, repeat=False)
        anim.save(filename, dpi=100, writer='pillow')
        # close the plt
        plt.close(fig)

    # Helper function to plot vectors on a sphere.
    fig = plt.figure(figsize=(8, 8), dpi=100)
    fig.subplots_adjust(left=0, right=1, bottom=0, top=1)
    ax = fig.add_subplot(111, projection='3d')

    # Draw the penultimate bloch vector
    update(len(bloch_vectors) - 3)
    # Draw the last bloch vector
    update(len(bloch_vectors) - 2)
Q
Quleaf 已提交
1522 1523

    plt.show()
Q
Quleaf 已提交
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603


def pauli_basis(n):
    r"""生成 n 量子比特的泡利基空间
    Args:
        n (int): 量子比特的数量

    Return:
        tuple:
            basis_str: 泡利基空间的一组基底表示(array形式)
            label_str: 泡利基空间对应的一组基底表示(标签形式),形如``[ 'X', 'Y', 'Z', 'I']``
    """
    sigma_x = np.array([[0, 1],  [1, 0]], dtype=np.complex128)
    sigma_y = np.array([[0, -1j], [1j, 0]], dtype=np.complex128)
    sigma_z = np.array([[1, 0],  [0, -1]], dtype=np.complex128)
    sigma_id = np.array([[1, 0],  [0, 1]], dtype=np.complex128)
    pauli = [sigma_x, sigma_y, sigma_z, sigma_id]
    labels = ['X', 'Y', 'Z', 'I']

    num_qubits = n
    num = 1
    if num_qubits > 0:
        basis_str = pauli[:]
        label_str = labels[:]
        pauli_basis = pauli[:]
        palui_label = labels[:]
        while num < num_qubits:
            length = len(basis_str)
            for i in range(4):
                for j in range(length):
                    basis_str.append(np.kron(basis_str[j], pauli_basis[i]))
                    label_str.append(label_str[j] + palui_label[i])
            basis_str = basis_str[-1:-4**(num+1)-1:-1]
            label_str = label_str[-1:-4**(num+1)-1:-1]
            num += 1
        return basis_str, label_str


def decompose(matrix):
    r"""生成 n 量子比特的泡利基空间
    Args:
        matrix (numpy.ndarray): 要分解的矩阵

    Return:
        pauli_form (list): 返回矩阵分解后的哈密顿量,形如 ``[[1, 'Z0, Z1'], [2, 'I']]``
    """
    if np.log2(len(matrix)) % 1 != 0:
        print("Please input correct matrix!")
        return -1
    basis_space, label_str = pauli_basis(np.log2(len(matrix)))
    coefficients = []  # 对应的系数
    pauli_word = []  # 对应的label
    pauli_form = []  # 输出pauli_str list形式:[[1, 'Z0, Z1'], [2, 'I']]
    for i in range(len(basis_space)):
        # 求系数
        a_ij = 1/len(matrix) * np.trace(matrix@basis_space[i])
        if a_ij != 0:
            if a_ij.imag != 0:
                coefficients.append(a_ij)
            else:
                coefficients.append(a_ij.real)
            pauli_word.append(label_str[i])
    for i in range(len(coefficients)):
        pauli_site = []  # 临时存放一个基
        pauli_site.append(coefficients[i])
        word = ''
        for j in range(len(pauli_word[0])):
            if pauli_word[i] == 'I'*int(np.log2(len(matrix))):
                word = 'I'  # 和Hamiltonian类似,若全是I就用一个I指代
                break
            if pauli_word[i][j] == 'I':
                continue   # 如果是I就不加数字下标
            if j != 0 and len(word) != 0:
                word += ','
            word += pauli_word[i][j]
            word += str(j)  # 对每一个label加标签,说明是作用在哪个比特
        pauli_site.append(word)  # 添加上对应作用的门
        pauli_form.append(pauli_site)

    return pauli_form
Y
yangguohao 已提交
1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624

def img_to_density_matrix(img_file):
    r"""将图片编码为密度矩阵
    Args:
        img_file: 图片文件

    Return:
        rho:密度矩阵 ``
    """
    img_matrix = matplotlib.image.imread(img_file)
    
    #将图片转为灰度图
    img_matrix = img_matrix.mean(axis=2)
    
    #填充矩阵,使其变为[2**n,2**n]的矩阵
    length = int(2**np.ceil(np.log2(np.max(img_matrix.shape))))
    img_matrix = np.pad(img_matrix,((0,length-img_matrix.shape[0]),(0,length-img_matrix.shape[1])),'constant')
    #trace为1的密度矩阵
    rho = img_matrix@img_matrix.T
    rho = rho/np.trace(rho)
    return rho