def test_dmrg_excited(eps=1.e-12): # checks ground state and 2 excited states (in same symmetry sector) for a small system # (without truncation) L, g = 8, 1.3 bc = 'finite' model_params = dict(L=L, J=1., g=g, bc_MPS=bc, conserve='parity', verbose=0) M = TFIChain(model_params) # compare to exact solution ED = ExactDiag(M) ED.build_full_H_from_mpo() ED.full_diagonalization() # Note: energies sorted by chargesector (first 0), then ascending -> perfect for comparison print("Exact diag: E[:5] = ", ED.E[:5]) print("Exact diag: (smalles E)[:10] = ", np.sort(ED.E)[:10]) psi_ED = [ED.V.take_slice(i, 'ps*') for i in range(5)] print("charges : ", [psi.qtotal for psi in psi_ED]) # first DMRG run psi0 = mps.MPS.from_product_state(M.lat.mps_sites(), [0] * L, bc=bc) dmrg_pars = { 'verbose': 0, 'N_sweeps_check': 1, 'lanczos_params': { 'reortho': False }, 'diag_method': 'lanczos', 'combine': True } eng0 = dmrg.TwoSiteDMRGEngine(psi0, M, dmrg_pars) E0, psi0 = eng0.run() assert abs((E0 - ED.E[0]) / ED.E[0]) < eps ov = npc.inner(psi_ED[0], ED.mps_to_full(psi0), 'range', do_conj=True) assert abs(abs(ov) - 1.) < eps # unique groundstate: finite size gap! # second DMRG run for first excited state dmrg_pars['verbose'] = 1. dmrg_pars['orthogonal_to'] = [psi0] psi1 = mps.MPS.from_product_state(M.lat.mps_sites(), [0] * L, bc=bc) eng1 = dmrg.TwoSiteDMRGEngine(psi1, M, dmrg_pars) E1, psi1 = eng1.run() assert abs((E1 - ED.E[1]) / ED.E[1]) < eps ov = npc.inner(psi_ED[1], ED.mps_to_full(psi1), 'range', do_conj=True) assert abs(abs(ov) - 1.) < eps # unique groundstate: finite size gap! # and a third one to check with 2 eigenstates dmrg_pars['orthogonal_to'] = [psi0, psi1] # note: different intitial state necessary, otherwise H is 0 psi2 = mps.MPS.from_singlets(psi0.sites[0], L, [(0, 1), (2, 3), (4, 5), (6, 7)], bc=bc) eng2 = dmrg.TwoSiteDMRGEngine(psi2, M, dmrg_pars) E2, psi2 = eng2.run() print(E2) assert abs((E2 - ED.E[2]) / ED.E[2]) < eps ov = npc.inner(psi_ED[2], ED.mps_to_full(psi2), 'range', do_conj=True) assert abs(abs(ov) - 1.) < eps # unique groundstate: finite size gap!
def test_dmrg_excited(eps=1.e-12): # checks ground state and 2 excited states (in same symmetry sector) for a small system # (without truncation) L, g = 8, 1.1 bc = 'finite' model_params = dict(L=L, J=1., g=g, bc_MPS=bc, conserve='parity', verbose=0) M = TFIChain(model_params) # compare to exact solution ED = ExactDiag(M) ED.build_full_H_from_mpo() ED.full_diagonalization() # Note: energies sorted by chargesector (first 0), then ascending -> perfect for comparison print("Exact diag: E[:5] = ", ED.E[:5]) # first DMRG run psi0 = mps.MPS.from_product_state(M.lat.mps_sites(), [0] * L, bc=bc) dmrg_pars = { 'verbose': 1, 'N_sweeps_check': 1, 'lanczos_params': { 'reortho': True } } eng0 = dmrg.EngineCombine(psi0, M, dmrg_pars) E0, psi0 = eng0.run() assert abs((E0 - ED.E[0]) / ED.E[0]) < eps ov = npc.inner(ED.V.take_slice(0, 'ps*'), ED.mps_to_full(psi0), do_conj=True) assert abs(abs(ov) - 1.) < eps # unique groundstate: finite size gap! # second DMRG run for first excited state dmrg_pars['orthogonal_to'] = [psi0] psi1 = mps.MPS.from_product_state(M.lat.mps_sites(), [0] * L, bc=bc) eng1 = dmrg.EngineCombine(psi1, M, dmrg_pars) E1, psi1 = eng1.run() assert abs((E1 - ED.E[1]) / ED.E[1]) < eps ov = npc.inner(ED.V.take_slice(1, 'ps*'), ED.mps_to_full(psi1), do_conj=True) assert abs(abs(ov) - 1.) < eps # unique groundstate: finite size gap! # and a third one to check with 2 eigenstates dmrg_pars['orthogonal_to'] = [psi0, psi1] # note: different intitial state necessary, otherwise H is 0 psi2 = mps.MPS.from_product_state(M.lat.mps_sites(), [0, 1] * (L // 2), bc=bc) eng2 = dmrg.EngineCombine(psi2, M, dmrg_pars) E2, psi2 = eng2.run() print(E2) assert abs((E2 - ED.E[2]) / ED.E[2]) < eps ov = npc.inner(ED.V.take_slice(2, 'ps*'), ED.mps_to_full(psi2), do_conj=True) assert abs(abs(ov) - 1.) < eps # unique groundstate: finite size gap!
def example_exact_diagonalization(L, Jz): xxz_pars = dict(L=L, Jxx=1., Jz=Jz, hz=0.0, bc_MPS='finite') M = XXZChain(xxz_pars) product_state = ["up", "down"] * (xxz_pars['L'] // 2) # this selects a charge sector! psi_DMRG = MPS.from_product_state(M.lat.mps_sites(), product_state) charge_sector = psi_DMRG.get_total_charge(True) # ED charge sector should match ED = ExactDiag(M, charge_sector=charge_sector, max_size=2.e6) ED.build_full_H_from_mpo() # ED.build_full_H_from_bonds() # whatever you prefer print("start diagonalization") ED.full_diagonalization() # the expensive part for large L E0_ED, psi_ED = ED.groundstate() # return the ground state print("psi_ED =", psi_ED) print("run DMRG") dmrg.run(psi_DMRG, M, {'verbose': 0}) # modifies psi_DMRG in place! # first way to compare ED with DMRG: convert MPS to ED vector psi_DMRG_full = ED.mps_to_full(psi_DMRG) print("psi_DMRG_full =", psi_DMRG_full) ov = npc.inner(psi_ED, psi_DMRG_full, axes='range', do_conj=True) print("<psi_ED|psi_DMRG_full> =", ov) assert (abs(abs(ov) - 1.) < 1.e-13) # second way: convert ED vector to MPS psi_ED_mps = ED.full_to_mps(psi_ED) ov2 = psi_ED_mps.overlap(psi_DMRG) print("<psi_ED_mps|psi_DMRG> =", ov2) assert (abs(abs(ov2) - 1.) < 1.e-13) assert (abs(ov - ov2) < 1.e-13) # -> advantage: expectation_value etc. of MPS are available! print("<Sz> =", psi_ED_mps.expectation_value('Sz'))
def test_dmrg_diag_method(engine, diag_method, tol=1.e-6): bc_MPS = 'finite' model_params = dict(L=6, S=0.5, bc_MPS=bc_MPS, conserve='Sz', verbose=0) M = SpinChain(model_params) # chose total Sz= 4, not 3=6/2, i.e. not the sector with lowest energy! # make sure below that we stay in that sector, if we're supposed to. init_Sz_4 = ['up', 'down', 'up', 'up', 'up', 'down'] psi_Sz_4 = mps.MPS.from_product_state(M.lat.mps_sites(), init_Sz_4, bc=bc_MPS) dmrg_pars = { 'verbose': 1, 'N_sweeps_check': 1, 'combine': True, 'max_sweeps': 5, 'diag_method': diag_method, 'mixer': True, } ED = ExactDiag(M) ED.build_full_H_from_mpo() ED.full_diagonalization() if diag_method == "ED_all": charge_sector = None # allow to change the sector else: charge_sector = [2] # don't allow to change the sector E_ED, psi_ED = ED.groundstate(charge_sector=charge_sector) DMRGEng = dmrg.__dict__.get(engine) print("DMRGEng = ", DMRGEng) print("setting diag_method = ", dmrg_pars['diag_method']) eng = DMRGEng(psi_Sz_4.copy(), M, dmrg_pars) E0, psi0 = eng.run() print("E0 = {0:.15f}".format(E0)) assert abs(E_ED - E0) < tol ov = npc.inner(psi_ED, ED.mps_to_full(psi0), 'range', do_conj=True) assert abs(abs(ov) - 1) < tol
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_tebd(bc_MPS, g=0.5): L = 2 if bc_MPS == 'infinite' else 6 # xxz_pars = dict(L=L, Jxx=1., Jz=3., hz=0., bc_MPS=bc_MPS) # M = XXZChain(xxz_pars) # factor of 4 (2) for J (h) to change spin-1/2 to Pauli matrices model_pars = dict(L=L, Jx=0., Jy=0., Jz=-4., hx=2. * g, bc_MPS=bc_MPS, conserve=None) M = SpinChain(model_pars) state = ([[1, -1.], [1, -1.]] * L)[:L] # pointing in (-x)-direction psi = MPS.from_product_state(M.lat.mps_sites(), state, bc=bc_MPS) tebd_param = { 'verbose': 2, 'dt': 0.01, 'order': 2, 'delta_tau_list': [0.1, 1.e-4, 1.e-8], 'max_error_E': 1.e-9, 'trunc_params': { 'chi_max': 50, 'trunc_cut': 1.e-13 } } engine = tebd.Engine(psi, M, tebd_param) engine.run_GS() print("norm_test", psi.norm_test()) if bc_MPS == 'finite': psi.canonical_form() ED = ExactDiag(M) ED.build_full_H_from_mpo() ED.full_diagonalization() psi_ED = ED.groundstate() Etebd = np.sum(M.bond_energies(psi)) Eexact = np.min(ED.E) print("E_TEBD={Etebd:.14f} vs E_exact={Eex:.14f}".format(Etebd=Etebd, Eex=Eexact)) assert (abs((Etebd - Eexact) / Eexact) < 1.e-7) ov = npc.inner(psi_ED, ED.mps_to_full(psi), do_conj=True) print("compare with ED: overlap = ", abs(ov)**2) assert (abs(abs(ov) - 1.) < 1.e-7) # Test real time TEBD: should change on an eigenstate Sold = np.average(psi.entanglement_entropy()) for i in range(3): engine.run() Enew = np.sum(M.bond_energies(psi)) Snew = np.average(psi.entanglement_entropy()) assert (abs(Enew - Etebd) < 1.e-8) assert (abs(Sold - Snew) < 1.e-5) # somehow we need larger tolerance here.... if bc_MPS == 'infinite': Etebd = np.average(M.bond_energies(psi)) Eexact = e0_tranverse_ising(g) print("E_TEBD={Etebd:.14f} vs E_exact={Eex:.14f}".format(Etebd=Etebd, Eex=Eexact)) Sold = np.average(psi.entanglement_entropy()) for i in range(2): engine.run() Enew = np.average(M.bond_energies(psi)) Snew = np.average(psi.entanglement_entropy()) assert (abs(Etebd - Enew) < 1.e-7) assert (abs(Sold - Snew) < 1.e-5) # somehow we need larger tolerance here....
def test_dmrg(bc_MPS, combine, mixer, n, g=1.2): L = 2 if bc_MPS == 'infinite' else 8 model_params = dict(L=L, J=1., g=g, bc_MPS=bc_MPS, conserve=None, verbose=0) M = TFIChain(model_params) state = [0] * L # Ferromagnetic Ising psi = mps.MPS.from_product_state(M.lat.mps_sites(), state, bc=bc_MPS) dmrg_pars = { 'verbose': 5, 'combine': combine, 'mixer': mixer, 'chi_list': { 0: 10, 5: 30 }, 'max_E_err': 1.e-12, 'max_S_err': 1.e-8, 'N_sweeps_check': 4, 'mixer_params': { 'disable_after': 15, 'amplitude': 1.e-5 }, 'trunc_params': { 'svd_min': 1.e-10, }, 'max_N_for_ED': 20, # small enough that we test both diag_method=lanczos and ED_block! 'max_sweeps': 40, 'active_sites': n, } if not mixer: del dmrg_pars['mixer_params'] # avoid warning of unused parameter if bc_MPS == 'infinite': # if mixer is not None: # dmrg_pars['mixer_params']['amplitude'] = 1.e-12 # don't actually contribute... dmrg_pars['start_env'] = 1 res = dmrg.run(psi, M, dmrg_pars) if bc_MPS == 'finite': ED = ExactDiag(M) ED.build_full_H_from_mpo() ED.full_diagonalization() E_ED, psi_ED = ED.groundstate() ov = npc.inner(psi_ED, ED.mps_to_full(psi), 'range', do_conj=True) print("E_DMRG={Edmrg:.14f} vs E_exact={Eex:.14f}".format(Edmrg=res['E'], Eex=E_ED)) print("compare with ED: overlap = ", abs(ov)**2) assert abs(abs(ov) - 1.) < 1.e-10 # unique groundstate: finite size gap! var = M.H_MPO.variance(psi) assert var < 1.e-10 else: # compare exact solution for transverse field Ising model Edmrg = res['E'] Eexact = e0_tranverse_ising(g) print("E_DMRG={Edmrg:.12f} vs E_exact={Eex:.12f}".format(Edmrg=Edmrg, Eex=Eexact)) print("relative energy error: {err:.2e}".format(err=abs((Edmrg - Eexact) / Eexact))) print("norm err:", psi.norm_test()) Edmrg2 = np.mean(psi.expectation_value(M.H_bond)) Edmrg3 = M.H_MPO.expectation_value(psi) assert abs((Edmrg - Eexact) / Eexact) < 1.e-10 assert abs((Edmrg - Edmrg2) / Edmrg2) < max(1.e-10, np.max(psi.norm_test())) assert abs((Edmrg - Edmrg3) / Edmrg3) < max(1.e-10, np.max(psi.norm_test()))
def check_dmrg(L=4, bc_MPS='finite', engine='EngineCombine', mixer=None, g=1.5): model_params = dict(L=L, J=1., g=g, bc_MPS=bc_MPS, conserve=None, verbose=0) M = TFIChain(model_params) state = [0] * L # Ferromagnetic Ising psi = mps.MPS.from_product_state(M.lat.mps_sites(), state, bc=bc_MPS) dmrg_pars = { 'verbose': 5, 'engine': engine, 'mixer': mixer, 'chi_list': { 0: 10, 5: 30 }, 'max_E_err': 1.e-12, 'max_S_err': 1.e-8, 'N_sweeps_check': 4, 'mixer_params': { 'disable_after': 6, 'amplitude': 1.e-5 }, 'trunc_params': { 'svd_min': 1.e-10, }, 'lanczos_params': { 'reortho': True, 'N_cache': 20 }, 'max_sweeps': 40, } if mixer is None: del dmrg_pars['mixer_params'] # avoid warning of unused parameter if bc_MPS == 'infinite': if mixer is not None: dmrg_pars['mixer_params']['amplitude'] = 1.e-12 # don't actually contribute... dmrg_pars['start_env'] = 1 res = dmrg.run(psi, M, dmrg_pars) if bc_MPS == 'finite': ED = ExactDiag(M) ED.build_full_H_from_mpo() ED.full_diagonalization() psi_ED = ED.groundstate() ov = npc.inner(psi_ED, ED.mps_to_full(psi), do_conj=True) print("E_DMRG={Edmrg:.14f} vs E_exact={Eex:.14f}".format(Edmrg=res['E'], Eex=np.min(ED.E))) print("compare with ED: overlap = ", abs(ov)**2) assert abs(abs(ov) - 1.) < 1.e-10 # unique groundstate: finite size gap! else: # compare exact solution for transverse field Ising model Edmrg = res['E'] Eexact = e0_tranverse_ising(g) print("E_DMRG={Edmrg:.12f} vs E_exact={Eex:.12f}".format(Edmrg=Edmrg, Eex=Eexact)) print("relative energy error: {err:.2e}".format(err=abs((Edmrg - Eexact) / Eexact))) print("norm err:", psi.norm_test()) Edmrg2 = np.mean(psi.expectation_value(M.H_bond)) assert abs((Edmrg - Eexact) / Eexact) < 1.e-10 assert abs((Edmrg - Edmrg2) / Edmrg2) < max(1.e-10, np.max(psi.norm_test()))
xxz_pars = dict(L=4, Jxx=1., Jz=1., hz=0.0, bc_MPS='finite') M = XXZChain(xxz_pars) ED = ExactDiag(M, [0]) ED.build_full_H_from_mpo() # ED.build_full_H_from_bonds() # whatever you prefer print("start diagonalization") ED.full_diagonalization() psi_ED = ED.groundstate() print("psi_ED =", psi_ED) print("start DMRG") product_state = [0, 1] * (xxz_pars['L'] // 2) # this selects a charge sector! psi_DMRG = MPS.from_product_state(M.lat.mps_sites(), product_state) res = run_DMRG(psi_DMRG, M, {'verbose': 0}) # first way to compare ED with DMRG: convert MPS to ED vector psi_DMRG_full = ED.mps_to_full(psi_DMRG) print("psi_DMRG_full =", psi_DMRG_full) ov = abs(npc.inner(psi_ED, psi_DMRG_full, do_conj=True)) print("|<psi_ED|psi_DMRG>| =", ov) assert (abs(ov - 1.) < 1.e-13) # second way: convert ED vector to MPS psi_ED_mps = ED.full_to_mps(psi_ED) ov = psi_ED_mps.overlap(psi_DMRG) print("|<psi_ED_mps|psi_DMRG>| =", abs(ov)) assert (abs(abs(ov) - 1.) < 1.e-13) # -> advantange: expectation_value etc. of MPS are available! print("<Sz> =", psi_ED_mps.expectation_value('Sz'))