TDVP run time issue

How do I use this algorithm? What does that parameter do?
Post Reply
Mrinal
Posts: 2
Joined: 11 Dec 2020, 21:37

TDVP run time issue

Post by Mrinal »

Hello!!! First, I thank your group for making such a nice TeNPy Package. Here I attached the TDVP of the Bose-Hubbard ladder model.

Code: Select all

import numpy as np
from tenpy.networks.mps import MPS
from tenpy.models.lattice import Ladder
#from tenpy.models.hubbard import BoseHubbardChain
from tenpy.algorithms import dmrg
from tenpy.networks.site import BosonSite
from tenpy.models.model import MultiCouplingModel,CouplingMPOModel
import tenpy.linalg.np_conserved as npc
from mpl_toolkits.axes_grid1 import make_axes_locatable
from tenpy.algorithms import tdvp
import copy
import matplotlib.pyplot as plt
from matplotlib import style
from matplotlib.gridspec import GridSpec
import matplotlib.cm as cm
import matplotlib


class Bose_hubbard_ladder(CouplingMPOModel, MultiCouplingModel):
    
    def init_sites(self, model_params):
        n_max = model_params.get('n_max', 3)
        filling = model_params.get('filling', 0.5)
        conserve = model_params.get('conserve', 'N')
        if conserve == 'best':
            conserve = 'N'
            if self.verbose >= 1.:
                print(self.name + ": set conserve to", conserve)
        site = BosonSite(Nmax=n_max, conserve=conserve, filling=filling)
        return site
    
    def init_lattice(self,model_params):
        site = self.init_sites(model_params)
        L = model_params.get('L',2)
        bc_MPS = model_params.get('bc_MPS','infinite')
        bc = 'periodic' if bc_MPS =='infinite' else 'open'
        bc = model_params.get('bc',bc)
        lat = Ladder(L,site,bc=bc,bc_MPS=bc_MPS)
        return lat
    
    def init_terms(self, model_params):
        t1 = model_params.get('t1', 1.)
        t2 = model_params.get('t2', 1.)
        phi = model_params.get('phi', 0.)
        U = model_params.get('U', 0.)
        #V = model_params.get('V', 0.)
        mu = model_params.get('mu', 0)
        #for u in range(len(self.lat.unit_cell)):
        self.add_onsite(-mu - U / 2., 0, 'N')
        self.add_onsite(-mu - U / 2., 1, 'N')
        
        self.add_onsite(U / 2., 0, 'NN')
        self.add_onsite(U / 2., 1, 'NN')
        self.add_coupling(-t1, 0, 'Bd', 0, 'B', 1)
        self.add_coupling(-t1, 0, 'B', 0, 'Bd', 1)
        
        self.add_coupling(-t1, 1, 'Bd', 1, 'B', 1)
        self.add_coupling(-t1, 1, 'B', 1, 'Bd', 1) 
        
        self.add_coupling(-t2, 0, 'Bd', 1, 'B', 0) 
        self.add_coupling(-t2, 0, 'B', 1, 'Bd', 0) 
        



