Using the state vector directly

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

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)

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

@josh Tried importing from pennylane.qnodes import PassthruQNode but it now says there is not module named qnodes under pennylane. Has this feature been removed or moved elsewhere? Let me know. Thank you!

Hi @Pavan, since this reply was made, the PassthruQNode was integrated properly into PennyLane :slight_smile: In the latest version of PennyLane, the following should work:

import tensorflow as tf
import pennylane as qml

dev = qml.device('default.qubit', wires=2)

@qml.qnode(dev, diff_method="backprop")
def circuit(params):
    qml.RX(params[0], wires=0)
    qml.RX(params[1], wires=1)
    qml.CNOT(wires=[0, 1])
    return qml.state()

params = tf.Variable([0.3, 0.1])

with tf.GradientTape() as tape:
    state = circuit(params)

grad = tape.gradient(state, params)

print("State:", state)
print("Gradient:", grad)

Note that:

  • We specify diff_method="backprop", which indicates that the quantum gradient is computed using backprop.

  • We use qml.state() to directly return the state from the QNode.

1 Like

Hi @josh,

I am curious how to get states from qiskit real hardware. Now I could get the state from the QNode and qiskit ‘statevector_simulator’ using dev._state. But when I try to access the states of ‘qasm_simulator’ or IBM Q hardware, this does not work.

Are there any suggestions on this issue?

Thank you!

Probably, no one can get state vector from real QPUs.

  • State vecotr is not observable.
  • Measurement itself can change the state vector.

Even if we can measure an element of state vector of n-qubits, it requires at least 2^{n} times measurement and quantum speed up would be vanished. This is crusial point in case of HHL algorithm.

Thank you @Kuma-quant! I agree with this point. Actually, I am working on VQLS and therefore need the state vector. I was thinking about whether I should follow what HHL has done. And it seems, the answer is yes :frowning:


VQLS and HHL embed the solution, x = A^-1*b, into a state vector.
The x can’t be measured directly.
Therefore, the aim of the algorihms is that, calculating and extracting “partial” information of x.
For example, suppose H is an observable, <x|H|x> can be extracted by measurement. All we have to do is just add the measurement process of H at the end of HHL / VQLS.

Since the information we can acsess is limited,
HHL/VQLS is would not what you expected…
They are tricky.
But some applications have been proposed.