utils.py 58.3 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
Q
Quleaf 已提交
36

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


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

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

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

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

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

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

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

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

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

    return res


Q
Quleaf 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
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 已提交
122
        shape[n] = 2 ** n
Q
Quleaf 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
        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 已提交
139
        identity = paddle.reshape(identity, (2 ** n, 2 ** n))
Q
Quleaf 已提交
140 141 142 143 144 145 146 147 148 149 150

        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 已提交
151 152
def state_fidelity(rho, sigma):
    r"""计算两个量子态的保真度。
Q
Quleaf 已提交
153

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

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

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

    return fidelity


Q
Quleaf 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
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 已提交
190 191 192 193 194 195 196 197 198 199
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 已提交
200 201
        U (numpy.ndarray): 量子门 :math:`U` 的酉矩阵形式
        V (numpy.ndarray): 量子门 :math:`V` 的酉矩阵形式
Q
Quleaf 已提交
202 203 204

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

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


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

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

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

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

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

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

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

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

    .. math::

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

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

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

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


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

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

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

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

    Returns:
        float: 输入的量子态之间的相对熵
Q
Quleaf 已提交
263
    """
Q
Quleaf 已提交
264
    assert rho.shape == sig.shape, 'The shape of two quantum states are different'
Q
Quleaf 已提交
265
    res = np.trace(rho @ logm(rho) - rho @ logm(sig))
Q
Quleaf 已提交
266 267 268 269 270 271 272 273 274 275 276 277
    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 已提交
278
        Tensor: 输入矩阵的Kronecker积
Q
Quleaf 已提交
279 280

    .. code-block:: python
Q
Quleaf 已提交
281
  
Q
Quleaf 已提交
282 283 284 285 286 287 288
        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 已提交
289
    ``result`` 应为 :math:`A \otimes B \otimes C`
Q
Quleaf 已提交
290
    """
Q
Quleaf 已提交
291
    return reduce(lambda result, index: np.kron(result, index), args, np.kron(matrix_A, matrix_B), )
Q
Quleaf 已提交
292 293


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

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

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

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

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

Q
Quleaf 已提交
312
    ::
Q
Quleaf 已提交
313

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

    return matrix_dagger
Q
Quleaf 已提交
321 322


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

Q
Quleaf 已提交
326 327 328
    一个可观测量 :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 已提交
329

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

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

    # Parse pauli_str; 'x0,z1,y4' to 'xziiy'
    new_pauli_str = []
    for coeff, op_str in pauli_str:
Q
Quleaf 已提交
368 369
        init = list('i' * n)
        op_list = re.split(r',\s*', op_str.lower())
Q
Quleaf 已提交
370
        for op in op_list:
Q
Quleaf 已提交
371 372 373 374 375 376
            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 已提交
377 378 379 380 381 382 383
        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 已提交
384
            sub_matrices.append(pauli_dict[op.lower()])
Q
Quleaf 已提交
385 386 387 388 389 390
        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 已提交
391

Q
Quleaf 已提交
392

Q
Quleaf 已提交
393 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
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 已提交
421
    transposed_density_op = np.copy(density_op)
Q
Quleaf 已提交
422 423 424
    if sys_idx == 2:
        for j in [0, 2]:
            for i in [0, 2]:
Q
Quleaf 已提交
425
                transposed_density_op[i:i + 2, j:j + 2] = density_op[i:i + 2, j:j + 2].transpose()
Q
Quleaf 已提交
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
    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 已提交
444
    transposed_density_op = np.copy(density_op)
Q
Quleaf 已提交
445 446 447
    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 已提交
448 449 450

    return transposed_density_op

Q
Quleaf 已提交
451

Q
Quleaf 已提交
452 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
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 已提交
479
    eigen_val, _ = np.linalg.eig(density_op_T)
Q
Quleaf 已提交
480 481
    for val in eigen_val:
        if val < 0:
Q
Quleaf 已提交
482
            n = n + np.abs(val)
Q
Quleaf 已提交
483 484
    return n

Q
Quleaf 已提交
485

Q
Quleaf 已提交
486 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
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 已提交
512
    log2_n = np.log2(2 * n + 1)
Q
Quleaf 已提交
513 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
    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 已提交
544 545 546
    return ppt


Q
Quleaf 已提交
547 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
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 已提交
641 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
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 已提交
750
            list :哈密顿量中每一项的系数,i.e. ``[1.0, 2.0]``
Q
Quleaf 已提交
751 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
        """
        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 已提交
