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

Accelerate Jordan-Wigner transform #4046

Merged
merged 14 commits into from May 8, 2023
Merged

Accelerate Jordan-Wigner transform #4046

merged 14 commits into from May 8, 2023

Conversation

vincentmr
Copy link
Contributor

@vincentmr vincentmr commented Apr 27, 2023

Before submitting

Please complete the following checklist when submitting a PR:

  • All new features must include a unit test.
    If you've fixed a bug or added code that should be tested, add a test to the
    test directory!

  • All new functions and code must be clearly commented and documented.
    If you do make documentation changes, make sure that the docs build and
    render correctly by running make docs.

  • Ensure that the test suite passes, by running make test.

  • Add a new entry to the doc/releases/changelog-dev.md file, summarizing the
    change, and including a link back to the PR.

  • The PennyLane source code conforms to
    PEP8 standards.
    We check all of our code against Pylint.
    To lint modified files, simply pip install pylint, and then
    run pylint pennylane/path/to/file.py.

When all the above are checked, delete everything above the dashed
line and fill in the pull request template.


Context:
Jordan-Wigner transforms can take a while to compute. Most of the time is spent in __init__ of the various Pauli gates.

Description of the Change:
Cache Pauli gate objects.

Benefits:
Faster transforms (e.g. 160->38 sec. for generating the molecular Hamiltonian of an H2 molecule in the "cc-pvdz" basis).

Possible Drawbacks:
Deepcopy necessary?

Related GitHub Issues:

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

@codecov
Copy link

codecov bot commented Apr 27, 2023

Codecov Report

Merging #4046 (608cd8a) into master (1b6706b) will decrease coverage by 0.01%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master    #4046      +/-   ##
==========================================
- Coverage   99.73%   99.73%   -0.01%     
==========================================
  Files         345      345              
  Lines       30787    30784       -3     
==========================================
- Hits        30704    30701       -3     
  Misses         83       83              
Impacted Files Coverage Δ
pennylane/pauli/utils.py 100.00% <100.00%> (ø)
pennylane/qchem/observable_hf.py 98.83% <100.00%> (-0.07%) ⬇️

@vincentmr vincentmr changed the title Cache Pauli gate objects. Accelerate Jordan-Wigner transform Apr 27, 2023
@vincentmr vincentmr marked this pull request as ready for review April 27, 2023 19:43
@vincentmr vincentmr requested a review from soranjh April 27, 2023 19:43
Copy link
Contributor

@soranjh soranjh left a comment

Choose a reason for hiding this comment

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

Thanks @vincentmr for the improvement! Left a suggestion and also a timing comparison for computing molecular Hamiltonians (the molecules are H2, H3+, LiH, H2O).

image

pennylane/qchem/observable_hf.py Outdated Show resolved Hide resolved
@vincentmr
Copy link
Contributor Author

Thanks @vincentmr for the improvement! Left a suggestion and also a timing comparison for computing molecular Hamiltonians (the molecules are H2, H3+, LiH, H2O).

This is nice, thanks for the timing data.

@vincentmr
Copy link
Contributor Author

For the record, after the current changes, the biggest bottlenecks are _pauli_mult called in jordan_wigner and simplify called in qubit_observable.

@soranjh
Copy link
Contributor

soranjh commented May 1, 2023

For the record, after the current changes, the biggest bottlenecks are _pauli_mult called in jordan_wigner and simplify called in qubit_observable.

Thanks @vincentmr. The qchem module will be upgraded to use more efficient Pauli arithmetic.

@vincentmr vincentmr requested a review from Jaybsoni May 1, 2023 21:55
pennylane/pauli/utils.py Outdated Show resolved Hide resolved
Copy link
Contributor

@obliviateandsurrender obliviateandsurrender left a comment

Choose a reason for hiding this comment

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

Thanks @vincentmr, overall looks good! Does caching have any ill-effect on memory profiles for building larger observables? It might be nice to have some memory-benchmarks too.

@vincentmr
Copy link
Contributor Author

Thanks @vincentmr, overall looks good! Does caching have any ill-effect on memory profiles for building larger observables? It might be nice to have some memory-benchmarks too.

Calling _get_pauli_map for 4 up to 32 wires, the time for the first call grows linearly, but then it is pretty much constant.

mprof: Sampling memory every 0.1s
running new process
time(4)=0.0002491474151611328
time(4)=4.76837158203125e-07
time(8)=0.00027823448181152344
time(8)=7.152557373046875e-07
time(12)=0.0004432201385498047
time(12)=4.76837158203125e-07
time(16)=0.0005674362182617188
time(16)=4.76837158203125e-07
time(20)=0.0006365776062011719
time(20)=4.76837158203125e-07
time(24)=0.0007550716400146484
time(24)=4.76837158203125e-07
time(28)=0.0008456707000732422
time(28)=4.76837158203125e-07
time(32)=0.0014255046844482422
time(32)=9.5367431640625e-07

There is no noticeable differences between the memory profiles for any wire count. They all look like (on my laptop)
Figure_1

Co-authored-by: Utkarsh <utkarshazad98@gmail.com>
Copy link
Contributor

@obliviateandsurrender obliviateandsurrender left a comment

Choose a reason for hiding this comment

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

:shipit:

@vincentmr vincentmr merged commit 90de65d into master May 8, 2023
43 checks passed
@vincentmr vincentmr deleted the optim/jordan_wigner branch May 8, 2023 16:15
vincentmr added a commit that referenced this pull request May 8, 2023
@vincentmr vincentmr mentioned this pull request May 8, 2023
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.

None yet

3 participants