Beispiel #1
0
def pspace(h1e, eri, norb, nelec, hdiag, np=400):
    if isinstance(nelec, (int, numpy.number)):
        neleca = nelec // 2
    else:
        neleca, nelecb = nelec
        assert (neleca == nelecb)
    h1e = numpy.ascontiguousarray(h1e)
    eri = pyscf.ao2mo.restore(1, eri, norb)
    na = cistring.num_strings(norb, neleca)
    addr = numpy.argsort(hdiag)[:np]
    # symmetrize addra/addrb
    addra = addr // na
    addrb = addr % na
    stra = numpy.array([cistring.addr2str(norb, neleca, ia) for ia in addra],
                       dtype=numpy.long)
    strb = numpy.array([cistring.addr2str(norb, neleca, ib) for ib in addrb],
                       dtype=numpy.long)
    np = len(addr)
    h0 = numpy.zeros((np, np))
    libfci.FCIpspace_h0tril(h0.ctypes.data_as(ctypes.c_void_p),
                            h1e.ctypes.data_as(ctypes.c_void_p),
                            eri.ctypes.data_as(ctypes.c_void_p),
                            stra.ctypes.data_as(ctypes.c_void_p),
                            strb.ctypes.data_as(ctypes.c_void_p),
                            ctypes.c_int(norb), ctypes.c_int(np))

    for i in range(np):
        h0[i, i] = hdiag[addr[i]]
    h0 = pyscf.lib.hermi_triu(h0)
    return addr, h0
Beispiel #2
0
def pspace(h1e, eri, norb, nelec, hdiag, np=400):
    neleca, nelecb = direct_spin1._unpack_nelec(nelec)
    h1e_a = numpy.ascontiguousarray(h1e[0])
    h1e_b = numpy.ascontiguousarray(h1e[1])
    g2e_aa = ao2mo.restore(1, eri[0], norb)
    g2e_ab = ao2mo.restore(1, eri[1], norb)
    g2e_bb = ao2mo.restore(1, eri[2], norb)
    link_indexa = cistring.gen_linkstr_index_trilidx(range(norb), neleca)
    link_indexb = cistring.gen_linkstr_index_trilidx(range(norb), nelecb)
    nb = link_indexb.shape[0]
    addr = numpy.argsort(hdiag)[:np]
    addra = addr // nb
    addrb = addr % nb
    stra = numpy.array([cistring.addr2str(norb,neleca,ia) for ia in addra],
                       dtype=numpy.long)
    strb = numpy.array([cistring.addr2str(norb,nelecb,ib) for ib in addrb],
                       dtype=numpy.long)
    np = len(addr)
    h0 = numpy.zeros((np,np))
    libfci.FCIpspace_h0tril_uhf(h0.ctypes.data_as(ctypes.c_void_p),
                                h1e_a.ctypes.data_as(ctypes.c_void_p),
                                h1e_b.ctypes.data_as(ctypes.c_void_p),
                                g2e_aa.ctypes.data_as(ctypes.c_void_p),
                                g2e_ab.ctypes.data_as(ctypes.c_void_p),
                                g2e_bb.ctypes.data_as(ctypes.c_void_p),
                                stra.ctypes.data_as(ctypes.c_void_p),
                                strb.ctypes.data_as(ctypes.c_void_p),
                                ctypes.c_int(norb), ctypes.c_int(np))

    for i in range(np):
        h0[i,i] = hdiag[addr[i]]
    h0 = lib.hermi_triu(h0)
    return addr, h0
