Ejemplo n.º 1
0
    def test_rdm(self):
        norb, nelec = 10, 4
        strs = cistring.gen_strings4orblist(range(norb), nelec)
        numpy.random.seed(11)
        mask = numpy.random.random(len(strs)) > .6
        strsa = strs[mask]
        mask = numpy.random.random(len(strs)) > .7
        strsb = strs[mask]
        ci_strs = (strsa, strsb)
        ci_coeff = select_ci._as_SCIvector(numpy.random.random((len(strsa),len(strsb))), ci_strs)
        ci0 = select_ci.to_fci(ci_coeff, norb, (nelec,nelec))
        dm1ref, dm2ref = direct_spin1.make_rdm12s(ci0, norb, (nelec,nelec))
        dm1 = select_ci.make_rdm1s(ci_coeff, norb, (nelec,nelec))
        self.assertAlmostEqual(abs(dm1[0]-dm1ref[0]).sum(), 0, 9)
        self.assertAlmostEqual(abs(dm1[1]-dm1ref[1]).sum(), 0, 9)
        dm2 = select_ci.make_rdm2s(ci_coeff, norb, (nelec,nelec))
        self.assertAlmostEqual(abs(dm2[0]-dm2ref[0]).sum(), 0, 9)
        self.assertAlmostEqual(abs(dm2[1]-dm2ref[1]).sum(), 0, 9)
        self.assertAlmostEqual(abs(dm2[2]-dm2ref[2]).sum(), 0, 9)

        ci1_coeff = select_ci._as_SCIvector(numpy.random.random((len(strsa),len(strsb))), ci_strs)
        ci1 = select_ci.to_fci(ci1_coeff, norb, (nelec,nelec))
        dm1ref, dm2ref = direct_spin1.trans_rdm12s(ci1, ci0, norb, (nelec,nelec))
        dm1 = select_ci.trans_rdm1s(ci1_coeff, ci_coeff, norb, (nelec,nelec))
        self.assertAlmostEqual(abs(dm1[0]-dm1ref[0]).sum(), 0, 9)
        self.assertAlmostEqual(abs(dm1[1]-dm1ref[1]).sum(), 0, 9)
Ejemplo n.º 2
0
    def test_rdm(self):
        norb, nelec = 10, 4
        strs = cistring.gen_strings4orblist(range(norb), nelec)
        numpy.random.seed(11)
        mask = numpy.random.random(len(strs)) > .6
        strsa = strs[mask]
        mask = numpy.random.random(len(strs)) > .7
        strsb = strs[mask]
        ci_strs = (strsa, strsb)
        ci_coeff = select_ci._as_SCIvector(
            numpy.random.random((len(strsa), len(strsb))), ci_strs)
        ci0 = select_ci.to_fci(ci_coeff, norb, (nelec, nelec))
        dm1ref, dm2ref = direct_spin1.make_rdm12s(ci0, norb, (nelec, nelec))
        dm1 = select_ci.make_rdm1s(ci_coeff, norb, (nelec, nelec))
        self.assertAlmostEqual(abs(dm1[0] - dm1ref[0]).sum(), 0, 9)
        self.assertAlmostEqual(abs(dm1[1] - dm1ref[1]).sum(), 0, 9)
        dm2 = select_ci.make_rdm2s(ci_coeff, norb, (nelec, nelec))
        self.assertAlmostEqual(abs(dm2[0] - dm2ref[0]).sum(), 0, 9)
        self.assertAlmostEqual(abs(dm2[1] - dm2ref[1]).sum(), 0, 9)
        self.assertAlmostEqual(abs(dm2[2] - dm2ref[2]).sum(), 0, 9)

        ci1_coeff = select_ci._as_SCIvector(
            numpy.random.random((len(strsa), len(strsb))), ci_strs)
        ci1 = select_ci.to_fci(ci1_coeff, norb, (nelec, nelec))
        dm1ref, dm2ref = direct_spin1.trans_rdm12s(ci1, ci0, norb,
                                                   (nelec, nelec))
        dm1 = select_ci.trans_rdm1s(ci1_coeff, ci_coeff, norb, (nelec, nelec))
        self.assertAlmostEqual(abs(dm1[0] - dm1ref[0]).sum(), 0, 9)
        self.assertAlmostEqual(abs(dm1[1] - dm1ref[1]).sum(), 0, 9)
