def test_from_fcivec(self): myci = scf.UHF(gto.M()).apply(ci.CISD) nocca, noccb = nelec = (3,2) nvira, nvirb = 5, 6 myci.nocc = nocc = nocca, noccb nmo = 8 myci.nmo = (nmo,nmo) numpy.random.seed(12) civec = numpy.random.random(myci.vector_size()) ci0 = ucisd.to_fcivec(civec, nmo, nelec) self.assertAlmostEqual(abs(civec-ucisd.from_fcivec(ci0, nmo, nelec)).max(), 0, 9) nocc = 3 nvir = 5 nmo = nocc + nvir c1a = numpy.random.random((nocc,nvir)) c1b = numpy.random.random((nocc,nvir)) c2aa = numpy.random.random((nocc,nocc,nvir,nvir)) c2bb = numpy.random.random((nocc,nocc,nvir,nvir)) c2ab = numpy.random.random((nocc,nocc,nvir,nvir)) c1 = (c1a, c1b) c2 = (c2aa, c2ab, c2bb) cisdvec = ucisd.amplitudes_to_cisdvec(1., c1, c2) fcivec = ucisd.to_fcivec(cisdvec, nmo, nocc*2) cisdvec1 = ucisd.from_fcivec(fcivec, nmo, nocc*2) self.assertAlmostEqual(abs(cisdvec-cisdvec1).max(), 0, 12) ci1 = ucisd.to_fcivec(cisdvec1, nmo, (nocc,nocc)) self.assertAlmostEqual(abs(fcivec-ci1).max(), 0, 12)
def test_from_fcivec(self): myci = scf.UHF(gto.M()).apply(ci.CISD) nocca, noccb = nelec = (3, 2) nvira, nvirb = 5, 6 myci.nocc = nocc = nocca, noccb nmo = 8 myci.nmo = (nmo, nmo) numpy.random.seed(12) civec = numpy.random.random(myci.vector_size()) ci0 = ucisd.to_fcivec(civec, nmo, nelec) self.assertAlmostEqual( abs(civec - ucisd.from_fcivec(ci0, nmo, nelec)).max(), 0, 9) nocc = 3 nvir = 5 nmo = nocc + nvir c1a = numpy.random.random((nocc, nvir)) c1b = numpy.random.random((nocc, nvir)) c2aa = numpy.random.random((nocc, nocc, nvir, nvir)) c2bb = numpy.random.random((nocc, nocc, nvir, nvir)) c2ab = numpy.random.random((nocc, nocc, nvir, nvir)) c1 = (c1a, c1b) c2 = (c2aa, c2ab, c2bb) cisdvec = ucisd.amplitudes_to_cisdvec(1., c1, c2) fcivec = ucisd.to_fcivec(cisdvec, nmo, nocc * 2) cisdvec1 = ucisd.from_fcivec(fcivec, nmo, nocc * 2) self.assertAlmostEqual(abs(cisdvec - cisdvec1).max(), 0, 12) ci1 = ucisd.to_fcivec(cisdvec1, nmo, (nocc, nocc)) self.assertAlmostEqual(abs(fcivec - ci1).max(), 0, 12)
def check_frozen(frozen): myci = ci.UCISD(mf) myci.frozen = frozen myci.nroots = 2 myci.kernel() nocc = myci.nocc nmo = myci.nmo norb = mf.mo_coeff[0].shape[1] nfroz = len(frozen[0]) cibra = (myci.ci[0] + myci.ci[1]) * numpy.sqrt(.5) fcibra = ucisd.to_fcivec(cibra, norb, mol.nelec, myci.frozen) fciket = ucisd.to_fcivec(myci.ci[1], norb, mol.nelec, myci.frozen) fcidm1 = fci.direct_spin1.trans_rdm1s(fcibra, fciket, norb, mol.nelec) cidm1 = myci.trans_rdm1(cibra, myci.ci[1], nmo, nocc) self.assertAlmostEqual(abs(fcidm1[0]-cidm1[0]).max(), 0, 12) self.assertAlmostEqual(abs(fcidm1[1]-cidm1[1]).max(), 0, 12)
def test_overlap(self): nmo = 8 nocc = nocca, noccb = (4,3) numpy.random.seed(2) nvira, nvirb = nmo-nocca, nmo-noccb cibra = ucisd.amplitudes_to_cisdvec(numpy.random.rand(1), (numpy.random.rand(nocca,nvira), numpy.random.rand(noccb,nvirb)), (numpy.random.rand(nocca,nocca,nvira,nvira), numpy.random.rand(nocca,noccb,nvira,nvirb), numpy.random.rand(noccb,noccb,nvirb,nvirb))) ciket = ucisd.amplitudes_to_cisdvec(numpy.random.rand(1), (numpy.random.rand(nocca,nvira), numpy.random.rand(noccb,nvirb)), (numpy.random.rand(nocca,nocca,nvira,nvira), numpy.random.rand(nocca,noccb,nvira,nvirb), numpy.random.rand(noccb,noccb,nvirb,nvirb))) fcibra = ucisd.to_fcivec(cibra, nmo, nocc) fciket = ucisd.to_fcivec(ciket, nmo, nocc) s_mo = numpy.random.random((nmo,nmo)) s0 = fci.addons.overlap(fcibra, fciket, nmo, nocc, s_mo) s1 = ucisd.overlap(cibra, ciket, nmo, nocc, (s_mo, s_mo)) self.assertAlmostEqual(s1, s0, 9)
def test_overlap(self): nmo = 8 nocc = nocca, noccb = (4, 3) numpy.random.seed(2) nvira, nvirb = nmo - nocca, nmo - noccb cibra = ucisd.amplitudes_to_cisdvec( numpy.random.rand(1), (numpy.random.rand(nocca, nvira), numpy.random.rand(noccb, nvirb)), (numpy.random.rand(nocca, nocca, nvira, nvira), numpy.random.rand(nocca, noccb, nvira, nvirb), numpy.random.rand(noccb, noccb, nvirb, nvirb))) ciket = ucisd.amplitudes_to_cisdvec( numpy.random.rand(1), (numpy.random.rand(nocca, nvira), numpy.random.rand(noccb, nvirb)), (numpy.random.rand(nocca, nocca, nvira, nvira), numpy.random.rand(nocca, noccb, nvira, nvirb), numpy.random.rand(noccb, noccb, nvirb, nvirb))) fcibra = ucisd.to_fcivec(cibra, nmo, nocc) fciket = ucisd.to_fcivec(ciket, nmo, nocc) s_mo = numpy.random.random((nmo, nmo)) s0 = fci.addons.overlap(fcibra, fciket, nmo, nocc, s_mo) s1 = ucisd.overlap(cibra, ciket, nmo, nocc, (s_mo, s_mo)) self.assertAlmostEqual(s1, s0, 9)
def to_fcivec(cisdvec, nelec, orbspin, frozen=0): assert(numpy.count_nonzero(orbspin == 0) == numpy.count_nonzero(orbspin == 1)) norb = len(orbspin) frozen_mask = numpy.zeros(norb, dtype=bool) if isinstance(frozen, (int, numpy.integer)): frozen_mask[:frozen] = True else: frozen_mask[frozen] = True frozen = (numpy.where(frozen_mask[orbspin == 0])[0], numpy.where(frozen_mask[orbspin == 1])[0]) nelec = (numpy.count_nonzero(orbspin[:nelec] == 0), numpy.count_nonzero(orbspin[:nelec] == 1)) orbspin = orbspin[~frozen_mask] nmo = len(orbspin) nocc = numpy.count_nonzero(~frozen_mask[:sum(nelec)]) ucisdvec = to_ucisdvec(cisdvec, nmo, nocc, orbspin) return ucisd.to_fcivec(ucisdvec, norb//2, nelec, frozen)