Skip to content

Commit

Permalink
Added function that checks not only the words but the coeffs of a ham…
Browse files Browse the repository at this point in the history
…iltonian and added to the tests. This is much quicker test than converting to matrix and doing a dense diagonalization.

Signed-off-by: Scott Thornton <[email protected]>
  • Loading branch information
wsttiger committed Feb 12, 2025
1 parent c8fcc53 commit 78e6c28
Showing 1 changed file with 47 additions and 3 deletions.
50 changes: 47 additions & 3 deletions libs/solvers/python/tests/test_jordan_wigner.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,45 @@
from pyscf import gto, scf, fci
import numpy as np

def extract_words(hamiltonian: cudaq.SpinOperator):
result = []
hamiltonian.for_each_term(lambda term: result.append(term.to_string(False)))
return result

def extract_coefficients(hamiltonian: cudaq.SpinOperator):
result = []
hamiltonian.for_each_term(lambda term: result.append(term.get_coefficient()))
return result

def extract_spin_op_to_dict(op: cudaq.SpinOperator) -> dict:
d = {}
coeffs = extract_coefficients(op)
words = extract_words(op)
for c,w in zip(coeffs, words):
d[w] = c
return d

def jw_molecule_compare_hamiltonians_test(xyz):
# Compute energy using CUDA-Q/OpenFermion
of_hamiltonian, data = cudaq.chemistry.create_molecular_hamiltonian(
xyz, 'sto-3g', 1, 0)

# Compute energy using CUDA-QX
molecule = solvers.create_molecule(xyz, 'sto-3g', 0, 0, casci=True)
cqx_op = solvers.jordan_wigner(molecule.hpq,
molecule.hpqrs,
core_energy=molecule.energies['nuclear_energy'])

of_hamiltonian_dict = extract_spin_op_to_dict(of_hamiltonian)
cqx_op_dict = extract_spin_op_to_dict(cqx_op)

for k in of_hamiltonian_dict.keys():
assert(k in cqx_op_dict.keys())
for k in cqx_op_dict.keys():
assert(k in of_hamiltonian_dict.keys())

for k in of_hamiltonian_dict.keys():
np.isclose(of_hamiltonian_dict[k], cqx_op_dict[k], 1e-12)

def jw_molecule_test(xyz):
# Compute FCI energy
Expand Down Expand Up @@ -47,16 +86,21 @@ def jw_molecule_test(xyz):

def test_ground_state():
xyz = [('H', (0., 0., 0.)), ('H', (0., 0., .7474))]
jw_molecule_compare_hamiltonians_test(xyz)
jw_molecule_test(xyz)
xyz = [('H', (0., 0., 0.)), ('H', (0., 0., .7474)), ('H', (1., 0., 0.)),
('H', (1., 0., .7474))]
jw_molecule_compare_hamiltonians_test(xyz)
jw_molecule_test(xyz)
xyz = [('H', (0., 0., 0.)), ('H', (1.0, 0., 0.)),
('H', (0.322, 2.592, 0.1)), ('H', (1.2825, 2.292, 0.1))]
jw_molecule_compare_hamiltonians_test(xyz)
jw_molecule_test(xyz)
xyz = [('Li', (0., 0., 0.)), ('H', (0., 0., 1.1774))]
jw_molecule_compare_hamiltonians_test(xyz)
jw_molecule_test(xyz)
# xyz = [('O', (0.000000, 0.000000, 0.000000)),
# ('H', (0.757000, 0.586000, 0.000000)),
# ('H', (-0.757000, 0.586000, 0.000000))]
xyz = [('O', (0.000000, 0.000000, 0.000000)),
('H', (0.757000, 0.586000, 0.000000)),
('H', (-0.757000, 0.586000, 0.000000))]
jw_molecule_compare_hamiltonians_test(xyz)
# jw_molecule_test(xyz)

0 comments on commit 78e6c28

Please sign in to comment.