Beispiel #3
0
def pspace(h1e, eri, norb, nelec, hdiag=None, np=400):
    '''pspace Hamiltonian to improve Davidson preconditioner. See, CPL, 169, 463
    '''
    neleca, nelecb = _unpack_nelec(nelec)
    h1e = numpy.ascontiguousarray(h1e)
    eri = ao2mo.restore(1, eri, norb)
    nb = cistring.num_strings(norb, nelecb)
    if hdiag is None:
        hdiag = make_hdiag(h1e, eri, norb, nelec)
    if hdiag.size < np:
        addr = numpy.arange(hdiag.size)
    else:
        try:
            addr = numpy.argpartition(hdiag, np - 1)[:np]
        except AttributeError:
            addr = numpy.argsort(hdiag)[:np]
    addra, addrb = divmod(addr, nb)
    stra = numpy.array([cistring.addr2str(norb, neleca, ia) for ia in addra],
                       dtype=numpy.uint64)
    strb = numpy.array([cistring.addr2str(norb, nelecb, ib) for ib in addrb],
                       dtype=numpy.uint64)
    np = len(addr)
    h0 = numpy.zeros((np, np))
    libfci.FCIpspace_h0tril(h0.ctypes.data_as(ctypes.c_void_p),
                            h1e.ctypes.data_as(ctypes.c_void_p),
                            eri.ctypes.data_as(ctypes.c_void_p),
                            stra.ctypes.data_as(ctypes.c_void_p),
                            strb.ctypes.data_as(ctypes.c_void_p),
                            ctypes.c_int(norb), ctypes.c_int(np))

    for i in range(np):
        h0[i, i] = hdiag[addr[i]]
    h0 = lib.hermi_triu(h0)
    return addr, h0
Beispiel #4
0
def pspace(h1e, eri, norb, nelec, hdiag, np=400):
    if isinstance(nelec, (int, numpy.integer)):
        neleca = nelec//2
    else:
        neleca, nelecb = nelec
        assert(neleca == nelecb)
    h1e = numpy.ascontiguousarray(h1e)
    eri = pyscf.ao2mo.restore(1, eri, norb)
    na = cistring.num_strings(norb, neleca)
    addr = numpy.argsort(hdiag)[:np]
# symmetrize addra/addrb
    addra = addr // na
    addrb = addr % na
    stra = numpy.array([cistring.addr2str(norb,neleca,ia) for ia in addra],
                       dtype=numpy.long)
    strb = numpy.array([cistring.addr2str(norb,neleca,ib) for ib in addrb],
                       dtype=numpy.long)
    np = len(addr)
    h0 = numpy.zeros((np,np))
    libfci.FCIpspace_h0tril(h0.ctypes.data_as(ctypes.c_void_p),
                            h1e.ctypes.data_as(ctypes.c_void_p),
                            eri.ctypes.data_as(ctypes.c_void_p),
                            stra.ctypes.data_as(ctypes.c_void_p),
                            strb.ctypes.data_as(ctypes.c_void_p),
                            ctypes.c_int(norb), ctypes.c_int(np))

    for i in range(np):
        h0[i,i] = hdiag[addr[i]]
    h0 = pyscf.lib.hermi_triu_(h0)
    return addr, h0
Beispiel #5
0
def pspace(h1e, eri, norb, nelec, hdiag, np=400):
    '''pspace Hamiltonian to improve Davidson preconditioner. See, CPL, 169, 463
    '''
    neleca, nelecb = _unpack_nelec(nelec)
    h1e = numpy.ascontiguousarray(h1e)
    eri = ao2mo.restore(1, eri, norb)
    nb = cistring.num_strings(norb, nelecb)
    if hdiag.size < np:
        addr = numpy.arange(hdiag.size)
    else:
        try:
            addr = numpy.argpartition(hdiag, np-1)[:np]
        except AttributeError:
            addr = numpy.argsort(hdiag)[:np]
    addra, addrb = divmod(addr, nb)
    stra = numpy.array([cistring.addr2str(norb,neleca,ia) for ia in addra],
                       dtype=numpy.uint64)
    strb = numpy.array([cistring.addr2str(norb,nelecb,ib) for ib in addrb],
                       dtype=numpy.uint64)
    np = len(addr)
    h0 = numpy.zeros((np,np))
    libfci.FCIpspace_h0tril(h0.ctypes.data_as(ctypes.c_void_p),
                            h1e.ctypes.data_as(ctypes.c_void_p),
                            eri.ctypes.data_as(ctypes.c_void_p),
                            stra.ctypes.data_as(ctypes.c_void_p),
                            strb.ctypes.data_as(ctypes.c_void_p),
                            ctypes.c_int(norb), ctypes.c_int(np))

    for i in range(np):
        h0[i,i] = hdiag[addr[i]]
    h0 = lib.hermi_triu(h0)
    return addr, h0
