def kernel_ft_smpl(h1e, g2e, norb, nelec, T, m=50,\ nsmpl=20000, Tmin=1e-3, symm='SOC', **kwargs): if symm is 'RHF': from pyscf.fci import direct_spin1 as fcisolver elif symm is 'SOC': from pyscf.fci import fci_slow_spinless as fcisolver elif symm is 'UHF': from pyscf.fci import direct_uhf as fcisolver else: from pyscf.fci import direct_spin1 as fcisolver if T < Tmin: return fcisolver.kernel(h1e, g2e, norb, nelec)[0] h2e = fcisolver.absorb_h1e(h1e, g2e, norb, nelec, .5) if symm is 'SOC': na = cistring.num_strings(norb, nelec) ci0 = numpy.random.randn(na) else: na = cistring.num_strings(norb, nelec // 2) ci0 = numpy.random.randn(na * na) ci0 = ci0 / numpy.linalg.norm(ci0) hdiag = fcisolver.make_hdiag(h1e, g2e, norb, nelec) hdiag = hdiag - hdiag[0] def hop(c): hc = fcisolver.contract_2e(h2e, c, norb, nelec) return hc.reshape(-1) E = ftsmpl.ft_smpl_E(hop, ci0, T, nsamp=nsmpl) return E
def rdm12s_ft_smpl(h1e, g2e, norb, nelec, T, \ m=50, nsmpl=20000, Tmin=1e-3, symm='RHF', **kwargs): if symm is 'RHF': from pyscf.fci import direct_spin1 as fcisolver elif symm is 'SOC': from pyscf.fci import fci_slow_spinless as fcisolver elif symm is 'UHF': from pyscf.fci import direct_uhf as fcisolver else: from pyscf.fci import direct_spin1 as fcisolver if T < Tmin: e, c = fcisolver.kernel(h1e, g2e, norb, nelec) dm1, dm2 = fcisolver.make_rdm12s(c, norb, nelec) dm1 = numpy.asarray(dm1) dm2 = numpy.asarray(dm2) else: h2e = fcisolver.absorb_h1e(h1e, g2e, norb, nelec, .5) if symm is 'SOC': na = cistring.num_strings(norb, nelec) ci0 = numpy.random.randn(na) else: na = cistring.num_strings(norb, nelec // 2) ci0 = numpy.random.randn(na * na) hdiag = fcisolver.make_hdiag(h1e, g2e, norb, nelec) hdiag = hdiag - hdiag[0] disp = numpy.exp(T) * 0.5 ci0 = ci0 / numpy.linalg.norm(ci0) def hop(c): hc = fcisolver.contract_2e(h2e, c, norb, nelec) return hc.reshape(-1) def qud(v1): dm1, dm2 = fcisolver.make_rdm12s(v1, norb, nelec) return dm1, dm2 dm1, dm2, e = ftsmpl.ft_smpl_rdm12s(qud, hop, ci0, T, norb, nsamp=nsmpl, M=m) if symm is 'UHF': return dm1, dm2, e elif len(dm1.shape) == 3: return numpy.sum(dm1, axis=0), numpy.sum(dm2, axis=0), e else: return dm1, dm2, e
def kernel(h1e, g2e, norb, nelec): h2e = direct_spin1.absorb_h1e(h1e, g2e, norb, nelec, .5) if isinstance(nelec, (int, numpy.integer)): neleca = nelec//2 else: neleca = nelec[0] na = cistring.num_strings(norb, neleca) ci0 = numpy.zeros((na,na)) ci0[0,0] = 1 def hop(c): hc = direct_spin1.contract_2e(h2e, c, norb, nelec) return hc.reshape(-1) hdiag = direct_spin1.make_hdiag(h1e, g2e, norb, nelec) precond = lambda x, e, *args: x/(hdiag-e+1e-4) e, c = pyscf.lib.davidson(hop, ci0.reshape(-1), precond) return e, c
def kernel(h1e, g2e, norb, nelec): h2e = direct_spin1.absorb_h1e(h1e, g2e, norb, nelec, .5) if isinstance(nelec, (int, numpy.integer)): neleca = nelec // 2 else: neleca = nelec[0] na = cistring.num_strings(norb, neleca) ci0 = numpy.zeros((na, na)) ci0[0, 0] = 1 def hop(c): hc = direct_spin1.contract_2e(h2e, c, norb, nelec) return hc.reshape(-1) hdiag = direct_spin1.make_hdiag(h1e, g2e, norb, nelec) precond = lambda x, e, *args: x / (hdiag - e + 1e-4) e, c = pyscf.lib.davidson(hop, ci0.reshape(-1), precond) return e, c
def make_hdiag(h1e, eri, norb, nelec): hdiag = direct_spin1.make_hdiag(h1e, eri, norb, nelec) na = int(numpy.sqrt(hdiag.size)) # symmetrize hdiag to reduce numerical error hdiag = lib.transpose_sum(hdiag.reshape(na, na), inplace=True) * .5 return hdiag.ravel()
def make_hdiag(self, h1e, eri, norb, nelec): nelec = _unpack_nelec(nelec, self.spin) return direct_spin1.make_hdiag(h1e, eri, norb, nelec)
def make_hdiag(self, h1e, eri, norb, nelec): return direct_spin1.make_hdiag(h1e, eri, norb, nelec)
def make_hdiag_csf_slower(h1e, eri, norb, nelec, smult, csd_mask=None, hdiag_det=None): ''' This is tricky because I need the diagonal blocks for each configuration in order to get the correct csf hdiag values, not just the diagonal elements for each determinant. ''' t0, w0 = time.clock(), time.time() tstr = tlib = tloop = wstr = wlib = wloop = 0 if hdiag_det is None: hdiag_det = make_hdiag(h1e, eri, norb, nelec) eri = ao2mo.restore(1, eri, norb) neleca, nelecb = _unpack_nelec(nelec) min_npair, npair_csd_offset, npair_dconf_size, npair_sconf_size, npair_sdet_size = get_csdaddrs_shape( norb, neleca, nelecb) _, npair_csf_offset, _, _, npair_csf_size = get_csfvec_shape( norb, neleca, nelecb, smult) npair_econf_size = npair_dconf_size * npair_sconf_size max_npair = nelecb ncsf_all = count_all_csfs(norb, neleca, nelecb, smult) ndeta_all = cistring.num_strings(norb, neleca) ndetb_all = cistring.num_strings(norb, nelecb) ndet_all = ndeta_all * ndetb_all hdiag_csf = np.ascontiguousarray(np.zeros(ncsf_all, dtype=np.float64)) hdiag_csf_check = np.ones(ncsf_all, dtype=np.bool) for npair in range(min_npair, max_npair + 1): ipair = npair - min_npair nconf = npair_econf_size[ipair] ndet = npair_sdet_size[ipair] ncsf = npair_csf_size[ipair] if ncsf == 0: continue nspin = neleca + nelecb - 2 * npair csd_offset = npair_csd_offset[ipair] csf_offset = npair_csf_offset[ipair] hdiag_conf = np.ascontiguousarray( np.zeros((nconf, ndet, ndet), dtype=np.float64)) if csd_mask is None: det_addr = get_nspin_dets(norb, neleca, nelecb, nspin).ravel(order='C') else: det_addr = csd_mask[csd_offset:][:nconf * ndet] if ndet == 1: # Closed-shell singlets assert (ncsf == 1) hdiag_csf[csf_offset:][:nconf] = hdiag_det[det_addr.flat] hdiag_csf_check[csf_offset:][:nconf] = False continue umat = get_spin_evecs(nspin, neleca, nelecb, smult) det_addra, det_addrb = divmod(det_addr, ndetb_all) t1, w1 = time.clock(), time.time() det_stra = cistring.addrs2str(norb, neleca, det_addra).reshape(nconf, ndet, order='C') det_strb = cistring.addrs2str(norb, nelecb, det_addrb).reshape(nconf, ndet, order='C') tstr += time.clock() - t1 wstr += time.time() - w1 det_addr = det_addr.reshape(nconf, ndet, order='C') diag_idx = np.diag_indices(ndet) triu_idx = np.triu_indices(ndet) ipair_check = 0 # It looks like the library call below is, itself, usually responsible for about 50% of the # clock and wall time that this function consumes. t1, w1 = time.clock(), time.time() for iconf in range(nconf): addr = det_addr[iconf] assert (len(addr) == ndet) stra = det_stra[iconf] strb = det_strb[iconf] t2, w2 = time.clock(), time.time() libfci.FCIpspace_h0tril( hdiag_conf[iconf].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(ndet)) tlib += time.clock() - t2 wlib += time.time() - w2 #hdiag_conf[iconf][diag_idx] = hdiag_det[addr] #hdiag_conf[iconf] = lib.hermi_triu(hdiag_conf[iconf]) for iconf in range(nconf): hdiag_conf[iconf] = lib.hermi_triu(hdiag_conf[iconf]) for iconf in range(nconf): hdiag_conf[iconf][diag_idx] = hdiag_det[det_addr[iconf]] tloop += time.clock() - t1 wloop += time.time() - w1 hdiag_conf = np.tensordot(hdiag_conf, umat, axes=1) hdiag_conf = (hdiag_conf * umat[np.newaxis, :, :]).sum(1) hdiag_csf[csf_offset:][:nconf * ncsf] = hdiag_conf.ravel(order='C') hdiag_csf_check[csf_offset:][:nconf * ncsf] = False assert (np.count_nonzero(hdiag_csf_check) == 0 ), np.count_nonzero(hdiag_csf_check) #print ("Total time in hdiag_csf: {}, {}".format (time.clock () - t0, time.time () - w0)) #print (" Loop: {}, {}".format (tloop, wloop)) #print (" Library: {}, {}".format (tlib, wlib)) #print (" Cistring: {}, {}".format (tstr, wstr)) return hdiag_csf
def make_hdiag_csf(h1e, eri, norb, nelec, smult, csd_mask=None, hdiag_det=None): if hdiag_det is None: hdiag_det = make_hdiag(h1e, eri, norb, nelec) eri = ao2mo.restore(1, eri, norb) tlib = wlib = 0 neleca, nelecb = _unpack_nelec(nelec) min_npair, npair_csd_offset, npair_dconf_size, npair_sconf_size, npair_sdet_size = get_csdaddrs_shape( norb, neleca, nelecb) _, npair_csf_offset, _, _, npair_csf_size = get_csfvec_shape( norb, neleca, nelecb, smult) npair_econf_size = npair_dconf_size * npair_sconf_size max_npair = nelecb ncsf_all = count_all_csfs(norb, neleca, nelecb, smult) ndeta_all = cistring.num_strings(norb, neleca) ndetb_all = cistring.num_strings(norb, nelecb) ndet_all = ndeta_all * ndetb_all hdiag_csf = np.ascontiguousarray(np.zeros(ncsf_all, dtype=np.float64)) hdiag_csf_check = np.ones(ncsf_all, dtype=np.bool) for npair in range(min_npair, max_npair + 1): ipair = npair - min_npair nconf = npair_econf_size[ipair] ndet = npair_sdet_size[ipair] ncsf = npair_csf_size[ipair] if ncsf == 0: continue nspin = neleca + nelecb - 2 * npair csd_offset = npair_csd_offset[ipair] csf_offset = npair_csf_offset[ipair] hdiag_conf = np.ascontiguousarray( np.zeros((nconf, ndet, ndet), dtype=np.float64)) if csd_mask is None: det_addr = get_nspin_dets(norb, neleca, nelecb, nspin).ravel(order='C') else: det_addr = csd_mask[csd_offset:][:nconf * ndet] if ndet == 1: # Closed-shell singlets assert (ncsf == 1) hdiag_csf[csf_offset:][:nconf] = hdiag_det[det_addr.flat] hdiag_csf_check[csf_offset:][:nconf] = False continue det_addra, det_addrb = divmod(det_addr, ndetb_all) det_stra = np.ascontiguousarray( cistring.addrs2str(norb, neleca, det_addra).reshape(nconf, ndet, order='C')) det_strb = np.ascontiguousarray( cistring.addrs2str(norb, nelecb, det_addrb).reshape(nconf, ndet, order='C')) det_addr = det_addr.reshape(nconf, ndet, order='C') hdiag_conf = np.ascontiguousarray( np.zeros((nconf, ndet, ndet), dtype=np.float64)) hdiag_conf_det = np.ascontiguousarray(hdiag_det[det_addr], dtype=np.float64) t1 = time.clock() w1 = time.time() libcsf.FCICSFhdiag(hdiag_conf.ctypes.data_as(ctypes.c_void_p), hdiag_conf_det.ctypes.data_as(ctypes.c_void_p), eri.ctypes.data_as(ctypes.c_void_p), det_stra.ctypes.data_as(ctypes.c_void_p), det_strb.ctypes.data_as(ctypes.c_void_p), ctypes.c_uint(norb), ctypes.c_uint(nconf), ctypes.c_uint(ndet)) tlib += time.clock() - t1 wlib += time.time() - w1 umat = get_spin_evecs(nspin, neleca, nelecb, smult) hdiag_conf = np.tensordot(hdiag_conf, umat, axes=1) hdiag_conf *= umat[np.newaxis, :, :] hdiag_csf[csf_offset:][:nconf * ncsf] = hdiag_conf.sum(1).ravel(order='C') hdiag_csf_check[csf_offset:][:nconf * ncsf] = False assert (np.count_nonzero(hdiag_csf_check) == 0 ), np.count_nonzero(hdiag_csf_check) #print ("Time in hdiag_csf library: {}, {}".format (tlib, wlib)) return hdiag_csf
def make_hdiag(h1e, eri, norb, nelec): hdiag = direct_spin1.make_hdiag(h1e, eri, norb, nelec) na = int(numpy.sqrt(hdiag.size)) # symmetrize hdiag to reduce numerical error hdiag = lib.transpose_sum(hdiag.reshape(na,na), inplace=True) * .5 return hdiag.ravel()