def check_frozen(frozen): myci = ci.GCISD(mf) myci.frozen = frozen myci.nroots = 3 myci.kernel() nocc = myci.nocc nmo = myci.nmo nfroz = len(frozen) try: ket_id = 1 fciket = gcisd.to_fcivec(myci.ci[ket_id], mol.nelectron, orbspin, myci.frozen) except RuntimeError: ket_id = 2 fciket = gcisd.to_fcivec(myci.ci[ket_id], mol.nelectron, orbspin, myci.frozen) # spin-forbidden transition cidm1 = myci.trans_rdm1(myci.ci[0], myci.ci[1], nmo, nocc) self.assertAlmostEqual(abs(cidm1[idxa[:,None],idxa]).max(), 0, 7) self.assertAlmostEqual(abs(cidm1[idxb[:,None],idxb]).max(), 0, 7) cibra = (myci.ci[0] + myci.ci[ket_id]) * numpy.sqrt(.5) fcibra = gcisd.to_fcivec(cibra, mol.nelectron, orbspin, myci.frozen) fcidm1 = fci.direct_spin1.trans_rdm1s(fcibra, fciket, nmo_1c, mol.nelectron) cidm1 = myci.trans_rdm1(cibra, myci.ci[ket_id], nmo, nocc) self.assertAlmostEqual(abs(fcidm1[0]-cidm1[idxa[:,None],idxa]).max(), 0, 12) self.assertAlmostEqual(abs(fcidm1[1]-cidm1[idxb[:,None],idxb]).max(), 0, 12)
def test_rdm_h4(self): mol = gto.Mole() mol.verbose = 7 mol.output = '/dev/null' mol.atom = [ ['O', ( 0., 0. , 0. )], ['H', ( 0., -0.757, 0.587)], ['H', ( 0., 0.757 , 0.587)],] mol.spin = 2 mol.basis = 'sto-3g' mol.build() mf = scf.RHF(mol).run(conv_tol=1e-14) myci = ci.GCISD(mf) eris = myci.ao2mo() ecisd, civec = myci.kernel(eris=eris) self.assertAlmostEqual(ecisd, -0.035165114624046617, 8) nmo = eris.mo_coeff.shape[1] rdm1 = myci.make_rdm1(civec, nmo, mol.nelectron) rdm2 = myci.make_rdm2(civec, nmo, mol.nelectron) mo = eris.mo_coeff[:7] + eris.mo_coeff[7:] eri = ao2mo.kernel(mf._eri, mo, compact=False).reshape([nmo]*4) eri[eris.orbspin[:,None]!=eris.orbspin,:,:] = 0 eri[:,:,eris.orbspin[:,None]!=eris.orbspin] = 0 h1e = reduce(numpy.dot, (mo.T, mf.get_hcore(), mo)) h1e[eris.orbspin[:,None]!=eris.orbspin] = 0 e2 = (numpy.einsum('ij,ji', h1e, rdm1) + numpy.einsum('ijkl,ijkl', eri, rdm2) * .5) e2 += mol.energy_nuc() self.assertAlmostEqual(myci.e_tot, e2, 9) dm1 = numpy.einsum('ijkk->ji', rdm2)/(mol.nelectron-1) self.assertAlmostEqual(abs(rdm1 - dm1).max(), 0, 9)
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.GHF(mol).run() myci = ci.GCISD(mf) myci.nroots = 3 myci.run() self.assertAlmostEqual(myci.e_tot[2], -1.9802158893844912, 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.RHF(mol).run(conv_tol=1e-14) myci = ci.GCISD(mf) myci.max_memory = .1 myci.frozen = [2,3,4,5] myci.direct = True ecisd, civec = myci.kernel() self.assertAlmostEqual(ecisd, -0.048829195509732602, 8)
def test_contract(self): '''cross check with UCISD''' mol = gto.M() mol.nelectron = 6 nocc, nvir = mol.nelectron // 2, 4 nmo = nocc + nvir nmo_pair = nmo * (nmo + 1) // 2 mf = scf.UHF(mol) numpy.random.seed(12) mf._eri = numpy.random.random(nmo_pair * (nmo_pair + 1) // 2) * .2 mf.mo_coeff = numpy.random.random((2, nmo, nmo)) mf.mo_energy = [numpy.arange(0., nmo)] * 2 mf.mo_occ = numpy.zeros((2, nmo)) mf.mo_occ[:, :nocc] = 1 h1 = numpy.random.random((nmo, nmo)) * .1 h1 = h1 + h1.T + numpy.diag(numpy.arange(nmo)) mf.get_hcore = lambda *args: h1 mf1 = scf.addons.convert_to_ghf(mf) mf1.get_hcore = lambda *args: scipy.linalg.block_diag(h1, h1) gci = ci.GCISD(mf1) c2 = numpy.random.random( (nocc * 2, nocc * 2, nvir * 2, nvir * 2)) * .1 - .1 c2 = c2 - c2.transpose(0, 1, 3, 2) c2 = c2 - c2.transpose(1, 0, 2, 3) c1 = numpy.random.random((nocc * 2, nvir * 2)) * .1 c0 = .5 civec = gci.amplitudes_to_cisdvec(c0, c1, c2) civecref = gci.contract(civec, gci.ao2mo()) c0ref, c1ref, c2ref = gci.cisdvec_to_amplitudes(civecref) c1ref = gci.spin2spatial(c1ref) c2ref = gci.spin2spatial(c2ref) c1 = gci.spin2spatial(c1) c2 = gci.spin2spatial(c2) myci = ci.UCISD(mf) civec = myci.amplitudes_to_cisdvec(c0, c1, c2) cinew = myci.contract(civec, myci.ao2mo()) c0new, c1new, c2new = myci.cisdvec_to_amplitudes(cinew) self.assertAlmostEqual(abs(c0new - c0ref).max(), 0, 12) self.assertAlmostEqual(abs(c1new[0] - c1ref[0]).max(), 0, 12) self.assertAlmostEqual(abs(c1new[1] - c1ref[1]).max(), 0, 12) self.assertAlmostEqual(abs(c2new[0] - c2ref[0]).max(), 0, 12) self.assertAlmostEqual(abs(c2new[1] - c2ref[1]).max(), 0, 12) self.assertAlmostEqual(abs(c2new[2] - c2ref[2]).max(), 0, 12) self.assertAlmostEqual(lib.finger(cinew), -102.17887236599671, 9)
def test_trans_rdm1(self): numpy.random.seed(1) norb = 4 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 = fci.direct_spin1.trans_rdm1s(fcibra, fciket, norb, nocc * 2) myci1 = ci.GCISD(scf.GHF(gto.M())) myci1.nmo = norb = 8 myci1.nocc = nocc = 4 orbspin = numpy.zeros(norb, dtype=int) orbspin[1::2] = 1 myci1.mo_coeff = lib.tag_array(numpy.eye(norb), orbspin=orbspin) myci1.mo_occ = numpy.zeros(norb) myci1.mo_occ[:nocc] = 1 cibra = myci1.from_rcisdvec(cibra, (nocc // 2, nocc // 2), orbspin) ciket = myci1.from_rcisdvec(ciket) cidm1 = myci1.trans_rdm1(cibra, ciket, norb, nocc) self.assertAlmostEqual(abs(cidm1[0::2, 0::2] - fcidm1[0]).max(), 0, 12) self.assertAlmostEqual(abs(cidm1[1::2, 1::2] - fcidm1[1]).max(), 0, 12) cibra = myci1.to_ucisdvec(cibra, orbspin) ciket = myci1.to_ucisdvec(ciket) myci2 = ci.UCISD(scf.UHF(gto.M())) cidm1 = myci2.trans_rdm1(cibra, ciket, (norb // 2, norb // 2), (nocc // 2, nocc // 2)) self.assertAlmostEqual(abs(cidm1[0] - fcidm1[0]).max(), 0, 12) self.assertAlmostEqual(abs(cidm1[1] - fcidm1[1]).max(), 0, 12)
def test_h4_a(self): '''Compare to FCI''' 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 = '3-21g' mol.build() mf = scf.UHF(mol).run(conv_tol=1e-14) ehf0 = mf.e_tot - mol.energy_nuc() gmf = scf.addons.convert_to_ghf(mf) myci = ci.GCISD(gmf) eris = myci.ao2mo() numpy.random.seed(12) nocca, noccb = mol.nelec nmo = mol.nao_nr() nvira = nmo - nocca nvirb = nmo - noccb #cisdvec = myci.get_init_guess(eris)[1] c1a = .1 * numpy.random.random((nocca,nvira)) c1b = .1 * numpy.random.random((noccb,nvirb)) c2aa = .1 * numpy.random.random((nocca,nocca,nvira,nvira)) c2bb = .1 * numpy.random.random((noccb,noccb,nvirb,nvirb)) c2ab = .1 * numpy.random.random((nocca,noccb,nvira,nvirb)) c1 = myci.spatial2spin((c1a, c1b)) c2 = myci.spatial2spin((c2aa, c2ab, c2bb)) cisdvec = myci.amplitudes_to_cisdvec(1., c1, c2) self.assertEqual(cisdvec.size, myci.vector_size()) hcisd0 = myci.contract(cisdvec, eris) 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])) h2e = fci.direct_uhf.absorb_h1e((h1a,h1b), (eri_aa,eri_ab,eri_bb), h1a.shape[0], mol.nelec, .5) fcivec = myci.to_fcivec(cisdvec, mol.nelectron, eris.orbspin) hci1 = fci.direct_uhf.contract_2e(h2e, fcivec, h1a.shape[0], mol.nelec) hci1 -= ehf0 * fcivec hcisd1 = myci.from_fcivec(hci1, mol.nelectron, eris.orbspin) self.assertAlmostEqual(abs(hcisd1-hcisd0).max(), 0, 9) hdiag0 = myci.make_diagonal(eris) hdiag0 = myci.to_fcivec(hdiag0, mol.nelectron, eris.orbspin).ravel() hdiag0 = myci.from_fcivec(hdiag0, mol.nelectron, eris.orbspin).ravel() hdiag1 = fci.direct_uhf.make_hdiag((h1a,h1b), (eri_aa,eri_ab,eri_bb), h1a.shape[0], mol.nelec) hdiag1 = myci.from_fcivec(hdiag1, mol.nelectron, eris.orbspin).ravel() self.assertAlmostEqual(abs(abs(hdiag0)-abs(hdiag1)).max(), 0, 9) ecisd = myci.kernel()[0] efci = fci.direct_uhf.kernel((h1a,h1b), (eri_aa,eri_ab,eri_bb), h1a.shape[0], mol.nelec)[0] self.assertAlmostEqual(ecisd, -0.037067274690894436, 9) self.assertTrue(myci.e_tot-mol.energy_nuc() - efci < 0.002)
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.spin = 2 mol.basis = '3-21g' mol.build() mf = scf.GHF(mol).run(conv_tol=1e-14) myci = ci.GCISD(mf) myci.kernel() self.assertAlmostEqual(myci.e_tot, -0.86423570617209888, 8) mf = scf.RHF(mol).run(conv_tol=1e-14) myci = ci.GCISD(mf) myci.kernel() self.assertAlmostEqual(myci.e_tot, -0.86423570617209888, 8) 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 = 0 mol.basis = '3-21g' mol.build() mf = scf.UHF(mol).run(conv_tol=1e-14) myci = ci.GCISD(mf) myci.kernel() self.assertAlmostEqual(myci.e_tot, -0.86423570617209888, 8) mf = scf.UHF(mol).run(conv_tol=1e-14) gmf = scf.addons.convert_to_ghf(mf) ehf0 = mf.e_tot - mol.energy_nuc() myci = gcisd.GCISD(gmf) 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(myci.e_tot-mol.energy_nuc(), efci, 9) dm1ref, dm2ref = fci.direct_uhf.make_rdm12s(fcivec, h1a.shape[0], mol.nelec) nmo = myci.nmo rdm1 = myci.make_rdm1(myci.ci, nmo, mol.nelectron) rdm2 = myci.make_rdm2(myci.ci, nmo, mol.nelectron) idxa = eris.orbspin == 0 idxb = eris.orbspin == 1 self.assertAlmostEqual(abs(dm1ref[0] - rdm1[idxa][:,idxa]).max(), 0, 6) self.assertAlmostEqual(abs(dm1ref[1] - rdm1[idxb][:,idxb]).max(), 0, 6) self.assertAlmostEqual(abs(dm2ref[0] - rdm2[idxa][:,idxa][:,:,idxa][:,:,:,idxa]).max(), 0, 6) self.assertAlmostEqual(abs(dm2ref[1] - rdm2[idxa][:,idxa][:,:,idxb][:,:,:,idxb]).max(), 0, 6) self.assertAlmostEqual(abs(dm2ref[2] - rdm2[idxb][:,idxb][:,:,idxb][:,:,:,idxb]).max(), 0, 6)