Hello! The above code is to train the QNN model on binarized MNIST dataset and 1000 randomly selected training images. However, when i run the above code i get the same validation accuracy in every epoch, even the training accuracy seems very random.
I have tried with different quantum circuits, even built-in templates but to no success. Is there anything I am doing wrong in here? Any pointers would be appreciated. Thanks
PS: the same setting seems to work well when there is no quantum layer.
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
# Preprocess the data
train_images = train_images.reshape((60000, 784))
test_images = test_images.reshape((10000, 784))
# Normalize pixel values to be between 0 and 1
train_images, test_images = train_images / 255.0, test_images / 255.0
# Convert labels to binary: 0 for even digits, 1 for odd digits
train_labels = train_labels % 2
test_labels = test_labels % 2
# Create a subset of 1000 training images randomly
subset_indices = np.random.choice(len(train_images), 1000, replace=False)
train_images_subset = train_images[subset_indices]
train_labels_subset = train_labels[subset_indices]
n_qubits = 10
layers = 4
dev = qml.device("lightning.qubit", wires=n_qubits)
@qml.qnode(dev, interface="tf", diff_method='best')
def qnode(inputs, weights):
qml.templates.AmplitudeEmbedding([a for a in inputs], wires=range(n_qubits), pad_with=0., normalize=True)
for j in range(layers):
for i in range(n_qubits):
qml.RX(weights[i], wires=i)
qml.RY(weights[i], wires=i)
for k in range(n_qubits-1):
qml.CZ(wires=[k,k+1])
# return [qml.expval(qml.PauliZ(wires=[i])) for i in range(1)]
return qml.expval(qml.PauliZ(0) @ qml.PauliZ(1))
weight_shapes = {"weights": (n_qubits)}
weights = np.random.normal(0, np.pi, size=(n_qubits))
tf.keras.backend.set_floatx('float64')
inputs = tf.keras.Input(shape=(784,))
x = qml.qnn.KerasLayer(qnode, weight_shapes, output_dim=1)(inputs)
model = tf.keras.Model(inputs=inputs, outputs=x)
# Compile the model
opt = tf.keras.optimizers.legacy.Adam(learning_rate=0.01)
model.compile(opt,
loss='binary_crossentropy',
metrics=['accuracy'])
# Train the model
model.fit(train_images_subset, train_labels_subset, epochs=25, validation_split=0.2)
the output of qml.about()
.
`Platform info: macOS-13.5.2-arm64-arm-64bit
Python version: 3.11.4
Numpy version: 1.23.5
Scipy version: 1.10.1
Installed devices:
- default.gaussian (PennyLane-0.32.0)
- default.mixed (PennyLane-0.32.0)
- default.qubit (PennyLane-0.32.0)
- default.qubit.autograd (PennyLane-0.32.0)
- default.qubit.jax (PennyLane-0.32.0)
- default.qubit.tf (PennyLane-0.32.0)
- default.qubit.torch (PennyLane-0.32.0)
- default.qutrit (PennyLane-0.32.0)
- null.qubit (PennyLane-0.32.0)
- lightning.qubit (PennyLane-Lightning-0.32.0)
- qiskit.aer (PennyLane-qiskit-0.31.0)
- qiskit.basicaer (PennyLane-qiskit-0.31.0)
- qiskit.ibmq (PennyLane-qiskit-0.31.0)
- qiskit.ibmq.circuit_runner (PennyLane-qiskit-0.31.0)
- qiskit.ibmq.sampler (PennyLane-qiskit-0.31.0)
- qiskit.remote (PennyLane-qiskit-0.31.0)’