def Bose_Hubbard_ladder(L,U,t1,t2,mu,N0,nmax,tmax,dt, verbose,Bond_dmrg):
    model_params = dict(L=L,
                        n_max = nmax,
                        t1=t1,
                        t2=t2,
                        U=U,
                        mu=mu,
                        bc_MPS='finite',
                        conserve='N',
                        verbose=1)
    M = Bose_hubbard_ladder(model_params)
    product_state=[0]*M.lat.N_sites
    
    product_state[int(L)] = 1
    product_state[int(L-1)] = 1

    psi = MPS.from_product_state(M.lat.mps_sites(), product_state, bc=M.lat.bc_MPS)
    
    tdvp_params = {
        'start_time': 0,
        'dt': dt,
        'trunc_params': {
            'chi_max': 50,
            'svd_min': 1.e-10,
            'trunc_cut': None
        }
    }
        
    
    tdvp_engine = tdvp.Engine(psi, M, tdvp_params)
    times = []
    N_list = []

    for i in range(int(tmax/dt)):
        tdvp_engine.run_two_sites(N_steps=1)
        times.append(tdvp_engine.evolved_time)
        N_list.append(psi.expectation_value("N"))
    
    Nf_list = []
    for k in N_list:
        nl = []
        nu = []
        for i in range(2*L):
            if i%2 ==0:
                nl.append(k[i])
            else:
                nu.append(k[i])
        Nf = []
        for i in range(L):
            Nf.append(nu[i]+nl[i])
        Nf_list.append(Nf)
        
                
        
    print(N_list[0])
    
    L_center = int(L/2)
    t_f = tmax
    txt_font_size = 20
    
    """*=*=*=*=*=*=*=*=*=*=*==*=*==*=**=***==***=**=***=*=***=**=**==***=*=***==*==***==*===**"""
    cvals  = [1.0, 1.4, 3.0]
    colors = ["white","red","darkred"]
    #colors = ["blue","yellow","red"]
    norm=plt.Normalize(min(cvals),max(cvals))
    tuples = list(zip(map(norm,cvals), colors))
    cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", tuples)
    """*=*=*=*=*=*=*=*=*=*=*==*=*==*=**=***==***=**=***=*=***=**=**==***=*=***==*==***==*===**"""
    
    fig = plt.figure(figsize =(10,10))
    ax1=fig.add_subplot(111)
    im1 = ax1.imshow(Nf_list,extent=[-L_center,L_center,t_f,0],interpolation='Nearest', aspect='auto',cmap=cmap, vmin=0.,vmax=1.)

    ax1.tick_params(axis='x', which='both', direction='out',top='off',labelsize=txt_font_size)
    ax1.tick_params(axis='y', which='both', direction='out',right='off',labelsize=txt_font_size)
   
    ax1.set_xlabel('Position (Sites)',fontweight="bold",fontsize=txt_font_size)
    ax1.set_ylabel("Time (units of $ J^{-1}$)",fontweight="bold",fontsize=txt_font_size+2)
    ax1 = plt.gca()
    
    divider = make_axes_locatable(ax1)
    cax = divider.append_axes("right", size="5%", pad=0.1)
    cbar=plt.colorbar(im1, cax=cax)
    cbar.set_label('$\mathbf{<n>}$', rotation=90)
    plt.show()

   
    
if __name__ == "__main__":
    Bose_Hubbard_ladder(L=20,U=0.0,t1=1,t2=1.0,mu=0.0,N0=2,nmax=2,tmax=7.0,dt=0.01, verbose=True,Bond_dmrg=50)
    print("*=" * 100)

        
    
    
1. To run this code I face some warnings like that -

Code: Select all

/home/.local/lib/python3.6/site-packages/tenpy/tools/optimization.py:278: UserWarning: Couldn't load compiled cython code. Code will run a bit slower.
  warnings.warn("Couldn't load compiled cython code. Code will run a bit slower.")
/home/.local/lib/python3.6/site-packages/tenpy/linalg/svd_robust.py:54: UserWarning: Import problems: the work-around `svd_gesvd` will fail.
  warnings.warn("Import problems: the work-around `svd_gesvd` will fail.")
parameter 'verbose'=1 for Bose_hubbard_ladder
/home/.local/lib/python3.6/site-packages/tenpy/tools/params.py:124: UserWarning: unused parameters for Bose_hubbard_ladder:
['L', 'U', 'bc_MPS', 'conserve', 'mu', 'n_max', 'phi', 't1', 't2']
  warnings.warn(msg.format(keys=sorted(unused), descr=warn))
I install and upgrade the cython, still, these issues present there :cry: .

2. This code is running and results are matched with Exact and openmps results. But problem is that it takes a huge time (4-5 hours or more) to produce the results compared to the Exact and OpenMPS results(5 to 10 min). The same scenario occurs for 2D also. Can you please help me to understand why this is happening? :cry: :cry:
User avatar
Johannes
Site Admin
Posts: 413
Joined: 21 Jul 2018, 12:52
Location: TU Munich

Re: TDVP run time issue