889
        r"""将 pauli_str 分解为系数、泡利字符串的简化形式以及它们分别作用的量子比特下标。
Q
Quleaf 已提交
890 891

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

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

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

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

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

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

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

        Returns:
            np.ndarray: Z 基底下的哈密顿量矩阵形式
        """
        coefs, pauli_words, sites = self.decompose_with_sites()
Y
yangguohao 已提交
925
        n_qubit = 1
Q
Quleaf 已提交
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
        for site in sites:
            if type(site[0]) is int:
                n_qubit = max(n_qubit, max(site) + 1)
        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 已提交
945
    r"""矩阵表示下的自旋算符,可以用来构建哈密顿量矩阵或者自旋可观测量。
Q
Quleaf 已提交
946 947 948

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

        Args:
            size (int): 系统的大小(有几个量子比特)
Q
Quleaf 已提交
953
            use_sparse (bool): 是否使用 sparse matrix 计算,默认为 ``False``
Q
Quleaf 已提交
954 955 956
        """
        self.size = size
        self.id = sparse.eye(2, dtype='complex128')
Q
Quleaf 已提交
957 958 959 960 961 962
        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 已提交
963 964
        self.__sparse = use_sparse
        for i in range(self.size):
Q
Quleaf 已提交
965 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
            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 已提交
995 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

    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 已提交
1029 1030 1031 1032 1033 1034
        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 已提交
1035 1036 1037 1038
    """

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


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

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

Q
Quleaf 已提交
1070
    Returns:
Q
Quleaf 已提交
1071
        bloch_vector (numpy.ndarray): 存储bloch向量的 x,y,z 坐标,向量的模长,向量的颜色
Q
Quleaf 已提交
1072 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
    """

    # 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 已提交
1108 1109
        view_dist=None,
        set_color=None
Q
Quleaf 已提交
1110 1111 1112 1113 1114
):
    r"""将 Bloch 向量展示在 Bloch 球面上

    Args:
        ax (Axes3D(fig)): 画布的句柄
Q
Quleaf 已提交
1115 1116 1117
        bloch_vectors (numpy.ndarray): 存储bloch向量的 x,y,z 坐标,向量的模长,向量的颜色
        show_arrow (bool): 是否展示向量的箭头,默认为 False
        clear_plt (bool): 是否要清空画布,默认为 True,每次画图的时候清空画布再画图
Q
Quleaf 已提交
1118
        rotating_angle_list (list): 旋转角度的列表,用于展示旋转轨迹
Q
Quleaf 已提交
1119 1120 1121 1122
        view_angle (list): 视图的角度,
            第一个元素为关于xy平面的夹角[0-360],第二个元素为关于xz平面的夹角[0-360], 默认为 (30, 45)
        view_dist (int): 视图的距离,默认为 7
        set_color (str): 设置指定的颜色,请查阅cmap表,默认为 "红-黑-根据向量的模长渐变" 颜色方案
Q
Quleaf 已提交
1123 1124 1125
    """
    # Assign a value to an empty variable
    if view_angle is None:
Q
Quleaf 已提交
1126
        view_angle = (30, 45)
Q
Quleaf 已提交
1127 1128 1129
    if view_dist is None:
        view_dist = 7
    # Define my_color
Q
Quleaf 已提交
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
    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 已提交
1144 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

    # 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 已提交
1254
            bloch_vectors[:, 0], bloch_vectors[:, 1], bloch_vectors[:, 2], c=color, alpha=1.0
Q
Quleaf 已提交
1255 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
        )

    # 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 已提交
1281
            arrow_length_ratio=0.05, color=color, alpha=1.0
Q
Quleaf 已提交
1282
        )
Y
yangguohao 已提交
1283
        
Q
Quleaf 已提交
1284

