1# 2# CheMPS2: a spin-adapted implementation of DMRG for ab initio quantum chemistry 3# Copyright (C) 2013-2018 Sebastian Wouters 4# 5# This program is free software; you can redistribute it and/or modify 6# it under the terms of the GNU General Public License as published by 7# the Free Software Foundation; either version 2 of the License, or 8# (at your option) any later version. 9# 10# This program is distributed in the hope that it will be useful, 11# but WITHOUT ANY WARRANTY; without even the implied warranty of 12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13# GNU General Public License for more details. 14# 15# You should have received a copy of the GNU General Public License along 16# with this program; if not, write to the Free Software Foundation, Inc., 17# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18# 19 20import numpy as np 21import sys 22import PyCheMPS2 23import ctypes 24 25# Set the seed of the random number generator and cout.precision 26Initializer = PyCheMPS2.PyInitialize() 27Initializer.Init() 28 29######################## 30### 1D Hubbard model ### 31######################## 32 33L = 10 # Number of lattice sites 34Group = 0 # C1 symmetry 35U = 2.0 # On-site repulsion 36T = -1.0 # Hopping term 37 38TwoS = 5 # Two times the targeted spin 39Nelec = 9 # The number of electrons 40Irrep = 0 # The targeted irrep 41 42# The Hamiltonian initializes all its matrix elements to 0.0 43orbirreps = np.zeros([L], dtype=ctypes.c_int) 44Ham = PyCheMPS2.PyHamiltonian(L, Group, orbirreps) 45for cnt in range(0, L): 46 Ham.setVmat(cnt, cnt, cnt, cnt, U) 47for cnt in range(0, L-1): 48 Ham.setTmat(cnt, cnt+1, T) 49 50# Setting up the Problem 51Prob = PyCheMPS2.PyProblem(Ham, TwoS, Nelec, Irrep) 52 53# Setting up the ConvergenceScheme 54# setInstruction(instruction, D, Econst, maxSweeps, noisePrefactor) 55OptScheme = PyCheMPS2.PyConvergenceScheme(2) # 2 instructions 56OptScheme.setInstruction(0, 30, 1e-10, 3, 0.1) 57OptScheme.setInstruction(1, 1000, 1e-10, 10, 0.0) 58 59# Do DMRG calculation and print the correlations 60theDMRG = PyCheMPS2.PyDMRG(Prob, OptScheme) 61EnergyDMRG = theDMRG.Solve() 62theDMRG.calc2DMandCorrelations() 63theDMRG.printCorrelations() 64 65# Clean-up 66# theDMRG.deleteStoredMPS() 67theDMRG.deleteStoredOperators() 68del theDMRG 69del OptScheme 70del Prob 71 72# Do FCI calculation 73Nel_up = ( Nelec + TwoS ) / 2 74Nel_down = ( Nelec - TwoS ) / 2 75maxMemWorkMB = 10.0 76FCIverbose = 1 77theFCI = PyCheMPS2.PyFCI(Ham, Nel_up, Nel_down, Irrep, maxMemWorkMB, FCIverbose) 78GSvector = np.zeros([ theFCI.getVecLength() ], dtype=ctypes.c_double) 79theFCI.FillRandom( theFCI.getVecLength() , GSvector ) 80EnergyFCI = theFCI.GSDavidson(GSvector) 81 82# Clean-up 83del theFCI 84del Ham 85del Initializer 86 87# Check whether the test succeeded 88if (np.fabs(EnergyDMRG - EnergyFCI) < 1e-8): 89 print("================> Did test 4 succeed : yes") 90else: 91 print("================> Did test 4 succeed : no") 92 93