Skip to content

Add from_fermion_operator method to MoelcularHamiltonian class #386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

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

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@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.

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