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.
any updates sir? @nathan
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: https://github.com/XanaduAI/pennylane/tree/master/pennylane/interfaces
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?
I am talking about the state vector if it could be differentiable and how can I do it? @nathan
Sorry, replied to the wrong subject. If you want to try hacking in a differentiable state vector, you’ll have to inspect the code of https://github.com/XanaduAI/pennylane/blob/master/pennylane/beta/plugins/default_tensor_tf.py
Unfortunately, I can’t really provide you much more specific guidance beyond what I’ve already written above (about having to register custom gradients of the
_state function with Tensorflow)
Hi @kareem_essafty, the master version of PennyLane on GitHub has a new feature called the
PassthruQNode. Using this QNode, with the
default.tensor.tf device, should do what you want. For example, consider the following:
import tensorflow as tf import pennylane as qml from pennylane.qnodes import PassthruQNode dev = qml.device('default.tensor.tf', wires=2) def circuit(params): qml.RX(params, wires=0) qml.RX(params, wires=1) qml.CNOT(wires=[0, 1]) return qml.expval(qml.PauliZ(0)) qnode = PassthruQNode(circuit, dev) params = tf.Variable([0.3, 0.1]) with tf.GradientTape() as tape: tape.watch(params) qnode(params) state = dev._state grad = tape.gradient(state, params) print("State:", state) print("Gradient:", grad)
This gives the output:
State: tf.Tensor( [[ 0.98753537+0.j 0. -0.04941796j] [-0.00746879+0.j 0. -0.14925138j]], shape=(2, 2), dtype=complex128) Gradient: tf.Tensor([-0.09933467 -0.09933467], shape=(2,), dtype=float32)
Thanks @josh! The part about explicitly declaring it to be a
PassThruQNode was the missing piece of my earlier suggestion
You saved me. Thank you very much
I had to use qiskit directly and compute everything from the scratch
Glad we could help. Just for clarity, do you mean that you had previously been using qiskit directly and computing everything by hand, but now you can automate that process using the suggestions above?
Yeah I used qiskit within keras layers and also specified the gradient functions just to use the state vector and differentiate it with respect to the loss