-
Notifications
You must be signed in to change notification settings - Fork 603
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
Add a transform get_unitary_matrix
to obtain matrix representations of circuits
#1462
Comments
Oh! Actually, should we have both? The QNode function can just call this qfunc transform and pass the device wires as an argument. This feels more flexible, since then you can use it even if you don't have a device |
Yes, great idea! 💯 |
I'm interested to look into this :) |
@ingstra fantastic 🌟 Please feel free to ask us any questions you might have! |
Which file should the transforms be defined in? Oh and another question :) |
Hi @ingstra ,
You can put it in a new file in the
Yes we'd like to have the transform work for arbitrary-sized gates. All of the existing gates do have matrix representations available regardless of size that you can use as a starting point (for example, you can do For non-controlled gates, one way to work around this would be to apply SWAPs (but this could get computationally expensive). For controlled gates, there is a trick you can use to construct them using the projectors |
Gotcha, thanks. I got something that works for single-qubit gates now, but ran into a bit of a side-issue. |
Hi @ingstra, indeed! The reason is that For now, could it work to use the |
I see! Thanks. Yes it works :) |
Created an issue to clear this up: |
Status update: I now have a quantum function transform that works for (has been tested on) single-qubit gates, two-qubit controlled gates, and Toffoli with adjacent or nonadjacent wires. My first idea was actually using the One thing I was briefly thinking about was checking the |
I did some more testing today, and it turned out that it was just a fluke everything worked yesterday, but now I think it works (famous last words). Anyway, I started replacing |
Sry for spamming but here's a question: I now have a quantum function transform named |
Hi @ingstra , thanks for all your work on this! 🚀 I think you are correct that there is an issue in the current. BTW you can make a PR whenever you like, happy to quickly review it in an intermediate state (sometimes it's helpful to have just for pointing out where you might have questions). |
Thanks! I created a PR. In get_unitary matrix I copied some code from adjoint.py because it felt appropriate, but I don't really know how the tape stuff works so I don't know if all of it it is necessary/appropriate here. Edit: Oh it seems to fail some tests. Interesting, it was fine when I just ran the tests locally. It seems to be some issue with the |
Thanks @ingstra , I will leave a few comments regarding your questions over in the PR! |
Right now this does not work with a circuit containing a controlled op generated by |
I'm having some problems in
|
In this case since the matrix is legit not available, I'd say don't worry about it. (This should maybe be written up as a separate issue, actually.)
I would need a bit more context, but one thing to note is that PennyLane operators are queued to the underlying tape upon construction, so you cannot apply an already-constructed operation as is done above. You'll need to use the
The identity is coming out because unless the operations are manually applied like this, nothing will be queued. This might solve the problem; let me know if it doesn't (seeing the full function would be helpful here). |
@glassnotes |
Closing as #1609 has been merged in 🎉 |
As suggested in #1458, it would be useful to have a transform to obtain the matrix representation of a quantum function. The transform should look and behave like the following pseudocode (similar to the
draw
orspecs
transform):This transform should be able to process all operations, including controlled gates on non-adjacent wires or wires in reverse order.
Note: this was written as a QNode transform because wire order is important (and we could extract that from the device). However, it could also be written as a tape or quantum function transform where the wire order must be passed explicitly every time.
The functions
compute_matrix_from_ops_one_qubit
andcompute_matrix_from_ops_two_qubit
contain some simple starting code. Once the new transform is implemented, usage of these should be replaced by the transform, as well as matrix computation in other parts of the code (such asSingleExcitation
andDoubleExcitation
tests).List of tasks for completion:
compute_matrix_from_ops_one_qubit
andcompute_matrix_from_ops_two_qubit
;ControlledPhaseShift
SingleExcitationPlus
SingleExcitationMinus
DoubleExcitation
The text was updated successfully, but these errors were encountered: