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 function for convert circuit parameters to numpy #3899
Conversation
Codecov Report
@@ Coverage Diff @@
## master #3899 +/- ##
=======================================
Coverage 99.56% 99.56%
=======================================
Files 348 349 +1
Lines 30382 30410 +28
=======================================
+ Hits 30250 30278 +28
Misses 132 132
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
Hello. You may have forgotten to update the changelog!
|
Co-authored-by: Matthew Silverman <matthews@xanadu.ai>
it's as if you magically looked at my local branch and nabbed the changes I made! looks excellent. I want to confirm a thing or two before approving, but I will likely approve today. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks Christina! I think that this looks good. Is there a timeline for when this would replace tape.unwrap
?
Co-authored-by: Mudit Pandey <mudit.pandey@xanadu.ai>
[sc-36752] |
@mudit2812 We are working on a PR to replace the use of |
Co-authored-by: Matthew Silverman <matthews@xanadu.ai>
* convert to numpy parameters transform * tests * update math module * move to transforms folder * measurement test too * Update pennylane/transforms/convert_to_numpy_parameters.py Co-authored-by: Matthew Silverman <matthews@xanadu.ai> * improve tests, copy over trainable_params * Apply suggestions from code review Co-authored-by: Mudit Pandey <mudit.pandey@xanadu.ai> * see also derective and tensor data * fix docstring * Update pennylane/transforms/convert_to_numpy_parameters.py Co-authored-by: Matthew Silverman <matthews@xanadu.ai> * tensor bugfix --------- Co-authored-by: Matthew Silverman <matthews@xanadu.ai> Co-authored-by: Mudit Pandey <mudit.pandey@xanadu.ai>
Currently, we use the
qml.tape.Unwrap
context manager to convert parameters to numpy before sending them to a device.This context manager modifies the input in-place and resets it at the end. It uses
QuantumScript.set_parameters
, which iterates over all operations, modifies them in place, and then checks batching.Notably, it calls
op._check_batching
for every single parameter that gets set, not every operator that has parameter set.This causes massive performance problems for large observables. We are also setting the parameters for operators that might not even need it.
This PR proposes a transform
convert_to_numpy_parameters
that creates a new quantum script with numpy or built-ins parameters. Currently, it is in thedevices.preprocessing
module, as it was designed thinking about new devices, and making it easy for new devices to convert to vanilla numpy parameters.Given the huge performance problems with
Unwrap
, we should consider using this everywhere instead.When using a
Sum
object instead of a Hamiltonian,Unwrap
takes 1min 36seconds, a great deal more time.convert_to_numpy_parameters
takes 8.42 ms, twice the amount of time as forHamiltonian
, but still substantially faster than theUnwrap
case.If we are going to immediately replace
Unwrap
with this instead, we should better consider the location for this conversion utility.