File size: 3,853 Bytes
6288873
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from functools import partial

import jax.numpy as jnp
from jax import jit

from varipeps.peps import PEPS_Tensor
from varipeps.contractions import apply_contraction_jitted

from typing import Sequence, List, Tuple, Literal

Corner_Literal = Literal["top-left", "top-right", "bottom-left", "bottom-right"]


@partial(jit, static_argnums=(5,))
def _four_sites_quadrat_workhorse(
    top_left: jnp.ndarray,
    top_right: jnp.ndarray,
    bottom_left: jnp.ndarray,
    bottom_right: jnp.ndarray,
    gates: Tuple[jnp.ndarray, ...],
    real_result: bool = False,
) -> List[jnp.ndarray]:
    density_matrix = jnp.tensordot(top_left, top_right, ((5, 6, 7), (2, 3, 4)))
    density_matrix = jnp.tensordot(density_matrix, bottom_left, ((2, 3, 4), (5, 6, 7)))
    density_matrix = jnp.tensordot(
        density_matrix, bottom_right, ((4, 5, 6, 9, 10, 11), (2, 3, 4, 5, 6, 7))
    )

    density_matrix = density_matrix.transpose(0, 2, 4, 6, 1, 3, 5, 7)
    density_matrix = density_matrix.reshape(
        density_matrix.shape[0]
        * density_matrix.shape[1]
        * density_matrix.shape[2]
        * density_matrix.shape[3],
        density_matrix.shape[4]
        * density_matrix.shape[5]
        * density_matrix.shape[6]
        * density_matrix.shape[7],
    )

    norm = jnp.trace(density_matrix)

    if real_result:
        return [
            jnp.real(jnp.tensordot(density_matrix, g, ((0, 1), (0, 1))) / norm)
            for g in gates
        ]
    else:
        return [
            jnp.tensordot(density_matrix, g, ((0, 1), (0, 1))) / norm for g in gates
        ]


def calc_four_sites_quadrat_multiple_gates(
    peps_tensors: Sequence[jnp.ndarray],
    peps_tensor_objs: Sequence[PEPS_Tensor],
    gates: Sequence[jnp.ndarray],
) -> List[jnp.ndarray]:
    """
    Calculate the four site expectation values for three as quadrat ordered
    PEPS tensor and their environment.

    The order of the PEPS sequence have to be
    [top-left, top-right, bottom-left, bottom-right].

    The gate is applied in the order [top-left, top-right, bottom-left, bottom-right].

    Args:
      peps_tensors (:term:`sequence` of :obj:`jax.numpy.ndarray`):
        The PEPS tensor arrays. Have to be the same objects as the tensor
        attribute of the `peps_tensor_obj` argument.
      peps_tensor_objs (:term:`sequence` of :obj:`~varipeps.peps.PEPS_Tensor`):
        PEPS tensor objects.
      gates (:term:`sequence` of :obj:`jax.numpy.ndarray`):
        Sequence with the gates which should be applied to the PEPS tensors.
        Gates are expected to be a matrix with first axis corresponding to
        the Hilbert space and the second axis corresponding to the dual room.
    Returns:
      :obj:`list` of :obj:`jax.numpy.ndarray`:
        List with the calculated expectation values of each gate.
    """
    density_matrix_top_left = apply_contraction_jitted(
        "density_matrix_four_sites_top_left",
        [peps_tensors[0]],
        [peps_tensor_objs[0]],
        [],
    )

    density_matrix_top_right = apply_contraction_jitted(
        "density_matrix_four_sites_top_right",
        [peps_tensors[1]],
        [peps_tensor_objs[1]],
        [],
    )

    density_matrix_bottom_left = apply_contraction_jitted(
        "density_matrix_four_sites_bottom_left",
        [peps_tensors[2]],
        [peps_tensor_objs[2]],
        [],
    )

    density_matrix_bottom_right = apply_contraction_jitted(
        "density_matrix_four_sites_bottom_right",
        [peps_tensors[3]],
        [peps_tensor_objs[3]],
        [],
    )

    real_result = all(jnp.allclose(g, g.T.conj()) for g in gates)

    return _four_sites_quadrat_workhorse(
        density_matrix_top_left,
        density_matrix_top_right,
        density_matrix_bottom_left,
        density_matrix_bottom_right,
        tuple(gates),
        real_result,
    )