Y
yangguohao 已提交
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
def plot_n_qubit_state_in_bloch_sphere(
        state,
        show_qubits=None,
        **args
):
    r"""将输入的多量子比特的量子态展示在 Bloch 球面上

    Args:
        state (list(numpy.ndarray or paddle.Tensor)): 输入的量子态列表,可以支持态矢量和密度矩阵,
        该函数下,列表内每一个量子态对应一张单独的图片
        show_qubits(list(list)):若为多量子比特,则给出要展示的量子比特,默认为 None,表示全展示
        show_arrow (bool): 是否展示向量的箭头,默认为 ``False``
        save_gif (bool): 是否存储 gif 动图,默认为 ``False``
        filename (str): 存储的 gif 动图的名字
        view_angle (list or tuple): 视图的角度,
            第一个元素为关于 xy 平面的夹角 [0-360],第二个元素为关于 xz 平面的夹角 [0-360], 默认为 ``(30, 45)``
        view_dist (int): 视图的距离,默认为 7
        set_color (str): 若要设置指定的颜色,请查阅 ``cmap`` 表。默认为红色到黑色的渐变颜色
    """
    assert type(state) == list or type(state) == paddle.Tensor or type(state) == np.ndarray, \
        'the type of "state" must be "list" or "paddle.Tensor" or "np.ndarray".'
    if type(state) == paddle.Tensor or type(state) == np.ndarray:
        state = [state]
    state_len = len(state)
    assert state_len >= 1, '"state" is NULL.'
    for i in range(state_len):
        assert type(state[i]) == paddle.Tensor or type(state[i]) == np.ndarray, \
            'the type of "state[i]" should be "paddle.Tensor" or "numpy.ndarray".'

Y
yangguohao 已提交
1314 1315 1316 1317 1318 1319 1320
    if show_qubits is None:
        show_qubits = [None]*state_len
    else:
        assert len(show_qubits)==state_len,'show_qubits大小需要和state相同'
        for i in range(state_len):
            assert type(show_qubits[i])==list,'the type of show_qubits should be None or list'
            assert 0<len(show_qubits[i])<=state[i].shape[0], '要展示的量子比特数目错误'
Y
yangguohao 已提交
1321 1322
    
    for i in range(state_len):
Y
yangguohao 已提交
1323
        #若为多量子态
Y
yangguohao 已提交
1324 1325 1326
        if state[i].shape[0]>2:
            s = []
            if show_qubits[i] is None:
Y
yangguohao 已提交
1327
                qubits_list = list(range(int(np.log2(state[i].shape[0]))))
Y
yangguohao 已提交
1328 1329 1330 1331 1332
            else:
                qubits_list = show_qubits[i]
            rho = paddle.to_tensor(state[i])
            for q in qubits_list:
                s.append(partial_trace_discontiguous(rho,[q]))
Y
yangguohao 已提交
1333 1334
            #多量子态的子图的箭头向量改为蓝色
            plot_state_in_bloch_sphere(s,n_qubit=len(qubits_list),set_color='#0000FF',**args)
Y
yangguohao 已提交
1335 1336
        else:
            plot_state_in_bloch_sphere(state[i],**args)
Q
Quleaf 已提交
1337 1338 1339

def plot_state_in_bloch_sphere(
        state,
Y
yangguohao 已提交
1340
        qubits_list=None,
Q
Quleaf 已提交
1341 1342 1343 1344
        show_arrow=False,
        save_gif=False,
        filename=None,
        view_angle=None,
Q
Quleaf 已提交
1345 1346
        view_dist=None,
        set_color=None
Q
Quleaf 已提交
1347 1348 1349 1350 1351
):
    r"""将输入的量子态展示在 Bloch 球面上

    Args:
        state (list(numpy.ndarray or paddle.Tensor)): 输入的量子态列表,可以支持态矢量和密度矩阵
Y
yangguohao 已提交
1352
        n_qubit (int): 若为多量子态,则为需要展示的量子比特的数目
Q
Quleaf 已提交
1353 1354 1355
        show_arrow (bool): 是否展示向量的箭头,默认为 ``False``
        save_gif (bool): 是否存储 gif 动图,默认为 ``False``
        filename (str): 存储的 gif 动图的名字
Q
Quleaf 已提交
1356 1357
        view_angle (list or tuple): 视图的角度,
            第一个元素为关于 xy 平面的夹角 [0-360],第二个元素为关于 xz 平面的夹角 [0-360], 默认为 ``(30, 45)``
Q
Quleaf 已提交
1358
        view_dist (int): 视图的距离,默认为 7
Q
Quleaf 已提交
1359
        set_color (str): 若要设置指定的颜色,请查阅 ``cmap`` 表。默认为红色到黑色的渐变颜色
Q
Quleaf 已提交
1360 1361 1362 1363 1364
    """
    # 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 已提交
1365
        'the type of "state" must be "list" or "paddle.Tensor" or "np.ndarray".'
Q
Quleaf 已提交
1366 1367 1368
    if type(state) == paddle.Tensor or type(state) == np.ndarray:
        state = [state]
    state_len = len(state)
