def test_model_H_conversion(L=6): bc = 'finite' model_params = {'L': L, 'hz': np.random.random([L]), 'bc_MPS': bc} m = XXZChain(model_params) # can we run the conversion? # conversion from bond to MPO in NearestNeighborModel H_MPO = m.calc_H_MPO_from_bond() # conversion from MPO to bond in MPOModel H_bond = m.calc_H_bond_from_MPO() # compare: did we get the correct result? ED = ExactDiag(m) ED.build_full_H_from_bonds() H0 = ED.full_H # this should be correct ED.full_H = None m.H_MPO = H_MPO ED.build_full_H_from_mpo() full_H_mpo = ED.full_H # the one generated by NearstNeighborModel.calc_H_MPO_from_bond() print("npc.norm(H0 - full_H_mpo) = ", npc.norm(H0 - full_H_mpo)) assert npc.norm(H0 - full_H_mpo) < 1.e-14 # round off errors on order of 1.e-15 m.H_bond = H_bond ED.full_H = None ED.build_full_H_from_bonds() full_H_bond = ED.full_H # the one generated by NearstNeighborModel.calc_H_MPO_from_bond() print("npc.norm(H0 - full_H_bond) = ", npc.norm(H0 - full_H_bond)) assert npc.norm( H0 - full_H_bond) < 1.e-14 # round off errors on order of 1.e-15
def test_CouplingMPOModel_group(): m1 = MyMod(dict(x=0.5, L=5, bc_MPS='finite')) model_params = {'L': 6, 'hz': np.random.random([6]), 'bc_MPS': 'finite'} m2 = XXZChain(model_params) for m in [m1, m2]: print("model = ", m) assert m.H_MPO.max_range == 1 # test grouping sites ED = ExactDiag(m) # ED.build_full_H_from_mpo() ED.build_full_H_from_bonds() m.group_sites(n=2) assert m.H_MPO.max_range == 1 ED_gr = ExactDiag(m) ED_gr.build_full_H_from_mpo() H = ED.full_H.split_legs().to_ndarray() Hgr = ED_gr.full_H.split_legs() Hgr.idrop_labels() Hgr = Hgr.split_legs().to_ndarray() assert np.linalg.norm(H - Hgr) < 1.e-14 ED_gr.full_H = None ED_gr.build_full_H_from_bonds() Hgr = ED_gr.full_H.split_legs() Hgr.idrop_labels() Hgr = Hgr.split_legs().to_ndarray() assert np.linalg.norm(H - Hgr) < 1.e-14
def test_ED(): # just quickly check that it runs without errors for a small system xxz_pars = dict(L=4, Jxx=1., Jz=1., hz=0.0, bc_MPS='finite') M = XXZChain(xxz_pars) ED = ExactDiag(M) ED.build_full_H_from_mpo() H, ED.full_H = ED.full_H, None ED.build_full_H_from_bonds() H2 = ED.full_H assert (npc.norm(H - H2, np.inf) < 1.e-14) ED.full_diagonalization() psi = ED.groundstate() print("select charge_sector =", psi.qtotal) ED2 = ExactDiag(M, psi.qtotal) ED2.build_full_H_from_mpo() ED2.full_diagonalization() psi2 = ED2.groundstate() full_psi2 = psi.zeros_like() full_psi2[ED2._mask] = psi2 ov = npc.inner(psi, full_psi2, do_conj=True) print("overlab <psi | psi2> = 1. -", 1. - ov) assert (abs(abs(ov) - 1.) < 1.e-15) # starting from a random guess in the correct charge sector, # check if we can also do lanczos. np.random.seed(12345) psi3 = npc.Array.from_func(np.random.random, psi2.legs, qtotal=psi2.qtotal, shape_kw='size') E0, psi3, N = lanczos(ED2, psi3) print("Lanczos E0 =", E0) ov = npc.inner(psi3, psi2, do_conj=True) print("overlab <psi2 | psi3> = 1. -", 1. - ov) assert (abs(abs(ov) - 1.) < 1.e-15)
def test_CouplingMPOModel_group(): m = MyMod(dict(x=0.5, L=5, bc_MPS='finite')) assert m.H_MPO.max_range == 1 # test grouping sites ED = ExactDiag(m) # ED.build_full_H_from_mpo() ED.build_full_H_from_bonds() m.group_sites(n=2) assert m.H_MPO.max_range == 1 ED_gr = ExactDiag(m) ED_gr.build_full_H_from_mpo() H = ED.full_H.split_legs().to_ndarray() Hgr = ED_gr.full_H.split_legs() Hgr.idrop_labels() Hgr = Hgr.split_legs().to_ndarray() assert np.linalg.norm(H - Hgr) == 0 ED_gr.full_H = None ED_gr.build_full_H_from_bonds() Hgr = ED_gr.full_H.split_legs() Hgr.idrop_labels() Hgr = Hgr.split_legs().to_ndarray() assert np.linalg.norm(H - Hgr) == 0
def test_CouplingMPOModel_group(): class MyMod(model.CouplingMPOModel, model.NearestNeighborModel): def __init__(self, model_params): model.CouplingMPOModel.__init__(self, model_params) def init_sites(self, model_params): return tenpy.networks.site.SpinHalfSite('parity') def init_terms(self, model_params): x = get_parameter(model_params, 'x', 1., self.name) self.add_onsite_term(0.25, 0, 'Sz') self.add_onsite_term(0.25, 4, 'Sz') self.add_coupling_term(x, 0, 1, 'Sx', 'Sx') self.add_coupling_term(2. * x, 1, 2, 'Sy', 'Sy') self.add_coupling_term(3. * x, 3, 4, 'Sy', 'Sy') m = MyMod(dict(x=0.5, L=5, bc_MPS='finite')) m.test_sanity() for Hb in m.H_bond: if Hb is not None: Hb.test_sanity() # test grouping sites ED = ExactDiag(m) # ED.build_full_H_from_mpo() ED.build_full_H_from_bonds() m.group_sites(n=2) ED_gr = ExactDiag(m) ED_gr.build_full_H_from_mpo() H = ED.full_H.split_legs().to_ndarray() Hgr = ED_gr.full_H.split_legs() Hgr.idrop_labels() Hgr = Hgr.split_legs().to_ndarray() assert np.linalg.norm(H - Hgr) == 0 ED_gr.full_H = None ED_gr.build_full_H_from_bonds() Hgr = ED_gr.full_H.split_legs() Hgr.idrop_labels() Hgr = Hgr.split_legs().to_ndarray() assert np.linalg.norm(H - Hgr) == 0
def test_SpinChainNNN_comparison(): model_pars = { 'hz': 0.5, 'Jx': -2., 'Jy': -2., 'Jz': 0.4, 'L': 3, 'conserve': 'Sz', 'bc_MPS': 'finite' } M1 = spins_nnn.SpinChainNNN(model_pars.copy()) model_pars['L'] = 2 * model_pars['L'] M2 = spins_nnn.SpinChainNNN2(model_pars.copy()) M2.group_sites(2) M2nn = NearestNeighborModel.from_MPOModel(M2) ED1 = ExactDiag(M1) ED2 = ExactDiag(M2) ED2nn = ExactDiag(M2nn) ED1.build_full_H_from_mpo() ED2.build_full_H_from_mpo() ED2nn.build_full_H_from_bonds() assert (ED1.full_H - ED2.full_H).norm() < 1.e-13 assert (ED1.full_H - ED2nn.full_H).norm() < 1.e-13