Using the state vector directly

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[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:
    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)
2 Likes

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?

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

@Dobby

Yes.
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.

Hi Josh, can I do this in the ‘default.gaussian’ backend? I want to output the covariance matrix in a differentiable way. I just tried it and an error says “QuantumFunctionError: Returning the state is not supported”

Hey @Felix_Zhao! Welcome to the forum :smiley:

This isn’t a supported measurement for the default.gaussian device. It sounds like you should take a look at some of our other photonic software package offerings:

If you have further questions, I think it might be best to start a new forum post :slight_smile:

1 Like