Closed iamlucaswolf closed 3 years ago
Last issue for today, promise! 😅
No need to worry --- issue reporting is great, it's improving the library one issue at a time 🙂
This issue is actually very related to #944. What is happening here, is that in non-tape mode, we do not support TF backprop in non-tape mode. Switching over to tape mode, and a similar model to your example now runs:
import pennylane as qml
import tensorflow as tf
import numpy as np
tf.keras.backend.set_floatx('float64')
qml.enable_tape()
device = qml.device('default.qubit.tf', wires=4)
@qml.qnode(device, interface="tf", diff_method="backprop")
def circuit(inputs, thetas):
for idx, bit in enumerate(inputs):
qml.RX(np.pi * bit, wires=idx)
qml.RZ(np.pi * bit, wires=idx)
for theta in thetas:
qml.RX(theta[0], wires=0)
qml.RX(theta[1], wires=1)
qml.RY(theta[2], wires=2)
qml.RZ(theta[3], wires=3)
qml.CNOT(wires=[0, 1])
qml.CNOT(wires=[1, 2])
qml.CNOT(wires=[2, 3])
qml.CNOT(wires=[3, 0])
return [qml.expval(qml.PauliZ(i)) for i in range(4)]
layer = qml.qnn.KerasLayer(circuit, weight_shapes={'thetas': (2, 4)}, output_dim=4)
with tf.GradientTape() as tape:
res = layer(tf.constant([[1, 0, 0, 0]], dtype=tf.float64))
grad = tape.gradient(res, layer.trainable_weights)
print("Output:", res)
print("Gradient:", grad)
This gives output
Output: tf.Tensor([[-0.58071214 -0.624736 0.48711172 -0.40601237]], shape=(1, 4), dtype=float64)
Gradient: [<tf.Tensor: shape=(2, 4), dtype=float64, numpy=
array([[ 5.03389399e-02, -1.17089383e-02, -5.86374458e-01,
5.55111512e-17],
[-3.53413082e-01, -6.29945412e-01, 5.84276547e-01,
0.00000000e+00]])>]
@iamlucaswolf since this issue is similar to #944, I might close this one in favour of the other one.
Last issue for today, promise! :sweat_smile:
Issue description
So I have my little circuit that I run on a
DefaultQubitTF
device...... and executing the circuit works just fine.
But once I wrap this in a
KerasLayer
:The same happens when using
tf.constant
instead ofnp.array
as input data. I also restarted the Jupyter kernel between all these calls to make sure that the layer is in a valid state.Am I missing something here?
Expected behavior: (What you expect to happen) N/A
Actual behavior: (What actually happens) N/A
Reproduces how often: (What percentage of the time does it reproduce?) Always.
System information: (post the output of import pennylane as qml; qml.about())
Name: PennyLane Version: 0.13.0 Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc. Home-page: https://github.com/XanaduAI/pennylane Author: None Author-email: None License: Apache License 2.0 Location: /home/iamlucaswolf/.cache/pypoetry/virtualenvs/quantum-rl-eji9J1nt-py3.8/lib/python3.8/site-packages Requires: semantic-version, appdirs, autograd, numpy, toml, scipy, networkx Required-by: Platform info: Linux-5.4.0-53-generic-x86_64-with-glibc2.27 Python version: 3.8.6 Numpy version: 1.18.5 Scipy version: 1.5.4 Installed devices:
default.gaussian (PennyLane-0.13.0) default.mixed (PennyLane-0.13.0) default.qubit (PennyLane-0.13.0) default.qubit.autograd (PennyLane-0.13.0) default.qubit.tf (PennyLane-0.13.0) default.tensor (PennyLane-0.13.0) default.tensor.tf (PennyLane-0.13.0)
Source code and tracebacks
Stacktrace from (1)
Stacktrace from (2)