File size: 2,857 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
import jax.numpy as jnp

from varipeps import varipeps_config
from varipeps.ctmrg import calc_ctmrg_env, CTMRGNotConvergedError
from varipeps.peps import PEPS_Unit_Cell

from . import overlap_single_site
from . import overlap_four_sites

overlap_mapping = {
    (1, 1): overlap_single_site.Overlap_Single_Site,
    (2, 2): overlap_four_sites.Overlap_Four_Sites_Square,
}


def calculate_overlap(
    unitcell_A, unitcell_B, chi, max_chi, *, test_automatic_overlap_conv=True
):
    structure_A = tuple(tuple(i) for i in unitcell_A.data.structure)
    structure_B = tuple(tuple(i) for i in unitcell_B.data.structure)

    if structure_A != structure_B:
        raise ValueError("Structure of both unit cells have to be the same.")

    size = unitcell_A.get_size()
    num_tensors = size[0] * size[1]
    overlap_func = overlap_mapping[size].calc_overlap

    unitcell_A = unitcell_A.convert_to_full_transfer()
    unitcell_B = unitcell_B.convert_to_full_transfer()

    norm_A = overlap_func(unitcell_A)
    norm_B = overlap_func(unitcell_B)

    overlap_tensors = [
        type(t).from_tensor(
            t.tensor / norm_A ** (1 / (2 * num_tensors)), t.d, t.D, chi, max_chi=max_chi
        )
        for t in unitcell_A.get_unique_tensors()
    ]

    for i, e in enumerate(overlap_tensors):
        e.tensor_conj = unitcell_B.get_unique_tensors()[i].tensor.conj() / norm_B ** (
            1 / (2 * num_tensors)
        )

    overlap_unitcell = PEPS_Unit_Cell.from_tensor_list(overlap_tensors, structure_A)

    if test_automatic_overlap_conv:
        tmp_max_steps = varipeps_config.ctmrg_max_steps
        tmp_fail = varipeps_config.ctmrg_fail_if_not_converged

        varipeps_config.ctmrg_fail_if_not_converged = False
        varipeps_config.ctmrg_max_steps = 10

        overlap_unitcell, _ = calc_ctmrg_env(
            [i.tensor for i in overlap_tensors], overlap_unitcell
        )
        overlap_AB = overlap_func(overlap_unitcell)

        varipeps_config.ctmrg_max_steps = 1
        for count in range(tmp_max_steps):
            old_overlap_AB = overlap_AB

            overlap_unitcell, _ = calc_ctmrg_env(
                [i.tensor for i in overlap_tensors], overlap_unitcell
            )
            overlap_AB = overlap_func(overlap_unitcell)

            if (
                jnp.abs(overlap_AB - old_overlap_AB)
                <= varipeps_config.ctmrg_convergence_eps
            ):
                varipeps_config.ctmrg_fail_if_not_converged = tmp_fail
                varipeps_config.ctmrg_max_steps = tmp_max_steps
                break
        if count == (tmp_max_steps - 1):
            raise CTMRGNotConvergedError
    else:
        overlap_unitcell, _ = calc_ctmrg_env(
            [i.tensor for i in overlap_tensors], overlap_unitcell
        )

        overlap_AB = overlap_func(overlap_unitcell)

    return overlap_AB