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

Reimplement qubit unitary #6986

Open
wants to merge 13 commits into
base: e2e-sparse-default-qubit
Choose a base branch
from

Conversation

JerryChen97
Copy link
Contributor

Context:
Our current sparse repr for QubitUntiary is quite confusing: its matrix() return a sparse matrix by default, if this obj was instantiated by a CSR, which is inconsistent from what we expected for other operators, that matrix() should return a dense and sparse_matrix() gives out the sparse, well separated from each other.

Therefore, a slight re-work is needed to make things clearer. Basically, we will overload the has_matrix and has_sparse from parents and have a more friendly and straightforward usage.

Description of the Change:

Benefits:

Possible Drawbacks:

Related GitHub Issues:

@JerryChen97 JerryChen97 changed the base branch from master to e2e-sparse-default-qubit February 20, 2025 19:10
@JerryChen97 JerryChen97 self-assigned this Feb 20, 2025
@JerryChen97 JerryChen97 marked this pull request as ready for review February 20, 2025 22:07
Co-authored-by: Christina Lee <christina@xanadu.ai>
@AmintorDusko
Copy link
Contributor

@JerryChen97, are these CI tests supposed to fail?
Is there anything you can do to fix that?

@JerryChen97
Copy link
Contributor Author

@JerryChen97, are these CI tests supposed to fail? Is there anything you can do to fix that?

Sorry, forgot to fix them on e2e branch. I'll fix soon

Comment on lines +241 to +242
if sp.sparse.issparse(state):
raise TypeError("State should not be sparse in default qubit pipeline")
Copy link
Contributor

Choose a reason for hiding this comment

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

How would we get a sparse state in the first place?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It won't happen for the whole pipeline and any common users.
This is to block such misuse in our own dev. One previous test fail was caused by such reason and the error was not clear because it only emerges in deeper callstack. Having such blocker would help prevent such scenario.

Comment on lines 199 to 200
if sp.sparse.issparse(U):
return U.toarray()
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm kinda going back and forth about whether or not we should raise MatrixUndefinedError here if U is sparse. I am leaning toward only providing a dense matrix U is dense and only provide a sparse matrix if U is sparse.

Then it's up to whatever consumes the operator to doing any dense-sparse or sparse to dense conversions.

Mind giving that a try and seeing what happens?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Trying here: 7030e31

Copy link
Contributor

Choose a reason for hiding this comment

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

Where the failures just unit tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@albi3ro vastly most are Controlled.sparse_matrix(wire_order=..) not implemented.

Copy link
Contributor Author

@JerryChen97 JerryChen97 Feb 21, 2025

Choose a reason for hiding this comment

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

which is still interesting though. Why before I've never seen them triggered?

Copy link
Contributor

Choose a reason for hiding this comment

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

Why would changing QubitUnitary have changed Controlled tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@albi3ro it seems that the deferred measurement converts some Conditionals into Controlled, and then the Controlled used Controlled(QubitUnitary(...))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@albi3ro it seems that the deferred measurement converts some Conditionals into Controlled, and then the Controlled used Controlled(QubitUnitary(...))

This can be observed by putting break point between results1 and results2 inside tests/devices/qubit/test_simulate.py::TestMidMeasurements::test_simple_dynamic_circuit, at commit 24a8f1b

@JerryChen97
Copy link
Contributor Author

External fail is exactly the same as this morning. Just ignore it until we merge this PR to e2e branch and update there with master

JerryChen97 added a commit that referenced this pull request Feb 21, 2025
**Context:**
In our on-going sparse matrix epic, we used to postponed the full
implementation of controlled ops family due to others of higher
priorities. However, in a related PR
https://github.com/PennyLaneAI/pennylane/actions/runs/13464002433/job/37625640042?pr=6986
it seems inevitable to have the `wire_order` branch of
`Controlled.sparse_matrix` method implemented.

For the sake of traceability, and to prevent some the mentioned PR from
being furthermore spaghettized with high entanglement, we implement the
`wire_order` branch here with unit tests, and watch and fix whatever new
impact introduced as an independent PR instead of hotfix overthere.

**Description of the Change:**
Direct call `expand_matrix` just as what `matrix()` did above
`sparse_matrix`, right before format-convert.
Convert the error-raise test into a consistency-check with
`matrix(wire_order=...)`

**Benefits:**
Fully fledged sparse `Controlled`

Unblock #6986

**Possible Drawbacks:**
Small chance to still have incompatibility in the future, which is also
why we would like to have this separated such that easy to revert.

**Related GitHub Issues:**
[sc-84949]

---------

Co-authored-by: Andrija Paurevic <46359773+andrijapau@users.noreply.github.com>
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.

3 participants