I implemented a simple Neural Net with Pennylane and Qiskit for classifying two half moons: notebook for two half moons (GitHub). It works somehow (not overwhelmingly by any means):

enter image description here

I tried running the program on a public quantum device (IBM) and found very quickly that it would take months, not days, to complete the training. For this reason I built a much simpler net (my hope is that one qubit is enough), which is supposed to solve the classification task: notebook for linear classification (GitHub). It should divide the classes using a straight line:

enter image description here

While the full notebook (a very small one) can be found on the above linked GitHub page, the following snipped is relevant:

# Convert quantum layer to keras
n_layers = 4
weight_shapes = {"weights": (n_layers, n_qubits)}
qlayer = qml.qnn.KerasLayer(qnode, weight_shapes, output_dim=n_qubits)

# Define classical layers
clayer_1 = tf.keras.layers.Dense(1)
clayer_2 = tf.keras.layers.Dense(1, activation="sigmoid")

model = tf.keras.models.Sequential([clayer_1, qlayer, clayer_2])
opt = tf.keras.optimizers.Adam(learning_rate=0.1)
model.compile(optimizer=opt, loss="binary_crossentropy", metrics=["accuracy"])
fitting = model.fit(x, y_hot, epochs=4, batch_size=5, validation_split=0.25, verbose=2)

Possibly it is a very simple error, which causes the net is recognizing all input as one and the same class:

enter image description here

I would be very grateful for any hint that pushes me towards the proper direction.


2 Answers 2


This is Catalina from Xanadu.

If you run this tutorial does it work for you? It only uses 2 qubits and uses the moons dataset too.

If the tutorial doesn't work then it's probably not a problem in your code but in the connection to the backend. Have you tried different IBM devices? Their errors and waiting times vary a lot so trying different ones can help.

  • $\begingroup$ Yes - I ran this model multiple times on different quantum computers (IBM devices). Even after several days he was processing epoch 1. I do not have connection problems with the backend, because I could see that the quantum device successfully performed increasingly many of my jobs. The devices have a long queue time, currently of the order of 15 minutes per circuit i.e. per datapoint. Thats why I am really try to simplyfy the classification problem using a much simpler net with less data points. $\endgroup$ Nov 17, 2021 at 6:46

enter image description here

Hi Eldar, For classification problems, you could also use a quantum computer in the way shown in the attached image. It is a method called the Universal Quantum Classifier - please let me know if you need more details.

  • $\begingroup$ This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review $\endgroup$
    – Peter-Jan
    Aug 24, 2023 at 10:27
  • $\begingroup$ @Peter-Jan, I think the original post was an attempt to perform a binary classification by trying a 1 qubit hybrid quantum-classical net. The subsequent answer by states that 2 qubits would work. My answer for the question: how to indeed use just 1 qubit as a classifier? I believe that was the intent of the original post. $\endgroup$ Aug 25, 2023 at 0:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.