def test_h4(self): mol = gto.Mole() mol.verbose = 7 mol.output = '/dev/null' mol.atom = [ ['H', (1., -1., 0.)], ['H', (0., -1., -1.)], ['H', (1., -0.5, 0.)], ['H', (0., -1., 1.)], ] mol.charge = 2 mol.spin = 2 mol.basis = '3-21g' mol.build() mf = scf.RHF(mol).run(conv_tol=1e-14) myci = ci.CISD(mf) myci.kernel() self.assertAlmostEqual(myci.e_tot, -0.50569591904536926, 8) mf = scf.UHF(mol).run(conv_tol=1e-14) myci = ci.CISD(mf) ecisd = myci.kernel()[0] self.assertAlmostEqual(ecisd, -0.03598992307519909, 8) self.assertAlmostEqual(myci.e_tot, -0.50569591904536926, 8) eris = myci.ao2mo() ecisd = myci.kernel(eris=eris)[0] eri_aa = ao2mo.kernel(mf._eri, mf.mo_coeff[0]) eri_bb = ao2mo.kernel(mf._eri, mf.mo_coeff[1]) eri_ab = ao2mo.kernel( mf._eri, [mf.mo_coeff[0], mf.mo_coeff[0], mf.mo_coeff[1], mf.mo_coeff[1]]) h1a = reduce(numpy.dot, (mf.mo_coeff[0].T, mf.get_hcore(), mf.mo_coeff[0])) h1b = reduce(numpy.dot, (mf.mo_coeff[1].T, mf.get_hcore(), mf.mo_coeff[1])) efci, fcivec = fci.direct_uhf.kernel( (h1a, h1b), (eri_aa, eri_ab, eri_bb), h1a.shape[0], mol.nelec) self.assertAlmostEqual(mf.e_tot + ecisd, efci + mol.energy_nuc(), 9) dm1ref, dm2ref = fci.direct_uhf.make_rdm12s(fcivec, h1a.shape[0], mol.nelec) rdm1 = myci.make_rdm1(myci.ci, myci.get_nmo(), myci.get_nocc()) rdm2 = myci.make_rdm2(myci.ci, myci.get_nmo(), myci.get_nocc()) self.assertAlmostEqual(abs(dm1ref[0] - rdm1[0]).max(), 0, 5) self.assertAlmostEqual(abs(dm1ref[1] - rdm1[1]).max(), 0, 5) self.assertAlmostEqual(abs(dm2ref[0] - rdm2[0]).max(), 0, 5) self.assertAlmostEqual(abs(dm2ref[1] - rdm2[1]).max(), 0, 5) self.assertAlmostEqual(abs(dm2ref[2] - rdm2[2]).max(), 0, 5)
def test_trans_rdm(self): numpy.random.seed(1) myci = ci.CISD(scf.RHF(gto.M())) myci.nmo = norb = 4 myci.nocc = nocc = 2 nvir = norb - nocc c2 = numpy.random.random((nocc,nocc,nvir,nvir)) c2 = c2 + c2.transpose(1,0,3,2) cibra = numpy.hstack((numpy.random.random(1+nocc*nvir), c2.ravel())) c2 = numpy.random.random((nocc,nocc,nvir,nvir)) c2 = c2 + c2.transpose(1,0,3,2) ciket = numpy.hstack((numpy.random.random(1+nocc*nvir), c2.ravel())) cibra /= ci.cisd.dot(cibra, cibra, norb, nocc)**.5 ciket /= ci.cisd.dot(ciket, ciket, norb, nocc)**.5 fcibra = ci.cisd.to_fcivec(cibra, norb, nocc*2) fciket = ci.cisd.to_fcivec(ciket, norb, nocc*2) fcidm1, fcidm2 = fci.direct_spin1.make_rdm12(fciket, norb, nocc*2) cidm1 = ci.cisd.make_rdm1(myci, ciket, norb, nocc) cidm2 = ci.cisd.make_rdm2(myci, ciket, norb, nocc) self.assertAlmostEqual(abs(fcidm1-cidm1).max(), 0, 12) self.assertAlmostEqual(abs(fcidm2-cidm2).max(), 0, 12) fcidm1 = fci.direct_spin1.trans_rdm1(fcibra, fciket, norb, nocc*2) cidm1 = ci.cisd.trans_rdm1(myci, cibra, ciket, norb, nocc) self.assertAlmostEqual(abs(fcidm1-cidm1).max(), 0, 12)
def test_rdm1(self): mol = gto.Mole() mol.verbose = 0 mol.atom = [ ['O', ( 0., 0. , 0. )], ['H', ( 0., -0.757, 0.587)], ['H', ( 0., 0.757 , 0.587)],] mol.basis = '631g' mol.build() mf = scf.RHF(mol).run(conv_tol=1e-14) myci = ci.CISD(mf) myci.frozen = 1 myci.kernel() nmo = myci.nmo nocc = myci.nocc d1 = cisd._gamma1_intermediates(myci, myci.ci, nmo, nocc) myci.max_memory = 0 d2 = cisd._gamma2_intermediates(myci, myci.ci, nmo, nocc, True) dm2 = cisd.ccsd_rdm._make_rdm2(myci, d1, d2, with_dm1=True, with_frozen=True) dm1 = numpy.einsum('ijkk->ij', dm2)/(mol.nelectron-1) h1 = reduce(numpy.dot, (mf.mo_coeff.T, mf.get_hcore(), mf.mo_coeff)) eri = ao2mo.restore(1, ao2mo.kernel(mf._eri, mf.mo_coeff), nmo+1) e1 = numpy.einsum('ij,ji', h1, dm1) e1+= numpy.einsum('ijkl,ijkl', eri, dm2) * .5 e1+= mol.energy_nuc() self.assertAlmostEqual(e1, myci.e_tot, 7)
def test_reset(self): mol = gto.M(atom='He') mol1 = gto.M(atom='C') myci = ci.CISD(scf.UHF(mol).newton()) myci.reset(mol1) self.assertTrue(myci.mol is mol1) self.assertTrue(myci._scf.mol is mol1)
def test_make_natural_orbitals_from_unrestricted(self): from pyscf import mp, ci, cc npt = numpy.testing mol = gto.M(atom = 'O 0 0 0; O 0 0 1.2', basis = '3-21g', spin = 2) myhf = scf.UHF(mol).run() ncas, nelecas = (8, 12) mymc = mcscf.CASCI(myhf, ncas, nelecas) # Test MP2 # Trusted results from ORCA v4.2.1 rmp2_noons = [1.99992786,1.99992701,1.99414062,1.98906552,1.96095173,1.96095165,1.95280755,1.02078458,1.02078457,0.04719006,0.01274288,0.01274278,0.00728679,0.00582683,0.00543964,0.00543962,0.00290772,0.00108258] mymp = mp.UMP2(myhf).run() noons, natorbs = mcscf.addons.make_natural_orbitals(mymp) npt.assert_array_almost_equal(rmp2_noons, noons) mymc.kernel(natorbs) # The tests below are only to ensure that `make_natural_orbitals` can # run at all since we've confirmed above that the NOONs are correct. # Test CISD mycisd = ci.CISD(myhf).run() noons, natorbs = mcscf.addons.make_natural_orbitals(mycisd) mymc.kernel(natorbs) # Test CCSD myccsd = cc.CCSD(myhf).run() noons, natorbs = mcscf.addons.make_natural_orbitals(myccsd) mymc.kernel(natorbs)
def test_make_natural_orbitals_from_restricted(self): from pyscf import mp, ci, cc npt = numpy.testing mol = gto.M(atom='C 0 0 0; O 0 0 1.2', basis='3-21g', spin=0) myhf = scf.RHF(mol).run() ncas, nelecas = (8, 10) mymc = mcscf.CASCI(myhf, ncas, nelecas) # Test MP2 # Trusted results from ORCA v4.2.1 rmp2_noons = [ 1.99992732, 1.99989230, 1.99204357, 1.98051334, 1.96825487, 1.94377615, 1.94376239, 0.05792320, 0.05791037, 0.02833335, 0.00847013, 0.00531989, 0.00420320, 0.00420280, 0.00257965, 0.00101638, 0.00101606, 0.00085503 ] mymp = mp.MP2(myhf).run() noons, natorbs = mcscf.addons.make_natural_orbitals(mymp) npt.assert_array_almost_equal(rmp2_noons, noons, decimal=5) mymc.kernel(natorbs) # The tests below are only to ensure that `make_natural_orbitals` can # run at all since we've confirmed above that the NOONs are correct. # Test CISD mycisd = ci.CISD(myhf).run() noons, natorbs = mcscf.addons.make_natural_orbitals(mycisd) mymc.kernel(natorbs) # Test CCSD myccsd = cc.CCSD(myhf).run() noons, natorbs = mcscf.addons.make_natural_orbitals(myccsd) mymc.kernel(natorbs)
def test_contract(self): mol = gto.M() mol.nelectron = 6 nocc, nvir = mol.nelectron // 2, 4 nmo = nocc + nvir nmo_pair = nmo * (nmo + 1) // 2 mf = scf.RHF(mol) numpy.random.seed(12) mf._eri = numpy.random.random(nmo_pair * (nmo_pair + 1) // 2) mf.mo_coeff = numpy.random.random((nmo, nmo)) mf.mo_energy = numpy.arange(0., nmo) mf.mo_occ = numpy.zeros(nmo) mf.mo_occ[:nocc] = 2 dm = mf.make_rdm1() vhf = mf.get_veff(mol, dm) h1 = numpy.random.random((nmo, nmo)) * .1 h1 = h1 + h1.T mf.get_hcore = lambda *args: h1 myci = ci.CISD(mf) eris = myci.ao2mo(mf.mo_coeff) eris.ehf = (h1 * dm).sum() + (vhf * dm).sum() * .5 c2 = numpy.random.random((nocc, nocc, nvir, nvir)) * .1 c2 = c2 + c2.transpose(1, 0, 3, 2) civec = numpy.hstack( (numpy.random.random(nocc * nvir + 1) * .1, c2.ravel())) hcivec = ci.cisd.contract(myci, civec, eris) self.assertAlmostEqual(finger(hcivec), 2059.5730673341673, 9) e2 = ci.cisd.dot(civec, hcivec + eris.ehf * civec, nocc, nvir) self.assertAlmostEqual(e2, 7226.7494656749295, 9)
def test_contract(self): mol = gto.M() mol.nelectron = 6 nocc, nvir = mol.nelectron//2, 4 nmo = nocc + nvir nmo_pair = nmo*(nmo+1)//2 mf = scf.RHF(mol) numpy.random.seed(12) mf._eri = numpy.random.random(nmo_pair*(nmo_pair+1)//2) mf.mo_coeff = numpy.random.random((nmo,nmo)) mf.mo_energy = numpy.arange(0., nmo) mf.mo_occ = numpy.zeros(nmo) mf.mo_occ[:nocc] = 2 dm = mf.make_rdm1() vhf = mf.get_veff(mol, dm) h1 = numpy.random.random((nmo,nmo)) * .1 h1 = h1 + h1.T mf.get_hcore = lambda *args: h1 myci = ci.CISD(mf) eris = myci.ao2mo(mf.mo_coeff) eris.ehf = (h1*dm).sum() + (vhf*dm).sum()*.5 c2 = numpy.random.random((nocc,nocc,nvir,nvir)) * .1 c2 = c2 + c2.transpose(1,0,3,2) c1 = numpy.random.random(nocc*nvir+1) * .1 c0, c1 = c1[0], c1[1:].reshape(nocc,nvir) civec = myci.amplitudes_to_cisdvec(c0, c1, c2) hcivec = ci.cisd.contract(myci, civec, eris) self.assertAlmostEqual(lib.finger(hcivec), 2059.5730673341673, 9) e2 = ci.cisd.dot(civec, hcivec+eris.ehf*civec, nmo, nocc) self.assertAlmostEqual(e2, 7226.7494656749295, 9) rdm2 = myci.make_rdm2(civec, nmo, nocc) self.assertAlmostEqual(lib.finger(rdm2), 2.0492023431953221, 9) def fcicontract(h1, h2, norb, nelec, ci0): g2e = fci.direct_spin1.absorb_h1e(h1, h2, norb, nelec, .5) ci1 = fci.direct_spin1.contract_2e(g2e, ci0, norb, nelec) return ci1 ci0 = myci.to_fcivec(civec, nmo, mol.nelec) self.assertAlmostEqual(abs(civec-myci.from_fcivec(ci0, nmo, nocc*2)).max(), 0, 9) h2e = ao2mo.kernel(mf._eri, mf.mo_coeff) h1e = reduce(numpy.dot, (mf.mo_coeff.T, h1, mf.mo_coeff)) ci1 = fcicontract(h1e, h2e, nmo, mol.nelec, ci0) ci2 = myci.to_fcivec(hcivec, nmo, mol.nelec) e1 = numpy.dot(ci1.ravel(), ci0.ravel()) e2 = ci.cisd.dot(civec, hcivec+eris.ehf*civec, nmo, nocc) self.assertAlmostEqual(e1, e2, 9) dovov, dvvvv, doooo, doovv, dovvo, dvvov, dovvv, dooov = \ ci.cisd._gamma2_intermediates(myci, civec, nmo, nocc) self.assertAlmostEqual(lib.finger(numpy.array(dovov)), 0.02868859991188923, 9) self.assertAlmostEqual(lib.finger(numpy.array(dvvvv)),-0.05524957311823144, 9) self.assertAlmostEqual(lib.finger(numpy.array(doooo)), 0.01014399192065793, 9) self.assertAlmostEqual(lib.finger(numpy.array(doovv)), 0.02761239887072825, 9) self.assertAlmostEqual(lib.finger(numpy.array(dovvo)), 0.09971200182238759, 9) self.assertAlmostEqual(lib.finger(numpy.array(dovvv)), 0.12777531252787638, 9) self.assertAlmostEqual(lib.finger(numpy.array(dooov)), 0.18667669732858014, 9)
def get_h(self, iexe=True, href=None, frozen=0): """ get hamitonian of the sysem, which is to be described by a hf/dft single slater determinant :param href: reference hamiltonian, could be hf/uhf/rohf/rks/uks :type href: str """ meth = self.meth self.xc = None if href is None: if meth in [ 'eht', 'hf', 'mp2', 'cisd', 'ccsd', ]: fun = scf.RHF if self.restricted else scf.UHF mf = fun(self.mol) elif meth in ['pbe', 'b3lyp', 'w95xb']: fun = dft.RKS if self.restricted else dft.UKS mf = fun(self.mol) mf.xc = meth self.xc = meth else: raise Exception('Unknow method: %s' % meth) else: dct = {'rohf':scf.ROHF, 'rhf':scf.RHF, 'hf':scf.RHF,\ 'rks': dft.RKS, 'uks':dft.UKS, 'ks':dft.RKS} assert href in dct fun = dct[href] mf = fun(self.mol) if 'ks' in href: mf.xc = meth if iexe: mf.kernel() self.mf = mf h2 = None self.isd = T # single (slater) determinant # final hamiltonian if meth[:2] in ['mp', 'ci', 'cc']: self.isd = F if meth in ['mp2', 'mp3', 'mp4']: h2 = mp.MP2(self.mf) #.set(frozen=frozen) elif meth in [ 'cisd', ]: h2 = ci.CISD(self.mf) #.set(frozen=frozen) elif meth in ['ccsd', 'ccsd(t)']: h2 = cc.CCSD(self.mf) #.set(frozen=frozen) h2.direct = True else: raise Exception('Todo') if frozen: h2.set(frozen=frozen) if iexe: h2.kernel() self.h2 = h2
def test_dump_chk(self): mol = gto.M(atom=''' O 0. 0. .0 H 0. -0.757 0.587 H 0. 0.757 0.587''', basis='631g') mf = scf.RHF(mol).run() ci_scanner = ci.CISD(mf).as_scanner() ci_scanner(mol) ci_scanner.nmo = mf.mo_energy.size ci_scanner.nocc = mol.nelectron // 2 ci_scanner.dump_chk() myci = ci.CISD(mf) myci.__dict__.update(lib.chkfile.load(mf.chkfile, 'cisd')) self.assertAlmostEqual(abs(ci_scanner.ci-myci.ci).max(), 0, 9) ci_scanner.e_corr = -1 ci_scanner.chkfile = None ci_scanner.dump_chk(frozen=2) self.assertEqual(lib.chkfile.load(mf.chkfile, 'cisd/e_corr'), myci.e_corr)
def check_frozen(frozen): myci = ci.CISD(mf) myci.frozen = frozen myci.nroots = 2 myci.kernel() nocc = myci.nocc nmo = myci.nmo nfroz = len(frozen) cibra = (myci.ci[0] + myci.ci[1]) * numpy.sqrt(.5) fcibra = ci.cisd.to_fcivec(cibra, nmo+nfroz, mol.nelectron, myci.frozen) fciket = ci.cisd.to_fcivec(myci.ci[1], nmo+nfroz, mol.nelectron, myci.frozen) fcidm1 = fci.direct_spin1.trans_rdm1(fcibra, fciket, nmo+nfroz, mol.nelectron) cidm1 = myci.trans_rdm1(cibra, myci.ci[1], nmo, nocc) self.assertAlmostEqual(abs(fcidm1-cidm1).max(), 0, 12)
def test_h4(self): mol = gto.Mole() mol.verbose = 0 mol.atom = [ ['H', (1., -1., 0.)], ['H', (0., -1., -1.)], ['H', (1., -0.5, 0.)], ['H', (0., -1., 1.)], ] mol.charge = 2 mol.basis = '3-21g' mol.build() mf = scf.RHF(mol).run() ecisd = ci.CISD(mf).kernel()[0] self.assertAlmostEqual(ecisd, -0.024780739973407784, 12)
def test_rdm(self): mol = gto.Mole() mol.verbose = 5 mol.output = '/dev/null' mol.atom = [ ['O', (0., 0., 0.)], ['H', (0., -0.757, 0.587)], ['H', (0., 0.757, 0.587)], ] mol.basis = { 'H': 'sto-3g', 'O': 'sto-3g', } mol.build() mf = scf.RHF(mol).run(conv_tol=1e-14) myci = ci.CISD(mf) myci.frozen = 1 eris = myci.ao2mo() ecisd, civec = myci.kernel(eris=eris) self.assertAlmostEqual(ecisd, -0.048800218694077746, 8) nmo = myci.nmo nocc = myci.nocc strs = fci.cistring.gen_strings4orblist(range(nmo + 1), nocc + 1) mask = (strs & 1) != 0 sub_strs = strs[mask] addrs = fci.cistring.strs2addr(nmo + 1, nocc + 1, sub_strs) na = len(strs) ci0 = numpy.zeros((na, na)) ci0[addrs[:, None], addrs] = myci.to_fcivec(civec, nmo, nocc * 2) ref1, ref2 = fci.direct_spin1.make_rdm12(ci0, (nmo + 1), (nocc + 1) * 2) rdm1 = myci.make_rdm1(civec) rdm2 = myci.make_rdm2(civec) self.assertTrue(numpy.allclose(rdm2, ref2)) self.assertAlmostEqual( abs(rdm2 - rdm2.transpose(2, 3, 0, 1)).sum(), 0, 9) self.assertAlmostEqual( abs(rdm2 - rdm2.transpose(1, 0, 3, 2)).sum(), 0, 9) dm1 = numpy.einsum('ijkk->ij', rdm2) / (mol.nelectron - 1) self.assertAlmostEqual(abs(rdm1 - dm1).sum(), 0, 9) h1 = reduce(numpy.dot, (mf.mo_coeff.T, mf.get_hcore(), mf.mo_coeff)) eri = ao2mo.restore(1, ao2mo.kernel(mf._eri, mf.mo_coeff), nmo + 1) e1 = numpy.einsum('ij,ji', h1, rdm1) e1 += numpy.einsum('ijkl,ijkl', eri, rdm2) * .5 e1 += mol.energy_nuc() self.assertAlmostEqual(e1, myci.e_tot, 7)
def setUp(self): geometry = [('H', (0., 0., 0.)), ('H', (0., 0., 0.7414))] basis = '6-31g' multiplicity = 1 charge = 0 self.molecule = PyscfMolecularData(geometry, basis, multiplicity, charge) mol = prepare_pyscf_molecule(self.molecule) mol.verbose = 0 self.molecule._pyscf_data['mol'] = mol self.molecule._pyscf_data['scf'] = mf = scf.RHF(mol).run() self.molecule._pyscf_data['mp2'] = mp.MP2(mf).run() self.molecule._pyscf_data['cisd'] = ci.CISD(mf).run() self.molecule._pyscf_data['ccsd'] = cc.CCSD(mf).run() self.molecule._pyscf_data['fci'] = fci.FCI(mf).run()
def test_ao_direct(self): mol = gto.Mole() mol.verbose = 0 mol.atom = [ ['O', (0., 0., 0.)], ['H', (0., -0.757, 0.587)], ['H', (0., 0.757, 0.587)], ] mol.basis = 'ccpvtz' mol.build() mf = scf.RHF(mol).run(conv_tol=1e-14) myci = ci.CISD(mf) myci.max_memory = 1 myci.direct = True ecisd, civec = myci.kernel() self.assertAlmostEqual(ecisd, -0.2694965385156135, 8)
def test_ao_direct(self): mol = gto.Mole() mol.verbose = 0 mol.atom = [ ['O', ( 0., 0. , 0. )], ['H', ( 0., -0.757, 0.587)], ['H', ( 0., 0.757 , 0.587)],] mol.spin = 2 mol.basis = 'ccpvdz' mol.build() mf = scf.UHF(mol).run(conv_tol=1e-14) myci = ci.CISD(mf) myci.max_memory = .1 myci.frozen = [[1,2],[1,2]] myci.direct = True ecisd, civec = myci.kernel() self.assertAlmostEqual(ecisd, -0.04754878399464485, 8)
def test_ao_direct(self): mol = gto.Mole() mol.verbose = 0 mol.atom = [ ['O', ( 0., 0. , 0. )], ['H', ( 0., -0.757, 0.587)], ['H', ( 0., 0.757 , 0.587)],] mol.basis = 'ccpvdz' mol.build() mf = scf.RHF(mol).set(conv_tol=1e-14).newton().run() myci = ci.CISD(mf) myci.max_memory = .1 myci.nmo = 16 myci.nocc = 5 myci.direct = True ecisd, civec = myci.kernel() self.assertAlmostEqual(ecisd, -0.1319371817220385, 8)
def test_multi_roots(self): mol = gto.Mole() mol.verbose = 0 mol.atom = [ ['H', ( 1.,-1. , 0. )], ['H', ( 0.,-1. ,-1. )], ['H', ( 1.,-0.5 , 0. )], ['H', ( 0.,-1. , 1. )], ] mol.basis = '3-21g' mol.build() mf = scf.RHF(mol).run(conv_tol=1e-14) myci = ci.CISD(mf) myci.nroots = 3 myci.run() myci.dump_chk() self.assertAlmostEqual(myci.e_tot[2], -1.6979890451316759, 8)
def kernel(self, h1, h2, norb, nelec, ci0=None, ecore=0, **kwargs): fakemol = gto.M(verbose=0) nelec = numpy.sum(nelec) fakemol.nelectron = nelec fake_hf = scf.RHF(fakemol) fake_hf._eri = ao2mo.restore(8, h2, norb) fake_hf.get_hcore = lambda *args: h1 fake_hf.get_ovlp = lambda *args: numpy.eye(norb) fake_hf.kernel() self.myci = ci.CISD(fake_hf) self.myci.conv_tol = 1e-7 self.myci.max_cycle = 150 self.myci.max_space = 12 self.myci.lindep = 1e-10 self.myci.nroots = 1 self.myci.level_shift = 0.2 # in precond e_corr, civec = self.myci.kernel() e = fake_hf.e_tot + e_corr return e + ecore, civec
def test_h4(self): mol = gto.Mole() mol.verbose = 0 mol.atom = [ ['H', ( 1.,-1. , 0. )], ['H', ( 0.,-1. ,-1. )], ['H', ( 1.,-0.5 , 0. )], ['H', ( 0.,-1. , 1. )], ] mol.charge = 2 mol.basis = '3-21g' mol.build() mf = scf.RHF(mol).run(conv_tol=1e-14) ecisd = ci.CISD(mf).kernel()[0] self.assertAlmostEqual(ecisd, -0.024780739973407784, 8) h2e = ao2mo.kernel(mf._eri, mf.mo_coeff) h1e = reduce(numpy.dot, (mf.mo_coeff.T, mf.get_hcore(), mf.mo_coeff)) eci = fci.direct_spin0.kernel(h1e, h2e, mf.mo_coeff.shape[1], mol.nelectron)[0] eci = eci + mol.energy_nuc() - mf.e_tot self.assertAlmostEqual(ecisd, eci, 9)
def test_ao2mo(self): mol = gto.Mole() mol.verbose = 0 mol.atom = [ ['O', (0., 0., 0.)], ['H', (0., -0.757, 0.587)], ['H', (0., 0.757, 0.587)], ] mol.basis = 'ccpvtz' mol.build() mf = scf.RHF(mol).run(conv_tol=1e-14) myci = ci.CISD(mf) eris0 = ci.cisd._make_eris_incore(myci) eris1 = ci.cisd._make_eris_outcore(myci) self.assertAlmostEqual(abs(eris0.oooo - eris1.oooo).max(), 0, 11) self.assertAlmostEqual(abs(eris0.vooo - eris1.vooo).max(), 0, 11) self.assertAlmostEqual(abs(eris0.vvoo - eris1.vvoo).max(), 0, 11) self.assertAlmostEqual(abs(eris0.voov - eris1.voov).max(), 0, 11) self.assertAlmostEqual(abs(eris0.vovv - eris1.vovv).max(), 0, 11) self.assertAlmostEqual(abs(eris0.vvvv - eris1.vvvv).max(), 0, 11)
def test_rdm(self): mol = gto.Mole() mol.verbose = 5 mol.output = '/dev/null' mol.atom = [ ['O', (0., 0., 0.)], ['H', (0., -0.757, 0.587)], ['H', (0., 0.757, 0.587)], ] mol.basis = { 'H': 'sto-3g', 'O': 'sto-3g', } mol.build() mf = scf.RHF(mol).run() myci = ci.CISD(mf) ecisd, civec = myci.kernel() self.assertAlmostEqual(ecisd, -0.048878084082066106, 12) nmo = mf.mo_coeff.shape[1] nocc = mol.nelectron // 2 ci0 = myci.to_fci(civec, nmo, nocc * 2) ref1, ref2 = fci.direct_spin1.make_rdm12(ci0, nmo, nocc * 2) rdm1 = myci.make_rdm1(civec) rdm2 = myci.make_rdm2(civec) self.assertTrue(numpy.allclose(rdm2, ref2)) self.assertAlmostEqual(finger(rdm1), 2.2685199485281689, 9) self.assertAlmostEqual(finger(rdm2), -3.7944039102480649, 9) self.assertAlmostEqual( abs(rdm2 - rdm2.transpose(2, 3, 0, 1)).sum(), 0, 9) self.assertAlmostEqual( abs(rdm2 - rdm2.transpose(1, 0, 3, 2)).sum(), 0, 9) h1e = reduce(numpy.dot, (mf.mo_coeff.T, mf.get_hcore(), mf.mo_coeff)) h2e = ao2mo.kernel(mf._eri, mf.mo_coeff) h2e = ao2mo.restore(1, h2e, nmo) e2 = (numpy.dot(h1e.flatten(), rdm1.flatten()) + numpy.dot(h2e.flatten(), rdm2.flatten()) * .5) self.assertAlmostEqual(ecisd + mf.e_tot - mol.energy_nuc(), e2, 9) dm1 = numpy.einsum('ijkk->ij', rdm2) / (mol.nelectron - 1) self.assertAlmostEqual(abs(rdm1 - dm1).sum(), 0, 9)
def test_cisd(self): from pyscf import gto, scf, ci mol = gto.M( atom='Be 0 0 0', basis='631g', verbose=0) mf = scf.RHF(mol) mf.conv_tol = 1e-12 Escf = mf.kernel() myci = ci.CISD(mf).run() #print('RCISD correlation energy', myci.e_corr) ref = myci.e_corr # run naive CISD calculation mos = mf.mo_coeff nmo = mos.shape[1] hcore = mf.get_hcore() ha = numpy.einsum('mp,mn,nq->pq', mos, hcore, mos) hb = ha.copy() I = integrals.get_phys(mol, mos, mos, mos, mos) N = mol.nelectron na = N//2 nb = na basis = ci_utils.ucisd_basis(nmo, na, nb) nd = len(basis) H = numpy.zeros((nd, nd)) const = -Escf + mol.energy_nuc() for i, b in enumerate(basis): for j, k in enumerate(basis): H[i, j] = ci_utils.ci_matrixel( b[0], b[1], k[0], k[1], ha, hb, I, I, I, const) eout, v = numpy.linalg.eigh(H) #print('RCISD correlation energy', eout[0]) diff = abs(ref - eout[0]) self.assertTrue(diff < 1e-12)
#!/usr/bin/env python # # Author: Qiming Sun <*****@*****.**> # ''' CISD analytical nuclear gradients. ''' from pyscf import gto, scf, ci mol = gto.M(atom=[['O', 0., 0., 0], ['H', 0., -0.757, 0.587], ['H', 0., 0.757, 0.587]], basis='ccpvdz') mf = scf.RHF(mol).run() postmf = ci.CISD(mf).run() g = postmf.nuc_grad_method() g.kernel() mf = scf.UHF(mol).x2c().run() postmf = ci.CISD(mf).run() # PySCF-1.6.1 and newer supports the .Gradients method to create a grad # object after grad module was imported. It is equivalent to call the # .nuc_grad_method method. from pyscf import grad g = postmf.Gradients() g.kernel()
c.build_op_matrices() #ci_vector.expand_to_full_space() ci_vector.expand_each_fock_space() e_prev = 0 thresh_conv = 1e-8 ci_vector_ref = ci_vector.copy() e_last = 0 #ci_vector.print_configs() ci_vector, pt_vector, e0, e2 = bc_cipsi(ci_vector_ref.copy(), clustered_ham, thresh_cipsi=1e-14, thresh_ci_clip=0, max_iter=1) print("\npyscf cisd") from pyscf import ci myci = ci.CISD(myhf).run() from pyscf import mp mp2 = mp.MP2(myhf).run() print("\n") print(" BC-CISD: %12.8f " % (e0 + enu)) print(" PYSCF CISD: %12.8f " % myci.e_tot) print(" BC-CI(D): %12.8f " % (e2 + enu)) print(" PYSCF MP2 : %12.8f " % mp2.e_tot) print(" HCI: %12.8f Dim:%6d" % (ehci + enu, hci_dim)) print(" FCI: %12.8f Dim:%6d" % (efci + enu, fci_dim))
#!/usr/bin/env python # # Author: Qiming Sun <*****@*****.**> # ''' A simple example to run CISD calculation. ''' from pyscf import gto, scf, ci mol = gto.M( atom = 'H 0 0 0; F 0 0 1.1', basis = 'ccpvdz') mf = scf.RHF(mol).run() mycc = ci.CISD(mf).run() print('CISD correlation energy', mycc.e_corr)
from openfermionpyscf import prepare_pyscf_molecule from openfermionpyscf import PyscfMolecularData from pyscf import scf, mp, ci, cc, fci geometry = [('H', (0., 0., 0.)), ('H', (0., 0., 0.7414))] basis = '6-31g' multiplicity = 1 charge = 0 molecule = PyscfMolecularData(geometry, basis, multiplicity, charge) mol = prepare_pyscf_molecule(molecule) mol.verbose = 0 molecule._pyscf_data['mol'] = mol molecule._pyscf_data['scf'] = mf = scf.RHF(mol).run() molecule._pyscf_data['mp2'] = mp.MP2(mf).run() molecule._pyscf_data['cisd'] = ci.CISD(mf).run() molecule._pyscf_data['ccsd'] = cc.CCSD(mf).run() molecule._pyscf_data['fci'] = fci.FCI(mf).run() def test_accessing_rdm(): mo = molecule.canonical_orbitals overlap = molecule.overlap_integrals h1 = molecule.one_body_integrals h2 = molecule.two_body_integrals mf = molecule._pyscf_data['scf'] e_core = mf.energy_nuc() rdm1 = molecule.cisd_one_rdm rdm2 = molecule.cisd_two_rdm e_ref = molecule._pyscf_data['cisd'].e_tot
def energy_gradient(self, input_xyz): mol = gto.Mole() mol.atom = input_xyz mol.basis = self.basis mol.charge = self.charge if self.spin != 0: mol.spin = self.spin-1 else: mol.spin = self.spin print("spin:", mol.spin) print("charge:", mol.charge) #mol.unit = 'Bohr' mol.unit = 'Angstrom' #mol.symmetry = True mol.build() #mf.conv_tol = 1e-14 if self.theory == 'DFT': if self.spin != 0: mf = dft.UKS(mol) mf.xc = self.xc e = mf.kernel() g = mf.nuc_grad_method().kernel() h = 0 #hess = mf.Hessian().kernel() #h = hess.transpose(0,2,1,3).reshape(len(input_xyz)*3, len(input_xyz)*3, order='C') else: mf = dft.RKS(mol) mf.xc = self.xc e = mf.kernel() g = mf.nuc_grad_method().kernel() hess = mf.Hessian().kernel() h = hess.transpose(0,2,1,3).reshape(len(input_xyz)*3, len(input_xyz)*3, order='C') return e, g, h if self.theory == 'RHF': #Restricted HF calc mf = scf.RHF(mol).run() e = mf.kernel() mf.dump_scf_summary() g = mf.nuc_grad_method().kernel() hess = mf.Hessian().kernel() h = hess.transpose(0,2,1,3).reshape(len(input_xyz)*3, len(input_xyz)*3, order='C') return e, g, h if self.theory == 'UHF': #Unrestricted HF calc mf = scf.UHF(mol).run() e = mf.kernel() mf.dump_scf_summary() g = mf.nuc_grad_method().kernel() hess = mf.Hessian().kernel() h = hess.transpose(0,2,1,3).reshape(len(input_xyz)*3, len(input_xyz)*3, order='C') return e, g, h if self.theory == 'ROHF': #Restricted Open shell HF calc mf = scf.ROHF(mol).run() e = mf.kernel() mf.dump_scf_summary() g = mf.nuc_grad_method().kernel() h = 0 #hess = mf.Hessian().kernel() #h = hess.transpose(0,2,1,3).reshape(len(input_xyz)*3, len(input_xyz)*3, order='C') return e, g, h if self.theory == 'MP2': #Perturbation second order calc mf = scf.RHF(mol).run() postmf = mp.MP2(mf).run() e = mf.kernel() + postmf.kernel()[0] g2 = postmf.nuc_grad_method() g = g2.kernel() h = 0 return e, g, h if self.theory == 'CISD': #CI for singles and double excitations mf = scf.RHF(mol).run() postmf = ci.CISD(mf).run() e = postmf.kernel() g2 = postmf.nuc_grad_method() g = g2.kernel() h = 0 return e, g, h if self.theory == 'CCSD': #Couple Cluster for singles and doubles mf = scf.RHF(mol).run() postmf = cc.CCSD(mf).run() e = mf.kernel() + postmf.kernel()[0] g2 = postmf.nuc_grad_method() g = g2.kernel() h = 0 return e, g, h if self.theory == 'CCSD(T)': #Couple Cluster for singles and doubles, pertub triples mf = scf.RHF(mol).run() mycc = cc.CCSD(mf).run() et = mycc.ccsd_t() #postmf = cc.ccsd_t(mf).run() #e = mf.kernel() + postmf.kernel()[0] e = mycc.e_tot + et g=0 #g2 = postmf.nuc_grad_method() #g = g2.kernel() h = 0 return e, g, h if self.theory == 'CASSCF': mf = scf.RHF(mol).run() postmf = mcscf.CASSCF(mf, self.active_space, self.nelec).run() e = postmf.kernel() g2 = postmf.nuc_grad_method() g = g2.kernel() h = 0 return e, g, h if self.theory == 'CASCI': mf = scf.RHF(mol).run() h = 0 return e, g, h
import numpy from pyscf import gto, scf, ci, mcscf, tddft, qmmm mol = gto.M(atom=''' C 1.1879 -0.3829 0.0000 C 0.0000 0.5526 0.0000 O -1.1867 -0.2472 0.0000 H -1.9237 0.3850 0.0000 H 2.0985 0.2306 0.0000 H 1.1184 -1.0093 0.8869 H 1.1184 -1.0093 -0.8869 H -0.0227 1.1812 0.8852 H -0.0227 1.1812 -0.8852 ''', basis='3-21g') numpy.random.seed(1) coords = numpy.random.random((5, 3)) * 10 charges = (numpy.arange(5) + 1.) * -.1 mf = qmmm.mm_charge(scf.RHF(mol), coords, charges).run() mf.nuc_grad_method().run() ci.CISD(mf).run().nuc_grad_method().run() mc = mcscf.CASCI(mf, 6, 6).run() mc.nuc_grad_method().run() tddft.TDA(mf).run().nuc_grad_method().run(state=2)
def run_pyscf(molecule, run_scf=True, run_mp2=False, run_cisd=False, run_ccsd=False, run_fci=False, verbose=False): """ This function runs a pyscf calculation. Args: molecule: An instance of the MolecularData or PyscfMolecularData class. run_scf: Optional boolean to run SCF calculation. run_mp2: Optional boolean to run MP2 calculation. run_cisd: Optional boolean to run CISD calculation. run_ccsd: Optional boolean to run CCSD calculation. run_fci: Optional boolean to FCI calculation. verbose: Boolean whether to print calculation results to screen. Returns: molecule: The updated PyscfMolecularData object. Note the attributes of the input molecule are also updated in this function. """ # Prepare pyscf molecule. pyscf_molecule = prepare_pyscf_molecule(molecule) molecule.n_orbitals = int(pyscf_molecule.nao_nr()) molecule.n_qubits = 2 * molecule.n_orbitals molecule.nuclear_repulsion = float(pyscf_molecule.energy_nuc()) # Run SCF. pyscf_scf = compute_scf(pyscf_molecule) pyscf_scf.verbose = 0 # Custom properties pyscf_scf.chkfile = 'chkfiles/rohf.chk' pyscf_scf.init_guess = 'chkfile' #pyscf_scf.max_cycle = 1000 pyscf_scf.max_cycle = 0 # Use chkfile pyscf_scf.verbose = 4 pyscf_scf.run() pyscf_scf.analyze() molecule.hf_energy = float(pyscf_scf.e_tot) if verbose: print('Hartree-Fock energy for {} ({} electrons) is {}.'.format( molecule.name, molecule.n_electrons, molecule.hf_energy)) # Hold pyscf data in molecule. They are required to compute density # matrices and other quantities. molecule._pyscf_data = pyscf_data = {} pyscf_data['mol'] = pyscf_molecule pyscf_data['scf'] = pyscf_scf # Populate fields. molecule.canonical_orbitals = pyscf_scf.mo_coeff.astype(float) molecule.orbital_energies = pyscf_scf.mo_energy.astype(float) # Get integrals. # Try to load integrals from file if they exist try: print("Attempting to load integrals...") one_body_integrals = np.load("1e-integrals.npy") two_body_integrals = np.load("2e-integrals.npy") except FileNotFoundError: print("Integrals not found. Recalculating...") one_body_integrals, two_body_integrals = compute_integrals( pyscf_molecule, pyscf_scf) np.save("1e-integrals.npy", one_body_integrals) np.save("2e-integrals.npy", two_body_integrals) print("Integrals loaded") molecule.one_body_integrals = one_body_integrals molecule.two_body_integrals = two_body_integrals molecule.overlap_integrals = pyscf_scf.get_ovlp() # Run MP2. if run_mp2: if molecule.multiplicity != 1: print("WARNING: RO-MP2 is not available in PySCF.") else: pyscf_mp2 = mp.MP2(pyscf_scf) pyscf_mp2.verbose = 0 pyscf_mp2.run() # molecule.mp2_energy = pyscf_mp2.e_tot # pyscf-1.4.4 or higher molecule.mp2_energy = pyscf_scf.e_tot + pyscf_mp2.e_corr pyscf_data['mp2'] = pyscf_mp2 if verbose: print('MP2 energy for {} ({} electrons) is {}.'.format( molecule.name, molecule.n_electrons, molecule.mp2_energy)) # Run CISD. if run_cisd: pyscf_cisd = ci.CISD(pyscf_scf) pyscf_cisd.verbose = 0 pyscf_cisd.run() molecule.cisd_energy = pyscf_cisd.e_tot pyscf_data['cisd'] = pyscf_cisd if verbose: print('CISD energy for {} ({} electrons) is {}.'.format( molecule.name, molecule.n_electrons, molecule.cisd_energy)) # Run CCSD. if run_ccsd: pyscf_ccsd = cc.CCSD(pyscf_scf) pyscf_ccsd.verbose = 0 pyscf_ccsd.run() molecule.ccsd_energy = pyscf_ccsd.e_tot pyscf_data['ccsd'] = pyscf_ccsd if verbose: print('CCSD energy for {} ({} electrons) is {}.'.format( molecule.name, molecule.n_electrons, molecule.ccsd_energy)) # Run FCI. if run_fci: pyscf_fci = fci.FCI(pyscf_molecule, pyscf_scf.mo_coeff) pyscf_fci.verbose = 0 molecule.fci_energy = pyscf_fci.kernel()[0] pyscf_data['fci'] = pyscf_fci if verbose: print('FCI energy for {} ({} electrons) is {}.'.format( molecule.name, molecule.n_electrons, molecule.fci_energy)) # Return updated molecule instance. print("Ending pyscf...") pyscf_molecular_data = PyscfMolecularData.__new__(PyscfMolecularData) pyscf_molecular_data.__dict__.update(molecule.__dict__) print("Saving...") #pyscf_molecular_data.save() print("Saved") return pyscf_molecular_data