Using the state vector directly

hi,
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 :blush:

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

Hi @kareem_essafty,

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.

Hey,
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.

Hi @kareem_essafty,

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=...)

@qml.qnode(tf_dev)
def some_circuit():
    ...

some_circuit(...) # you need to execute the circuit to update the state

state = tf_dev._state # this should be differentiable
1 Like

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:

@nathan
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.

Nathan

1 Like

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

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