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

StatePrep accepts sparse matrices as input #6863

Merged
merged 57 commits into from
Feb 3, 2025

Conversation

JerryChen97
Copy link
Contributor

@JerryChen97 JerryChen97 commented Jan 20, 2025

Context:

To make PennyLane use sparse matrics, at the atomic level we need to provide at least two features:

  • states in PL can start from sparse
  • necessary operators (e.g. QubitUnitary) have their sprase version along with the default sparse-sparse, sparse-dense multiplication

This Pull Request focus on fulfilling the first.

Note that up to v0.40, PL implements state initialization using class StatePrep from ops/qubit/state_preparation.py. Conceptually, we treated the state preparation as a generalized operation here, although unlike many other operators which are typically differentiable, StatePrep shall be expected as non-differentiable in most of the practical context.

Recall the interface of StatePrep,

class StatePrep(state, wires, pad_with=None, normalize=False, validate_norm=True)

where state is the input values for the initalization, wires is the total number of qubits of the final state, pad_with refers to a specific feature with which the user can use a shorter state of length less than 2**wires and PL will automatically fill the remaining component with the given pad_with. normalize and validate_norm are standard controls for the norm preprocessing of a state-vector.

Revisiting this, we would like to support specific input type of state as sparse matrices. Specifically, for simplicity of the first feature, we just use csr_matrix from SciPy.sparse (Compressed Sparse RowColumn format; check https://docs.scipy.org/doc/scipy/reference/sparse.html for futher details). Meanwhile, note that a nonzero pad_with is clearly against the idea of sparsity since it makes the state dense, so we will disable this option if the previous sparsity of state is given by a user.

Description of the Change:

  • Introduced extra type hint and conditional branch inside the initialization of StatePrep, implementing it overloading
  • Implemented the private method _sparse_statevec_permute to align with the original default StatePrep.state_vector where the input states can be embedded into the given larger wire sets with permuted wire order.
  • check the parent classes for other supported operations, e.g. equal updated: out-of-scope and not necesary. Leave it to future improvement.
  • improvement of the permute algorithm logic

Benefits:

  • Extend the support range of StatePrep by overloading it with new features of sparse matrices.

Possible Drawbacks:

  • Do not support batched states for simplicity of our MVP implementation. Open for discussion whether or not this should be included as one of the necessary requirements.
  • Documentations remain missing for now, because StatePrep is actually facing public users, and it does not make sense to add notes in docs before the successful implementation of the whole workflow. They will be added in the future PR that complets the end-to-end sparse-matrix feature of PennyLane (i.e., at least a measurement can be done, which requires modification on the qubit simulator workflow)

Related GitHub Issues:
[sc-82067]

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.

@JerryChen97 JerryChen97 self-assigned this Jan 24, 2025
@JerryChen97 JerryChen97 added enhancement ✨ New feature or request WIP 🚧 Work-in-progress labels Jan 24, 2025
@JerryChen97 JerryChen97 marked this pull request as ready for review January 27, 2025 15:56
Copy link

codecov bot commented Jan 27, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.59%. Comparing base (759dbe8) to head (397d65f).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #6863   +/-   ##
=======================================
  Coverage   99.59%   99.59%           
=======================================
  Files         480      480           
  Lines       45421    45474   +53     
=======================================
+ Hits        45236    45289   +53     
  Misses        185      185           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@AmintorDusko AmintorDusko self-requested a review January 27, 2025 19:37
@JerryChen97 JerryChen97 added review-ready 👌 PRs which are ready for review by someone from the core team. and removed WIP 🚧 Work-in-progress labels Jan 27, 2025
@JerryChen97
Copy link
Contributor Author

I'll udpate changelog if we are OK with the current implementation, or until we converge at the final implementation

Copy link
Contributor

@AmintorDusko AmintorDusko left a comment

Choose a reason for hiding this comment

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

Hi @JerryChen97! Nice work! I left a few suggestions and comments.
Also, regarding your PR description, I'd like to point out that CSR stands for Compressed Sparse Row. Adding documentation from the beginning is usually a better choice, but I will let the CORE team decide.
Please feel free to tag me again whenever you're ready.
Cheers!

pennylane/ops/qubit/state_preparation.py Show resolved Hide resolved
pennylane/ops/qubit/state_preparation.py Outdated Show resolved Hide resolved
pennylane/ops/qubit/state_preparation.py Outdated Show resolved Hide resolved
pennylane/ops/qubit/state_preparation.py Outdated Show resolved Hide resolved
pennylane/ops/qubit/state_preparation.py Outdated Show resolved Hide resolved
pennylane/ops/qubit/state_preparation.py Outdated Show resolved Hide resolved
pennylane/ops/qubit/state_preparation.py Outdated Show resolved Hide resolved
pennylane/ops/qubit/state_preparation.py Outdated Show resolved Hide resolved
pennylane/ops/qubit/state_preparation.py Outdated Show resolved Hide resolved
pennylane/ops/qubit/state_preparation.py Outdated Show resolved Hide resolved
Copy link
Contributor

@AmintorDusko AmintorDusko left a comment

Choose a reason for hiding this comment

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

Good job!

Copy link
Contributor

@PietropaoloFrisoni PietropaoloFrisoni left a comment

Choose a reason for hiding this comment

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

🚀

doc/releases/changelog-dev.md Outdated Show resolved Hide resolved
pennylane/ops/qubit/state_preparation.py Outdated Show resolved Hide resolved
pennylane/ops/qubit/state_preparation.py Outdated Show resolved Hide resolved
pennylane/ops/qubit/state_preparation.py Outdated Show resolved Hide resolved
Copy link
Contributor

@PietropaoloFrisoni PietropaoloFrisoni left a comment

Choose a reason for hiding this comment

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

Nothing else for me except for the last suggestions to the doc. Nice work!

pennylane/ops/qubit/state_preparation.py Outdated Show resolved Hide resolved
pennylane/ops/qubit/state_preparation.py Outdated Show resolved Hide resolved
pennylane/ops/qubit/state_preparation.py Outdated Show resolved Hide resolved
Co-authored-by: Pietropaolo Frisoni <[email protected]>
@JerryChen97 JerryChen97 enabled auto-merge (squash) February 3, 2025 21:40
@JerryChen97 JerryChen97 disabled auto-merge February 3, 2025 22:41
…-sparse-matrices-as-input' into `StatePrep`-accepts-sparse-matrices-as-input
@JerryChen97 JerryChen97 enabled auto-merge (squash) February 3, 2025 22:55
@JerryChen97 JerryChen97 merged commit 8cefc9a into master Feb 3, 2025
46 checks passed
@JerryChen97 JerryChen97 deleted the `StatePrep`-accepts-sparse-matrices-as-input branch February 3, 2025 23:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement ✨ New feature or request 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.

4 participants