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

MPS emulator for LUCJ circuits #334

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

Conversation

bartandrews
Copy link
Contributor

@bartandrews bartandrews commented Oct 25, 2024

The aims for this pull request:

  • Add a method to convert a molecular Hamiltonian to an MPO.
  • Add a method that can construct an LUCJ circuit as an MPS.
  • Create a "how-to guide" jupyter notebook.

@bartandrews bartandrews marked this pull request as draft October 27, 2024 09:52
@bartandrews bartandrews changed the title Add method to convert MolecularHamiltonian to MPO MPS emulator for LUCJ circuits Oct 27, 2024
@bartandrews bartandrews marked this pull request as ready for review October 29, 2024 14:51
@bartandrews
Copy link
Contributor Author

bartandrews commented Oct 29, 2024

Note that the mkl and mkl-devel packages (and cython) needs to be installed in the environment for TeNPy to be compiled against MKL for faster performance, see https://tenpy.readthedocs.io/en/latest/install/extra.html I'm not sure of the best way to incorporate this. It works fine without it, but power users need to make sure that TeNPy is compiled optimally.

Note that by default, i.e. after running pip install physics-tenpy, the cython code will be compiled, but the numpy/scipy versions will be linked against OpenBLAS, and not MKL. It seems debatable whether MKL is better than OpenBLAS. If we did want MKL, it is better to use conda rather than pip, since conda ships numpy/scipy packages linked against MKL (and these packages are maintained). pip does have packages like intel-numpy and intel-scipy but these packages are out-of-date (i.e. do not support the latest python version).

I think that this issue transcends tenpy and becomes a more general question of whether or not to link numpy/scipy against MKL, which is a matter of personal preference. Therefore, I suggest that we leave this as is.

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.

Thank you Bart! I'll begin with some high-level comments that will involve some code restructuring.

python/ffsim/hamiltonians/molecular_hamiltonian.py Outdated Show resolved Hide resolved
python/ffsim/tenpy/circuits/gates.py Outdated Show resolved Hide resolved
python/ffsim/tenpy/circuits/lucj_circuit.py Outdated Show resolved Hide resolved
python/ffsim/tenpy/circuits/lucj_circuit.py Outdated Show resolved Hide resolved
@bartandrews
Copy link
Contributor Author

Thank you Bart! I'll begin with some high-level comments that will involve some code restructuring.

Thank you for reviewing this! 😃 I will implement these changes asap

@bartandrews bartandrews marked this pull request as draft November 15, 2024 14:24
python/ffsim/tenpy/hamiltonians/molecular_hamiltonian.py Outdated Show resolved Hide resolved
python/ffsim/tenpy/hamiltonians/molecular_hamiltonian.py Outdated Show resolved Hide resolved
python/ffsim/tenpy/hamiltonians/molecular_hamiltonian.py Outdated Show resolved Hide resolved
python/ffsim/tenpy/hamiltonians/molecular_hamiltonian.py Outdated Show resolved Hide resolved
python/ffsim/tenpy/gates/abstract_gates.py Outdated Show resolved Hide resolved
python/ffsim/tenpy/gates/basic_gates.py Outdated Show resolved Hide resolved
python/ffsim/tenpy/gates/diag_coulomb.py Outdated Show resolved Hide resolved
python/ffsim/tenpy/hamiltonians/molecular_hamiltonian.py Outdated Show resolved Hide resolved
@bartandrews bartandrews marked this pull request as ready for review November 23, 2024 11:11
abs(mpo_expectation.imag),
rtol=1e-05,
atol=1e-08,
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't the expectation values be the same? Why do you only compare the absolute values of the real and imaginary parts?

Copy link
Contributor Author

@bartandrews bartandrews Dec 7, 2024

Choose a reason for hiding this comment

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

Yes, I also think that the expectation values should be the same. The diagonal matrix elements are exactly the same. For the off-diagonal matrix elements, sometimes there is a minus sign for either the real or imaginary part. I tried tinkering with the Hamiltonian to fix this, however I really don't understand why this is... 😕 If you have any ideas, please do let me know. I was planning to get back to it this week.

# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""Tests for the TeNPy basic gates."""
Copy link
Collaborator

Choose a reason for hiding this comment

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

I see that in the gate tests, you do the following:

  1. Apply the gate to a product state as a state vector
  2. Apply the gate to a product state as an MPS
  3. Check that the expectation value on a molecular Hamiltonian matches in both cases.

Is it possible to retrieve the state vector amplitudes from the MPS? If so, then we don't need to introduce a Hamiltonian. We can just directly check that amplitudes stored by the MPS match the exact ones.

Another question is whether we can apply the evolution not to a product state, but to a random state instead. The basic gates often have trivial action on a product state. For example, applying a Givens rotation between two occupied orbitals doesn't do anything.

Copy link
Contributor Author

@bartandrews bartandrews Dec 9, 2024

Choose a reason for hiding this comment

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

It is possible to convert an MPS to statevector using the mps_to_full function. Yes, I agree that we should remove the MolecularHamiltonian from these gate tests, and keep the tests for the molecular Hamiltonian separate.

Generating a completely random MPS is non-trivial, since a generic random state from Hilbert space will have volume law entanglement, whereas MPS are good at representing states with area law entanglement. I presume that you mean a completely random state, and not just a random product state? One solution is to start with a random product state and then perform a TEBD RandomUnitaryEvolution to introduce some short-range entanglement. This will not result in a completely random state but it will be entangled to as much as the computational cost / bond dimension will allow.

Copy link
Collaborator

Choose a reason for hiding this comment

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

A uniformly random state would be ideal, but perhaps unnecessary. We should be able to represent such a state for small test sizes though, right? You can generate a uniformly random state vector using ffsim.random.random_state_vector. Is it possible to convert such a state vector into an MPS using Tenpy? Otherwise, I think your idea of generating a random evolution should be fine.

Copy link
Contributor Author

@bartandrews bartandrews Dec 9, 2024

Choose a reason for hiding this comment

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

Yes, for small system sizes you may be able to convert a completely random statevector into an MPS using the full_to_mps function. However, the bond dimension will blow up quite quickly. I will try this in the first instance and if it's not tractable, I will try the random unitary evolution approach.

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.

2 participants