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

[WIP] [experimental] mps simulator backend #3583

Closed
wants to merge 22 commits into from
Closed

Conversation

Qottmann
Copy link
Contributor

@Qottmann Qottmann commented Dec 27, 2022

check out the demo.ipynb (or for always the latest version go "Files changed" => "demo.ipynb" => "View file")

Rough prototype, likely wont become a PennyLane feature.
A simple MPS simulator to showcase this approach's benefits.

  • Ease of use: user does not need to think about optimal contraction strategies
  • Well established: can rely on 20+ years of algorithms that still form the state of the art in many areas of computational quantum physics
  • Allow for approximations (unlike full contraction simulators)
  • Single parameter to control the degree of the approximation (bond dimension, can be used like a black box)
  • Polynomial scaling in the system size ($L \chi^2 2$ coefficients to store)
    image
  • Faithfully simulate shallow 50 qubits circuits in ~2 minutes on laptop CPUs (no HPC knowledge necessary)
    image
    [ $D$ depth of circuit, $\chi$ bond dimension, increased to see if the result saturates (as no exact result is known for such a large system size!). For $D=13$ no saturation yet, so one would need to crank up bond dimension a little further.]

The purpose of this PR is exploration and prototyping. I.e. this is a brute force prototype written in basic numpy, so dont expect clean nor optimized code. Though most theory is already well established, there are still some choices to be made. In particular how to treat long-range interactions. My current preference is writing long-distance gates as MPOs (see #3583 (comment) as well as iTensor forum discussion)

In particular things that may be improved:

  • Pre-compute / hardcode 2-site decomposition of standard 2-site gates like CNOT
  • Truncate MPO bond dimension (currently fixed to its max, 4)
  • Combine MPOs into one bigger MPO (instead of consecutively applying MPO-MPS contractions and restoring MPS) Needs exploration if actually better
  • Any room for optimizing all the seemingly pointless identity contractions in the long-range cases?
  • Allow arbitrary site gates (minor improvement to current implementation, just wasnt priority so far)
  • Specifically for CNOT, there might be some clever ways of just permuting some of the indices? (needs exploration)
  • Expval capabilities still limited to Hamiltonians basically
  • Overall code quality, thorough testing, integrating with the latest device API (this is based on a veeery old prototype)

@Qottmann Qottmann added the do not merge ⚠️ Do not merge the pull request until this label is removed label Dec 27, 2022
@github-actions
Copy link
Contributor

Hello. You may have forgotten to update the changelog!
Please edit doc/releases/changelog-dev.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

@Qottmann

This comment was marked as outdated.

@Qottmann

This comment was marked as outdated.

Utheta = np.tensordot(U_site, theta, axes=([1], [1])) # i [i*], vL [i] vR
Utheta = np.transpose(Utheta, [1, 0, 2]) # i vL vR >> vL i vR

# no truncation (unsure atm)
Copy link
Contributor Author

@Qottmann Qottmann Dec 28, 2022

Choose a reason for hiding this comment

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

I'm a bit confused here, do I need to do any SVD for single site updates? In single-site DMRG yes, but here this is different. Maybe @chaeyeunpark knows about this?

(tests with full state vector simulator agree atm)

Copy link
Contributor

Choose a reason for hiding this comment

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

I will have a look in the new year. I also want to tag @ikurecic here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I know now it is wrong, curious to know how to do it for single site (atm I am just forcing a two-site update)

@Qottmann

This comment was marked as outdated.

# if you get an error message "LinAlgError: SVD did not converge",
# uncomment the following line. (This requires TeNPy to be installed.)
# from tenpy.linalg.svd_robust import svd # (works like scipy.linalg.svd)

from tenpy.linalg.svd_robust import svd
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This might be annoying for people to test, I needed it for some examples that wouldnt converge otherwise

Copy link
Contributor

Choose a reason for hiding this comment

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

Few suggestions here. If this SVD algorithm (in tenpy) is not difficult, we can make our own version. Otherwise, we should make tenpy an optional dependency. It is still strange that svd in scipy is not robust enough.

@Qottmann Qottmann marked this pull request as draft March 27, 2023 10:27
@Qottmann
Copy link
Contributor Author

Qottmann commented Mar 28, 2023

Latest update lets you construct an MPO operator for a long distance 2-site operators (e.g. CNOT(1, 10)) (generalization to multi-site operators straight forward) and apply MPO-MPS products to restore an MPS (no truncation, so the new bond dimension is $4\chi$ since the MPO has bond dim 4). New update (see below) now with contraction and explicit testing

@Qottmann
Copy link
Contributor Author

Qottmann commented Mar 30, 2023

Integration of MPO-MPS contraction complete.

Added some basic explicit testing for my own sanity, see pennylane/devices/tests/test_mpssim.py

capabilities/restrictions are currently:

  • Arbitrary single and two-site operations
  • Only local expectation values of at most two sites

The latter point is very limiting at the moment, but I dont think I will manage to implement something more general here. A workaround and what I am currently doing is outputting the mps via measurement = qml.state() and then performing the mps methods SimpleMPS.site_expectation_value() and SimpleMPS.bond_expectation_value()

Edit: Support expval of Hamiltonians now, problem is that actually this old version of the default numpy simulator is not supporting Hamiltonian expvals.

@albi3ro albi3ro deleted the branch device_api_redesign January 19, 2024 00:57
@albi3ro albi3ro closed this Jan 19, 2024
@trbromley trbromley deleted the mpssim branch March 7, 2024 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
do not merge ⚠️ Do not merge the pull request until this label is removed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants