def spin_square(self, fcivec, norb, nelec): from pyscf.fci import spin_op if self.nroots == 1: return spin_op.spin_square0(fcivec, norb, nelec) else: ss = [spin_op.spin_square0(c, norb, nelec) for c in fcivec] return [x[0] for x in ss], [x[1] for x in ss]
def print_line (c, ne): try: ss, smult = spin_square0 (c, norb, ne) hc = contract_2e (eri, c, norb, ne) chc = c.conj ().ravel ().dot (hc.ravel ()) except AssertionError as e: assert (any ([n<0 for n in ne])) ss, smult, chc = (0.0, 1.0, 0.0) cc = linalg.norm (c) ndeta, ndetb = c.shape print (" {:>6d} {:>6d} {:>5d} {:>5d} {:5.3f} {:13.9f} {:5.3f} {:5.3f}".format (ne[0], ne[1], ndeta, ndetb, cc, chc, ss, smult))
def test_spin_square(self): norb, nelec = 10, 4 strs = cistring.make_strings(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 = selected_ci._as_SCIvector(numpy.random.random((len(strsa),len(strsb))), ci_strs) ci0 = selected_ci.to_fci(ci_coeff, norb, (nelec,nelec)) ss0 = selected_ci.spin_square(ci_coeff, norb, (nelec,nelec)) ss1 = spin_op.spin_square0(ci0, norb, (nelec,nelec)) self.assertAlmostEqual(ss0[0], ss1[0], 9)
def spin_square(self, fcivec, norb, nelec): nelec = _unpack_nelec(nelec, self.spin) return spin_op.spin_square0(fcivec, norb, nelec)
def get_cc_chc_smult (eri, c, norb, ne): cc = c.conj ().ravel ().dot (c.ravel ()) chc = c.conj ().ravel ().dot (contract_2e (eri, c, norb, ne).ravel ()) ss, smult = spin_square0 (c, norb, ne) return cc, chc, smult
def debug_lagrange(self, Lvec, bvec, Aop, Adiag, iroot=None, mo=None, ci=None, **kwargs): if iroot is None: iroot = self.iroot if mo is None: mo = self.base.mo_coeff if ci is None: ci = self.base.ci lib.logger.info( self, '{} gradient: iroot = {}'.format(self.base.__class__.__name__, iroot)) ngorb = self.ngorb nci = self.nci nroots = self.nroots ndet = nci // nroots ncore = self.base.ncore ncas = self.base.ncas nelecas = self.base.nelecas nocc = ncore + ncas nlag = self.nlag ci = np.asarray(self.base.ci).reshape(nroots, -1) err = Aop(Lvec) + bvec eorb = self.base.unpack_uniq_var(err[:ngorb]) eci = err[ngorb:].reshape(nroots, -1) borb = self.base.unpack_uniq_var(bvec[:ngorb]) bci = bvec[ngorb:].reshape(nroots, -1) Lorb = self.base.unpack_uniq_var(Lvec[:ngorb]) Lci = Lvec[ngorb:].reshape(nroots, ndet) Aci = Adiag[ngorb:].reshape(nroots, ndet) Lci_ci_ovlp = ( np.asarray(ci).reshape(nroots, -1).conjugate() @ Lci.T).T Lci_Lci_ovlp = (Lci.conjugate() @ Lci.T).T eci_ci_ovlp = ( np.asarray(ci).reshape(nroots, -1).conjugate() @ eci.T).T bci_ci_ovlp = ( np.asarray(ci).reshape(nroots, -1).conjugate() @ bci.T).T ci_ci_ovlp = ci.conjugate() @ ci.T lib.logger.debug( self, "{} gradient RHS, inactive-active orbital rotations:\n{}".format( self.base.__class__.__name__, borb[:ncore, ncore:nocc])) lib.logger.debug( self, "{} gradient RHS, inactive-external orbital rotations:\n{}".format( self.base.__class__.__name__, borb[:ncore, nocc:])) lib.logger.debug( self, "{} gradient RHS, active-external orbital rotations:\n{}".format( self.base.__class__.__name__, borb[ncore:nocc, nocc:])) lib.logger.debug( self, "{} gradient residual, inactive-active orbital rotations:\n{}". format(self.base.__class__.__name__, eorb[:ncore, ncore:nocc])) lib.logger.debug( self, "{} gradient residual, inactive-external orbital rotations:\n{}". format(self.base.__class__.__name__, eorb[:ncore, nocc:])) lib.logger.debug( self, "{} gradient residual, active-external orbital rotations:\n{}". format(self.base.__class__.__name__, eorb[ncore:nocc, nocc:])) lib.logger.debug( self, "{} gradient Lagrange factor, inactive-active orbital rotations:\n{}" .format(self.base.__class__.__name__, Lorb[:ncore, ncore:nocc])) lib.logger.debug( self, "{} gradient Lagrange factor, inactive-external orbital rotations:\n{}" .format(self.base.__class__.__name__, Lorb[:ncore, nocc:])) lib.logger.debug( self, "{} gradient Lagrange factor, active-external orbital rotations:\n{}" .format(self.base.__class__.__name__, Lorb[ncore:nocc, nocc:])) ''' lib.logger.debug (self, "{} gradient RHS, inactive-inactive orbital rotations (redundant!):\n{}".format ( self.base.__class__.__name__, borb[:ncore,:ncore])) lib.logger.debug (self, "{} gradient RHS, active-active orbital rotations (redundant!):\n{}".format ( self.base.__class__.__name__, borb[ncore:nocc,ncore:nocc])) lib.logger.debug (self, "{} gradient RHS, external-external orbital rotations (redundant!):\n{}".format ( self.base.__class__.__name__, borb[nocc:,nocc:])) lib.logger.debug (self, "{} gradient Lagrange factor, inactive-inactive orbital rotations (redundant!):\n{}".format ( self.base.__class__.__name__, Lorb[:ncore,:ncore])) lib.logger.debug (self, "{} gradient Lagrange factor, active-active orbital rotations (redundant!):\n{}".format ( self.base.__class__.__name__, Lorb[ncore:nocc,ncore:nocc])) lib.logger.debug (self, "{} gradient Lagrange factor, external-external orbital rotations (redundant!):\n{}".format ( self.base.__class__.__name__, Lorb[nocc:,nocc:])) ''' lib.logger.debug( self, "{} gradient Lagrange factor, CI part overlap with true CI SA space:\n{}" .format(self.base.__class__.__name__, Lci_ci_ovlp)) lib.logger.debug( self, "{} gradient Lagrange factor, CI part self overlap matrix:\n{}". format(self.base.__class__.__name__, Lci_Lci_ovlp)) lib.logger.debug( self, "{} gradient Lagrange factor, CI vector self overlap matrix:\n{}". format(self.base.__class__.__name__, ci_ci_ovlp)) lib.logger.debug( self, "{} gradient Lagrange factor, CI part response overlap with SA space:\n{}" .format(self.base.__class__.__name__, bci_ci_ovlp)) lib.logger.debug( self, "{} gradient Lagrange factor, CI part residual overlap with SA space:\n{}" .format(self.base.__class__.__name__, eci_ci_ovlp)) neleca, nelecb = _unpack_nelec(nelecas) spin = neleca - nelecb + 1 csf = CSFTransformer(ncas, neleca, nelecb, spin) ecsf = csf.vec_det2csf(eci, normalize=False, order='C') err_norm_det = linalg.norm(err) err_norm_csf = linalg.norm(np.append(eorb, ecsf.ravel())) lib.logger.debug( self, "{} gradient: determinant residual = {}, CSF residual = {}".format( self.base.__class__.__name__, err_norm_det, err_norm_csf)) ci_lbls, ci_csf = csf.printable_largest_csf(ci, 10, isdet=True, normalize=True, order='C') bci_lbls, bci_csf = csf.printable_largest_csf(bci, 10, isdet=True, normalize=False, order='C') eci_lbls, eci_csf = csf.printable_largest_csf(eci, 10, isdet=True, normalize=False, order='C') Lci_lbls, Lci_csf = csf.printable_largest_csf(Lci, 10, isdet=True, normalize=False, order='C') Aci_lbls, Aci_csf = csf.printable_largest_csf(Aci, 10, isdet=True, normalize=False, order='C') ncsf = bci_csf.shape[1] for iroot in range(self.nroots): lib.logger.debug( self, "{} gradient Lagrange factor, CI part root {} spin square: {}". format(self.base.__class__.__name__, iroot, spin_square0(Lci[iroot], ncas, nelecas))) lib.logger.debug(self, "Base CI vector") for icsf in range(ncsf): lib.logger.debug( self, '{} {}'.format(ci_lbls[iroot, icsf], ci_csf[iroot, icsf])) lib.logger.debug(self, "CI gradient:") for icsf in range(ncsf): lib.logger.debug( self, '{} {}'.format(bci_lbls[iroot, icsf], bci_csf[iroot, icsf])) lib.logger.debug(self, "CI residual:") for icsf in range(ncsf): lib.logger.debug( self, '{} {}'.format(eci_lbls[iroot, icsf], eci_csf[iroot, icsf])) lib.logger.debug(self, "CI Lagrange vector:") for icsf in range(ncsf): lib.logger.debug( self, '{} {}'.format(Lci_lbls[iroot, icsf], Lci_csf[iroot, icsf])) lib.logger.debug(self, "Diagonal of Hessian matrix CI part:") for icsf in range(ncsf): lib.logger.debug( self, '{} {}'.format(Aci_lbls[iroot, icsf], Aci_csf[iroot, icsf])) '''
h1e = reduce(numpy.dot, (m.mo_coeff.T, m.get_hcore(), m.mo_coeff)) eri = ao2mo.kernel(m._eri, m.mo_coeff, compact=False) eri = eri.reshape(norb, norb, norb, norb) e1, c1 = kernel(h1e, eri, norb, nelec) e2, c2 = direct_spin1.kernel(h1e, eri, norb, nelec) print(e1, e1 - -11.894559902235565, 'diff to FCI', e1 - e2) print(c1.shape, c2.shape) dm1_1 = make_rdm1(c1, norb, nelec) dm1_2 = direct_spin1.make_rdm1(c2, norb, nelec) print(abs(dm1_1 - dm1_2).sum()) dm2_1 = make_rdm2(c1, norb, nelec) dm2_2 = direct_spin1.make_rdm12(c2, norb, nelec)[1] print(abs(dm2_1 - dm2_2).sum()) myci = SelectedCI() e, c = kernel_fixed_space(myci, h1e, eri, norb, nelec, c1._strs) print(e - -11.894559902235565) print(myci.large_ci(c1, norb, nelec)) print( myci.spin_square(c1, norb, nelec)[0] - spin_op.spin_square0(to_fci(c1, norb, nelec), norb, nelec)[0]) myci = SelectedCI() myci = addons.fix_spin_(myci) e1, c1 = myci.kernel(h1e, eri, norb, nelec) print(e1, e1 - -11.89467612053687) print(myci.spin_square(c1, norb, nelec))
nelec = mol.nelectron h1e = reduce(numpy.dot, (m.mo_coeff.T, m.get_hcore(), m.mo_coeff)) eri = ao2mo.kernel(m._eri, m.mo_coeff, compact=False) eri = eri.reshape(norb,norb,norb,norb) e1, c1 = kernel(h1e, eri, norb, nelec) e2, c2 = direct_spin1.kernel(h1e, eri, norb, nelec) print(e1, e1 - -11.894559902235565, 'diff to FCI', e1-e2) print(c1.shape, c2.shape) dm1_1 = make_rdm1(c1, norb, nelec) dm1_2 = direct_spin1.make_rdm1(c2, norb, nelec) print(abs(dm1_1 - dm1_2).sum()) dm2_1 = make_rdm2(c1, norb, nelec) dm2_2 = direct_spin1.make_rdm12(c2, norb, nelec)[1] print(abs(dm2_1 - dm2_2).sum()) myci = SelectCI() e, c = kernel_fixed_space(myci, h1e, eri, norb, nelec, c1._strs) print(e - -11.894559902235565) print(myci.large_ci(c1, norb, nelec)) print(myci.spin_square(c1, norb, nelec)[0] - spin_op.spin_square0(to_fci(c1, norb, nelec), norb, nelec)[0]) myci = SelectCI() myci = addons.fix_spin_(myci) e1, c1 = myci.kernel(h1e, eri, norb, nelec) print(e1, e1 - -11.89467612053687) print(myci.spin_square(c1, norb, nelec))
norb = m.mo_coeff.shape[1] nelec = mol.nelectron h1e = reduce(numpy.dot, (m.mo_coeff.T, m.get_hcore(), m.mo_coeff)) eri = ao2mo.kernel(m._eri, m.mo_coeff, compact=False) eri = eri.reshape(norb, norb, norb, norb) e1, c1 = kernel(h1e, eri, norb, nelec) e2, c2 = direct_spin1.kernel(h1e, eri, norb, nelec) print(e1, e1 - -11.894559902235565, "diff to FCI", e1 - e2) print(c1.shape, c2.shape) dm1_1 = make_rdm1(c1, norb, nelec) dm1_2 = direct_spin1.make_rdm1(c2, norb, nelec) print(abs(dm1_1 - dm1_2).sum()) dm2_1 = make_rdm2(c1, norb, nelec) dm2_2 = direct_spin1.make_rdm12(c2, norb, nelec)[1] print(abs(dm2_1 - dm2_2).sum()) myci = SelectCI() e, c = kernel_fixed_space(myci, h1e, eri, norb, nelec, c1._strs) print(e - -11.894559902235565) print(myci.large_ci(c1, norb, nelec)) print(myci.spin_square(c1, norb, nelec)[0] - spin_op.spin_square0(to_fci(c1, norb, nelec), norb, nelec)[0]) myci = SelectCI() myci = addons.fix_spin_(myci) e1, c1 = myci.kernel(h1e, eri, norb, nelec) print(e1, e1 - -11.89467612053687) print(myci.spin_square(c1, norb, nelec))
def spin_square(self, fcivec, norb, nelec): if self.nroots == 1: return spin_op.spin_square0(fcivec, norb, nelec) else: ss = [spin_op.spin_square0(c, norb, nelec) for c in fcivec] return [x[0] for x in ss], [x[1] for x in ss]