Beispiel #6
0
def pspace(h1e, eri, norb, nelec, hdiag, np=400):
    '''pspace Hamiltonian to improve Davidson preconditioner. See, CPL, 169, 463
    '''
    if isinstance(nelec, (int, numpy.integer)):
        nelecb = nelec//2
        neleca = nelec - nelecb
    else:
        neleca, nelecb = nelec
    h1e = numpy.ascontiguousarray(h1e)
    eri = pyscf.ao2mo.restore(1, eri, norb)
    nb = cistring.num_strings(norb, nelecb)
    addr = numpy.argsort(hdiag)[:np]
    addra = addr // nb
    addrb = addr % nb
    stra = numpy.array([cistring.addr2str(norb,neleca,ia) for ia in addra],
                       dtype=numpy.uint64)
    strb = numpy.array([cistring.addr2str(norb,nelecb,ib) for ib in addrb],
                       dtype=numpy.uint64)
    np = len(addr)
    h0 = numpy.zeros((np,np))
    libfci.FCIpspace_h0tril(h0.ctypes.data_as(ctypes.c_void_p),
                            h1e.ctypes.data_as(ctypes.c_void_p),
                            eri.ctypes.data_as(ctypes.c_void_p),
                            stra.ctypes.data_as(ctypes.c_void_p),
                            strb.ctypes.data_as(ctypes.c_void_p),
                            ctypes.c_int(norb), ctypes.c_int(np))

    for i in range(np):
        h0[i,i] = hdiag[addr[i]]
    h0 = pyscf.lib.hermi_triu_(h0)
    return addr, h0
Beispiel #7
0
def large_ci(ci, norb, nelec, tol=.1):
    '''Search for the largest CI coefficients
    '''
    neleca, nelecb = _unpack(nelec)
    idx = numpy.argwhere(abs(ci) > tol)
    res = []
    for i, j in idx:
        res.append((ci[i, j], bin(cistring.addr2str(norb, neleca, i)),
                    bin(cistring.addr2str(norb, nelecb, j))))
    return res
Beispiel #8
0
    def test_str2addr(self):
        self.assertEqual(cistring.str2addr(6, 3, int('0b11001' ,2)), 7)
        self.assertEqual(cistring.str2addr(6, 3, int('0b11010' ,2)), 8)
        self.assertEqual(cistring.str2addr(7, 4, int('0b110011',2)), 9)
        self.assertEqual(cistring.str2addr(6, 3, cistring.addr2str(6, 3, 7)), 7)
        self.assertEqual(cistring.str2addr(6, 3, cistring.addr2str(6, 3, 8)), 8)
        self.assertEqual(cistring.str2addr(7, 4, cistring.addr2str(7, 4, 9)), 9)

        self.assertTrue(all(numpy.arange(20) ==
                            cistring.strs2addr(6, 3, cistring.addrs2str(6, 3, range(20)))))
Beispiel #9
0
def large_ci(ci, norb, nelec, tol=.1):
    '''Search for the largest CI coefficients
    '''
    neleca, nelecb = _unpack(nelec)
    idx = numpy.argwhere(abs(ci) > tol)
    res = []
    for i,j in idx:
        res.append((ci[i,j],
                    bin(cistring.addr2str(norb, neleca, i)),
                    bin(cistring.addr2str(norb, nelecb, j))))
    return res
Beispiel #10
0
def large_ci(ci, norb, nelec, tol=.1):
    if isinstance(nelec, (int, numpy.integer)):
        neleca = nelecb = nelec//2
    else:
        neleca, nelecb = nelec
    idx = numpy.argwhere(abs(ci) > tol)
    res = []
    for i,j in idx:
        res.append((ci[i,j],
                    bin(cistring.addr2str(norb, neleca, i)),
                    bin(cistring.addr2str(norb, nelecb, j))))
    return res
