Open eddddddy opened 2 years ago
I believe this will be fixed with the new operator arithmetic:
>>> s_prod = qml.s_prod(tf.Variable(0.3), qml.PauliX(0))
>>> qml.matrix(s_prod)
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[0. , 0.3],
[0.3, 0. ]], dtype=float32)>
However I just realised that we still need to support the dunder methods for all interfaces.
That's great! Is the above code also differentiable in the expected way? If so, then it's definitely something I can replace in my code to get the behaviour I want.
That's great! Is the above code also differentiable in the expected way? If so, then it's definitely something I can replace in my code to get the behaviour I want.
@albi3ro or @Jaybsoni might know better.
import tensorflow as tf
dev = qml.device("default.qubit", wires=5)
@qml.qnode(dev, interface="tensorflow", diff_method='backprop')
def circuit(s):
return qml.expval(qml.s_prod(s, qml.PauliZ(0)))
x = tf.Variable(2.0)
with tf.GradientTape() as tape:
res = circuit(x)
tape.gradient(res, [x])
Works absolutely fine.
Yes I think this is another issue with Hamiltonian
, it uses this sparse representation by default. This is a problem because the I don't think the logic is compatible with any other interface other then scipy sparse arrays!
Expected behavior
qml.matrix
should respect the framework-specific tensors used to construct theqml.Hamiltonian
, and return the matrix representation of that Hamiltonian in the same framework. For example, executing the following code should yield a TensorFlow tensor:Actual behavior
The code above instead produces a NumPy array:
Additional information
The matrix representation of the Hamiltonian is computed in
qml.utils.sparse_hamiltonian
, which uses a SciPy sparse matrix data structure and thus converts the framework-specific tensors into NumPy arrays.Source code
No response
Tracebacks
No response
System information
Existing GitHub issues