|
| 1 | +import numpy as np |
| 2 | +from scipy.sparse.linalg import LinearOperator, eigs |
| 3 | + |
| 4 | +import jax.numpy as jnp |
| 5 | +from jaxlib.xla_extension import XlaRuntimeError |
| 6 | + |
| 7 | +from varipeps.contractions import apply_contraction_jitted |
| 8 | +from varipeps.peps import PEPS_Unit_Cell |
| 9 | + |
| 10 | + |
| 11 | +def calculate_correlation_length(unitcell: PEPS_Unit_Cell): |
| 12 | + use_full = True |
| 13 | + |
| 14 | + full_transfer_right = None |
| 15 | + full_transfer_bottom = None |
| 16 | + |
| 17 | + try: |
| 18 | + for _, view in unitcell.iter_one_row(0): |
| 19 | + transfer_right = apply_contraction_jitted( |
| 20 | + "corrlength_transfer_right", |
| 21 | + (view[0, 0][0][0].tensor,), |
| 22 | + (view[0, 0][0][0],), |
| 23 | + (), |
| 24 | + ) |
| 25 | + transfer_right = transfer_right.reshape( |
| 26 | + np.prod(transfer_right.shape[:4]), np.prod(transfer_right.shape[4:]) |
| 27 | + ) |
| 28 | + |
| 29 | + if full_transfer_right is None: |
| 30 | + full_transfer_right = transfer_right |
| 31 | + else: |
| 32 | + full_transfer_right = full_transfer_right @ transfer_right |
| 33 | + |
| 34 | + initial_vector_right = apply_contraction_jitted( |
| 35 | + "corrlength_vector_right", |
| 36 | + (unitcell[0, -1][0][0].tensor,), |
| 37 | + (view[0, -1][0][0],), |
| 38 | + (), |
| 39 | + ) |
| 40 | + initial_vector_right = initial_vector_right.reshape(-1) |
| 41 | + |
| 42 | + for _, view in unitcell.iter_one_column(0): |
| 43 | + transfer_bottom = apply_contraction_jitted( |
| 44 | + "corrlength_transfer_bottom", |
| 45 | + (view[0, 0][0][0].tensor,), |
| 46 | + (view[0, 0][0][0],), |
| 47 | + (), |
| 48 | + ) |
| 49 | + transfer_bottom = transfer_bottom.reshape( |
| 50 | + np.prod(transfer_bottom.shape[:4]), np.prod(transfer_bottom.shape[4:]) |
| 51 | + ) |
| 52 | + |
| 53 | + if full_transfer_bottom is None: |
| 54 | + full_transfer_bottom = transfer_bottom |
| 55 | + else: |
| 56 | + full_transfer_bottom = full_transfer_bottom @ transfer_bottom |
| 57 | + |
| 58 | + initial_vector_bottom = apply_contraction_jitted( |
| 59 | + "corrlength_vector_bottom", |
| 60 | + (unitcell[-1, 0][0][0].tensor,), |
| 61 | + (view[-1, 0][0][0],), |
| 62 | + (), |
| 63 | + ) |
| 64 | + initial_vector_bottom = initial_vector_bottom.reshape(-1) |
| 65 | + |
| 66 | + eig_right, eigvec_right = eigs( |
| 67 | + full_transfer_right, k=5, v0=initial_vector_right, which="LM" |
| 68 | + ) |
| 69 | + |
| 70 | + eig_right = np.abs(eig_right) |
| 71 | + eig_right /= eig_right[0] |
| 72 | + |
| 73 | + corr_len_right = -1 / np.log(eig_right[1]) |
| 74 | + |
| 75 | + eig_bottom, eigvec_bottom = eigs( |
| 76 | + full_transfer_bottom, k=5, v0=initial_vector_bottom, which="LM" |
| 77 | + ) |
| 78 | + |
| 79 | + eig_bottom = np.abs(eig_bottom) |
| 80 | + eig_bottom /= eig_bottom[0] |
| 81 | + |
| 82 | + corr_len_bottom = -1 / np.log(eig_bottom[1]) |
| 83 | + |
| 84 | + return (corr_len_right, eig_right), (corr_len_bottom, eig_bottom) |
| 85 | + except XlaRuntimeError: |
| 86 | + initial_vector_left = apply_contraction_jitted( |
| 87 | + "corrlength_vector_left", |
| 88 | + (unitcell[0, 0][0][0].tensor,), |
| 89 | + (view[0, 0][0][0],), |
| 90 | + (), |
| 91 | + ) |
| 92 | + initial_vector_left = initial_vector_left.reshape(-1) |
| 93 | + |
| 94 | + initial_vector_top = apply_contraction_jitted( |
| 95 | + "corrlength_vector_top", |
| 96 | + (unitcell[0, 0][0][0].tensor,), |
| 97 | + (view[0, 0][0][0],), |
| 98 | + (), |
| 99 | + ) |
| 100 | + initial_vector_top = initial_vector_top.reshape(-1) |
| 101 | + |
| 102 | + def left_matvec(vec): |
| 103 | + vec = jnp.asarray(vec) |
| 104 | + for _, view in unitcell.iter_one_row(0): |
| 105 | + if vec.ndim != 4: |
| 106 | + vec = vec.reshape( |
| 107 | + view[0, 0][0][0].T1.shape[0], |
| 108 | + view[0, 0][0][0].tensor.shape[0], |
| 109 | + view[0, 0][0][0].tensor.shape[0], |
| 110 | + view[0, 0][0][0].T3.shape[0], |
| 111 | + ) |
| 112 | + vec = apply_contraction_jitted( |
| 113 | + "corrlength_absorb_one_column", |
| 114 | + (view[0, 0][0][0].tensor,), |
| 115 | + (view[0, 0][0][0],), |
| 116 | + (vec,), |
| 117 | + ) |
| 118 | + return vec.reshape(-1) |
| 119 | + |
| 120 | + left_lin_op = LinearOperator( |
| 121 | + (initial_vector_left.shape[0], initial_vector_left.shape[0]), |
| 122 | + matvec=left_matvec, |
| 123 | + ) |
| 124 | + |
| 125 | + eig_left, eigvec_left = eigs( |
| 126 | + left_lin_op, k=5, v0=initial_vector_left, which="LM" |
| 127 | + ) |
| 128 | + |
| 129 | + eig_left = np.abs(eig_left) |
| 130 | + eig_left /= eig_left[0] |
| 131 | + |
| 132 | + corr_len_left = -1 / np.log(eig_left[1]) |
| 133 | + |
| 134 | + def top_matvec(vec): |
| 135 | + vec = jnp.asarray(vec) |
| 136 | + for _, view in unitcell.iter_one_row(0): |
| 137 | + if vec.ndim != 4: |
| 138 | + vec = vec.reshape( |
| 139 | + view[0, 0][0][0].T4.shape[3], |
| 140 | + view[0, 0][0][0].tensor.shape[4], |
| 141 | + view[0, 0][0][0].tensor.shape[4], |
| 142 | + view[0, 0][0][0].T2.shape[3], |
| 143 | + ) |
| 144 | + vec = apply_contraction_jitted( |
| 145 | + "corrlength_absorb_one_row", |
| 146 | + (view[0, 0][0][0].tensor,), |
| 147 | + (view[0, 0][0][0],), |
| 148 | + (vec,), |
| 149 | + ) |
| 150 | + return vec.reshape(-1) |
| 151 | + |
| 152 | + top_lin_op = LinearOperator( |
| 153 | + (initial_vector_top.shape[0], initial_vector_top.shape[0]), |
| 154 | + matvec=top_matvec, |
| 155 | + ) |
| 156 | + |
| 157 | + eig_top, eigvec_top = eigs(top_lin_op, k=5, v0=initial_vector_top, which="LM") |
| 158 | + |
| 159 | + eig_top = np.abs(eig_top) |
| 160 | + eig_top /= eig_top[0] |
| 161 | + |
| 162 | + corr_len_top = -1 / np.log(eig_top[1]) |
| 163 | + |
| 164 | + return (corr_len_left, eig_left), (corr_len_top, eig_top) |
0 commit comments