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

Compressed Sparse Row (CSR) representation for Hamiltonians and Tensors #2561

Merged
merged 26 commits into from
May 20, 2022

Conversation

AmintorDusko
Copy link
Contributor

@AmintorDusko AmintorDusko commented May 11, 2022

Context:
This PR changes the Pennylane sparse representation to Compressed Sparse Row (CSR) representation.

Description of the Change:
When converting a Hamiltonian to a sparse format, this format will be CSR.
The COO format previously used in all parts of PennyLane was left as an internal format in parts of the code where it will be beneficial.

Benefits:
The expval calculation speed of Sparse Hamiltonians has increased by 20%-30%.

Possible Drawbacks:

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.

@AmintorDusko
Copy link
Contributor Author

AmintorDusko commented May 11, 2022

Some benchmarks below:
dense
sparse

The sparse Hamiltonian representation shows an increase of ~25% in speed with the CSR representation.
As expected, we observe just a slight improvement in time for dense Hamiltonian cases.

@AmintorDusko AmintorDusko changed the title Compressed Sparse Row (CSR) representation for Hamiltonians and Tensors [WIP] Compressed Sparse Row (CSR) representation for Hamiltonians and Tensors May 11, 2022
@AmintorDusko AmintorDusko added the WIP 🚧 Work-in-progress label May 11, 2022
@codecov
Copy link

codecov bot commented May 11, 2022

Codecov Report

Merging #2561 (dfaf3f3) into master (da89442) will not change coverage.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #2561   +/-   ##
=======================================
  Coverage   99.58%   99.58%           
=======================================
  Files         243      243           
  Lines       19596    19596           
=======================================
  Hits        19515    19515           
  Misses         81       81           
Impacted Files Coverage Δ
pennylane/devices/default_qubit.py 100.00% <100.00%> (ø)
pennylane/operation.py 96.73% <100.00%> (ø)
pennylane/ops/qubit/observables.py 100.00% <100.00%> (ø)
pennylane/ops/qubit/qchem_ops.py 100.00% <100.00%> (ø)
pennylane/utils.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update da89442...dfaf3f3. Read the comment docs.

@AmintorDusko AmintorDusko added review-ready 👌 PRs which are ready for review by someone from the core team. and removed WIP 🚧 Work-in-progress labels May 12, 2022
@AmintorDusko AmintorDusko changed the title [WIP] Compressed Sparse Row (CSR) representation for Hamiltonians and Tensors Compressed Sparse Row (CSR) representation for Hamiltonians and Tensors May 12, 2022
@soranjh
Copy link
Contributor

soranjh commented May 12, 2022

The sparse Hamiltonian representation shows an increase of ~25% in speed with the CSR representation. As expected, we observe just a slight improvement in time for dense Hamiltonian cases.

Thanks @AmintorDusko. I am curious how CRS compares with COO in terms of the time needed to create the sparse Hamiltonian matrix and also the size of the sparse matrix.

@AmintorDusko
Copy link
Contributor Author

AmintorDusko commented May 12, 2022

The sparse Hamiltonian representation shows an increase of ~25% in speed with the CSR representation. As expected, we observe just a slight improvement in time for dense Hamiltonian cases.

Thanks @AmintorDusko. I am curious how CRS compares with COO in terms of the time needed to create the sparse Hamiltonian matrix and also the size of the sparse matrix.

Hi @soranjh, thanks for asking.
The time to create the sparse Hamiltonians is essentially the same. Mainly because I kept the COO representation internally to build the Hamiltonian and convert to CSR after the Hamiltonian is built. Scipy conversion from COO to CSR is very efficient.
Regarding the size, CSR, due to its internal structure, will usually be smaller or in the worst case have the COO size.
I'm attaching some images to illustrate what I'm saying.
time
size

Copy link
Contributor

@antalszava antalszava left a comment

Choose a reason for hiding this comment

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

Looks good 🎉 The changes look good, though it would be great to get an extra pair of eyes here before merging.

@AmintorDusko
Copy link
Contributor Author

Looks good tada The changes look good, though it would be great to get an extra pair of eyes here before merging.

Thank you, @antalszava! I will wait for one more approval before merging.

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.

Looks good to me @AmintorDusko, just left two questions.

pennylane/devices/default_qubit.py Outdated Show resolved Hide resolved
pennylane/operation.py Outdated Show resolved Hide resolved
…ennyLaneAI/pennylane into feature/CSR_Hamiltonian_Representation
@AmintorDusko
Copy link
Contributor Author

Thank you, @soranjh, @antalszava and @josh146!

@AmintorDusko AmintorDusko merged commit e32c07d into master May 20, 2022
@AmintorDusko AmintorDusko deleted the feature/CSR_Hamiltonian_Representation branch May 20, 2022 21:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
review-ready 👌 PRs which are ready for review by someone from the core team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants