Skip to content

Conversation

miamico
Copy link

@miamico miamico commented Apr 16, 2025

Add the feature to define a Molecular Hamiltonian from a Fermion Operator

Closes #385

@CLAassistant
Copy link

CLAassistant commented Apr 16, 2025

CLA assistant check
All committers have signed the CLA.

@miamico
Copy link
Author

miamico commented Apr 16, 2025

This will close issue #385

@kevinsung
Copy link
Collaborator

This needs a test.

@miamico
Copy link
Author

miamico commented Apr 17, 2025

Do you think something like the following would work as a test?

norb = 10

rng1 = np.random.default_rng(0) 

# generate a random molecular hamiltonian 
mol_hamiltonian = ffsim.random.random_molecular_hamiltonian(norb=norb, seed=rng1)

# convert to fermion op
mol_hamiltonian_fops = ffsim.fermion_operator(mol_hamiltonian)
mol_hamiltonian_fops.normal_ordered()
mol_hamiltonian_fops.simplify()

# convert back from fermion op to ham
mol_hamiltonian_from_ferm = molecular_hamiltonian_from_fermion_operator(mol_hamiltonian_fops)


# check they are matching
tol = 1e-8
assert abs(mol_hamiltonian.constant - mol_hamiltonian_from_ferm.constant) < tol, "Constant mismatch!"
assert np.allclose(mol_hamiltonian.one_body_tensor, mol_hamiltonian_from_ferm.one_body_tensor, atol=tol), "One‑body mismatch!"
assert np.allclose(mol_hamiltonian.two_body_tensor, mol_hamiltonian_from_ferm.two_body_tensor, atol=tol), "Two‑body mismatch!"

@kevinsung
Copy link
Collaborator

Do you think something like the following would work as a test?

norb = 10

rng1 = np.random.default_rng(0) 

# generate a random molecular hamiltonian 
mol_hamiltonian = ffsim.random.random_molecular_hamiltonian(norb=norb, seed=rng1)

# convert to fermion op
mol_hamiltonian_fops = ffsim.fermion_operator(mol_hamiltonian)
mol_hamiltonian_fops.normal_ordered()
mol_hamiltonian_fops.simplify()

# convert back from fermion op to ham
mol_hamiltonian_from_ferm = molecular_hamiltonian_from_fermion_operator(mol_hamiltonian_fops)


# check they are matching
tol = 1e-8
assert abs(mol_hamiltonian.constant - mol_hamiltonian_from_ferm.constant) < tol, "Constant mismatch!"
assert np.allclose(mol_hamiltonian.one_body_tensor, mol_hamiltonian_from_ferm.one_body_tensor, atol=tol), "One‑body mismatch!"
assert np.allclose(mol_hamiltonian.two_body_tensor, mol_hamiltonian_from_ferm.two_body_tensor, atol=tol), "Two‑body mismatch!"

Yes, but I don't think we should do the normal ordering.

@miamico miamico requested a review from kevinsung June 16, 2025 16:39
Copy link
Collaborator

@kevinsung kevinsung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the checks needs to pass before this can be merged. See the developer guide for information on how to run the checks locally.

@miamico
Copy link
Author

miamico commented Aug 27, 2025

addressed the comments and pushed the changes. tox is raising some errors locally, let's see if the tests pass in CI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add MolecularHamiltonian.from_fermion_operator
3 participants