Using the state vector directly

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.

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

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:

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.