def test(): r = 1.5 geometry = [('H', (0,0,1*r)), ('H', (0,0,2*r)), ('H', (0,0,3*r)), ('H', (0,0,4*r))] charge = 0 spin = 0 basis = 'sto-3g' [n_orb, n_a, n_b, h, g, mol, E_nuc, E_scf, C, S] = pyscf_helper.init(geometry,charge,spin,basis) print(" n_orb: %4i" %n_orb) print(" n_a : %4i" %n_a) print(" n_b : %4i" %n_b) sq_ham = pyscf_helper.SQ_Hamiltonian() sq_ham.init(h, g, C, S) print(" HF Energy: %12.8f" %(E_nuc + sq_ham.energy_of_determinant(range(n_a),range(n_b)))) ehf = E_nuc + sq_ham.energy_of_determinant(range(n_a),range(n_b)) assert(abs(ehf - -1.82913741) < 1e-7) fermi_ham = sq_ham.export_FermionOperator() hamiltonian = openfermion.transforms.get_sparse_operator(fermi_ham) s2 = vqe_methods.Make_S2(n_orb) #build reference configuration occupied_list = [] for i in range(n_a): occupied_list.append(i*2) for i in range(n_b): occupied_list.append(i*2+1) print(" Build reference state with %4i alpha and %4i beta electrons" %(n_a,n_b), occupied_list) reference_ket = scipy.sparse.csc_matrix(openfermion.jw_configuration_state(occupied_list, 2*n_orb)).transpose() [e,v] = scipy.sparse.linalg.eigsh(hamiltonian.real,1,which='SA',v0=reference_ket.todense()) for ei in range(len(e)): S2 = v[:,ei].conj().T.dot(s2.dot(v[:,ei])) print(" State %4i: %12.8f au <S2>: %12.8f" %(ei,e[ei]+E_nuc,S2)) fermi_ham += FermionOperator((),E_nuc) pyscf.molden.from_mo(mol, "full.molden", sq_ham.C) # Francesco, change this to singlet_GSD() if you want generalized singles and doubles pool = operator_pools.singlet_SD() pool.init(n_orb, n_occ_a=n_a, n_occ_b=n_b, n_vir_a=n_orb-n_a, n_vir_b=n_orb-n_b) [e,v,params] = vqe_methods.adapt_vqe(fermi_ham, pool, reference_ket, theta_thresh=1e-9) print(" Final ADAPT-VQE energy: %12.8f" %e) print(" <S^2> of final state : %12.8f" %(v.conj().T.dot(s2.dot(v))[0,0].real)) assert(abs(-1.99471290 - e) < 1e-7)
import vqe_methods import operator_pools import sys r = 2.684 geometry = [('H', (0, 0, -r)), ('Be', (0, 0, 0)), ('H', (0, 0, r))] filename = "beh2_r2684_adapt_sd.out" sys.stdout = open(filename, 'w') vqe_methods.adapt_vqe(geometry, pool=operator_pools.singlet_SD(), adapt_thresh=1e-7)
print(" Finished: %20.12f" % trial_model.curr_energy) print(" -----------New ansatz----------- ") print(" %4s %40s %12s" % ("#", "Term", "Coeff")) for si in range(len(ansatz_ops)): s = ansatz_ops[si] opstring = "" for t in s.terms: opstring += str(t) break print(" %4i %40s %12.8f" % (si, opstring, parameters[si])) return # }}} if __name__ == "__main__": r = 1.5 #geometry = [('H', (0,0,1*r)), ('H', (0,0,2*r)), ('H', (0,0,3*r)), ('H', (0,0,4*r))] geometry = [('H', (0, 0, 0)), ('Li', (0, 0, r * 2.39))] #geometry = [('H', (0,0,1*r)), ('H', (0,0,2*r)), ('H', (0,0,3*r)), ('H', (0,0,4*r)), ('H', (0,0,5*r)), ('H', (0,0,6*r))] #vqe_methods.ucc(geometry,pool = operator_pools.singlet_SD()) #vqe_methods.adapt_vqe(geometry,pool = operator_pools.singlet_SD()) #vqe_methods.adapt_vqe(geometry,pool = operator_pools.hamiltonian(), adapt_thresh=1e-7, theta_thresh=1e-8) #vqe_methods.adapt_vqe(geometry,pool = operator_pools.singlet_SD(), adapt_thresh=1e-1, adapt_conver='uncertainty') vqe_methods.adapt_vqe(geometry, pool=operator_pools.spin_complement_GSD(), adapt_thresh=1e-2, theta_thresh=1e-9)
import vqe_methods import operator_pools r = 1.5 geometry = [('H', (0, 0, 1 * r)), ('H', (0, 0, 2 * r)), ('H', (0, 0, 3 * r)), ('H', (0, 0, 4 * r)), ('H', (0, 0, 5 * r)), ('H', (0, 0, 6 * r))] vqe_methods.adapt_vqe(geometry, pool=operator_pools.singlet_SD())
reference_ket = scipy.sparse.csc_matrix( openfermion.jw_configuration_state(occupied_list, 2 * n_orb)).transpose() [e, v] = scipy.sparse.linalg.eigsh(hamiltonian.real, 1, which='SA', v0=reference_ket.todense()) for ei in range(len(e)): S2 = v[:, ei].conj().T.dot(s2.dot(v[:, ei])) print(" State %4i: %12.8f au <S2>: %12.8f" % (ei, e[ei] + E_nuc, S2)) fermi_ham += FermionOperator((), E_nuc) pyscf.molden.from_mo(mol, "full.molden", sq_ham.C) pool = operator_pools.singlet_SD() pool.init(n_orb, n_occ_a=n_a, n_occ_b=n_b, n_vir_a=n_orb - n_a, n_vir_b=n_orb - n_b) [e, v, params] = vqe_methods.adapt_vqe(fermi_ham, pool, reference_ket, theta_thresh=1e-9) # [e,v,params] = vqe_methods.seqGO(fermi_ham, pool, reference_ket, theta_thresh=1e-9) print(" Final ADAPT-VQE energy: %12.8f" % e) print(" <S^2> of final state : %12.8f" % (v.conj().T.dot(s2.dot(v))[0, 0].real))