Beispiel #11
0
    def test_str2addr(self):
        self.assertEqual(cistring.str2addr(6, 3, int('0b11001', 2)), 7)
        self.assertEqual(cistring.str2addr(6, 3, int('0b11010', 2)), 8)
        self.assertEqual(cistring.str2addr(7, 4, int('0b110011', 2)), 9)
        self.assertEqual(cistring.str2addr(6, 3, cistring.addr2str(6, 3, 7)),
                         7)
        self.assertEqual(cistring.str2addr(6, 3, cistring.addr2str(6, 3, 8)),
                         8)
        self.assertEqual(cistring.str2addr(7, 4, cistring.addr2str(7, 4, 9)),
                         9)

        self.assertTrue(
            all(
                numpy.arange(20) == cistring.strs2addr(
                    6, 3, cistring.addrs2str(6, 3, range(20)))))
Beispiel #12
0
def pspace(h1e, eri, norb, nelec, hdiag=None, np=400):
    neleca, nelecb = direct_spin1._unpack_nelec(nelec)
    h1e_a = numpy.ascontiguousarray(h1e[0])
    h1e_b = numpy.ascontiguousarray(h1e[1])
    g2e_aa = ao2mo.restore(1, eri[0], norb)
    g2e_ab = ao2mo.restore(1, eri[1], norb)
    g2e_bb = ao2mo.restore(1, eri[2], norb)
    link_indexa = cistring.gen_linkstr_index_trilidx(range(norb), neleca)
    link_indexb = cistring.gen_linkstr_index_trilidx(range(norb), nelecb)
    nb = link_indexb.shape[0]
    if hdiag is None:
        hdiag = make_hdiag(h1e, eri, norb, nelec)
    if hdiag.size < np:
        addr = numpy.arange(hdiag.size)
    else:
        try:
            addr = numpy.argpartition(hdiag, np - 1)[:np]
        except AttributeError:
            addr = numpy.argsort(hdiag)[:np]
    addra = addr // nb
    addrb = addr % nb
    stra = numpy.array([cistring.addr2str(norb, neleca, ia) for ia in addra],
                       dtype=numpy.long)
    strb = numpy.array([cistring.addr2str(norb, nelecb, ib) for ib in addrb],
                       dtype=numpy.long)
    np = len(addr)
    h0 = numpy.zeros((np, np))
    libfci.FCIpspace_h0tril_uhf(h0.ctypes.data_as(ctypes.c_void_p),
                                h1e_a.ctypes.data_as(ctypes.c_void_p),
                                h1e_b.ctypes.data_as(ctypes.c_void_p),
                                g2e_aa.ctypes.data_as(ctypes.c_void_p),
                                g2e_ab.ctypes.data_as(ctypes.c_void_p),
                                g2e_bb.ctypes.data_as(ctypes.c_void_p),
                                stra.ctypes.data_as(ctypes.c_void_p),
                                strb.ctypes.data_as(ctypes.c_void_p),
                                ctypes.c_int(norb), ctypes.c_int(np))

    for i in range(np):
        h0[i, i] = hdiag[addr[i]]
    h0 = lib.hermi_triu(h0)
    return addr, h0
