def test_canonical_form(bc, method): psi = random_MPS(8, 2, 6, form=None, bc=bc) psi2 = psi.copy() norm = np.sqrt(psi2.overlap(psi2, ignore_form=True)) print("norm =", norm) psi2.norm /= norm # normalize psi2 norm2 = psi.overlap(psi2, ignore_form=True) print("norm2 =", norm2) assert abs(norm2 - norm) < 1.e-14 * norm meth = getattr(psi, method) meth(renormalize=False) # psi.canonical_form_[infinite[2]]() psi.test_sanity() assert abs(psi.norm - norm) < 1.e-14 * norm psi.norm = 1. # normalized psi ov = psi.overlap(psi2, ignore_form=True) print("normalized states: overlap <psi_canonical|psi> = 1.-", 1. - ov) assert abs(ov - 1.) < 1.e-14 print("norm_test") print(psi.norm_test()) assert np.max(psi.norm_test()) < 1.e-14 # SB = AS to good precision psi3 = psi.copy() # call canonical_form again, it shouldn't do anything now meth(renormalize=True) psi.test_sanity() ov = psi.overlap(psi3) assert abs(ov - 1.) < 1.e-14 if method in ['canonical_form_finite', 'canonical_form_infinite2']: # check that A = SB S^-1 is orthonormal for i in range(psi.L): A = psi.get_B(i, 'A') c = npc.tensordot(A, A.conj(), axes=[['vL', 'p'], ['vL*', 'p*']]) A_err = (c - npc.diag(1., c.legs[0])).norm() print(A_err) assert A_err < 1.e-13
def test_TransferMatrix(chi=4, d=2): psi = random_MPS(2, d, chi, bc='infinite', form=None) full_TM = npc.tensordot(psi._B[0], psi._B[0].conj(), axes=['p', 'p*']) full_TM = npc.tensordot(full_TM, psi._B[1], axes=['vR', 'vL']) full_TM = npc.tensordot(full_TM, psi._B[1].conj(), axes=[['vR*', 'p'], ['vL*', 'p*']]) full_TM = full_TM.combine_legs([['vL', 'vL*'], ['vR', 'vR*']], qconj=[+1, -1]) full_TM_dense = full_TM.to_ndarray() eta_full, w_full = np.linalg.eig(full_TM_dense) sort = np.argsort(np.abs(eta_full))[::-1] eta_full = eta_full[sort] w_full = w_full[:, sort] TM = mps.TransferMatrix(psi, psi, charge_sector=0, form=None) eta, w = TM.eigenvectors(3) print("transfer matrix yields eigenvalues ", eta) print(eta.shape, eta_full.shape) print(psi.dtype) # note: second and third eigenvalue are complex conjugates if bool(eta[2].imag > 0.) == bool(eta_full[2].imag > 0.): npt.assert_allclose(eta[:3], eta_full[:3]) else: npt.assert_allclose(eta[:3], eta_full[:3].conj()) # compare largest eigenvector w0_full = w_full[:, 0] w0 = w[0].to_ndarray() assert (abs(np.sum(w0_full)) > 1.e-20) # should be the case for random stuff w0_full /= np.sum(w0_full) # fixes norm & phase w0 /= np.sum(w0) npt.assert_allclose(w0, w0_full)
def test_MPO_var(L=8, tol=1.e-13): xxz_pars = dict(L=L, Jx=1., Jy=1., Jz=1.1, hz=0.1, bc_MPS='finite', conserve=None) M = SpinChain(xxz_pars) psi = random_MPS(L, 2, 10) exp_val = M.H_MPO.expectation_value(psi) ED = ExactDiag(M) ED.build_full_H_from_mpo() psi_full = ED.mps_to_full(psi) exp_val_full = npc.inner(psi_full, npc.tensordot(ED.full_H, psi_full, axes=1), axes='range', do_conj=True) assert abs(exp_val - exp_val_full) / abs(exp_val_full) < tol Hsquared = M.H_MPO.variance(psi, 0.) Hsquared_full = npc.inner(psi_full, npc.tensordot(ED.full_H, npc.tensordot(ED.full_H, psi_full, axes=1), axes=1), axes='range', do_conj=True) assert abs(Hsquared - Hsquared_full) / abs(Hsquared_full) < tol var = M.H_MPO.variance(psi) var_full = Hsquared_full - exp_val_full**2 assert abs(var - var_full) / abs(var_full) < tol
def test_canonical_form(bc): psi = random_MPS(8, 2, 6, form=None, bc=bc) psi2 = psi.copy() norm = np.sqrt(psi2.overlap(psi2, ignore_form=True)) print("norm =", norm) psi2.norm /= norm # normalize psi2 norm2 = psi.overlap(psi2, ignore_form=True) print("norm2 =", norm2) assert abs(norm2 - norm) < 1.e-14 * norm psi.canonical_form(renormalize=False) psi.test_sanity() assert abs(psi.norm - norm) < 1.e-14 * norm psi.norm = 1. # normalized psi ov = psi.overlap(psi2, ignore_form=True) print("normalized states: overlap <psi_canonical|psi> = 1.-", 1. - ov) assert abs(ov - 1.) < 1.e-14 print("norm_test") print(psi.norm_test()) assert np.max(psi.norm_test()) < 1.e-14