recently i was trying to return the dev._state from the quantum node and I learnt that this can’t be done. so, in the cost function for example I started to directly manipulate the state vector and i ignored the output from the qnode. this made autograde produce warnings that the output is not correlated with the circuit and I understood where i made the mistake. so my question here is: Is there any way that I can benefit from the dev._state value let’s say for example to extract the quantum Hilbert space or even create a self-attention layer from the state vector itself just as a direct application?
thanks for the new release
any help about this please?
Hi @kareem_essafty! With PennyLane v0.8, you can call
dev.state to get the statevector from the device. However, note that this will not be differentiable, unlike items returned from QNodes!
hi @josh, that’s exactly my problem i need to differentiate the output with state vector is there any way i could hack this into pennylane? your help will actually save me a lot of time
If you want access to just one probability (or a small set of probabilities), you could just make an appropriate measurement of a Pauli operator, then readjust to convert these to a probability. Doing it that way would make it differentiable.
I am bit confused. Or may be I explained myself incorrectly. I just want the qnode to return the state vector and the differentiation happens as with any other observable. The reason for this is that I want to use the quantum state directly inside a classical neural network.
As a short-term workaround, you might be able to get what you are looking for (a differentiable quantum state) by using the
default.tensor.tf plugin (which runs a simulation in tensorflow) and examining
dev._state. Note that it requires installing tensorflow and tensornetwork libraries separately to use this.
tf_dev = qml.device('default.tensor.tf, wires=...)
some_circuit(...) # you need to execute the circuit to update the state
state = tf_dev._state # this should be differentiable
Is it like the ket vector in strawberryfields and tensorflow? If it’s the same then that’s super great.
Yes, but in this case a qubit statevector/let
Thanks a lot for your responses
Unfortunately it didn’t give me what i hoped for. i always get there are no gradients for “variable 0” or “phi” or when I use autograd it says the output doesn’t seem to be related to the input. that’s because the Qnode must return a measurement
That particular plugin uses tensorflow, so you’ll need to try the tensorflow interface
I have done that sir @nathan
In order to help further, it would be good to have a minimal (non-)working code example of what you’re trying to do.
@nathan in this colab notebook you will find a simple circuit that i’m trying to optimize its Ry parameter so that the final state would be entangled. I know that I can use a hermitian measurement to achieve the same result but i need the state vector in a similar context.
sorry about not including a working code.
I had a chance to look into this today. Thanks for providing the example.
It looks like you can’t directly get a differentiable quantum state using the method I described above (my hope that it would automatically work, unfortunately, was too optimistic). There’s no fundamental reason PennyLane couldn’t support this, but we’d have to upgrade the code to explicitly make it work.
@josh can maybe comment more, but I think the reason is we need to register custom gradients with TensorFlow in order for things to be differentiable. We’ve registered custom gradients for measurements like
expval, but it looks like we’ll have to manually register them for
dev.state as well.
Thank you so much for your response. Just point out where to start and I believe I will manage. I don’t want to annoy any member of the team or delay them. Just your guidelines and I will start working. @nathan @josh
I recommend you check out the existing interfaces located here: pennylane/pennylane/interfaces at master · PennyLaneAI/pennylane · GitHub
See if you can first hack together even a semi-working jax version. If you manage to get that working, we can support you in moving it from a hack to a full-fledged feature
Will it make the state vector differentiable?
Or you’re speaking about the jax thread thing?