Hi,
I was trying the diff-method = "adjoint"
with AmplitudeEmbedding
but it does not seem to be working (error-attached screenshot)
however, it works fine with AngleEmbedding
. And backprop
is working with both.
Below is my code:
n_train = 10000
n_test=3000
mnist_dataset = keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist_dataset.load_data()
x_train = x_train[:n_train]
y_train = y_train[:n_train]
x_test = x_test[:n_test]
y_test = y_test[:n_test]
x_train, x_test = (x_train / 255.0), (x_test / 255.0)
x_train = x_train.reshape(-1, 784) # 784 = 28x28
x_test = x_test.reshape(-1, 784)
x_test.shape
n_qubits = 1
dev = qml.device("default.qubit", wires=n_qubits)
@qml.qnode(dev, interface = "tf", diff_method="adjoint")
def qnode(inputs, weights):
qml.templates.AmplitudeEmbedding(inputs, wires=range(n_qubits), pad_with=0., normalize = True)
# qml.templates.AngleEmbedding(inputs, wires=range(n_qubits))
qml.templates.BasicEntanglerLayers(weights, wires=range(n_qubits),rotation = qml.RY)
# qml.templates.StronglyEntanglingLayers(weights, wires=range(n_qubits))
return [qml.expval(qml.PauliZ(wires=i)) for i in range(n_qubits)]
n_layers = 1
# weight_shapes = {"weights": (n_layers, n_qubits, 3)}
weight_shapes = {"weights": (n_layers, n_qubits)}
# re-define the layers
qlayer_1 = qml.qnn.KerasLayer(qnode, weight_shapes, output_dim=n_qubits)
qlayer_2 = qml.qnn.KerasLayer(qnode, weight_shapes, output_dim=n_qubits)
qlayer_3 = qml.qnn.KerasLayer(qnode, weight_shapes, output_dim=n_qubits)
qlayer_4 = qml.qnn.KerasLayer(qnode, weight_shapes, output_dim=n_qubits)
inputs = tf.keras.Input(shape=(784,))
x = tf.keras.layers.Dense(4)(inputs)
x_1, x_2, x_3, x_4 = tf.split(x, 4, axis=1)
x_1 = qlayer_1(x_1)
x_2 = qlayer_2(x_2)
x_3 = qlayer_3(x_3)
x_4 = qlayer_4(x_4)
x = tf.concat([x_1, x_2, x_3, x_4 ], axis=1)
outputs = tf.keras.layers.Dense(10, activation="softmax")(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
from tensorflow.keras.optimizers import SGD, Adam
opt = Adam(learning_rate=0.01)
model.compile(opt, loss="sparse_categorical_crossentropy", metrics=["accuracy"])
history = model.fit(x_train, y_train, epochs=100, batch_size=16, validation_data=(x_test, y_test))
Is the model above computes quantum gradients as well and optimize the loss, since I can not see any significant difference in performance when compared with its classical counterpart, except the convergence time of hybrid model (above) is significantly higher that classical one.
Moreover, if we increase the dataset size (currently MNIST (10k,3k)), should the percent increase of convergence time of hybrid model should be less than that of classical model (that is not the case with above model), because that is what quantum computation promises, faster computation?
is there any way that I can run this same model (above) on IBM real quantum device to better compare the performance and convergence time. I am curious if the quantum processor would recognize the keras commands like compile, fit
etc…
Any help would be appreciated.
thanks