Beispiel #13
0
def guess_wfnsym(ci, norb, nelec, orbsym):
    if isinstance(nelec, (int, numpy.integer)):
        nelecb = nelec//2
        neleca = nelec - nelecb
    else:
        neleca, nelecb = nelec
    na = cistring.num_strings(norb, neleca)
    nb = cistring.num_strings(norb, nelecb)
    if isinstance(ci, numpy.ndarray) and ci.ndim <= 2:
        idx = numpy.argmax(ci)
    else:
        idx = ci[0].argmax()
    stra = cistring.addr2str(norb, neleca, idx // nb)
    strb = cistring.addr2str(norb, nelecb, idx % nb )

    airrep = 0
    birrep = 0
    for i in range(norb):
        if (stra & (1<<i)):
            airrep ^= orbsym[i]
        if (strb & (1<<i)):
            birrep ^= orbsym[i]
    return airrep ^ birrep
Beispiel #14
0
def guess_wfnsym(ci, norb, nelec, orbsym):
    '''Guess the wavefunction symmetry based on the non-zero elements in the
    given CI coefficients.

    Args:
        ci : 2D array
            CI coefficients, row for alpha strings and column for beta strings.
        norb : int
            Number of orbitals.
        nelec : int or 2-item list
            Number of electrons, or 2-item list for (alpha, beta) electrons
        orbsym : list of int
            The irrep ID for each orbital.

    Returns:
        Irrep ID
    '''
    neleca, nelecb = _unpack(nelec)
    na = cistring.num_strings(norb, neleca)
    nb = cistring.num_strings(norb, nelecb)
    if isinstance(ci, numpy.ndarray) and ci.ndim <= 2:
        assert (ci.size == na * nb)
        idx = numpy.argmax(ci)
    else:
        assert (ci[0].size == na * nb)
        idx = ci[0].argmax()
    stra = cistring.addr2str(norb, neleca, idx // nb)
    strb = cistring.addr2str(norb, nelecb, idx % nb)

    airrep = 0
    birrep = 0
    for i in range(norb):
        if (stra & (1 << i)):
            airrep ^= orbsym[i]
        if (strb & (1 << i)):
            birrep ^= orbsym[i]
    return airrep ^ birrep
Beispiel #15
0
def guess_wfnsym(ci, norb, nelec, orbsym):
    '''Guess the wavefunction symmetry based on the non-zero elements in the
    given CI coefficients.

    Args:
        ci : 2D array
            CI coefficients, row for alpha strings and column for beta strings.
        norb : int
            Number of orbitals.
        nelec : int or 2-item list
            Number of electrons, or 2-item list for (alpha, beta) electrons
        orbsym : list of int
            The irrep ID for each orbital.

    Returns:
        Irrep ID
    '''
    neleca, nelecb = _unpack(nelec)
    na = cistring.num_strings(norb, neleca)
    nb = cistring.num_strings(norb, nelecb)
    if isinstance(ci, numpy.ndarray) and ci.ndim <= 2:
        assert(ci.size == na*nb)
        idx = numpy.argmax(ci)
    else:
        assert(ci[0].size == na*nb)
        idx = ci[0].argmax()
    stra = cistring.addr2str(norb, neleca, idx // nb)
    strb = cistring.addr2str(norb, nelecb, idx % nb )

    airrep = 0
    birrep = 0
    for i in range(norb):
        if (stra & (1<<i)):
            airrep ^= orbsym[i]
        if (strb & (1<<i)):
            birrep ^= orbsym[i]
    return airrep ^ birrep
Beispiel #16
0
def pspace(h1e, eri, norb, nelec, hdiag, np=400):
    if isinstance(nelec, (int, numpy.number)):
        nelecb = nelec // 2
        neleca = nelec - nelecb
    else:
        neleca, nelecb = nelec
    h1e_a = numpy.ascontiguousarray(h1e[0])
    h1e_b = numpy.ascontiguousarray(h1e[1])
    g2e_aa = pyscf.ao2mo.restore(1, eri[0], norb)
    g2e_ab = pyscf.ao2mo.restore(1, eri[1], norb)
    g2e_bb = pyscf.ao2mo.restore(1, eri[2], norb)
    link_indexa = cistring.gen_linkstr_index_trilidx(range(norb), neleca)
    link_indexb = cistring.gen_linkstr_index_trilidx(range(norb), nelecb)
    nb = link_indexb.shape[0]
    addr = numpy.argsort(hdiag)[:np]
    addra = addr // nb
    addrb = addr % nb
    stra = numpy.array([cistring.addr2str(norb, neleca, ia) for ia in addra],
                       dtype=numpy.long)
    strb = numpy.array([cistring.addr2str(norb, nelecb, ib) for ib in addrb],
                       dtype=numpy.long)
    np = len(addr)
    h0 = numpy.zeros((np, np))
    libfci.FCIpspace_h0tril_uhf(h0.ctypes.data_as(ctypes.c_void_p),
                                h1e_a.ctypes.data_as(ctypes.c_void_p),
                                h1e_b.ctypes.data_as(ctypes.c_void_p),
                                g2e_aa.ctypes.data_as(ctypes.c_void_p),
                                g2e_ab.ctypes.data_as(ctypes.c_void_p),
                                g2e_bb.ctypes.data_as(ctypes.c_void_p),
                                stra.ctypes.data_as(ctypes.c_void_p),
                                strb.ctypes.data_as(ctypes.c_void_p),
                                ctypes.c_int(norb), ctypes.c_int(np))

    for i in range(np):
        h0[i, i] = hdiag[addr[i]]
    h0 = pyscf.lib.hermi_triu(h0)
    return addr, h0
Beispiel #17
0
    def test_addr2str(self):
        self.assertEqual(bin(cistring.addr2str(6, 3, 7)), '0b11001')
        self.assertEqual(bin(cistring.addr2str(6, 3, 8)), '0b11010')
        self.assertEqual(bin(cistring.addr2str(7, 4, 9)), '0b110011')

        self.assertEqual(cistring.addr2str_o0(6, 3, 7), cistring.addr2str(6, 3, 7))
        self.assertEqual(cistring.addr2str_o0(6, 3, 8), cistring.addr2str(6, 3, 8))
        self.assertEqual(cistring.addr2str_o0(7, 4, 9), cistring.addr2str(7, 4, 9))

        self.assertEqual(bin(cistring.addr2str_o1(6, 3, 7)), '0b11001')
        self.assertEqual(bin(cistring.addr2str_o1(6, 3, 8)), '0b11010')
        self.assertEqual(bin(cistring.addr2str_o1(7, 4, 9)), '0b110011')
Beispiel #18
0
def _guess_nelec_f (fci, norb, nelec, norb_f, h1, h2):
    # Pick electron distribution by the lowest-energy single determinant
    nelec = direct_spin1._unpack_nelec (nelec)
    hdiag = fci.make_hdiag (h1, h2, norb, nelec)
    ndetb = cistring.num_strings (norb, nelec[1])
    addr_dp = np.divmod (np.argmin (hdiag), ndetb)
    str_dp = [cistring.addr2str (norb, n, c) for n,c in zip (nelec, addr_dp)]
    nelec_f = []
    for n in norb_f:
        ndet = 2**n
        c = np.zeros ((ndet, ndet))
        det = np.array ([str_dp[0] % ndet, str_dp[1] % ndet], dtype=np.integer)
        str_dp = [str_dp[0] // ndet, str_dp[1] // ndet]
        ne = [0,0]
        for spin in range (2):
            for iorb in range (n):
                ne[spin] += int (bool ((det[spin] & (1 << iorb))))
        nelec_f.append (ne)
    return nelec_f
Beispiel #19
0
    def test_addr2str(self):
        self.assertEqual(bin(cistring.addr2str(6, 3, 7)), '0b11001')
        self.assertEqual(bin(cistring.addr2str(6, 3, 8)), '0b11010')
        self.assertEqual(bin(cistring.addr2str(7, 4, 9)), '0b110011')

        self.assertEqual(cistring.addr2str_o0(6, 3, 7),
                         cistring.addr2str(6, 3, 7))
        self.assertEqual(cistring.addr2str_o0(6, 3, 8),
                         cistring.addr2str(6, 3, 8))
        self.assertEqual(cistring.addr2str_o0(7, 4, 9),
                         cistring.addr2str(7, 4, 9))

        self.assertEqual(bin(cistring.addr2str_o1(6, 3, 7)), '0b11001')
        self.assertEqual(bin(cistring.addr2str_o1(6, 3, 8)), '0b11010')
        self.assertEqual(bin(cistring.addr2str_o1(7, 4, 9)), '0b110011')
def print_RCAS_wavefunction(calc,norb,neleca,nelecb,threshold,f,frozen=0,path='./molecule'):


    import numpy     as np
    from   pyscf.fci import cistring
    from   numpy     import abs, argsort, shape

    ci_vec        = calc.ci
    ci_dim1       = ci_vec.shape[1]
    na,nb         = ci_vec.shape

    c_matrix = np.zeros((na*nb,2*(norb+frozen)))
    c_labels = []

    def excitation_str(strng):
      strng_r = bin(strng)[-1:1:-1]
      liszt_r = [ (i-frozen) for (x,i) in zip(strng_r,xrange(frozen+1,frozen+len(strng_r)+1)) if(x=="1" and i>max(frozen,frozen)) ]
      liszt_r = list(range(frozen)) + [ x + frozen-1 for x in liszt_r]
      return "".join( " %3d" % j for j in liszt_r)

    fmt  = "%s           %s  # %18.14f\n"
    fmt2 = " %18.14f 0.0  # %5d %18.14f\n"

    def ampl_and_totwt(ampl):
      totwt = 0.0
      for (i,a) in enumerate(ampl):
        totwt += a*a
        yield (a, i+1, totwt)

    full_join = (lambda list_cfg_str, list_ampl: \
                   "multidet_cfg\n" \
                   + "".join(list_cfg_str))

    cfg_list   = []
    ampl_list  = []
    tot_weight = 0.0
    ampl       = 0
    ci_vec_abs = -abs(ci_vec.flatten())
    ci_order   = ci_vec_abs.argsort()
    stop_dump  = lambda ndets : tot_weight > threshold #or ndets > 20

    for ii_ab in xrange(na*nb):
      i_ab = ci_order[ii_ab]
      ia   = i_ab // ci_dim1
      ib   = i_ab - ia * ci_dim1

      ampl = ci_vec[ia,ib]
      tot_weight += ampl**2
      ampl_list.append(ampl)
      cfg_list.append(fmt % (excitation_str(cistring.addr2str(norb,neleca,ia)),excitation_str(cistring.addr2str(norb,nelecb,ib)),ampl))

      sa  = ''.join(excitation_str(cistring.addr2str(norb,neleca,ia)))
      sb  = ''.join(excitation_str(cistring.addr2str(norb,nelecb,ib)))
      cab = ampl
      for x in sa.split():
          c_matrix[ii_ab,int(x)]=1.0
      for x in sb.split():
          c_matrix[ii_ab,int(x)+norb+frozen]=-1.0 
      c_labels.append(str(round(ampl,3)))
      if stop_dump(ii_ab+1): break
      if len(ampl_list)>20: break

    c_matrix = c_matrix[:ii_ab,:]
    c_labels = c_labels[:ii_ab]

    import matplotlib
    import matplotlib.pyplot as plt
    fig,ax = plt.subplots()
    extent = (0,c_matrix.shape[1],c_matrix.shape[0],0)
    im     = ax.imshow(c_matrix,vmin=-1,vmax=1,cmap='coolwarm')
    aolab  = c_labels
    molab  = list(range(norb+frozen))+list(range(norb+frozen))
    ax.set_xticks(np.arange(len(molab)))
    ax.set_yticks(np.arange(len(aolab)))
    ax.set_xticklabels(molab)
    ax.set_yticklabels(aolab)
    plt.setp(ax.get_xticklabels(),rotation=45,ha="right",rotation_mode="anchor")
    xposition = [0.5+x for x in range(len(molab))]
    for xc in xposition:
        ax.axvline(x=xc,color='k',linestyle='-')
        yposition = [0.5+x for x in range(len(aolab))]
    for yc in yposition:
        ax.axhline(y=yc,color='k',linestyle='-')
    ax.set_title("CI wavefunction")
    ax.set_xlabel("orbitals")
    ax.set_ylabel("configurations")
    plt.savefig(path+'_FCI.pdf', bbox_inches='tight')
    

    f.write("\n")
    f.write(full_join(cfg_list, ampl_list))
    f.close()
Beispiel #21
0
def csdaddrs2csdstrs(norb, neleca, nelecb, csdaddrs):
    ''' This is extremely slow because of the amount of repetition in dconf_addr, sconf_addr, and spins_addr! '''
    t_start = time.time()
    min_npair, npair_offset, npair_dconf_size, npair_sconf_size, npair_spins_size = get_csdaddrs_shape(
        norb, neleca, nelecb)
    csdstrs = np.empty((4, len(csdaddrs)), dtype=np.int64)
    for npair, offset, dconf_size, sconf_size, spins_size in zip(
            range(min_npair,
                  min(neleca, nelecb) + 1), npair_offset, npair_dconf_size,
            npair_sconf_size, npair_spins_size):
        nspins = neleca + nelecb - 2 * npair
        nup = (nspins + neleca - nelecb) // 2
        assert ((nspins + neleca - nelecb) % 2 == 0)
        next_offset = offset + (dconf_size * sconf_size * spins_size)

        idx = (csdaddrs >= offset) & (csdaddrs < next_offset)
        if len(idx) == 1:
            if not idx[0]:
                continue
            else:
                idx = 0
        try:
            dconf_addr = (csdaddrs[idx] - offset) // (sconf_size * spins_size)
        except:
            print(idx)
            assert (False)
        dconf_rem = (csdaddrs[idx] - offset) % (sconf_size * spins_size)
        sconf_addr = dconf_rem // spins_size
        spins_addr = dconf_rem % spins_size
        csdstrs[0, idx] = npair

        t_ref = time.time()
        dconf_addr_uniq, dconf_addr_uniq2full = np.unique(dconf_addr,
                                                          return_inverse=True)
        try:
            csdstrs[1, idx] = cistring.addrs2str(
                norb, npair, dconf_addr_uniq)[dconf_addr_uniq2full]
        except TypeError:
            csdstrs[1, idx] = cistring.addr2str(norb, npair, dconf_addr_uniq)

        sconf_addr_uniq, sconf_addr_uniq2full = np.unique(sconf_addr,
                                                          return_inverse=True)
        try:
            csdstrs[2, idx] = cistring.addrs2str(
                norb - npair, nspins, sconf_addr_uniq)[sconf_addr_uniq2full]
        except TypeError:
            csdstrs[2, idx] = cistring.addr2str(norb - npair, nspins,
                                                sconf_addr_uniq)

        spins_addr_uniq, spins_addr_uniq2full = np.unique(spins_addr,
                                                          return_inverse=True)
        try:
            csdstrs[3, idx] = cistring.addrs2str(
                nspins, nup, spins_addr_uniq)[spins_addr_uniq2full]
        except TypeError:
            csdstrs[3, idx] = cistring.addr2str(nspins, nup, spins_addr_uniq)
        #print ("{:.2f} seconds in cistring".format (time.time () - t_ref))
        '''
        t_ref = time.time ()
        try:
            csdstrs[1,idx] = cistring.addrs2str (norb, npair, dconf_addr)
            csdstrs[2,idx] = cistring.addrs2str (norb - npair, nspins, sconf_addr)
            csdstrs[3,idx] = cistring.addrs2str (nspins, nup, spins_addr)
        except TypeError:
            csdstrs[1,idx] = cistring.addr2str (norb, npair, dconf_addr)
            csdstrs[2,idx] = cistring.addr2str (norb - npair, nspins, sconf_addr)
            csdstrs[3,idx] = cistring.addr2str (nspins, nup, spins_addr)
        print ("{:.2f} seconds in cistring".format (time.time () - t_ref))
        '''

    #print ("{:.2f} seconds spent in csdaddrs2csdstrs".format (time.time () - t_start))
    return csdstrs