def make_rdm1(self, t1=None, t2=None, l1=None, l2=None, ao_repr=False): '''Un-relaxed 1-particle density matrix in MO space''' from pyscf.cc import gccsd_rdm if t1 is None: t1 = self.t1 if t2 is None: t2 = self.t2 if l1 is None: l1 = self.l1 if l2 is None: l2 = self.l2 if l1 is None: l1, l2 = self.solve_lambda(t1, t2) return gccsd_rdm.make_rdm1(self, t1, t2, l1, l2, ao_repr=ao_repr)
def make_rdm1(self, t1=None, t2=None, l1=None, l2=None, ao_repr=False): '''Un-relaxed 1-particle density matrix in MO space''' from pyscf.cc import gccsd_rdm if t1 is None: t1 = self.t1 if t2 is None: t2 = self.t2 if l1 is None: l1 = self.l1 if l2 is None: l2 = self.l2 if l1 is None: l1, l2 = self.solve_lambda(t1, t2) return gccsd_rdm.make_rdm1(self, t1, t2, l1, l2, ao_repr=ao_repr)
def test_rdm_real(self): nocc = 6 nvir = 10 mf = scf.GHF(mol) nmo = nocc + nvir npair = nmo * (nmo // 2 + 1) // 4 numpy.random.seed(12) mf._eri = numpy.random.random(npair * (npair + 1) // 2) * .3 hcore = numpy.random.random((nmo, nmo)) * .5 hcore = hcore + hcore.T + numpy.diag(range(nmo)) * 2 mf.get_hcore = lambda *args: hcore mf.get_ovlp = lambda *args: numpy.eye(nmo) mf.mo_coeff = numpy.eye(nmo) mf.mo_occ = numpy.zeros(nmo) mf.mo_occ[:nocc] = 1 mycc = gccsd.GCCSD(mf) ecc, t1, t2 = mycc.kernel() l1, l2 = mycc.solve_lambda() dm1 = gccsd_rdm.make_rdm1(mycc, t1, t2, l1, l2) dm2 = gccsd_rdm.make_rdm2(mycc, t1, t2, l1, l2) nao = nmo // 2 mo_a = mf.mo_coeff[:nao] mo_b = mf.mo_coeff[nao:] eri = ao2mo.kernel(mf._eri, mo_a) eri += ao2mo.kernel(mf._eri, mo_b) eri1 = ao2mo.kernel(mf._eri, (mo_a, mo_a, mo_b, mo_b)) eri += eri1 eri += eri1.T eri = ao2mo.restore(1, eri, nmo) h1 = reduce(numpy.dot, (mf.mo_coeff.T.conj(), hcore, mf.mo_coeff)) e1 = numpy.einsum('ij,ji', h1, dm1) e1 += numpy.einsum('ijkl,ijkl', eri, dm2) * .5 e1 += mol.energy_nuc() self.assertAlmostEqual(e1, mycc.e_tot, 7) self.assertAlmostEqual( abs(dm2 - dm2.transpose(1, 0, 3, 2).conj()).max(), 0, 9) self.assertAlmostEqual( abs(dm2 - dm2.transpose(2, 3, 0, 1)).max(), 0, 9) self.assertAlmostEqual( abs(dm2 + dm2.transpose(2, 1, 0, 3)).max(), 0, 9) self.assertAlmostEqual( abs(dm2 + dm2.transpose(0, 3, 2, 1)).max(), 0, 9)
def test_rdm_real(self): nocc = 6 nvir = 10 mf = scf.GHF(mol) nmo = nocc + nvir npair = nmo*(nmo//2+1)//4 numpy.random.seed(12) mf._eri = numpy.random.random(npair*(npair+1)//2)*.3 hcore = numpy.random.random((nmo,nmo)) * .5 hcore = hcore + hcore.T + numpy.diag(range(nmo))*2 mf.get_hcore = lambda *args: hcore mf.get_ovlp = lambda *args: numpy.eye(nmo) mf.mo_coeff = numpy.eye(nmo) mf.mo_occ = numpy.zeros(nmo) mf.mo_occ[:nocc] = 1 mf.e_tot = mf.energy_elec()[0] mycc = gccsd.GCCSD(mf) ecc, t1, t2 = mycc.kernel() l1, l2 = mycc.solve_lambda() dm1 = gccsd_rdm.make_rdm1(mycc, t1, t2, l1, l2) dm2 = gccsd_rdm.make_rdm2(mycc, t1, t2, l1, l2) nao = nmo // 2 mo_a = mf.mo_coeff[:nao] mo_b = mf.mo_coeff[nao:] eri = ao2mo.kernel(mf._eri, mo_a) eri += ao2mo.kernel(mf._eri, mo_b) eri1 = ao2mo.kernel(mf._eri, (mo_a,mo_a,mo_b,mo_b)) eri += eri1 eri += eri1.T eri = ao2mo.restore(1, eri, nmo) h1 = reduce(numpy.dot, (mf.mo_coeff.T.conj(), hcore, mf.mo_coeff)) e1 = numpy.einsum('ij,ji', h1, dm1) e1+= numpy.einsum('ijkl,ijkl', eri, dm2) * .5 self.assertAlmostEqual(e1, mycc.e_tot, 7) self.assertAlmostEqual(abs(dm2-dm2.transpose(1,0,3,2).conj()).max(), 0, 9) self.assertAlmostEqual(abs(dm2-dm2.transpose(2,3,0,1) ).max(), 0, 9) self.assertAlmostEqual(abs(dm2+dm2.transpose(2,1,0,3) ).max(), 0, 9) self.assertAlmostEqual(abs(dm2+dm2.transpose(0,3,2,1) ).max(), 0, 9)
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)
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)