While running the beautiful qnn + tf.keras demo, I tried scaling the quantum circuit from 2 qubits to 10, and it worked for 2…7, but started erroring out on model.fit()
at 8 qubits. Code is 99% same as the demo, only few lines changed to make the num_qubits 8 instead of 2:
import tensorflow as tf
tf.get_logger().setLevel('ERROR')
import pennylane as qml
import matplotlib.pyplot as plt
import numpy as np
from sklearn.datasets import make_moons
# Set random seeds
np.random.seed(42)
tf.random.set_seed(42)
X, y = make_moons(n_samples=200, noise=0.1)
y_hot = tf.keras.utils.to_categorical(y, num_classes=2) # one-hot encoded labels
c = ["#1f77b4" if y_ == 0 else "#ff7f0e" for y_ in y] # colours for each class
plt.axis("off")
plt.scatter(X[:, 0], X[:, 1], c=c)
plt.show()
n_qubits = 8 # works for 2, 3, 4, ...7
dev = qml.device("default.qubit", wires=n_qubits)
@qml.qnode(dev)
def qnode(inputs, weights):
qml.AngleEmbedding(inputs, wires=range(n_qubits))
qml.BasicEntanglerLayers(weights, wires=range(n_qubits))
return [qml.expval(qml.PauliZ(wires=i)) for i in range(n_qubits)]
n_layers = 6
weight_shapes = {"weights": (n_layers, n_qubits)}
qlayer = qml.qnn.KerasLayer(qnode, weight_shapes, output_dim=n_qubits)
clayer_1 = tf.keras.layers.Dense(n_qubits)
clayer_2 = tf.keras.layers.Dense(2, activation="softmax")
model = tf.keras.models.Sequential([clayer_1, qlayer, clayer_2])
opt = tf.keras.optimizers.SGD(learning_rate=0.02)
model.compile(opt, loss="mae", metrics=["accuracy"])
fitting = model.fit(X, y_hot, epochs=1, batch_size=50, validation_split=0.5, verbose=1)
And the error I get is:
InvalidArgumentError: Exception encountered when calling layer 'keras_layer' (type KerasLayer).
{{function_node __wrapped__MatMul_device_/job:localhost/replica:0/task:0/device:GPU:0}} Matrix size-incompatible: In[0]: [4,4], In[1]: [100,128] [Op:MatMul] name:
Call arguments received by layer 'keras_layer' (type KerasLayer):
• inputs=tf.Tensor(shape=(50, 8), dtype=float32)
The versions I am using are:
tf = 2.15.0
pennylane = 0.33.1