Q
Quleaf 已提交
1369
    assert state_len >= 1, '"state" is NULL.'
Q
Quleaf 已提交
1370 1371
    for i in range(state_len):
        assert type(state[i]) == paddle.Tensor or type(state[i]) == np.ndarray, \
Q
Quleaf 已提交
1372 1373 1374 1375
            '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 已提交
1376 1377 1378 1379 1380

    # Assign a value to an empty variable
    if filename is None:
        filename = 'state_in_bloch_sphere.gif'
    if view_angle is None:
Q
Quleaf 已提交
1381
        view_angle = (30, 45)
Q
Quleaf 已提交
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
    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 已提交
1408 1409 1410 1411 1412
        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 已提交
1413 1414 1415

    # Dynamic update and save
    if save_gif:
Q
Quleaf 已提交
1416 1417 1418 1419 1420 1421
        # 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 已提交
1422
        anim = animation.FuncAnimation(fig, update, frames=frames_num, interval=600, repeat=False)
Q
Quleaf 已提交
1423 1424 1425 1426 1427 1428 1429
        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)
Y
yangguohao 已提交
1430 1431 1432 1433
    
    if qubits_list is None:#若为单量子态
        ax = fig.add_subplot(111, projection='3d')
        __plot_bloch_sphere(
Q
Quleaf 已提交
1434 1435
        ax, bloch_vectors, show_arrow, clear_plt=True,
        view_angle=view_angle, view_dist=view_dist, set_color=set_color
Y
yangguohao 已提交
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
        )
    else: #若为多量子态
        dim = np.ceil(sqrt(n_qubit))
        for i in range(1,n_qubit+1):
            ax = fig.add_subplot(dim,dim,i,projection='3d')
            bloch_vector=np.array([bloch_vectors[i-1]])
            __plot_bloch_sphere(
            ax, bloch_vector, show_arrow, clear_plt=True,
            view_angle=view_angle, view_dist=view_dist, set_color=set_color
            )
            
Q
Quleaf 已提交
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
    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 已提交
1457 1458
        view_dist=None,
        color_scheme=None,
Q
Quleaf 已提交
1459 1460 1461 1462 1463 1464 1465 1466 1467
):
    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 已提交
1468 1469
        view_angle (list or tuple): 视图的角度,
            第一个元素为关于 xy 平面的夹角 [0-360],第二个元素为关于 xz 平面的夹角 [0-360], 默认为 ``(30, 45)``
Q
Quleaf 已提交
1470
        view_dist (int): 视图的距离,默认为 7
Q
Quleaf 已提交
1471
        color_scheme (list(str,str,str)): 分别是初始颜色,轨迹颜色,结束颜色。若要设置指定的颜色,请查阅 ``cmap`` 表。默认为红色
Q
Quleaf 已提交
1472 1473 1474 1475 1476 1477 1478
    """
    # 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 已提交
1479
        'the type of rotating_angle should be "tuple" or "list".'
Q
Quleaf 已提交
1480 1481 1482 1483 1484
    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 已提交
1485 1486 1487 1488 1489 1490 1491
    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 已提交
1492 1493 1494 1495

    # Assign a value to an empty variable
    if filename is None:
        filename = 'rotation_in_bloch_sphere.gif'
Q
Quleaf 已提交
1496 1497 1498 1499 1500 1501 1502

    # 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 已提交
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540

    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 已提交
1541 1542
        frame = frame + 2
        if frame <= len(bloch_vectors) - 1:
Q
Quleaf 已提交
1543
            __plot_bloch_sphere(
Q
Quleaf 已提交
1544
                ax, bloch_vectors[1:frame], show_arrow=show_arrow, clear_plt=True,
Q
Quleaf 已提交
1545
                rotating_angle_list=rotating_angle_list,
Q
Quleaf 已提交
1546
                view_angle=view_angle, view_dist=view_dist, set_color=set_trac_color
Q
Quleaf 已提交
1547 1548 1549 1550 1551
            )

            # The starting and ending bloch vector has to be shown
            # show starting vector
            __plot_bloch_sphere(
Q
Quleaf 已提交
1552 1553 1554 1555 1556 1557 1558 1559 1560
                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 已提交
1561 1562 1563
            )

    if save_gif:
Q
Quleaf 已提交
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
        # 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 已提交
1586 1587

    plt.show()
Q
Quleaf 已提交
1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667


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