qinfo.py 25.2 KB
Newer Older
Q
Quleaf 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
# !/usr/bin/env python3
# Copyright (c) 2021 Institute for Quantum Computing, Baidu Inc. All Rights Reserved.
#
# 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.

r"""
The function for quantum information.
"""

import paddle_quantum
import math
import re
import numpy as np
from scipy.linalg import logm, sqrtm
import paddle
Q
Quleaf 已提交
26 27 28 29 30 31
from paddle import kron, matmul, transpose
from .state import State
from .base import get_dtype
from .intrinsic import _get_float_dtype
from .linalg import dagger, is_unitary, NKron
from .backend import Backend
Q
Quleaf 已提交
32
import matplotlib.image
Q
Quleaf 已提交
33
from typing import Optional, Tuple, List, Union
Q
Quleaf 已提交
34 35


Q
Quleaf 已提交
36 37
def partial_trace(state: Union[State, paddle.Tensor], 
                  dim1: int, dim2: int, A_or_B: int) -> Union[State, paddle.Tensor]:
Q
Quleaf 已提交
38 39 40
    r"""Calculate the partial trace of the quantum state.

    Args:
Q
Quleaf 已提交
41
        state: Input quantum state.
Q
Quleaf 已提交
42 43 44 45 46 47 48 49 50 51
        dim1: The dimension of system A.
        dim2: The dimension of system B.
        A_or_B: 1 or 2. 1 means to calculate partial trace on system A; 2 means to calculate partial trace on system B.

    Returns:
        Partial trace of the input quantum state.
    """
    if A_or_B == 2:
        dim1, dim2 = dim2, dim1
    
Q
Quleaf 已提交
52 53 54 55 56 57 58 59
    is_State = False
    if isinstance(state, State):
        is_State = True
        backend = state.backend
        rho_AB = state.data if backend != Backend.StateVector else state.ket @ state.bra
    else:
        rho_AB = state
    complex_dtype = rho_AB.dtype
Q
Quleaf 已提交
60

Q
Quleaf 已提交
61 62
    idty_B = paddle.eye(dim2).cast(complex_dtype)
    res = paddle.zeros([dim2, dim2]).cast(complex_dtype)
Q
Quleaf 已提交
63 64 65 66 67 68 69 70 71 72 73

    for dim_j in range(dim1):
        row_top = paddle.zeros([1, dim_j])
        row_mid = paddle.ones([1, 1])
        row_bot = paddle.zeros([1, dim1 - dim_j - 1])
        bra_j = paddle.concat([row_top, row_mid, row_bot], axis=1)
        bra_j = paddle.cast(bra_j, complex_dtype)

        if A_or_B == 1:
            row_tmp = kron(bra_j, idty_B)
            row_tmp_conj = paddle.conj(row_tmp)
Q
Quleaf 已提交
74
            res += (row_tmp @ rho_AB) @ paddle.transpose(row_tmp_conj, perm=[1, 0])
Q
Quleaf 已提交
75 76 77 78

        if A_or_B == 2:
            row_tmp = kron(idty_B, bra_j)
            row_tmp_conj = paddle.conj(row_tmp)
Q
Quleaf 已提交
79 80 81 82 83 84 85
            res += (row_tmp @ rho_AB) @ paddle.transpose(row_tmp_conj, perm=[1, 0])
    
    if is_State:
        if backend == Backend.StateVector:
            eigval, eigvec = paddle.linalg.eig(res)
            res = eigvec[:, paddle.argmax(paddle.real(eigval))]
        return State(res, backend=backend)
Q
Quleaf 已提交
86 87 88
    return res


Q
Quleaf 已提交
89 90
def partial_trace_discontiguous(state: Union[State, paddle.Tensor], 
                                preserve_qubits: list=None) -> Union[State, paddle.Tensor]:
Q
Quleaf 已提交
91 92 93
    r"""Calculate the partial trace of the quantum state with arbitrarily selected subsystem

    Args:
Q
Quleaf 已提交
94
        state: Input quantum state.
Q
Quleaf 已提交
95 96 97 98 99
        preserve_qubits: Remaining qubits, default is None, indicate all qubits remain.

    Returns:
        Partial trace of the quantum state with arbitrarily selected subsystem.
    """
