Using the state vector directly

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 :slightly_smiling_face:

1 Like

Thanks a lot for your responses :+1::+1:

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. :frowning: that’s because the Qnode must return a measurement :frowning:

That particular plugin uses tensorflow, so you’ll need to try the tensorflow interface

I have done that sir @nathan

Hi @kareem_essafty,

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

Hi @kareem_essafty,

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.


1 Like

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:

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 :slight_smile:

Will it make the state vector differentiable?
Or you’re speaking about the jax thread thing? :sweat_smile:

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

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 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('', wires=2)

def circuit(params):
    qml.RX(params[0], wires=0)
    qml.RX(params[1], 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:
    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)
1 Like

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

Hi @kareem_essafty,

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