Ejemplo n.º 3
0
 def test_contract_2e_symm(self):
     norb, nelec = 7, (4, 4)
     strs = cistring.gen_strings4orblist(range(norb), nelec[0])
     numpy.random.seed(11)
     mask = numpy.random.random(len(strs)) > .3
     strsa = strs[mask]
     mask = numpy.random.random(len(strs)) > .2
     strsb = strs[mask]
     ci_strs = (strsa, strsb)
     civec_strs = select_ci._as_SCIvector(
         numpy.random.random((len(strsa), len(strsb))), ci_strs)
     orbsym = (numpy.random.random(norb) * 4).astype(int)
     nn = norb * (norb + 1) // 2
     eri = ao2mo.restore(1,
                         (numpy.random.random(nn * (nn + 1) // 2) - .2)**3,
                         norb)
     oosym = orbsym[:, None] ^ orbsym
     oosym = oosym.reshape(-1, 1) ^ oosym.ravel()
     eri[oosym.reshape([norb] * 4) != 0] = 0
     ci0 = fci.select_ci.to_fci(civec_strs, norb, nelec)
     ci0 = fci.addons.symmetrize_wfn(ci0, norb, nelec, orbsym)
     civec_strs = fci.select_ci.from_fci(ci0, civec_strs._strs, norb, nelec)
     e1 = numpy.dot(
         civec_strs.ravel(),
         select_ci_symm.contract_2e(eri,
                                    civec_strs,
                                    norb,
                                    nelec,
                                    orbsym=orbsym).ravel())
     e2 = numpy.dot(
         ci0.ravel(),
         direct_spin1_symm.contract_2e(eri, ci0, norb, nelec,
                                       orbsym=orbsym).ravel())
     self.assertAlmostEqual(e1, e2, 9)
Ejemplo n.º 4
0
 def get_init_guess(self, ci_strs, norb, nelec, nroots, hdiag):
     '''Initial guess is the single Slater determinant
     '''
     wfnsym = direct_spin1_symm._id_wfnsym(self, norb, nelec, self.wfnsym)
     ci0 = direct_spin1_symm._get_init_guess(ci_strs[0], ci_strs[1],
                                             nroots, hdiag, self.orbsym, wfnsym)
     return [select_ci._as_SCIvector(x, ci_strs) for x in ci0]
Ejemplo n.º 5
0
 def get_init_guess(self, ci_strs, norb, nelec, nroots, hdiag):
     '''Initial guess is the single Slater determinant
     '''
     wfnsym = direct_spin1_symm._id_wfnsym(self, norb, nelec, self.wfnsym)
     ci0 = direct_spin1_symm._get_init_guess(ci_strs[0], ci_strs[1],
                                             nroots, hdiag, self.orbsym, wfnsym)
     return [select_ci._as_SCIvector(x, ci_strs) for x in ci0]
Ejemplo n.º 6
0
    def contract_2e(self, eri, civec_strs, norb, nelec, link_index=None, **kwargs):
# The argument civec_strs is a CI vector in function FCISolver.contract_2e.
# Save and patch self._strs to make this contract_2e function compatible to
# FCISolver.contract_2e.
        if hasattr(civec_strs, '_strs'):
            self._strs = civec_strs._strs
        else:
            assert(civec_strs.size == len(self._strs[0])*len(self._strs[1]))
            civec_strs = select_ci._as_SCIvector(civec_strs, self._strs)
        return contract_2e(eri, civec_strs, norb, nelec, link_index)
Ejemplo n.º 7
0
 def contract_2e(self, eri, civec_strs, norb, nelec, link_index=None,
                 orbsym=None, **kwargs):
     if orbsym is None:
         orbsym = self.orbsym
     if hasattr(civec_strs, '_strs'):
         self._strs = civec_strs._strs
     else:
         assert(civec_strs.size == len(self._strs[0])*len(self._strs[1]))
         civec_strs = select_ci._as_SCIvector(civec_strs, self._strs)
     return contract_2e(eri, civec_strs, norb, nelec, link_index, orbsym)
Ejemplo n.º 8
0
 def contract_2e(self, eri, civec_strs, norb, nelec, link_index=None,
                 orbsym=None, **kwargs):
     if orbsym is None:
         orbsym = self.orbsym
     if hasattr(civec_strs, '_strs'):
         self._strs = civec_strs._strs
     else:
         assert(civec_strs.size == len(self._strs[0])*len(self._strs[1]))
         civec_strs = select_ci._as_SCIvector(civec_strs, self._strs)
     return contract_2e(eri, civec_strs, norb, nelec, link_index, orbsym)
Ejemplo n.º 9
0
def contract_2e(eri, civec_strs, norb, nelec, link_index=None, orbsym=None):
    ci_coeff, nelec, ci_strs = select_ci._unpack(civec_strs, nelec)
    if link_index is None:
        link_index = select_ci._all_linkstr_index(ci_strs, norb, nelec)
    cd_indexa, dd_indexa, cd_indexb, dd_indexb = link_index
    na, nlinka = nb, nlinkb = cd_indexa.shape[:2]
    ma, mlinka = mb, mlinkb = dd_indexa.shape[:2]

    eri = ao2mo.restore(1, eri, norb)
    eri1 = eri.transpose(0, 2, 1, 3) - eri.transpose(0, 2, 3, 1)
    idx, idy = numpy.tril_indices(norb, -1)
    idx = idx * norb + idy
    eri1 = lib.take_2d(eri1.reshape(norb**2, -1), idx, idx) * 2
    lib.transpose_sum(eri1, inplace=True)
    eri1 *= .5
    eri1, dd_indexa, dimirrep = select_ci_symm.reorder4irrep(
        eri1, norb, dd_indexa, orbsym, -1)
    fcivec = ci_coeff.reshape(na, nb)
    ci1 = numpy.zeros_like(fcivec)
    # (aa|aa)
    if nelec[0] > 1:
        libfci.SCIcontract_2e_aaaa_symm(
            eri1.ctypes.data_as(ctypes.c_void_p),
            fcivec.ctypes.data_as(ctypes.c_void_p),
            ci1.ctypes.data_as(ctypes.c_void_p), ctypes.c_int(norb),
            ctypes.c_int(na), ctypes.c_int(nb), ctypes.c_int(ma),
            ctypes.c_int(mlinka), dd_indexa.ctypes.data_as(ctypes.c_void_p),
            dimirrep.ctypes.data_as(ctypes.c_void_p),
            ctypes.c_int(len(dimirrep)))

    h_ps = numpy.einsum('pqqs->ps', eri) * (.5 / nelec[0])
    eri1 = eri.copy()
    for k in range(norb):
        eri1[:, :, k, k] += h_ps
        eri1[k, k, :, :] += h_ps
    eri1 = ao2mo.restore(4, eri1, norb)
    lib.transpose_sum(eri1, inplace=True)
    eri1 *= .5
    eri1, cd_indexa, dimirrep = direct_spin1_symm.reorder4irrep(
        eri1, norb, cd_indexa, orbsym)
    # (bb|aa)
    libfci.SCIcontract_2e_bbaa_symm(eri1.ctypes.data_as(ctypes.c_void_p),
                                    fcivec.ctypes.data_as(ctypes.c_void_p),
                                    ci1.ctypes.data_as(ctypes.c_void_p),
                                    ctypes.c_int(norb), ctypes.c_int(na),
                                    ctypes.c_int(nb), ctypes.c_int(nlinka),
                                    ctypes.c_int(nlinkb),
                                    cd_indexa.ctypes.data_as(ctypes.c_void_p),
                                    cd_indexa.ctypes.data_as(ctypes.c_void_p),
                                    dimirrep.ctypes.data_as(ctypes.c_void_p),
                                    ctypes.c_int(len(dimirrep)))

    lib.transpose_sum(ci1, inplace=True)
    return select_ci._as_SCIvector(ci1.reshape(ci_coeff.shape), ci_strs)
Ejemplo n.º 10
0
def contract_2e(eri, civec_strs, norb, nelec, link_index=None, orbsym=None):
    ci_coeff, nelec, ci_strs = select_ci._unpack(civec_strs, nelec)
    if link_index is None:
        link_index = select_ci._all_linkstr_index(ci_strs, norb, nelec)
    cd_indexa, dd_indexa, cd_indexb, dd_indexb = link_index
    na, nlinka = nb, nlinkb = cd_indexa.shape[:2]
    ma, mlinka = mb, mlinkb = dd_indexa.shape[:2]

    eri = ao2mo.restore(1, eri, norb)
    eri1 = eri.transpose(0,2,1,3) - eri.transpose(0,2,3,1)
    idx,idy = numpy.tril_indices(norb, -1)
    idx = idx * norb + idy
    eri1 = lib.take_2d(eri1.reshape(norb**2,-1), idx, idx) * 2
    lib.transpose_sum(eri1, inplace=True)
    eri1 *= .5
    eri1, dd_indexa, dimirrep = select_ci_symm.reorder4irrep_minors(eri1, norb, dd_indexa, orbsym)
    fcivec = ci_coeff.reshape(na,nb)
    ci1 = numpy.zeros_like(fcivec)
    # (aa|aa)
    if nelec[0] > 1:
        libfci.SCIcontract_2e_aaaa_symm(eri1.ctypes.data_as(ctypes.c_void_p),
                                        fcivec.ctypes.data_as(ctypes.c_void_p),
                                        ci1.ctypes.data_as(ctypes.c_void_p),
                                        ctypes.c_int(norb),
                                        ctypes.c_int(na), ctypes.c_int(nb),
                                        ctypes.c_int(ma), ctypes.c_int(mlinka),
                                        dd_indexa.ctypes.data_as(ctypes.c_void_p),
                                        dimirrep.ctypes.data_as(ctypes.c_void_p),
                                        ctypes.c_int(len(dimirrep)))

    h_ps = numpy.einsum('pqqs->ps', eri) * (.5/nelec[0])
    eri1 = eri.copy()
    for k in range(norb):
        eri1[:,:,k,k] += h_ps
        eri1[k,k,:,:] += h_ps
    eri1 = ao2mo.restore(4, eri1, norb)
    lib.transpose_sum(eri1, inplace=True)
    eri1 *= .5
    eri1, cd_indexa, dimirrep = direct_spin1_symm.reorder4irrep(eri1, norb, cd_indexa, orbsym)
    # (bb|aa)
    libfci.SCIcontract_2e_bbaa_symm(eri1.ctypes.data_as(ctypes.c_void_p),
                                    fcivec.ctypes.data_as(ctypes.c_void_p),
                                    ci1.ctypes.data_as(ctypes.c_void_p),
                                    ctypes.c_int(norb),
                                    ctypes.c_int(na), ctypes.c_int(nb),
                                    ctypes.c_int(nlinka), ctypes.c_int(nlinkb),
                                    cd_indexa.ctypes.data_as(ctypes.c_void_p),
                                    cd_indexa.ctypes.data_as(ctypes.c_void_p),
                                    dimirrep.ctypes.data_as(ctypes.c_void_p),
                                    ctypes.c_int(len(dimirrep)))

    lib.transpose_sum(ci1, inplace=True)
    return select_ci._as_SCIvector(ci1.reshape(ci_coeff.shape), ci_strs)
Ejemplo n.º 11
0
 def test_spin_square(self):
     norb, nelec = 10, 4
     strs = cistring.gen_strings4orblist(range(norb), nelec)
     numpy.random.seed(11)
     mask = numpy.random.random(len(strs)) > .6
     strsa = strs[mask]
     mask = numpy.random.random(len(strs)) > .7
     strsb = strs[mask]
     ci_strs = (strsa, strsb)
     ci_coeff = select_ci._as_SCIvector(numpy.random.random((len(strsa),len(strsb))), ci_strs)
     ci0 = select_ci.to_fci(ci_coeff, norb, (nelec,nelec))
     ss0 = select_ci.spin_square(ci_coeff, norb, (nelec,nelec))
     ss1 = spin_op.spin_square0(ci0, norb, (nelec,nelec))
     self.assertAlmostEqual(ss0[0], ss1[0], 9)
Ejemplo n.º 12
0
 def test_spin_square(self):
     norb, nelec = 10, 4
     strs = cistring.gen_strings4orblist(range(norb), nelec)
     numpy.random.seed(11)
     mask = numpy.random.random(len(strs)) > .6
     strsa = strs[mask]
     mask = numpy.random.random(len(strs)) > .7
     strsb = strs[mask]
     ci_strs = (strsa, strsb)
     ci_coeff = select_ci._as_SCIvector(
         numpy.random.random((len(strsa), len(strsb))), ci_strs)
     ci0 = select_ci.to_fci(ci_coeff, norb, (nelec, nelec))
     ss0 = select_ci.spin_square(ci_coeff, norb, (nelec, nelec))
     ss1 = spin_op.spin_square0(ci0, norb, (nelec, nelec))
     self.assertAlmostEqual(ss0[0], ss1[0], 9)
Ejemplo n.º 13
0
 def contract_2e(self,
                 eri,
                 civec_strs,
                 norb,
                 nelec,
                 link_index=None,
                 **kwargs):
     # The argument civec_strs is a CI vector in function FCISolver.contract_2e.
     # Save and patch self._strs to make this contract_2e function compatible to
     # FCISolver.contract_2e.
     if hasattr(civec_strs, '_strs'):
         self._strs = civec_strs._strs
     else:
         assert (civec_strs.size == len(self._strs[0]) * len(self._strs[1]))
         civec_strs = select_ci._as_SCIvector(civec_strs, self._strs)
     return contract_2e(eri, civec_strs, norb, nelec, link_index)
Ejemplo n.º 14
0
 def test_contract1(self):
     myci = select_ci.SelectCI()
     nelec = (4,3)
     strsa = cistring.gen_strings4orblist(range(norb), nelec[0])
     strsb = cistring.gen_strings4orblist(range(norb), nelec[1])
     ci0 = select_ci._as_SCIvector(numpy.random.random((len(strsa),len(strsb))), (strsa,strsb))
     h2 = ao2mo.restore(1, eri, norb)
     c1 = myci.contract_2e(h2, ci0, norb, nelec)
     c2 = direct_spin1.contract_2e(h2, ci0, norb, nelec)
     self.assertAlmostEqual(abs(c1-c2).sum(), 0, 9)
     dm1_1 = myci.make_rdm1(c1, norb, nelec)
     dm1_2 = direct_spin1.make_rdm1(c2, norb, nelec)
     self.assertAlmostEqual(abs(dm1_1 - dm1_2).sum(), 0, 9)
     dm2_1 = myci.make_rdm2(c1, norb, nelec)
     dm2_2 = direct_spin1.make_rdm12(c2, norb, nelec)[1]
     self.assertAlmostEqual(abs(dm2_1 - dm2_2).sum(), 0, 9)
Ejemplo n.º 15
0
 def test_contract_2e_symm(self):
     norb, nelec = 7, (4,4)
     strs = cistring.gen_strings4orblist(range(norb), nelec[0])
     numpy.random.seed(11)
     mask = numpy.random.random(len(strs)) > .3
     strsa = strs[mask]
     mask = numpy.random.random(len(strs)) > .2
     strsb = strs[mask]
     ci_strs = (strsa, strsb)
     civec_strs = select_ci._as_SCIvector(numpy.random.random((len(strsa),len(strsb))), ci_strs)
     orbsym = (numpy.random.random(norb) * 4).astype(int)
     nn = norb*(norb+1)//2
     eri = (numpy.random.random(nn*(nn+1)//2)-.2)**3
     ci0 = select_ci.to_fci(civec_strs, norb, nelec)
     e1 = numpy.dot(civec_strs.ravel(), select_ci_symm.contract_2e(eri, civec_strs, norb, nelec, orbsym=orbsym).ravel())
     e2 = numpy.dot(ci0.ravel(), direct_spin1_symm.contract_2e(eri, ci0, norb, nelec, orbsym=orbsym).ravel())
     self.assertAlmostEqual(e1, e2, 9)
Ejemplo n.º 16
0
 def test_contract1(self):
     myci = select_ci.SCI()
     nelec = (4, 3)
     strsa = cistring.gen_strings4orblist(range(norb), nelec[0])
     strsb = cistring.gen_strings4orblist(range(norb), nelec[1])
     ci0 = select_ci._as_SCIvector(
         numpy.random.random((len(strsa), len(strsb))), (strsa, strsb))
     h2 = ao2mo.restore(1, eri, norb)
     c1 = myci.contract_2e(h2, ci0, norb, nelec)
     c2 = direct_spin1.contract_2e(h2, ci0, norb, nelec)
     self.assertAlmostEqual(abs(c1 - c2).sum(), 0, 9)
     dm1_1 = myci.make_rdm1(c1, norb, nelec)
     dm1_2 = direct_spin1.make_rdm1(c2, norb, nelec)
     self.assertAlmostEqual(abs(dm1_1 - dm1_2).sum(), 0, 9)
     dm2_1 = myci.make_rdm2(c1, norb, nelec)
     dm2_2 = direct_spin1.make_rdm12(c2, norb, nelec)[1]
     self.assertAlmostEqual(abs(dm2_1 - dm2_2).sum(), 0, 9)
Ejemplo n.º 17
0
def enlarge_space(myci, civec_strs, eri, norb, nelec):
    if isinstance(civec_strs, (tuple, list)):
        nelec, (strsa, strsb) = select_ci._unpack(civec_strs[0], nelec)[1:]
        ci_coeff = lib.asarray(civec_strs)
    else:
        ci_coeff, nelec, (strsa, strsb) = select_ci._unpack(civec_strs, nelec)
    na = nb = len(strsa)
    ci0 = ci_coeff.reshape(-1, na, nb)
    abs_ci = abs(ci0).max(axis=0)

    eri = ao2mo.restore(1, eri, norb)
    eri_pq_max = abs(eri.reshape(norb**2, -1)).max(axis=1).reshape(norb, norb)

    civec_a_max = abs_ci.max(axis=1)
    ci_aidx = numpy.where(civec_a_max > myci.ci_coeff_cutoff)[0]
    civec_a_max = civec_a_max[ci_aidx]
    strsa = strsa[ci_aidx]
    strsa_add = select_ci.select_strs(myci, eri, eri_pq_max, civec_a_max,
                                      strsa, norb, nelec[0])
    strsa = numpy.append(strsa, strsa_add)
    aidx = numpy.argsort(strsa)
    strsa = strsa[aidx]
    aidx = numpy.where(aidx < len(ci_aidx))[0]

    ci_bidx = ci_aidx
    strsb = strsa
    bidx = aidx

    ma = mb = len(strsa)
    cs = []
    for i in range(ci0.shape[0]):
        ci1 = numpy.zeros((ma, mb))
        tmp = lib.take_2d(ci0[i], ci_aidx, ci_bidx)
        lib.takebak_2d(ci1, tmp, aidx, bidx)
        cs.append(select_ci._as_SCIvector(ci1, (strsa, strsb)))

    if ci_coeff[0].ndim == 0 or ci_coeff[0].shape[-1] != nb:
        cs = [c.ravel() for c in cs]

    if (isinstance(ci_coeff, numpy.ndarray) and ci_coeff.shape[0] == na
            or ci_coeff.shape[0] == na * nb):
        cs = cs[0]
    return cs
Ejemplo n.º 18
0
def enlarge_space(myci, civec_strs, eri, norb, nelec):
    if isinstance(civec_strs, (tuple, list)):
        nelec, (strsa, strsb) = select_ci._unpack(civec_strs[0], nelec)[1:]
        ci_coeff = lib.asarray(civec_strs)
    else:
        ci_coeff, nelec, (strsa, strsb) = select_ci._unpack(civec_strs, nelec)
    na = nb = len(strsa)
    ci0 = ci_coeff.reshape(-1,na,nb)
    abs_ci = abs(ci0).max(axis=0)

    eri = ao2mo.restore(1, eri, norb)
    eri_pq_max = abs(eri.reshape(norb**2,-1)).max(axis=1).reshape(norb,norb)

    civec_a_max = abs_ci.max(axis=1)
    ci_aidx = numpy.where(civec_a_max > myci.ci_coeff_cutoff)[0]
    civec_a_max = civec_a_max[ci_aidx]
    strsa = strsa[ci_aidx]
    strsa_add = select_ci.select_strs(myci, eri, eri_pq_max, civec_a_max, strsa, norb, nelec[0])
    strsa = numpy.append(strsa, strsa_add)
    aidx = numpy.argsort(strsa)
    strsa = strsa[aidx]
    aidx = numpy.where(aidx < len(ci_aidx))[0]

    ci_bidx = ci_aidx
    strsb = strsa
    bidx = aidx

    ma = mb = len(strsa)
    cs = []
    for i in range(ci0.shape[0]):
        ci1 = numpy.zeros((ma,mb))
        tmp = lib.take_2d(ci0[i], ci_aidx, ci_bidx)
        lib.takebak_2d(ci1, tmp, aidx, bidx)
        cs.append(select_ci._as_SCIvector(ci1, (strsa,strsb)))

    if ci_coeff[0].ndim == 0 or ci_coeff[0].shape[-1] != nb:
        cs = [c.ravel() for c in cs]

    if (isinstance(ci_coeff, numpy.ndarray) and
        ci_coeff.shape[0] == na or ci_coeff.shape[0] == na*nb):
        cs = cs[0]
    return cs
Ejemplo n.º 19
0
    from functools import reduce
    from pyscf import gto
    from pyscf import scf
    from pyscf import ao2mo
    from pyscf import symm
    from pyscf.fci import cistring

    norb, nelec = 7, (4,4)
    strs = cistring.gen_strings4orblist(range(norb), nelec[0])
    numpy.random.seed(11)
    mask = numpy.random.random(len(strs)) > .3
    strsa = strs[mask]
    mask = numpy.random.random(len(strs)) > .2
    strsb = strs[mask]
    ci_strs = (strsa, strsb)
    civec_strs = select_ci._as_SCIvector(numpy.random.random((len(strsa),len(strsb))), ci_strs)
    orbsym = (numpy.random.random(norb) * 4).astype(int)
    nn = norb*(norb+1)//2
    eri = (numpy.random.random(nn*(nn+1)//2)-.2)**3

    ci0 = select_ci.to_fci(civec_strs, norb, nelec)
    e1 = numpy.dot(civec_strs.ravel(), contract_2e(eri, civec_strs, norb, nelec, orbsym=orbsym).ravel())
    e2 = numpy.dot(ci0.ravel(), direct_spin1_symm.contract_2e(eri, ci0, norb, nelec, orbsym=orbsym).ravel())
    print(e1-e2)

    mol = gto.Mole()
    mol.verbose = 0
    mol.output = None
    mol.atom = [
        ['O', ( 0., 0.    , 0.   )],
        ['H', ( 0., -0.757, 0.587)],
Ejemplo n.º 20
0
def contract_2e(eri, civec_strs, norb, nelec, link_index=None, orbsym=None):
    ci_coeff, nelec, ci_strs = select_ci._unpack(civec_strs, nelec)
    if link_index is None:
        link_index = select_ci._all_linkstr_index(ci_strs, norb, nelec)
    cd_indexa, dd_indexa, cd_indexb, dd_indexb = link_index
    na, nlinka = cd_indexa.shape[:2]
    nb, nlinkb = cd_indexb.shape[:2]
    ma, mlinka = dd_indexa.shape[:2]
    mb, mlinkb = dd_indexb.shape[:2]

    eri = ao2mo.restore(1, eri, norb)
    eri1 = eri.transpose(0,2,1,3) - eri.transpose(0,2,3,1)
    idx,idy = numpy.tril_indices(norb, -1)
    idx = idx * norb + idy
    eri1 = lib.take_2d(eri1.reshape(norb**2,-1), idx, idx) * 2
    eri1, dd_indexa, dimirrep = reorder4irrep(eri1, norb, dd_indexa, orbsym, -1)
    dd_indexb = reorder4irrep(eri1, norb, dd_indexb, orbsym, -1)[1]
    fcivec = ci_coeff.reshape(na,nb)
    # (bb|bb)
    if nelec[1] > 1:
        fcivecT = lib.transpose(fcivec)
        ci1T = numpy.zeros((nb,na))
        libfci.SCIcontract_2e_aaaa_symm(eri1.ctypes.data_as(ctypes.c_void_p),
                                        fcivecT.ctypes.data_as(ctypes.c_void_p),
                                        ci1T.ctypes.data_as(ctypes.c_void_p),
                                        ctypes.c_int(norb),
                                        ctypes.c_int(nb), ctypes.c_int(na),
                                        ctypes.c_int(mb), ctypes.c_int(mlinkb),
                                        dd_indexb.ctypes.data_as(ctypes.c_void_p),
                                        dimirrep.ctypes.data_as(ctypes.c_void_p),
                                        ctypes.c_int(len(dimirrep)))
        ci1 = lib.transpose(ci1T, out=fcivecT)
    else:
        ci1 = numpy.zeros_like(fcivec)
    # (aa|aa)
    if nelec[0] > 1:
        libfci.SCIcontract_2e_aaaa_symm(eri1.ctypes.data_as(ctypes.c_void_p),
                                        fcivec.ctypes.data_as(ctypes.c_void_p),
                                        ci1.ctypes.data_as(ctypes.c_void_p),
                                        ctypes.c_int(norb),
                                        ctypes.c_int(na), ctypes.c_int(nb),
                                        ctypes.c_int(ma), ctypes.c_int(mlinka),
                                        dd_indexa.ctypes.data_as(ctypes.c_void_p),
                                        dimirrep.ctypes.data_as(ctypes.c_void_p),
                                        ctypes.c_int(len(dimirrep)))

    h_ps = numpy.einsum('pqqs->ps', eri)
    eri1 = eri * 2
    for k in range(norb):
        eri1[:,:,k,k] += h_ps/nelec[0]
        eri1[k,k,:,:] += h_ps/nelec[1]
    eri1 = ao2mo.restore(4, eri1, norb)
    eri1, cd_indexa, dimirrep = reorder4irrep(eri1, norb, cd_indexa, orbsym)
    cd_indexb = reorder4irrep(eri1, norb, cd_indexb, orbsym)[1]
    # (bb|aa)
    libfci.SCIcontract_2e_bbaa_symm(eri1.ctypes.data_as(ctypes.c_void_p),
                                    fcivec.ctypes.data_as(ctypes.c_void_p),
                                    ci1.ctypes.data_as(ctypes.c_void_p),
                                    ctypes.c_int(norb),
                                    ctypes.c_int(na), ctypes.c_int(nb),
                                    ctypes.c_int(nlinka), ctypes.c_int(nlinkb),
                                    cd_indexa.ctypes.data_as(ctypes.c_void_p),
                                    cd_indexb.ctypes.data_as(ctypes.c_void_p),
                                    dimirrep.ctypes.data_as(ctypes.c_void_p),
                                    ctypes.c_int(len(dimirrep)))

    return select_ci._as_SCIvector(ci1.reshape(ci_coeff.shape), ci_strs)
Ejemplo n.º 21
0
    from functools import reduce
    from pyscf import gto
    from pyscf import scf
    from pyscf import ao2mo
    from pyscf import symm
    from pyscf.fci import cistring

    norb, nelec = 7, (4,4)
    strs = cistring.gen_strings4orblist(range(norb), nelec[0])
    numpy.random.seed(11)
    mask = numpy.random.random(len(strs)) > .3
    strsa = strs[mask]
    mask = numpy.random.random(len(strs)) > .2
    strsb = strs[mask]
    ci_strs = (strsa, strsb)
    civec_strs = select_ci._as_SCIvector(numpy.random.random((len(strsa),len(strsb))), ci_strs)
    orbsym = (numpy.random.random(norb) * 4).astype(int)
    nn = norb*(norb+1)//2
    eri = (numpy.random.random(nn*(nn+1)//2)-.2)**3

    ci0 = select_ci.to_fci(civec_strs, norb, nelec)
    ci0 = addons.symmetrize_wfn(ci0, norb, nelec, orbsym)
    civec_strs = select_ci.from_fci(ci0, civec_strs._strs, norb, nelec)
    e1 = numpy.dot(civec_strs.ravel(), contract_2e(eri, civec_strs, norb, nelec, orbsym=orbsym).ravel())
    e2 = numpy.dot(ci0.ravel(), direct_spin1_symm.contract_2e(eri, ci0, norb, nelec, orbsym=orbsym).ravel())
    print(e1-e2)

    mol = gto.Mole()
    mol.verbose = 0
    mol.output = None
    mol.atom = [
Ejemplo n.º 22
0
from pyscf import fci
from pyscf import lib
from pyscf.fci import cistring
from pyscf.fci import direct_spin1
from pyscf.fci import direct_spin1_symm
from pyscf.fci import select_ci
from pyscf.fci import select_ci_symm
from pyscf.fci import spin_op

norb = 6
nelec = 6
na = cistring.num_strings(norb, nelec//2)
ci_strs = [[0b111, 0b1011, 0b10101], [0b111, 0b1011, 0b1101]]
numpy.random.seed(12)
ci_coeff = (numpy.random.random((len(ci_strs[0]),len(ci_strs[1])))-.2)**3
civec_strs = select_ci._as_SCIvector(ci_coeff, ci_strs)
nn = norb*(norb+1)//2
eri = (numpy.random.random(nn*(nn+1)//2)-.2)**3
h1 = numpy.random.random((norb,norb))
h1 = h1 + h1.T

def finger(a):
    return numpy.dot(a.ravel(), numpy.cos(numpy.arange(a.size)))

class KnowValues(unittest.TestCase):
    def test_select_strs(self):
        myci = select_ci.SelectCI()
        myci.select_cutoff = 1e-3
        norb, nelec = 10, 4
        strs = cistring.gen_strings4orblist(range(norb), nelec)
        numpy.random.seed(11)
Ejemplo n.º 23
0
from pyscf import fci
from pyscf import lib
from pyscf.fci import cistring
from pyscf.fci import direct_spin1
from pyscf.fci import direct_spin1_symm
from pyscf.fci import select_ci
from pyscf.fci import select_ci_symm
from pyscf.fci import spin_op

norb = 6
nelec = 6
na = cistring.num_strings(norb, nelec // 2)
ci_strs = [[0b111, 0b1011, 0b10101], [0b111, 0b1011, 0b1101]]
numpy.random.seed(12)
ci_coeff = (numpy.random.random((len(ci_strs[0]), len(ci_strs[1]))) - .2)**3
civec_strs = select_ci._as_SCIvector(ci_coeff, ci_strs)
nn = norb * (norb + 1) // 2
eri = (numpy.random.random(nn * (nn + 1) // 2) - .2)**3
h1 = numpy.random.random((norb, norb))
h1 = h1 + h1.T


def finger(a):
    return numpy.dot(a.ravel(), numpy.cos(numpy.arange(a.size)))


class KnowValues(unittest.TestCase):
    def test_select_strs(self):
        myci = select_ci.SCI()
        myci.select_cutoff = 1e-3
        norb, nelec = 10, 4