Q
Quleaf 已提交
100 101 102 103 104 105 106 107
    is_State = False
    if isinstance(state, State):
        is_State = True
        backend = rho.backend
        rho = state.data if backend != Backend.StateVector else state.ket @ state.bra
    else:
        rho = state
    complex_dtype = rho.dtype
Q
Quleaf 已提交
108 109 110

    if preserve_qubits is None:
        return rho
Q
Quleaf 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    
    n = int(math.log2(rho.size) // 2)
    num_preserve = len(preserve_qubits)

    shape = paddle.ones((n + 1,))
    shape = 2 * shape
    shape[n] = 2 ** n
    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)
    identity = paddle.reshape(identity, (2 ** n, 2 ** n))

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

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

    if is_State:
        if backend == Backend.StateVector:
            eigval, eigvec = paddle.linalg.eig(result)
            result = eigvec[:, paddle.argmax(paddle.real(eigval))]
        return State(result, backend=backend)
    return result


def trace_distance(rho: Union[State, paddle.Tensor], sigma: Union[State, paddle.Tensor]) -> paddle.Tensor:
    r"""Calculate the fidelity of two quantum states.
Q
Quleaf 已提交
153 154 155 156 157 158 159 160 161

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

    Args:
        rho: Density matrix form of the quantum state.
        sigma: Density matrix form of the quantum state.

    Returns:
Q
Quleaf 已提交
162
        The fidelity between the input quantum states.
Q
Quleaf 已提交
163
    """
Q
Quleaf 已提交
164 165 166 167 168 169
    if isinstance(rho, State):
        rho = rho.data
        sigma = sigma.data
    assert rho.shape == sigma.shape, 'The shape of two quantum states are different'
    eigval, eigvec = paddle.linalg.eig(rho - sigma)
    return 0.5 * paddle.sum(paddle.abs(eigval))
Q
Quleaf 已提交
170 171


Q
Quleaf 已提交
172
def state_fidelity(rho: Union[State, paddle.Tensor], sigma: Union[State, paddle.Tensor]) -> paddle.Tensor:
Q
Quleaf 已提交
173 174 175 176 177 178 179 180 181 182 183 184
    r"""Calculate the fidelity of two quantum states.

    .. math::
        F(\rho, \sigma) = \text{tr}(\sqrt{\sqrt{\rho}\sigma\sqrt{\rho}})

    Args:
        rho: Density matrix form of the quantum state.
        sigma: Density matrix form of the quantum state.

    Returns:
        The fidelity between the input quantum states.
    """
Q
Quleaf 已提交
185 186 187 188 189 190
    
    if isinstance(rho, State):
        rho = rho.data
        sigma = sigma.data
    rho = rho.numpy()
    sigma = sigma.numpy()
Q
Quleaf 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
    assert rho.shape == sigma.shape, 'The shape of two quantum states are different'
    fidelity = np.trace(sqrtm(sqrtm(rho) @ sigma @ sqrtm(rho))).real

    return paddle.to_tensor(fidelity)


def gate_fidelity(U: paddle.Tensor, V: paddle.Tensor) -> paddle.Tensor:
    r"""calculate the fidelity between gates

    .. math::

        F(U, V) = |\text{tr}(UV^\dagger)|/2^n

    :math:`U` is a :math:`2^n\times 2^n` unitary gate

    Args:
        U: quantum gate :math:`U`  in matrix form
        V: quantum gate :math:`V`  in matrix form

    Returns:
        fidelity between gates
    
    """
Q
Quleaf 已提交
214 215 216
    complex_dtype = U.dtype
    V = paddle.cast(V, dtype=complex_dtype)
    assert U.shape == V.shape, 'The shape of two matrices are different'
Q
Quleaf 已提交
217 218 219 220 221
    
    fidelity = paddle.abs(paddle.trace(U @ dagger(V))) / U.shape[0]
    return fidelity


