File size: 1,162 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
from functools import partial

import jax
import jax.numpy as jnp
from jax import jit

from varipeps.contractions import apply_contraction_jitted


@partial(jit, static_argnums=(3,))
def _one_site_workhorse(
    peps_tensors,
    peps_tensor_objs,
    gates,
    real_result=False,
):
    density_matrix = apply_contraction_jitted(
        "triangular_ctmrg_one_site_expectation",
        [peps_tensors[0]],
        [peps_tensor_objs[0]],
        [],
    )

    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_triangular_one_site(
    peps_tensors,
    peps_tensor_objs,
    gates,
):
    if isinstance(peps_tensors, jnp.ndarray):
        peps_tensors = (peps_tensors,)
        peps_tensor_objs = (peps_tensor_objs,)

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

    return _one_site_workhorse(
        peps_tensors, peps_tensor_objs, tuple(gates), real_result
    )