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_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