Q
Quleaf 已提交
222
def purity(rho: Union[State, paddle.Tensor]) -> paddle.Tensor:
Q
Quleaf 已提交
223 224 225 226 227 228 229 230 231 232 233 234
    r"""Calculate the purity of a quantum state.

    .. math::

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

    Args:
        rho: Density matrix form of the quantum state.

    Returns:
        The purity of the input quantum state.
    """
Q
Quleaf 已提交
235 236
    if isinstance(rho, State):
        rho = rho.data
Q
Quleaf 已提交
237 238 239 240 241
    gamma = paddle.trace(paddle.matmul(rho, rho))

    return gamma.real()


Q
Quleaf 已提交
242
def von_neumann_entropy(rho: Union[State, paddle.Tensor]) -> paddle.Tensor:
Q
Quleaf 已提交
243 244 245 246 247 248 249 250 251 252 253 254
    r"""Calculate the von Neumann entropy of a quantum state.

    .. math::

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

    Args:
        rho: Density matrix form of the quantum state.

    Returns:
        The von Neumann entropy of the input quantum state.
    """
Q
Quleaf 已提交
255 256 257
    if isinstance(rho, State):
        rho = rho.data
    rho = rho.numpy()
Q
Quleaf 已提交
258 259 260 261 262 263 264 265 266 267
    rho_eigenvalues = np.real(np.linalg.eigvals(rho))
    entropy = 0
    for eigenvalue in rho_eigenvalues:
        if np.abs(eigenvalue) < 1e-8:
            continue
        entropy -= eigenvalue * np.log(eigenvalue)

    return paddle.to_tensor(entropy)


Q
Quleaf 已提交
268
def relative_entropy(rho: Union[State, paddle.Tensor], sig: Union[State, paddle.Tensor]) -> paddle.Tensor:
Q
Quleaf 已提交
269 270 271 272 273 274 275 276 277 278 279 280 281 282
    r"""Calculate the relative entropy of two quantum states.

    .. math::

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


    Args:
        rho: Density matrix form of the quantum state.
        sig: Density matrix form of the quantum state.

    Returns:
        Relative entropy between input quantum states.
    """
Q
Quleaf 已提交
283 284 285 286 287
    if isinstance(rho, State):
        rho = rho.data
        sig = sig.data
    rho = rho.numpy() 
    sig = sig.numpy()
Q
Quleaf 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
    assert rho.shape == sig.shape, 'The shape of two quantum states are different'
    res = np.trace(rho @ logm(rho) - rho @ logm(sig))
    return paddle.to_tensor(res.real)


def random_pauli_str_generator(n: int, terms: Optional[int] = 3) -> List:
    r"""Generate a random observable in list form.

    An observable :math:`O=0.3X\otimes I\otimes I+0.5Y\otimes I\otimes Z`'s list form is
    ``[[0.3, 'x0'], [0.5, 'y0,z2']]``.  Such an observable is generated by 
    ``random_pauli_str_generator(3, terms=2)`` 

    Args:
        n: Number of qubits.
        terms: Number of terms in the observable. Defaults to 3.

    Returns:
Q
Quleaf 已提交
305
        The randomly generated observable's list form.
Q
Quleaf 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
    """
    pauli_str = []
    for sublen in np.random.randint(1, high=n + 1, size=terms):
        # Tips: -1 <= coeff < 1
        coeff = np.random.rand() * 2 - 1
        ops = np.random.choice(['x', 'y', 'z'], size=sublen)
        pos = np.random.choice(range(n), size=sublen, replace=False)
        op_list = [ops[i] + str(pos[i]) for i in range(sublen)]
        pauli_str.append([coeff, ','.join(op_list)])
    return pauli_str


def pauli_str_to_matrix(pauli_str: list, n: int) -> paddle.Tensor:
    r"""Convert the input list form of an observable to its matrix form.

    For example, if the input ``pauli_str`` is ``[[0.7, 'z0,x1'], [0.2, 'z1']]`` and ``n=3``,
    then this function returns the observable :math:`0.7Z\otimes X\otimes I+0.2I\otimes Z\otimes I`
    in matrix form.

    Args:
        pauli_str: A list form of an observable.
        n: Number of qubits.

    Raises:
        ValueError: Only Pauli operator "I" can be accepted without specifying its position.

    Returns:
        The matrix form of the input observable.
    """
    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}

    # Parse pauli_str; 'x0,z1,y4' to 'xziiy'
    new_pauli_str = []
    for coeff, op_str in pauli_str:
        init = list('i' * n)
        op_list = re.split(r',\s*', op_str.lower())
        for op in op_list:
            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')
        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:
            sub_matrices.append(pauli_dict[op.lower()])
        if len(op_str) == 1:
            matrices.append(coeff * sub_matrices[0])
        else:
