forked from GagliardiGroup/las-qpe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_qpe.py
116 lines (92 loc) · 3.4 KB
/
test_qpe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
'''
Minimal test to make sure nothing breaks
while updating for qiskit version
Checking against hardcoded values
'''
import numpy as np
# PySCF imports
from pyscf import gto, scf, lib, mcscf, ao2mo
from pyscf.tools import fcidump
# mrh imports
from mrh.my_pyscf.mcscf.lasscf_o0 import LASSCF
from mrh.my_pyscf.mcscf.lasci import h1e_for_cas
# Qiskit imports
from qiskit.utils import QuantumInstance
from qiskit.algorithms import NumPyEigensolver
from qiskit import Aer
from get_geom import get_geom
from get_hamiltonian import get_hamiltonian
def test_hf():
'''Test the PySCF RHF'''
xyz = get_geom('close')
mol = gto.M (atom = xyz, basis = 'sto-3g', output='h4_sto3g.log',
symmetry=False)
# Do RHF
mf = scf.RHF(mol).run()
assert(np.allclose(mf.e_tot, -2.0272406157535965))
def test_las():
'''Test the MRH LASSCF against hardcoded value'''
xyz = get_geom('close')
mol = gto.M (atom = xyz, basis = 'sto-3g', output='h4_sto3g.log',
symmetry=False)
# Do RHF
mf = scf.RHF(mol).run()
# Set up LASSCF
las = LASSCF(mf, (2,2),(2,2), spin_sub=(1,1))
frag_atom_list = ((0,1),(2,3))
loc_mo_coeff = las.localize_init_guess(frag_atom_list, mf.mo_coeff)
# Run LASSCF
las.kernel(loc_mo_coeff)
assert(np.allclose(las.e_tot, -2.102635582026016))
def test_las_cas():
'''Test the 1-fragment MRH LASSCF against CASSCF value'''
xyz = get_geom('close')
mol = gto.M (atom = xyz, basis = 'sto-3g', output='h4_sto3g.log',
symmetry=False)
# Do RHF
mf = scf.RHF(mol).run()
# Set up LASSCF
las = LASSCF(mf, (4,),(4,), spin_sub=(1,))
frag_atom_list = ((0,1,2,3),)
loc_mo_coeff = las.localize_init_guess(frag_atom_list, mf.mo_coeff)
# Run LASSCF
las.kernel(loc_mo_coeff)
# Set up CASSCF
cas = mcscf.CASSCF(mf, 4, 4)
# Run CASSCF
cas.kernel(loc_mo_coeff)
assert(np.allclose(las.e_tot, cas.e_tot))
def test_frag_np_eig():
'''Test the LAS fragment numpy eigensolver against hardcoded value'''
xyz = get_geom('close')
mol = gto.M (atom = xyz, basis = 'sto-3g', output='h4_sto3g.log',
symmetry=False)
# Do RHF
mf = scf.RHF(mol).run()
# Set up LASSCF
las = LASSCF(mf, (2,2),(2,2), spin_sub=(1,1))
frag_atom_list = ((0,1),(2,3))
loc_mo_coeff = las.localize_init_guess(frag_atom_list, mf.mo_coeff)
# Run LASSCF
las.kernel(loc_mo_coeff)
loc_mo_coeff = las.mo_coeff
# Using the built-in LASCI functions h1e_for_cas, get_h2eff
h1_las = las.h1e_for_cas()
eri_las = las.get_h2eff(loc_mo_coeff)
# Storing each fragment's h1 and h2 as a list
h1_frag = []
h2_frag = []
# Then construct h1, h2 for each fragment
for idx in range(len(las.ncas_sub)):
h1_frag.append(h1_las[idx][0][0])
h2_frag.append(las.get_h2eff_slice(eri_las, idx))
np_val = [-1.62002897,-1.59428151]
for frag in range(len(las.ncas_sub)):
hamiltonian = get_hamiltonian(frag, las.nelecas_sub, las.ncas_sub, h1_frag, h2_frag)
# Set the backend
quantum_instance = QuantumInstance(backend = Aer.get_backend('aer_simulator'), shots=1024)
# Numpy solver to estimate error in QPE energy due to trotterization
np_solver = NumPyEigensolver(k=1)
ed_result = np_solver.compute_eigenvalues(hamiltonian)
print("NumPy result: ", ed_result.eigenvalues)
assert(np.allclose(ed_result.eigenvalues[0], np_val[frag]))