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# Read in the FCIDUMP
30psi4group = 5 # c2v: see chemps2/Irreps.h
31filename  = b'../../tests/matrixelements/CH4.STO3G.FCIDUMP'
32orbirreps = np.array([-1, -1], dtype=ctypes.c_int) # CheMPS2 reads it in from FCIDUMP
33Ham = PyCheMPS2.PyHamiltonian( -1, psi4group, orbirreps, filename )
34
35# Define the symmetry sector
36TwoS  = 0    # Two times the targeted spin
37Nelec = 10   # The number of electrons
38Irrep = 0    # The targeted irrep
39
40# Setting up the Problem
41Prob = PyCheMPS2.PyProblem(Ham, TwoS, Nelec, Irrep)
42
43# Setting up the ConvergenceScheme
44# setInstruction(instruction, D, Econst, maxSweeps, noisePrefactor)
45OptScheme = PyCheMPS2.PyConvergenceScheme(2) # 2 instructions
46OptScheme.setInstruction(0,   30, 1e-10,  3, 0.1)
47OptScheme.setInstruction(1, 1000, 1e-10, 10, 0.0)
48
49# Do DMRG calculation
50theDMRG = PyCheMPS2.PyDMRG(Prob, OptScheme)
51EnergyDMRG = theDMRG.Solve()
52do_3rdm = True
53theDMRG.calc_rdms_and_correlations( do_3rdm )
54
55# Get a diagonal part of the 4-RDM from DMRG
56L = Ham.getL()
57ham_orbz = 2
58dmrg_diag_4rdm = np.zeros([ L**6 ], dtype=ctypes.c_double)
59theDMRG.Symm4RDM( dmrg_diag_4rdm, ham_orbz, ham_orbz, True )
60
61# Do FCI calculation
62Nel_up   = ( Nelec + TwoS ) / 2
63Nel_down = ( Nelec - TwoS ) / 2
64maxMemWorkMB = 10.0
65FCIverbose = 1
66theFCI = PyCheMPS2.PyFCI(Ham, Nel_up, Nel_down, Irrep, maxMemWorkMB, FCIverbose)
67GSvector = np.zeros([ theFCI.getVecLength() ], dtype=ctypes.c_double)
68GSvector[ theFCI.LowestEnergyDeterminant() ] = 1.0
69EnergyFCI = theFCI.GSDavidson(GSvector)
70theFCI.CalcSpinSquared(GSvector)
71TwoRDM = np.zeros([ L**4 ], dtype=ctypes.c_double)
72theFCI.Fill2RDM(GSvector, TwoRDM)
73RMSerror2DM = 0.0
74for i in range(L):
75    for j in range(L):
76        for k in range(L):
77            for l in range(L):
78                temp = TwoRDM[i + L*(j + L*(k + L*l))] - theDMRG.get2DMA(i,j,k,l)
79                RMSerror2DM += temp*temp
80ThreeRDM = np.zeros([ L**6 ], dtype=ctypes.c_double)
81theFCI.Fill3RDM(GSvector, ThreeRDM)
82RMSerror3DM = 0.0
83for i in range(L):
84    for j in range(L):
85        for k in range(L):
86            for l in range(L):
87                for m in range(L):
88                    for n in range(L):
89                        temp = ThreeRDM[i + L*(j + L*(k + L*(l + L*(m + L*n))))] - theDMRG.get3DM(i,j,k,l,m,n)
90                        RMSerror3DM += temp*temp
91fci_diag_4rdm = np.zeros([ L**6 ], dtype=ctypes.c_double)
92theFCI.Diag4RDM( GSvector, ThreeRDM, ham_orbz, fci_diag_4rdm )
93RMSerror4DM = np.linalg.norm( 0.5 * dmrg_diag_4rdm - fci_diag_4rdm )
94RMSerror2DM = np.sqrt(RMSerror2DM)
95RMSerror3DM = np.sqrt(RMSerror3DM)
96print("Frobenius norm of the difference of the DMRG and FCI 2-RDM =", RMSerror2DM)
97print("Frobenius norm of the difference of the DMRG and FCI 3-RDM =", RMSerror3DM)
98print("Frobenius norm of the difference of the DMRG and FCI diag(4-RDM) for fixed orbital", ham_orbz, "=", RMSerror4DM)
99del theFCI
100
101OptScheme.setInstruction(0, 1500, 1e-10,  3, 0.0)
102OptScheme.setInstruction(1, 2000, 1e-10, 10, 0.0)
103EnergyDMRG = theDMRG.Solve()
104theDMRG.calc2DMandCorrelations()
105
106# Clean-up
107# theDMRG.deleteStoredMPS()
108theDMRG.deleteStoredOperators()
109del theDMRG
110del OptScheme
111del Prob
112del Ham
113del Initializer
114
115# Check whether the test succeeded
116if ((np.fabs(EnergyDMRG - EnergyFCI) < 1e-8) and (RMSerror2DM < 1e-3) and (RMSerror3DM < 1e-3) and (RMSerror4DM < 1e-3)):
117    print("================> Did test 10 succeed : yes")
118else:
119    print("================> Did test 10 succeed : no")
120
121