Q
Quleaf 已提交
361 362 363 364
            mat = sub_matrices[0]
            for idx in range(1, len(sub_matrices)):
                mat = np.kron(mat, sub_matrices[idx])
            matrices.append(coeff * mat)
Q
Quleaf 已提交
365

Q
Quleaf 已提交
366
    return paddle.to_tensor(sum(matrices), dtype=get_dtype())
Q
Quleaf 已提交
367 368


Q
Quleaf 已提交
369
def partial_transpose_2(density_op: Union[State, paddle.Tensor], sub_system: int = None) -> paddle.Tensor:
Q
Quleaf 已提交
370 371 372 373 374 375 376 377 378 379 380 381
    r"""Calculate the partial transpose :math:`\rho^{T_A}` of the input quantum state.

    Args:
        density_op: Density matrix form of the quantum state.
        sub_system: 1 or 2. 1 means to perform partial transpose on system A; 2 means to perform partial trace on system B. Default is 2.

    Returns:
        The partial transpose of the input quantum state.
    """
    sys_idx = 2 if sub_system is None else 1

    # Copy the density matrix and not corrupt the original one
Q
Quleaf 已提交
382 383 384 385
    if isinstance(density_op, State):
        density_op = density_op.data
    complex_dtype = density_op.dtype
    density_op = density_op.numpy()
Q
Quleaf 已提交
386 387 388 389 390 391 392 393 394
    transposed_density_op = np.copy(density_op)
    if sys_idx == 2:
        for j in [0, 2]:
            for i in [0, 2]:
                transposed_density_op[i:i + 2, j:j + 2] = density_op[i:i + 2, j:j + 2].transpose()
    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]

Q
Quleaf 已提交
395
    return paddle.to_tensor(transposed_density_op, dtype=complex_dtype)
Q
Quleaf 已提交
396 397


Q
Quleaf 已提交
398
def partial_transpose(density_op: Union[State, paddle.Tensor], n: int) -> paddle.Tensor:
Q
Quleaf 已提交
399 400 401 402 403 404 405 406 407 408
    r"""Calculate the partial transpose :math:`\rho^{T_A}` of the input quantum state.

    Args:
        density_op: Density matrix form of the quantum state.
        n: Number of qubits of the system to be transposed.

    Returns:
        The partial transpose of the input quantum state.
    """
    # Copy the density matrix and not corrupt the original one
Q
Quleaf 已提交
409 410 411 412
    if isinstance(density_op, State):
        density_op = density_op.data
    complex_dtype = density_op.dtype
    density_op = density_op.numpy()
Q
Quleaf 已提交
413 414 415 416 417
    transposed_density_op = np.copy(density_op)
    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 已提交
418
    return paddle.to_tensor(transposed_density_op, dtype=complex_dtype)
Q
Quleaf 已提交
419 420


Q
Quleaf 已提交
421
def negativity(density_op: Union[State, paddle.Tensor]) -> paddle.Tensor:
Q
Quleaf 已提交
422 423 424 425 426 427 428 429 430 431
    r"""Compute the Negativity :math:`N = ||\frac{\rho^{T_A}-1}{2}||` of the input quantum state.

    Args:
        density_op: Density matrix form of the quantum state.

    Returns:
        The Negativity of the input quantum state.
    """
    # Implement the partial transpose
    density_op_T = partial_transpose_2(density_op)
Q
Quleaf 已提交
432 433 434
    if isinstance(density_op_T, State):
        density_op_T = density_op_T.data
    density_op_T = density_op_T.numpy()
