Skip to content
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

Applying FermionOperator to wavefunction gives memory error #136

Open
kevinsung opened this issue Nov 17, 2024 · 4 comments
Open

Applying FermionOperator to wavefunction gives memory error #136

kevinsung opened this issue Nov 17, 2024 · 4 comments

Comments

@kevinsung
Copy link

This example has 4 electrons in 8 orbitals. Why is it trying to allocate a 64 GB array?

import fqe
from openfermion import FermionOperator, hermitian_conjugated

op = FermionOperator("5^ 3 2 12^") + FermionOperator("2 4^ 11^ 15 12 14^ 4 8^")
op += hermitian_conjugated(op)
wfn = fqe.get_wavefunction(4, 0, 8)
wfn.set_wfn(strategy="hartree-fock")
new_wfn = wfn.apply(op)
Traceback (most recent call last):
  File "/home/kjs/projects/scratch/fqe-repro.py", line 8, in <module>
    new_wfn = wfn.apply(op)
              ^^^^^^^^^^^^^
  File "/home/kjs/.pyenv/versions/scratch/lib/python3.12/site-packages/fqe/fqe_decorators.py", line 365, in convert
    hamil = build_hamiltonian(ops,
            ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/kjs/.pyenv/versions/scratch/lib/python3.12/site-packages/fqe/fqe_decorators.py", line 104, in build_hamiltonian
    ops_mat[index] = fermionops_tomatrix(term, norb)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/kjs/.pyenv/versions/scratch/lib/python3.12/site-packages/fqe/fqe_decorators.py", line 225, in fermionops_tomatrix
    tensor = numpy.zeros(tensor_dim, dtype=numpy.complex128)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
numpy.core._exceptions._ArrayMemoryError: Unable to allocate 64.0 GiB for an array with shape (16, 16, 16, 16, 16, 16, 16, 16) and data type complex128
@gkc1000
Copy link
Collaborator

gkc1000 commented Nov 17, 2024 via email

@shiozaki
Copy link
Collaborator

shiozaki commented Nov 18, 2024

If I remember correctly if you pass FermonOperator directly to apply, it would create a dense operator, because FQE does not know if your operator is better handled as a sparse operator or not. Can you first create a SparseHamiltonian object explicitly and pass that to apply?

https://github.com/quantumlib/OpenFermion-FQE/blob/master/src/fqe/hamiltonians/sparse_hamiltonian.py

@awhite862 please follow up when you get a chance.

@kevinsung
Copy link
Author

Ah okay this works:

new_wfn = wfn.apply(fqe.get_sparse_hamiltonian(op))

In this case it would be nice to automatically choose to use sparse instead of dense. I'll leave this issue open in case you want to track that, otherwise feel free to close.

@awhite862
Copy link
Contributor

We could potentially add some more sophisticated logic to try to guess the most efficient representation of the operator when it is passed as a FermionOperator. Currently it will only choose to treat the operator a SparseHamiltonian if there are two or fewer terms (in this example there are 4 including the hermitian conjugates). This is a reasonable choice if those terms are 1-body terms, but for many-body terms (like the 4-body terms in this example) it may not be an efficient choice. However, making the most efficient choice in general will be difficult, so it might be reasonable to leave this optimization up to the user.

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

No branches or pull requests

4 participants