Applying quantum layer keras for modulation images dataset

Hello! i’m working on a tutorial quantum nodes into kersa layer and using qnn layer for a modulation dataset. dataset contains 2D images of 11 different modulation types. these images contains 2 channels I and Q. i converted these images to a numpy file which has shape (2200, 100,100,2) where 2200 is the total no of images with size 100x100 and represent 2 channels. labels file shape is (2200,11). total classes are 11 and labels are categorical type. i want to apply this image dataset on qnn layer. i have tried but dont know how i modify this part of code shown below according to image dataset.

import pennylane as qml

n_qubits = 2
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)]

also should i have to add flatten layer here

clayer_1 = tf.keras.layers.Dense(2)
clayer_2 = tf.keras.layers.Dense(2, activation=“softmax”)
model = tf.keras.models.Sequential([clayer_1, qlayer, clayer_2])

Hello @Mahamiftikhar !

I’ll return to you soon. Would you mind sharing the output and/or error you getting from this script?

@ludmilaaasb i got no error but i got very low validation accuracy approx equal to 0. i’m sharing the code below

import pennylane as qml

n_qubits = 14
dev = qml.device(“default.qubit”, wires=n_qubits)

@qml.qnode(dev, interface=‘tf’, diff_method=‘backprop’)
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 = 17
weight_shapes = {“weights”: (n_layers, n_qubits)}
qlayer = qml.qnn.KerasLayer(qnode, weight_shapes, output_dim=n_qubits)
flayer = tf.keras.layers.Flatten(input_shape=[100, 100, 2])
clayer_1 = tf.keras.layers.Dense(2)
clayer_2 = tf.keras.layers.Dense(11, activation=“softmax”)
model = tf.keras.models.Sequential([flayer, clayer_1, qlayer, clayer_2])
model.compile(
optimizer = ‘adam’,
loss=“categorical_crossentropy”,
metrics=[“accuracy”],
)

Mahamiftikhar

1M

output: running very slow
Epoch 1/6
330/330 - 9664s - loss: 2.3560 - accuracy: 0.1012 - val_loss: 2.7034 - val_accuracy: 0.0000e+00 - 9664s/epoch - 29s/step
i dont know if i’m using right no. of qubits and layers. kindly correct me it its wrong

i got no error but i got very low validation accuracy approx equal to 0.

You have a quite big model, so probably it would take a lot of steps to train it. Have you considered increasing the number of steps?

it is very slow already as only 1 epochs take 9664s to execute. if i reduce the classical layers then also it is not showing accuracy above 20. can you please tell me, the no. of qubits i’m using here which is 14, is this right, according to the shape of data. also suggest can i apply some another quantum machine learning approach to my modulation dataset for classification problem.

Hello again!

it is very slow already as only 1 epochs take 9664s to execute. if i reduce the classical layers then also it is not showing accuracy above 20.

I’m trying to follow up with your questions but I am not really sure what is the problem here. I’m sorry, I guess I’m not understanding you correctly. :frowning_face:

Anyway, if you think something is wrong, or you are getting some warning or error messages, send us a minimal, self-contained working version of your code. If you are not really sure how to do that, I would like to recommend this video. It will help you give us the exact amount of information so we can help you better! :smiley:

can you please tell me, the no. of qubits i’m using here which is 14, is this right, according to the shape of data.

Well, this is a good question! I would say that it looks fine. But honestly, that’s homework for you! Setting proper parameters to a model you would like to train is a whole research field in Machine Learning! I’m afraid I can’t help you with this one. :face_with_diagonal_mouth:

also suggest can i apply some another quantum machine learning approach to my modulation dataset for classification problem.

Sure thing! I’ll suggest you take a look at this demo about convolutional networks. They are using the MNIST dataset! Maybe this could be a good starting point for your image classification problem. :wink:

I’m trying to follow up with your questions but I am not really sure what is the problem here. I’m sorry, I guess I’m not understanding you correctly. :frowning_face:

It’s ok :slightly_smiling_face: I’m trying to say that i got no errors while executing the code but the issue is that end result i got was not satisfactory as the accuracy this model gave is very low. I was just asking that is anything we change in my model to increase the accuracy.

Sure thing! I’ll suggest you take a look at this demo about convolutional networks. They are using the MNIST dataset! Maybe this could be a good starting point for your image classification problem. :wink:

Thanks for the suggestion. I will surely try this demo on my modulation dataset.

1 Like