Q
Quleaf 已提交
435 436 437 438 439 440 441

    # Calculate through the equivalent expression N = sum(abs(\lambda_i)) when \lambda_i<0
    n = 0.0
    eigen_val, _ = np.linalg.eig(density_op_T)
    for val in eigen_val:
        if val < 0:
            n = n + np.abs(val)
Q
Quleaf 已提交
442
    return paddle.to_tensor(n, dtype=_get_float_dtype(paddle_quantum.get_dtype()))
Q
Quleaf 已提交
443 444


Q
Quleaf 已提交
445
def logarithmic_negativity(density_op: Union[State, paddle.Tensor]) -> paddle.Tensor:
Q
Quleaf 已提交
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
    r"""Calculate the Logarithmic Negativity :math:`E_N = ||\rho^{T_A}||` of the input quantum state.

    Args:
        density_op: Density matrix form of the quantum state.

    Returns:
        The Logarithmic Negativity of the input quantum state.
    """
    # Calculate the negativity
    n = negativity(density_op)

    # Calculate through the equivalent expression
    log2_n = paddle.log2(2 * n + 1)
    return log2_n


Q
Quleaf 已提交
462
def is_ppt(density_op: Union[State, paddle.Tensor]) -> bool:
Q
Quleaf 已提交
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
    r"""Check if the input quantum state is PPT.

    Args:
        density_op: Density matrix form of the quantum state.

    Returns:
        Whether the input quantum state is PPT.
    """
    # 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
    return ppt


Q
Quleaf 已提交
480 481
def schmidt_decompose(psi: Union[State, paddle.Tensor], 
                      sys_A: List[int]=None) -> Tuple[paddle.Tensor, paddle.Tensor, paddle.Tensor]:
Q
Quleaf 已提交
482 483 484
    r"""Calculate the Schmidt decomposition of a quantum state :math:`\lvert\psi\rangle=\sum_ic_i\lvert i_A\rangle\otimes\lvert i_B \rangle`.

    Args:
Q
Quleaf 已提交
485
        psi: State vector form of the quantum state, with shape (2**n)
Q
Quleaf 已提交
486 487 488 489 490 491 492 493 494
        sys_A: Qubit indices to be included in subsystem A (other qubits are included in subsystem B), default are the first half qubits of :math:`\lvert \psi\rangle`

    Returns:
        contains elements

        * A one dimensional array composed of Schmidt coefficients, with shape ``(k)``
        * A high dimensional array composed of bases for subsystem A :math:`\lvert i_A\rangle`, with shape ``(k, 2**m, 1)``
        * A high dimensional array composed of bases for subsystem B :math:`\lvert i_B\rangle` , with shape ``(k, 2**m, 1)``
    """
Q
Quleaf 已提交
495 496 497 498
    if isinstance(psi, State):
        psi = psi.data
    psi = psi.numpy()
    complex_dtype = psi.dtype