Post by Johannes »

1) How did you try to install TeNPy? It looks like it's using a version installed with `pip` into the local home directory.
Can you try to pip uninstall physics-tenpy and then try to re-install TeNPy?
Moreover, the svd_gesvd hints that you're using a very old version of scipy?
What python are you using? The system-wide Python on a ubuntu?
Do you also have a C++ compiler?

You can avoid these issues if you just install miniconda and then install the conda-package - then it should work out of the box.

2) There might be some optimizations possible for TeNPy's TDVP implementation. Overall, it would actually be good to re-write TDVP to also use the MPS sweep engines - that's a long standing ToDo. Do you want to contribute? ;-)
Overall, the run-time will quite sensitive to the parameters you choose, and tweaking them can already make a big difference.
To allow a fair "apples vs apples" comparison between libraries, we need to make sure we're using the same settings.
I'm not familiar with OpenMPS. Would you mind to post the code you used for comparison such that I can try to compare?
Mrinal
Posts: 2
Joined: 11 Dec 2020, 21:37

Re: TDVP run time issue

Post by Mrinal »

Hi Johannes,
Thanks for the reply, as well as shame on me for the late reply. :D
1. After re-installing TeNPy, It's working nicely even for TDVP also.
2. If you want OpenMPS code, I can share it here, but TenPy has several advantages like it can handle complex hamiltonian, 2D DMRG also works very nice. I am quite happy to use TeNPy. OpenMPS a little bit faster due to the entire module written in FORTRAN. I would be pleased if I was able to contribute here.

3. Now, I have a small query regarding the TDVP or TEBD case: When I was calculating the expectation of the on-site number operator and compare it with my exact result, there is much deviation after few steps(like 50-60 steps). For example I attache some results of exact and TeNPy results after 100 steps(dt=0.01,ti=0,tf=1.0):
Model: (5*5) 2D square lattice, N=2,nmax=2,U=0, tx=1,ty=1,chi_max=500,
Initial state: 2 particles are placed at the site, L=12 (center of the square)
I did the time evolution and measure <ni> at 100 step

Exact results:
[[0.0474872 0.09948522 0.01423433 0.09948522 0.0474872 ]
[0.09948522 0.20842056 0.02982078 0.20842056 0.09948522]
[0.01423433 0.02982078 0.00426675 0.02982078 0.01423433]
[0.09948522 0.20842056 0.02982078 0.20842056 0.09948522]
[0.0474872 0.09948522 0.01423433 0.09948522 0.0474872 ]]

TeNPy Results:
[[0.04696842 0.09839919 0.01407563 0.09839544 0.04696963]
[0.10039903 0.21035328 0.03009599 0.21034523 0.10040352]
[0.01538543 0.03224 0.00461482 0.03223909 0.01538609]
[0.09945608 0.20837954 0.02981528 0.20837395 0.0994601 ]
[0.0459578 0.09628765 0.01377576 0.09628437 0.04595937]]

Is it trivial due to the variational principle used in TDVP?
I really appreciate any help you can provide.
User avatar
Johannes
Site Admin
Posts: 413
Joined: 21 Jul 2018, 12:52
Location: TU Munich

Re: TDVP run time issue

Post by Johannes »

1) Glad that this helped. Watch out for the new version of TeNPy that will be released in the next few days!
2) You're more than welcome to contribute! There's plenty of things to be done.

Hmm, I'm not completely sure, but it might just be that chi=500 is too small to reach that time. I did some similar expansion calculations back in the days when I started my PHD, see arXiv:1509.00696, in that case with hard-core bosons and (another implementation of) the tenpy.algorithms.mpo_evolution.ExpMPOEvolution. We were able to go up to t=1.5 hopping times with chi~=2000, but this was really the maximum we could do, but I could well imagine that you are more restricted to shorter times for soft-core bosons. Did you try comparing to another run at (significantly) smaller/higher bond dimension (say by a factor of 2), and see where the curves deviate?

Other sources of error in TDVP are the discretization time step and maybe even the cutoff N_max for the Lanczos evolution.
Post Reply