def test_mbpt2(self): myucc = uccsd.UCCSD(mf) e = myucc.kernel(mbpt2=True)[0] self.assertAlmostEqual(e, -0.12886859466216125, 10) emp2 = mp.MP2(mf).kernel()[0] self.assertAlmostEqual(e, emp2, 10) myucc = uccsd.UCCSD(mf_s2) e = myucc.kernel(mbpt2=True)[0] self.assertAlmostEqual(e, -0.096257842171487293, 10) emp2 = mp.MP2(mf_s2).kernel()[0] self.assertAlmostEqual(e, emp2, 10)
def UCCSD(mf, frozen=None, mo_coeff=None, mo_occ=None): from pyscf.soscf import newton_ah if isinstance(mf, newton_ah._CIAH_SOSCF) or not isinstance(mf, scf.uhf.UHF): mf = scf.addons.convert_to_uhf(mf) if getattr(mf, 'with_df', None): # TODO: DF-UCCSD with memory-efficient particle-particle ladder, # similar to dfccsd.RCCSD return uccsd.UCCSD(mf, frozen, mo_coeff, mo_occ) else: return uccsd.UCCSD(mf, frozen, mo_coeff, mo_occ)
def test_zero_beta_electrons(self): mol = gto.M(atom='H', basis=('631g', [[0, (.2, 1)], [0, (.5, 1)]]), spin=1, verbose=0) mf = scf.UHF(mol).run() mycc = uccsd.UCCSD(mf).run() self.assertAlmostEqual(mycc.e_corr, 0, 9) mol = gto.M(atom='He', basis=('631g', [[0, (.2, 1)], [0, (.5, 1)]]), spin=2, verbose=0) mf = scf.UHF(mol).run() mycc = uccsd.UCCSD(mf).run() self.assertAlmostEqual(mycc.e_corr, -2.6906675843462455e-05, 9) self.assertEqual(mycc.t1[1].size, 0) self.assertEqual(mycc.t2[1].size, 0) self.assertEqual(mycc.t2[2].size, 0)
def test_update_amps(self): mf = scf.UHF(mol).run() numpy.random.seed(21) mf.mo_coeff = [numpy.random.random(mf.mo_coeff[0].shape) * .1] * 2 mycc = uccsd.UCCSD(mf) eris = mycc.ao2mo() nocc = mol.nelectron // 2 nvir = mol.nao_nr() - nocc numpy.random.seed(1) t1r = numpy.random.random((nocc, nvir)) * .1 t2r = numpy.random.random((nocc, nocc, nvir, nvir)) * .1 t2r = t2r + t2r.transpose(1, 0, 3, 2) t1 = addons.spatial2spin(t1r) t2 = addons.spatial2spin(t2r) l1r = numpy.random.random((nocc, nvir)) * .1 l2r = numpy.random.random((nocc, nocc, nvir, nvir)) * .1 l2r = l2r + l2r.transpose(1, 0, 3, 2) l1 = addons.spatial2spin(l1r) l2 = addons.spatial2spin(l2r) l1ref, l2ref = update_l1l2(mf, t1, t2, l1, l2, eris.orbspin) eris = uccsd_lambda._eris_spatial2spin(mycc, eris) imds = uccsd_lambda.make_intermediates(mycc, t1, t2, eris) l1, l2 = uccsd_lambda.update_amps(mycc, t1, t2, l1, l2, eris, imds) self.assertAlmostEqual(abs(l1 - l1ref).max(), 0, 8) self.assertAlmostEqual(abs(l2 - l2ref).max(), 0, 8)
def __init__(self, mf, ncas=None, nelecas=None, occslst=None, ci=None, ndet=None, cutoff=None, frozen=0, beta=-1.0, dt=None): nea, neb = mf.mol.nelec moa, mob = mf.mo_coeff self.mo0 = moa.copy(), mob.copy() nmoa, nmob = moa.shape[1], mob.shape[1] self.nfc, self.nfv = get_frozen(mf, frozen) self.pbra = 0 self.pket = nea + neb - sum(self.nfc) self.mbpt2 = False self.h**o = mf.mo_energy[0][nea-1], mf.mo_energy[1][neb-1] print('nmo: {}, nele: {}'.format((nmoa, nmob), (nea, neb))) print('frozen core: {}, virtual: {}'.format(self.nfc, self.nfv)) self.occ = get_occ(mf, ncas, nelecas, occslst, ci, ndet, cutoff) ndet = self.occ.shape[0] self.vir = np.empty((ndet,nmoa+nmob-nea-neb),dtype=int) self.beta = np.ones(ndet) * beta self.dt = dt noa, nob = nea - self.nfc[0], neb - self.nfc[1] nva, nvb = nmoa - nea - self.nfv[0], nmob - neb - self.nfv[1] self.t1a = np.empty((ndet, noa, nva)) self.t1b = np.empty((ndet, nob, nvb)) self.t2aa = np.empty((ndet, noa, noa, nva, nva)) self.t2ab = np.empty((ndet, noa, nob, nva, nvb)) self.t2bb = np.empty((ndet, nob, nob, nvb, nvb)) self.cc = uccsd.UCCSD(mf, frozen)
def test_ao2mo(self): mycc = uccsd.UCCSD(mf) numpy.random.seed(2) nao = mol.nao mo = numpy.random.random((2, nao, nao)) eri_incore = mycc.ao2mo(mo) mycc.max_memory = 0 eri_outcore = mycc.ao2mo(mo) self.assertTrue(isinstance(eri_outcore.oovv, h5py.Dataset)) self.assertAlmostEqual(abs(eri_incore.oooo - eri_outcore.oooo).max(), 0, 12) self.assertAlmostEqual(abs(eri_incore.oovv - eri_outcore.oovv).max(), 0, 12) self.assertAlmostEqual(abs(eri_incore.ovoo - eri_outcore.ovoo).max(), 0, 12) self.assertAlmostEqual(abs(eri_incore.ovvo - eri_outcore.ovvo).max(), 0, 12) self.assertAlmostEqual(abs(eri_incore.ovov - eri_outcore.ovov).max(), 0, 12) self.assertAlmostEqual(abs(eri_incore.ovvv - eri_outcore.ovvv).max(), 0, 12) self.assertAlmostEqual(abs(eri_incore.vvvv - eri_outcore.vvvv).max(), 0, 12) self.assertAlmostEqual(abs(eri_incore.OOOO - eri_outcore.OOOO).max(), 0, 12) self.assertAlmostEqual(abs(eri_incore.OOVV - eri_outcore.OOVV).max(), 0, 12) self.assertAlmostEqual(abs(eri_incore.OVOO - eri_outcore.OVOO).max(), 0, 12) self.assertAlmostEqual(abs(eri_incore.OVVO - eri_outcore.OVVO).max(), 0, 12) self.assertAlmostEqual(abs(eri_incore.OVOV - eri_outcore.OVOV).max(), 0, 12) self.assertAlmostEqual(abs(eri_incore.OVVV - eri_outcore.OVVV).max(), 0, 12) self.assertAlmostEqual(abs(eri_incore.VVVV - eri_outcore.VVVV).max(), 0, 12) self.assertAlmostEqual(abs(eri_incore.ooOO - eri_outcore.ooOO).max(), 0, 12) self.assertAlmostEqual(abs(eri_incore.ooVV - eri_outcore.ooVV).max(), 0, 12) self.assertAlmostEqual(abs(eri_incore.ovOO - eri_outcore.ovOO).max(), 0, 12) self.assertAlmostEqual(abs(eri_incore.ovVO - eri_outcore.ovVO).max(), 0, 12) self.assertAlmostEqual(abs(eri_incore.ovOV - eri_outcore.ovOV).max(), 0, 12) self.assertAlmostEqual(abs(eri_incore.ovVV - eri_outcore.ovVV).max(), 0, 12) self.assertAlmostEqual(abs(eri_incore.vvVV - eri_outcore.vvVV).max(), 0, 12) self.assertAlmostEqual(abs(eri_incore.OOvv - eri_outcore.OOvv).max(), 0, 12) self.assertAlmostEqual(abs(eri_incore.OVoo - eri_outcore.OVoo).max(), 0, 12) self.assertAlmostEqual(abs(eri_incore.OVvo - eri_outcore.OVvo).max(), 0, 12) self.assertAlmostEqual(abs(eri_incore.OVvv - eri_outcore.OVvv).max(), 0, 12)
def test_h4_rdm(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.spin = 2 mol.basis = '6-31g' mol.build() mf = scf.UHF(mol).set(init_guess='1e').run(conv_tol=1e-14) ehf0 = mf.e_tot - mol.energy_nuc() mycc = uccsd.UCCSD(mf).run() mycc.solve_lambda() 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 = direct_uhf.kernel((h1a,h1b), (eri_aa,eri_ab,eri_bb), h1a.shape[0], mol.nelec) dm1ref, dm2ref = direct_uhf.make_rdm12s(fcivec, h1a.shape[0], mol.nelec) t1, t2 = mycc.t1, mycc.t2 l1, l2 = mycc.l1, mycc.l2 rdm1 = mycc.make_rdm1(t1, t2, l1, l2) rdm2 = mycc.make_rdm2(t1, t2, l1, l2) self.assertAlmostEqual(abs(dm1ref[0] - rdm1[0]).max(), 0, 6) self.assertAlmostEqual(abs(dm1ref[1] - rdm1[1]).max(), 0, 6) self.assertAlmostEqual(abs(dm2ref[0] - rdm2[0]).max(), 0, 6) self.assertAlmostEqual(abs(dm2ref[1] - rdm2[1]).max(), 0, 6) self.assertAlmostEqual(abs(dm2ref[2] - rdm2[2]).max(), 0, 6)
def UCCSD(mf, frozen=[[], []], mo_coeff=None, mo_occ=None): from pyscf.cc import uccsd from pyscf import lib from pyscf import scf if not isinstance(mf, scf.uhf.UHF): mf = scf.addons.convert_to_uhf(mf) return uccsd.UCCSD(mf, frozen, mo_coeff, mo_occ)
def test_add_vvVV(self): myucc = uccsd.UCCSD(mf_s2) nocca, noccb = 6, 4 nmo = mf_s2.mo_occ[0].size nvira, nvirb = nmo - nocca, nmo - noccb numpy.random.seed(9) t1 = [numpy.zeros((nocca, nvira)), numpy.zeros((noccb, nvirb))] t2 = [ numpy.random.random((nocca, nocca, nvira, nvira)) - .9, numpy.random.random((nocca, noccb, nvira, nvirb)) - .9, numpy.random.random((noccb, noccb, nvirb, nvirb)) - .9 ] t2[0] = t2[0] - t2[0].transpose(1, 0, 2, 3) t2[0] = t2[0] - t2[0].transpose(0, 1, 3, 2) t2[2] = t2[2] - t2[2].transpose(1, 0, 2, 3) t2[2] = t2[2] - t2[2].transpose(0, 1, 3, 2) eris1 = copy.copy(eris) idxa = lib.square_mat_in_trilu_indices(nvira) idxb = lib.square_mat_in_trilu_indices(nvirb) vvVV = eris1.vvVV[:, idxb][idxa] ref = lib.einsum('acbd,ijcd->ijab', vvVV, t2[1]) t2a = myucc._add_vvVV((t1[0] * 0, t1[1] * 0), t2[1], eris) self.assertAlmostEqual(abs(ref - t2a).max(), 0, 12) myucc.direct = True eris1.vvvv = None # == with_ovvv=True in the call below eris1.VVVV = None eris1.vvVV = None t1 = None myucc.mo_coeff, eris1.mo_coeff = eris1.mo_coeff, None t2b = myucc._add_vvVV(t1, t2[1], eris1) self.assertAlmostEqual(abs(ref - t2b).max(), 0, 12)
def setUpModule(): global mol, rhf, mf, myucc, mol_s2, mf_s2, eris mol = gto.Mole() mol.verbose = 7 mol.output = '/dev/null' mol.atom = [[8, (0., 0., 0.)], [1, (0., -0.757, 0.587)], [1, (0., 0.757, 0.587)]] mol.basis = '631g' mol.build() rhf = scf.RHF(mol) rhf.conv_tol_grad = 1e-8 rhf.kernel() mf = scf.addons.convert_to_uhf(rhf) myucc = cc.UCCSD(mf).run(conv_tol=1e-10) mol_s2 = gto.Mole() mol_s2.atom = [[8, (0., 0., 0.)], [1, (0., -0.757, 0.587)], [1, (0., 0.757, 0.587)]] mol_s2.basis = '631g' mol_s2.spin = 2 mol_s2.verbose = 5 mol_s2.output = '/dev/null' mol_s2.build() mf_s2 = scf.UHF(mol_s2).run() eris = uccsd.UCCSD(mf_s2).ao2mo()
def UCCSD(mf, frozen=0, mo_coeff=None, mo_occ=None): from pyscf.cc import uccsd from pyscf import scf if not isinstance(mf, scf.uhf.UHF): mf = scf.addons.convert_to_uhf(mf) if hasattr(mf, 'with_df') and 'pbc' in str(mf.__module__): raise NotImplementedError('DF-UCCSD') else: return uccsd.UCCSD(mf, frozen, mo_coeff, mo_occ)
def setUpModule(): global mol, mf, mycc mol = gto.Mole() mol.atom = [[8, (0., 0., 0.)], [1, (0., -0.757, 0.587)], [1, (0., 0.757, 0.587)]] mol.basis = '631g' mol.spin = 2 mol.build() mf = scf.UHF(mol).run() mycc = uccsd.UCCSD(mf)
def test_rdm(self): nocc = 5 nvir = 7 mol = gto.M() mf = scf.UHF(mol) mf.mo_occ = numpy.zeros((2, nocc + nvir)) mf.mo_occ[:, :nocc] = 1 mycc = uccsd.UCCSD(mf) def antisym(t2): t2 = t2 - t2.transpose(0, 1, 3, 2) t2 = t2 - t2.transpose(1, 0, 2, 3) return t2 orbspin = numpy.zeros((nocc + nvir) * 2, dtype=int) orbspin[1::2] = 1 numpy.random.seed(1) t1 = numpy.random.random((2, nocc, nvir)) * .1 - .1 t2ab = numpy.random.random((nocc, nocc, nvir, nvir)) * .1 - .1 t2aa = antisym(numpy.random.random((nocc, nocc, nvir, nvir)) * .1 - .1) t2bb = antisym(numpy.random.random((nocc, nocc, nvir, nvir)) * .1 - .1) t2 = (t2aa, t2ab, t2bb) l1 = numpy.random.random((2, nocc, nvir)) * .1 - .1 l2ab = numpy.random.random((nocc, nocc, nvir, nvir)) * .1 - .1 l2aa = antisym(numpy.random.random((nocc, nocc, nvir, nvir)) * .1 - .1) l2bb = antisym(numpy.random.random((nocc, nocc, nvir, nvir)) * .1 - .1) l2 = (l2aa, l2ab, l2bb) dm1a, dm1b = mycc.make_rdm1(t1, t2, l1, l2) dm2aa, dm2ab, dm2bb = mycc.make_rdm2(t1, t2, l1, l2) ia = orbspin == 0 ib = orbspin == 1 oa = orbspin[:nocc * 2] == 0 ob = orbspin[:nocc * 2] == 1 va = orbspin[nocc * 2:] == 0 vb = orbspin[nocc * 2:] == 1 t1 = addons.spatial2spin(t1, orbspin) t2 = addons.spatial2spin(t2, orbspin) l1 = addons.spatial2spin(l1, orbspin) l2 = addons.spatial2spin(l2, orbspin) mf1 = scf.GHF(mol) mf1.mo_occ = numpy.zeros((nocc + nvir) * 2) mf.mo_occ[:, :nocc * 2] = 1 mycc1 = gccsd.GCCSD(mf1) dm1 = mycc1.make_rdm1(t1, t2, l1, l2) dm2 = mycc1.make_rdm2(t1, t2, l1, l2) self.assertAlmostEqual(abs(dm1[ia][:, ia] - dm1a).max(), 0, 9) self.assertAlmostEqual(abs(dm1[ib][:, ib] - dm1b).max(), 0, 9) self.assertAlmostEqual( abs(dm2[ia][:, ia][:, :, ia][:, :, :, ia] - dm2aa).max(), 0, 9) self.assertAlmostEqual( abs(dm2[ia][:, ia][:, :, ib][:, :, :, ib] - dm2ab).max(), 0, 9) self.assertAlmostEqual( abs(dm2[ib][:, ib][:, :, ib][:, :, :, ib] - dm2bb).max(), 0, 9)
def UCCSD(mf, frozen=None, mo_coeff=None, mo_occ=None): __doc__ = uccsd.UCCSD.__doc__ from pyscf.soscf import newton_ah if isinstance(mf, newton_ah._CIAH_SOSCF) or not isinstance(mf, scf.uhf.UHF): mf = scf.addons.convert_to_uhf(mf) if getattr(mf, 'with_df', None): raise NotImplementedError('DF-UCCSD') else: return uccsd.UCCSD(mf, frozen, mo_coeff, mo_occ)
def UCCSD(mf, frozen=0, mo_coeff=None, mo_occ=None): import sys from pyscf import scf from pyscf.cc import uccsd if 'dft' in str(mf.__module__): sys.stderr.write( 'CCSD Warning: The first argument mf is a DFT object. ' 'CCSD calculation should be used with HF object') mf = scf.addons.convert_to_uhf(mf) if hasattr(mf, 'with_df') and mf.with_df: raise NotImplementedError('DF-UCCSD') else: return uccsd.UCCSD(mf, frozen, mo_coeff, mo_occ)
def test_h2o_rdm(self): mol = mol_s2 mf = mf_s2 mycc = uccsd.UCCSD(mf) mycc.frozen = 2 ecc, t1, t2 = mycc.kernel() l1, l2 = mycc.solve_lambda() dm1a, dm1b = mycc.make_rdm1(t1, t2, l1, l2) dm2aa, dm2ab, dm2bb = mycc.make_rdm2(t1, t2, l1, l2) mo_a = mf.mo_coeff[0] mo_b = mf.mo_coeff[1] nmoa = mo_a.shape[1] nmob = mo_b.shape[1] eriaa = ao2mo.kernel(mf._eri, mo_a, compact=False).reshape([nmoa] * 4) eribb = ao2mo.kernel(mf._eri, mo_b, compact=False).reshape([nmob] * 4) eriab = ao2mo.kernel(mf._eri, (mo_a, mo_a, mo_b, mo_b), compact=False) eriab = eriab.reshape([nmoa, nmoa, nmob, nmob]) hcore = mf.get_hcore() h1a = reduce(numpy.dot, (mo_a.T.conj(), hcore, mo_a)) h1b = reduce(numpy.dot, (mo_b.T.conj(), hcore, mo_b)) e1 = numpy.einsum('ij,ji', h1a, dm1a) e1 += numpy.einsum('ij,ji', h1b, dm1b) e1 += numpy.einsum('ijkl,ijkl', eriaa, dm2aa) * .5 e1 += numpy.einsum('ijkl,ijkl', eriab, dm2ab) e1 += numpy.einsum('ijkl,ijkl', eribb, dm2bb) * .5 e1 += mol.energy_nuc() self.assertAlmostEqual(e1, mycc.e_tot, 7) d1 = uccsd_rdm._gamma1_intermediates(mycc, mycc.t1, mycc.t2, mycc.l1, mycc.l2) mycc.max_memory = 0 d2 = uccsd_rdm._gamma2_intermediates(mycc, mycc.t1, mycc.t2, mycc.l1, mycc.l2, True) dm2 = uccsd_rdm._make_rdm2(mycc, d1, d2, with_dm1=True, with_frozen=True) e1 = numpy.einsum('ij,ji', h1a, dm1a) e1 += numpy.einsum('ij,ji', h1b, dm1b) e1 += numpy.einsum('ijkl,ijkl', eriaa, dm2[0]) * .5 e1 += numpy.einsum('ijkl,ijkl', eriab, dm2[1]) e1 += numpy.einsum('ijkl,ijkl', eribb, dm2[2]) * .5 e1 += mol.energy_nuc() self.assertAlmostEqual(e1, mycc.e_tot, 7)
def test_update_amps1(self): mf = scf.UHF(mol_s2) numpy.random.seed(9) nmo = mf_s2.mo_occ[0].size mf.mo_coeff = numpy.random.random((2, nmo, nmo)) - 0.5 mf.mo_occ = numpy.zeros((2, nmo)) mf.mo_occ[0, :6] = 1 mf.mo_occ[1, :5] = 1 mycc = uccsd.UCCSD(mf) nocca, noccb = 6, 5 nvira, nvirb = nmo - nocca, nmo - noccb nvira_pair = nvira * (nvira + 1) // 2 nvirb_pair = nvirb * (nvirb + 1) // 2 eris = mycc.ao2mo() fakeris = uccsd._ChemistsERIs() fakeris.mo_coeff = eris.mo_coeff fakeris.vvVV = eris.vvVV fakeris.mol = mol_s2 t2ab = numpy.random.random((nocca, noccb, nvira, nvirb)) t1a = numpy.zeros((nocca, nvira)) t1b = numpy.zeros((noccb, nvirb)) self.assertAlmostEqual(lib.finger(mycc._add_vvVV(None, t2ab, fakeris)), 21.652482203108928, 9) fakeris.vvVV = None mycc.direct = True mycc.max_memory = 0 self.assertAlmostEqual(lib.finger(mycc._add_vvVV(None, t2ab, fakeris)), 21.652482203108928, 9) t1 = (numpy.random.random( (nocca, nvira)), numpy.random.random((noccb, nvirb))) t2 = (numpy.random.random((nocca, nocca, nvira, nvira)), numpy.random.random((nocca, noccb, nvira, nvirb)), numpy.random.random((noccb, noccb, nvirb, nvirb))) t1, t2 = mycc.vector_to_amplitudes(mycc.amplitudes_to_vector(t1, t2)) t1, t2 = mycc.update_amps(t1, t2, eris) self.assertAlmostEqual(lib.finger(t1[0]), 49.912690337392938, 10) self.assertAlmostEqual(lib.finger(t1[1]), 74.596097348134776, 10) self.assertAlmostEqual(lib.finger(t2[0]), -41.784696524955393, 10) self.assertAlmostEqual(lib.finger(t2[1]), -9675.7677695314342, 7) self.assertAlmostEqual(lib.finger(t2[2]), 270.75447826471577, 8) self.assertAlmostEqual(lib.finger(mycc.amplitudes_to_vector(t1, t2)), 4341.9623137256776, 6)
def convert_to_uccsd(mycc): from pyscf import scf from pyscf.cc import uccsd, gccsd if isinstance(mycc, uccsd.UCCSD): return mycc elif isinstance(mycc, gccsd.GCCSD): raise NotImplementedError mf = scf.addons.convert_to_uhf(mycc._scf) ucc = uccsd.UCCSD(mf) assert(mycc._nocc is None) assert(mycc._nmo is None) ucc.__dict__.update(mycc.__dict__) ucc._scf = mf ucc.mo_coeff = mf.mo_coeff ucc.mo_occ = mf.mo_occ if not isinstance(mycc.frozen, (int, numpy.integer)): raise NotImplementedError ucc.t1, ucc.t2 = uccsd.amplitudes_from_rccsd(mycc.t1, mycc.t2) return ucc
[8 , (0. , 0. , 0.)], [1 , (0. , -.757 , .587)], [1 , (0. , .757 , .587)]] mol.basis = '631g' mol.build() rhf = scf.RHF(mol) rhf.conv_tol = 1e-14 rhf.scf() mcc = cc.CCSD(rhf) mcc.conv_tol = 1e-14 mcc.ccsd() t1a = t1b = mcc.t1 t2ab = mcc.t2 t2aa = t2bb = t2ab - t2ab.transpose(1,0,2,3) mcc = uccsd.UCCSD(scf.addons.convert_to_uhf(rhf)) e3a = kernel(mcc, mcc.ao2mo(), (t1a,t1b), (t2aa,t2ab,t2bb)) print(e3a - -0.00099642337843278096) mol = gto.Mole() mol.atom = [ [8 , (0. , 0. , 0.)], [1 , (0. , -.757 , .587)], [1 , (0. , .757 , .587)]] mol.spin = 2 mol.basis = '3-21g' mol.build() mf = scf.UHF(mol).run(conv_tol=1e-14) nao, nmo = mf.mo_coeff[0].shape numpy.random.seed(10) mf.mo_coeff = numpy.random.random((2,nao,nmo))
if __name__ == '__main__': from pyscf import gto from pyscf import scf from pyscf.cc import gccsd mol = gto.Mole() mol.atom = [ [8 , (0. , 0. , 0.)], [1 , (0. , -0.757 , 0.587)], [1 , (0. , 0.757 , 0.587)]] mol.basis = '631g' mol.spin = 2 mol.build() mf = scf.UHF(mol).run() mycc = gccsd.GCCSD(scf.addons.convert_to_ghf(mf)) eris = mycc.ao2mo() mycc.kernel() l1, l2 = mycc.solve_lambda(mycc.t1, mycc.t2, eris=eris) l1ref = mycc.spin2spatial(l1, mycc.mo_coeff.orbspin) l2ref = mycc.spin2spatial(l2, mycc.mo_coeff.orbspin) mycc = uccsd.UCCSD(mf) eris = mycc.ao2mo() mycc.kernel() conv, l1, l2 = kernel(mycc, eris, mycc.t1, mycc.t2, tol=1e-8) print(abs(l1[0]-l1ref[0]).max()) print(abs(l1[1]-l1ref[1]).max()) print(abs(l2[0]-l2ref[0]).max()) print(abs(l2[1]-l2ref[1]).max()) print(abs(l2[2]-l2ref[2]).max())
if __name__ == '__main__': from pyscf import gto from pyscf import scf from pyscf.cc import uccsd mol = gto.M( atom = [ ["O" , (0. , 0. , 0.)], [1 , (0. ,-0.757 , 0.587)], [1 , (0. , 0.757 , 0.587)]], basis = '631g', spin = 2, ) mf = scf.UHF(mol).run() mycc = uccsd.UCCSD(mf).run() g1 = mycc.Gradients().kernel() # O -0.0000000000 -0.0000000000 0.1474630318 # H 0.0000000000 0.1118073694 -0.0737315159 # H 0.0000000000 -0.1118073694 -0.0737315159 print(lib.finger(g1) - -0.22892718069135981) myccs = mycc.as_scanner() mol.atom[0] = ["O" , (0., 0., 0.001)] mol.build(0, 0) e1 = myccs(mol) mol.atom[0] = ["O" , (0., 0.,-0.001)] mol.build(0, 0) e2 = myccs(mol) print(g1[0,2], (e1-e2)/0.002*lib.param.BOHR)
def UCCSD(mf, frozen=[[], []], mo_coeff=None, mo_occ=None): from pyscf.cc import uccsd mf = _convert_to_uhf(mf) return uccsd.UCCSD(mf, frozen, mo_coeff, mo_occ)
def test_update_amps2(self): # compare to gccsd.update_amps mol = mol_s2 mf = mf_s2 myucc = uccsd.UCCSD(mf) nocca, noccb = 6, 4 nmo = mol.nao_nr() nvira, nvirb = nmo - nocca, nmo - noccb numpy.random.seed(9) t1 = [ numpy.random.random((nocca, nvira)) - .9, numpy.random.random((noccb, nvirb)) - .9 ] t2 = [ numpy.random.random((nocca, nocca, nvira, nvira)) - .9, numpy.random.random((nocca, noccb, nvira, nvirb)) - .9, numpy.random.random((noccb, noccb, nvirb, nvirb)) - .9 ] t2[0] = t2[0] - t2[0].transpose(1, 0, 2, 3) t2[0] = t2[0] - t2[0].transpose(0, 1, 3, 2) t2[2] = t2[2] - t2[2].transpose(1, 0, 2, 3) t2[2] = t2[2] - t2[2].transpose(0, 1, 3, 2) mo_a = mf.mo_coeff[0] + numpy.sin(mf.mo_coeff[0]) * .01j mo_b = mf.mo_coeff[1] + numpy.sin(mf.mo_coeff[1]) * .01j nao = mo_a.shape[0] eri = ao2mo.restore(1, mf._eri, nao) eri0aa = lib.einsum('pqrs,pi,qj,rk,sl->ijkl', eri, mo_a.conj(), mo_a, mo_a.conj(), mo_a) eri0ab = lib.einsum('pqrs,pi,qj,rk,sl->ijkl', eri, mo_a.conj(), mo_a, mo_b.conj(), mo_b) eri0bb = lib.einsum('pqrs,pi,qj,rk,sl->ijkl', eri, mo_b.conj(), mo_b, mo_b.conj(), mo_b) eri0ba = eri0ab.transpose(2, 3, 0, 1) nvira = nao - nocca nvirb = nao - noccb eris = uccsd._ChemistsERIs(mol) eris.oooo = eri0aa[:nocca, :nocca, :nocca, :nocca].copy() eris.ovoo = eri0aa[:nocca, nocca:, :nocca, :nocca].copy() eris.oovv = eri0aa[:nocca, :nocca, nocca:, nocca:].copy() eris.ovvo = eri0aa[:nocca, nocca:, nocca:, :nocca].copy() eris.ovov = eri0aa[:nocca, nocca:, :nocca, nocca:].copy() eris.ovvv = eri0aa[:nocca, nocca:, nocca:, nocca:].copy() eris.vvvv = eri0aa[nocca:, nocca:, nocca:, nocca:].copy() eris.OOOO = eri0bb[:noccb, :noccb, :noccb, :noccb].copy() eris.OVOO = eri0bb[:noccb, noccb:, :noccb, :noccb].copy() eris.OOVV = eri0bb[:noccb, :noccb, noccb:, noccb:].copy() eris.OVVO = eri0bb[:noccb, noccb:, noccb:, :noccb].copy() eris.OVOV = eri0bb[:noccb, noccb:, :noccb, noccb:].copy() eris.OVVV = eri0bb[:noccb, noccb:, noccb:, noccb:].copy() eris.VVVV = eri0bb[noccb:, noccb:, noccb:, noccb:].copy() eris.ooOO = eri0ab[:nocca, :nocca, :noccb, :noccb].copy() eris.ovOO = eri0ab[:nocca, nocca:, :noccb, :noccb].copy() eris.ooVV = eri0ab[:nocca, :nocca, noccb:, noccb:].copy() eris.ovVO = eri0ab[:nocca, nocca:, noccb:, :noccb].copy() eris.ovOV = eri0ab[:nocca, nocca:, :noccb, noccb:].copy() eris.ovVV = eri0ab[:nocca, nocca:, noccb:, noccb:].copy() eris.vvVV = eri0ab[nocca:, nocca:, noccb:, noccb:].copy() eris.OOoo = eri0ba[:noccb, :noccb, :nocca, :nocca].copy() eris.OVoo = eri0ba[:noccb, noccb:, :nocca, :nocca].copy() eris.OOvv = eri0ba[:noccb, :noccb, nocca:, nocca:].copy() eris.OVvo = eri0ba[:noccb, noccb:, nocca:, :nocca].copy() eris.OVov = eri0ba[:noccb, noccb:, :nocca, nocca:].copy() eris.OVvv = eri0ba[:noccb, noccb:, nocca:, nocca:].copy() eris.VVvv = eri0ba[noccb:, noccb:, nocca:, nocca:].copy() eris.focka = numpy.diag(mf.mo_energy[0]) eris.fockb = numpy.diag(mf.mo_energy[1]) eris.mo_energy = mf.mo_energy t1[0] = t1[0] + numpy.sin(t1[0]) * .05j t1[1] = t1[1] + numpy.sin(t1[1]) * .05j t2[0] = t2[0] + numpy.sin(t2[0]) * .05j t2[1] = t2[1] + numpy.sin(t2[1]) * .05j t2[2] = t2[2] + numpy.sin(t2[2]) * .05j t1new_ref, t2new_ref = uccsd.update_amps(myucc, t1, t2, eris) nocc = nocca + noccb orbspin = numpy.zeros(nao * 2, dtype=int) orbspin[1::2] = 1 orbspin[nocc - 1] = 0 orbspin[nocc] = 1 eri1 = numpy.zeros([nao * 2] * 4, dtype=numpy.complex) idxa = numpy.where(orbspin == 0)[0] idxb = numpy.where(orbspin == 1)[0] eri1[idxa[:, None, None, None], idxa[:, None, None], idxa[:, None], idxa] = eri0aa eri1[idxa[:, None, None, None], idxa[:, None, None], idxb[:, None], idxb] = eri0ab eri1[idxb[:, None, None, None], idxb[:, None, None], idxa[:, None], idxa] = eri0ba eri1[idxb[:, None, None, None], idxb[:, None, None], idxb[:, None], idxb] = eri0bb eri1 = eri1.transpose(0, 2, 1, 3) - eri1.transpose(0, 2, 3, 1) erig = gccsd._PhysicistsERIs() erig.oooo = eri1[:nocc, :nocc, :nocc, :nocc].copy() erig.ooov = eri1[:nocc, :nocc, :nocc, nocc:].copy() erig.ovov = eri1[:nocc, nocc:, :nocc, nocc:].copy() erig.ovvo = eri1[:nocc, nocc:, nocc:, :nocc].copy() erig.oovv = eri1[:nocc, :nocc, nocc:, nocc:].copy() erig.ovvv = eri1[:nocc, nocc:, nocc:, nocc:].copy() erig.vvvv = eri1[nocc:, nocc:, nocc:, nocc:].copy() mo_e = numpy.empty(nao * 2) mo_e[orbspin == 0] = mf.mo_energy[0] mo_e[orbspin == 1] = mf.mo_energy[1] erig.fock = numpy.diag(mo_e) erig.mo_energy = mo_e.real myccg = gccsd.GCCSD(scf.addons.convert_to_ghf(mf)) t1 = myccg.spatial2spin(t1, orbspin) t2 = myccg.spatial2spin(t2, orbspin) t1new, t2new = gccsd.update_amps(myccg, t1, t2, erig) t1new = myccg.spin2spatial(t1new, orbspin) t2new = myccg.spin2spatial(t2new, orbspin) self.assertAlmostEqual(abs(t1new[0] - t1new_ref[0]).max(), 0, 12) self.assertAlmostEqual(abs(t1new[1] - t1new_ref[1]).max(), 0, 12) self.assertAlmostEqual(abs(t2new[0] - t2new_ref[0]).max(), 0, 12) self.assertAlmostEqual(abs(t2new[1] - t2new_ref[1]).max(), 0, 12) self.assertAlmostEqual(abs(t2new[2] - t2new_ref[2]).max(), 0, 12)
rhf.conv_tol_grad = 1e-8 rhf.kernel() mf = scf.addons.convert_to_uhf(rhf) myucc = cc.UCCSD(mf).run(conv_tol=1e-10) mol_s2 = gto.Mole() mol_s2.atom = [[8, (0., 0., 0.)], [1, (0., -0.757, 0.587)], [1, (0., 0.757, 0.587)]] mol_s2.basis = '631g' mol_s2.spin = 2 mol_s2.verbose = 5 mol_s2.output = '/dev/null' mol_s2.build() mf_s2 = scf.UHF(mol_s2).run() eris = uccsd.UCCSD(mf_s2).ao2mo() def tearDownModule(): global mol, rhf, mf, myucc, mol_s2, mf_s2, eris mol.stdout.close() mol_s2.stdout.close() del mol, rhf, mf, myucc, mol_s2, mf_s2, eris class KnownValues(unittest.TestCase): # def test_with_df(self): # mf = scf.UHF(mol).density_fit(auxbasis='weigend').run() # mycc = cc.UCCSD(mf).run() # self.assertAlmostEqual(mycc.e_tot, -76.118403942938741, 7)
[8 , (0. , 0. , 0.)], [1 , (0. , -.757 , .587)], [1 , (0. , .757 , .587)]] mol.basis = '631g' mol.build() rhf = scf.RHF(mol) rhf.conv_tol = 1e-14 rhf.scf() mcc = cc.CCSD(rhf) mcc.conv_tol = 1e-14 mcc.ccsd() t1a = t1b = mcc.t1 t2ab = mcc.t2 t2aa = t2bb = t2ab - t2ab.transpose(1,0,2,3) e3a = kernel(_ERIS(uccsd.UCCSD(scf.addons.convert_to_uhf(rhf))), (t1a,t1b), (t2aa,t2ab,t2bb)) print(e3a - -0.00099642337843278096) mol = gto.Mole() mol.atom = [ [8 , (0. , 0. , 0.)], [1 , (0. , -.757 , .587)], [1 , (0. , .757 , .587)]] mol.spin = 2 mol.basis = '3-21g' mol.build() mf = scf.UHF(mol).run(conv_tol=1e-14) numpy.random.seed(10) mf.mo_coeff = numpy.random.random(mf.mo_coeff.shape)
def UCCSD(mf, frozen=[], mo_energy=None, mo_coeff=None, mo_occ=None): from pyscf.cc import uccsd return uccsd.UCCSD(mf, frozen, mo_energy, mo_coeff, mo_occ)
def test_rdm_vs_uccsd(self): mol = gto.Mole() mol.atom = [[8, (0., 0., 0.)], [1, (0., -0.757, 0.587)], [1, (0., 0.757, 0.587)]] mol.verbose = 5 mol.output = '/dev/null' mol.basis = '631g' mol.spin = 2 mol.build() mf = scf.UHF(mol).run() myucc = uccsd.UCCSD(mf) myucc.frozen = 1 myucc.kernel() udm1 = myucc.make_rdm1() udm2 = myucc.make_rdm2() mf = scf.addons.convert_to_ghf(mf) mygcc = gccsd.GCCSD(mf) mygcc.frozen = 2 ecc, t1, t2 = mygcc.kernel() l1, l2 = mygcc.solve_lambda() dm1 = gccsd_rdm.make_rdm1(mygcc, t1, t2, l1, l2) dm2 = gccsd_rdm.make_rdm2(mygcc, t1, t2, l1, l2) nao = mol.nao_nr() mo_a = mf.mo_coeff[:nao] mo_b = mf.mo_coeff[nao:] nmo = mo_a.shape[1] eri = ao2mo.kernel(mf._eri, mo_a + mo_b, compact=False).reshape([nmo] * 4) orbspin = mf.mo_coeff.orbspin sym_forbid = (orbspin[:, None] != orbspin) eri[sym_forbid, :, :] = 0 eri[:, :, sym_forbid] = 0 hcore = scf.RHF(mol).get_hcore() h1 = reduce(numpy.dot, (mo_a.T.conj(), hcore, mo_a)) h1 += reduce(numpy.dot, (mo_b.T.conj(), hcore, mo_b)) e1 = numpy.einsum('ij,ji', h1, dm1) e1 += numpy.einsum('ijkl,ijkl', eri, dm2) * .5 e1 += mol.energy_nuc() self.assertAlmostEqual(e1, mygcc.e_tot, 7) idxa = numpy.where(orbspin == 0)[0] idxb = numpy.where(orbspin == 1)[0] self.assertAlmostEqual( abs(dm1[idxa[:, None], idxa] - udm1[0]).max(), 0, 7) self.assertAlmostEqual( abs(dm1[idxb[:, None], idxb] - udm1[1]).max(), 0, 7) self.assertAlmostEqual( abs(dm2[idxa[:, None, None, None], idxa[:, None, None], idxa[:, None], idxa] - udm2[0]).max(), 0, 7) self.assertAlmostEqual( abs(dm2[idxa[:, None, None, None], idxa[:, None, None], idxb[:, None], idxb] - udm2[1]).max(), 0, 7) self.assertAlmostEqual( abs(dm2[idxb[:, None, None, None], idxb[:, None, None], idxb[:, None], idxb] - udm2[2]).max(), 0, 7) ut1 = [0] * 2 ul1 = [0] * 2 ut2 = [0] * 3 ul2 = [0] * 3 ut1[0] = myucc.t1[0] + numpy.cos(myucc.t1[0]) * .2j ut1[1] = myucc.t1[1] + numpy.cos(myucc.t1[1]) * .2j ul1[0] = myucc.l1[0] + numpy.cos(myucc.l1[0]) * .2j ul1[1] = myucc.l1[1] + numpy.cos(myucc.l1[1]) * .2j ut2[0] = myucc.t2[0] + numpy.sin(myucc.t2[0]) * .8j ut2[1] = myucc.t2[1] + numpy.sin(myucc.t2[1]) * .8j ut2[2] = myucc.t2[2] + numpy.sin(myucc.t2[2]) * .8j ul2[0] = myucc.l2[0] + numpy.sin(myucc.l2[0]) * .8j ul2[1] = myucc.l2[1] + numpy.sin(myucc.l2[1]) * .8j ul2[2] = myucc.l2[2] + numpy.sin(myucc.l2[2]) * .8j udm1 = myucc.make_rdm1(ut1, ut2, ul1, ul2) udm2 = myucc.make_rdm2(ut1, ut2, ul1, ul2) gt1 = mygcc.spatial2spin(ut1) gt2 = mygcc.spatial2spin(ut2) gl1 = mygcc.spatial2spin(ul1) gl2 = mygcc.spatial2spin(ul2) gdm1 = mygcc.make_rdm1(gt1, gt2, gl1, gl2) gdm2 = mygcc.make_rdm2(gt1, gt2, gl1, gl2) self.assertAlmostEqual( abs(gdm1[idxa[:, None], idxa] - udm1[0]).max(), 0, 9) self.assertAlmostEqual( abs(gdm1[idxb[:, None], idxb] - udm1[1]).max(), 0, 9) self.assertAlmostEqual( abs(gdm2[idxa[:, None, None, None], idxa[:, None, None], idxa[:, None], idxa] - udm2[0]).max(), 0, 9) self.assertAlmostEqual( abs(gdm2[idxa[:, None, None, None], idxa[:, None, None], idxb[:, None], idxb] - udm2[1]).max(), 0, 9) self.assertAlmostEqual( abs(gdm2[idxb[:, None, None, None], idxb[:, None, None], idxb[:, None], idxb] - udm2[2]).max(), 0, 9)