Q
Quleaf 已提交
499 500 501 502 503 504 505 506 507 508 509
    assert psi.ndim == 1, 'Psi must be a one dimensional vector.'
    assert np.log2(psi.size).is_integer(), 'The number of amplitutes must be an integral power of 2.'


    tot_qu = int(np.log2(psi.size))
    sys_A = sys_A if sys_A is not None else [i for i in range(tot_qu//2)]
    sys_B = [i for i in range(tot_qu) if i not in sys_A]

    # Permute qubit indices
    psi = psi.reshape([2] * tot_qu).transpose(sys_A + sys_B)

Q
Quleaf 已提交
510
    # construct amplitude matrix
Q
Quleaf 已提交
511 512 513 514 515 516
    amp_mtr = psi.reshape([2**len(sys_A), 2**len(sys_B)])

    # Standard process to obtain schmidt decomposition
    u, c, v = np.linalg.svd(amp_mtr)

    k = np.count_nonzero(c > 1e-13)
Q
Quleaf 已提交
517 518 519 520
    c = paddle.to_tensor(c[:k], dtype=complex_dtype)
    u = paddle.to_tensor(u.T[:k].reshape([k, -1, 1]), dtype=complex_dtype)
    v = paddle.to_tensor(v[:k].reshape([k, -1, 1]), dtype=complex_dtype)
    return c, u, v
Q
Quleaf 已提交
521 522


Q
Quleaf 已提交
523
def image_to_density_matrix(image_filepath: str) -> State:
Q
Quleaf 已提交
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
    r"""Encode image to density matrix

    Args:
        image_filepath: Path to the image file.

    Returns:
        The density matrix obtained by encoding
    """
    image_matrix = matplotlib.image.imread(image_filepath)

    # Converting images to grayscale
    image_matrix = image_matrix.mean(axis=2)

    # Fill the matrix so that it becomes a matrix whose shape is [2**n,2**n]
    length = int(2**np.ceil(np.log2(np.max(image_matrix.shape))))
    image_matrix = np.pad(image_matrix, ((0, length-image_matrix.shape[0]), (0, length-image_matrix.shape[1])), 'constant')
    # Density matrix whose trace  is 1
    rho = image_matrix@image_matrix.T
    rho = rho/np.trace(rho)
Q
Quleaf 已提交
543
    return State(paddle.to_tensor(rho), backend=paddle_quantum.Backend.DensityMatrix)
Q
Quleaf 已提交
544 545


Q
Quleaf 已提交
546
def shadow_trace(state: 'State', hamiltonian: paddle_quantum.Hamiltonian, 
Q
Quleaf 已提交
547 548 549 550 551 552 553 554
                 sample_shots: int, method: Optional[str] = 'CS') -> float:
    r"""Estimate the expectation value :math:`\text{trace}(H\rho)`  of an observable :math:`H`.

    Args:
        state: Quantum state.
        hamiltonian: Observable.
        sample_shots: Number of samples.
        method: Method used to , which should be one of “CS”, “LBCS”, and “APS”. Default is “CS”.
Q
Quleaf 已提交
555 556 557

    Raises:
        ValueError: Hamiltonian has a bad form
Q
Quleaf 已提交
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 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
    
    Returns:
        The estimated expectation value for the hamiltonian.
    """
    if not isinstance(hamiltonian, list):
        hamiltonian = hamiltonian.pauli_str
    num_qubits = state.num_qubits
    mode = state.backend
    if method == "LBCS":
        result, beta = paddle_quantum.shadow.shadow_sample(state, num_qubits, sample_shots, mode, hamiltonian, method)
    else:
        result = paddle_quantum.shadow.shadow_sample(state, num_qubits, sample_shots, mode, hamiltonian, method)

    def prepare_hamiltonian(hamiltonian, num_qubits):
        new_hamiltonian = list()
        for idx, (coeff, pauli_str) in enumerate(hamiltonian):
            pauli_str = re.split(r',\s*', pauli_str.lower())
            pauli_term = ['i'] * num_qubits
            for item in pauli_str:
                if len(item) > 1:
                    pauli_term[int(item[1:])] = item[0]
                elif item[0].lower() != 'i':
                    raise ValueError('Expecting I for ', item[0])
            new_term = [coeff, ''.join(pauli_term)]
            new_hamiltonian.append(new_term)
        return new_hamiltonian

    hamiltonian = prepare_hamiltonian(hamiltonian, num_qubits)

    sample_pauli_str = [item for item, _ in result]
    sample_measurement_result = [item for _, item in result]
    coeff_terms = []
    pauli_terms = []
    for coeff, pauli_term in hamiltonian:
        coeff_terms.append(coeff)
        pauli_terms.append(pauli_term)

    pauli2idx = {'x': 0, 'y': 1, 'z': 2}

    def estimated_weight_cs(sample_pauli_str, pauli_term):
        result = 1
        for i in range(num_qubits):
            if sample_pauli_str[i] == 'i' or pauli_term[i] == 'i':
                continue
            elif sample_pauli_str[i] == pauli_term[i]:
                result *= 3
            else:
                result = 0
        return result

    def estimated_weight_lbcs(sample_pauli_str, pauli_term, beta):
        # beta is 2-d, and the shape looks like (len, 3)
        assert len(sample_pauli_str) == len(pauli_term)
        result = 1
        for i in range(num_qubits):
            # The probability distribution is different at each qubit
            score = 0
            idx = pauli2idx[sample_pauli_str[i]]
            if sample_pauli_str[i] == 'i' or pauli_term[i] == 'i':
                score = 1
            elif sample_pauli_str[i] == pauli_term[i] and beta[i][idx] != 0:
                score = 1 / beta[i][idx]
            result *= score
        return result

    def estimated_value(pauli_term, measurement_result):
        value = 1
        for idx in range(num_qubits):
            if pauli_term[idx] != 'i' and measurement_result[idx] == '1':
                value *= -1
        return value

    # Define the functions required by APS
    def is_covered(pauli, pauli_str):
        for qubit_idx in range(num_qubits):
            if not pauli[qubit_idx] in ('i', pauli_str[qubit_idx]):
                return False
        return True

    def update_pauli_estimator(hamiltonian, pauli_estimator, pauli_str, measurement_result):
        for coeff, pauli_term in hamiltonian:
            last_estimator = pauli_estimator[pauli_term]['value'][-1]
            if is_covered(pauli_term, pauli_str):
                value = estimated_value(pauli_term, measurement_result)
                chose_number = pauli_estimator[pauli_term]['times']
                new_estimator = 1 / (chose_number + 1) * (chose_number * last_estimator + value)
                pauli_estimator[pauli_term]['times'] += 1
                pauli_estimator[pauli_term]['value'].append(new_estimator)
            else:
                pauli_estimator[pauli_term]['value'].append(last_estimator)

    trace_estimation = 0
    if method == "CS":
        for sample_idx in range(sample_shots):
            estimation = 0
            for i in range(len(pauli_terms)):
                value = estimated_value(pauli_terms[i], sample_measurement_result[sample_idx])
                weight = estimated_weight_cs(sample_pauli_str[sample_idx], pauli_terms[i])
                estimation += coeff_terms[i] * weight * value
            trace_estimation += estimation
        trace_estimation /= sample_shots
    elif method == "LBCS":
        for sample_idx in range(sample_shots):
            estimation = 0
            for i in range(len(pauli_terms)):
                value = estimated_value(pauli_terms[i], sample_measurement_result[sample_idx])
                weight = estimated_weight_lbcs(sample_pauli_str[sample_idx], pauli_terms[i], beta)
                estimation += coeff_terms[i] * weight * value
            trace_estimation += estimation
        trace_estimation /= sample_shots
    elif method == "APS":
        # Create a search dictionary for easy storage
        pauli_estimator = {}
        for coeff, pauli_term in hamiltonian:
            pauli_estimator[pauli_term] = {'times': 0, 'value': [0]}
        for sample_idx in range(sample_shots):
            update_pauli_estimator(
                hamiltonian,
                pauli_estimator,
                sample_pauli_str[sample_idx],
                sample_measurement_result[sample_idx]
            )
        for sample_idx in range(sample_shots):
            estimation = 0
            for coeff, pauli_term in hamiltonian:
                estimation += coeff * pauli_estimator[pauli_term]['value'][sample_idx + 1]
            trace_estimation = estimation

    return trace_estimation
Q
Quleaf 已提交
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


def tensor_product(state_a: Union[State, paddle.Tensor], state_b: Union[State, paddle.Tensor], *args: Union[State, paddle.Tensor]) -> State:
    r"""calculate tensor product (kronecker product) between at least two state. This function automatically returns State instance

    Args:
        state_a: State
        state_b: State
        *args: other states

    Raises:
        NotImplementedError: only accept state or tensor instances

    Returns:
        tensor product of input states

    Note:
        the backend should be density matrix.
    """
    if isinstance(state_a, State):
        data_a = state_a.data
    elif isinstance(state_a, paddle.Tensor):
        data_a = state_a
    else:
        raise NotImplementedError
    
    if isinstance(state_b, State):
        data_b = state_b.data
    elif isinstance(state_b, paddle.Tensor):
        data_b = state_b
    else:
        raise NotImplementedError
    
    addis = []
    for st in args:
        if isinstance(st, State):
            addis.append(st.data)
        elif isinstance(st, paddle.Tensor):
            addis.append(st)
        else:
            raise NotImplementedError
    
    res = paddle_quantum.linalg.NKron(data_a, data_b, *addis)
    return State(res)