The Quantum Combination Lock: Simple Quantum Machine Learning Classifier

Project Overview:

Quantum machine learning combines quantum circuits with the same basic idea behind ordinary machine learning, adjusting internal parameters until a model correctly separates different categories of data. In this project you will build a tiny quantum classifier that learns to tell two simple groups of data points apart, training the angles inside a quantum circuit the way a classical model trains weights, then testing how well it classifies new points it has never seen.

Materials Required:

A computer with internet access

A free Google account, to use Google Colab, no installation needed

Basic comfort reading Python

Background: How This Tiny Classifier Works

A quantum circuit can act like a very small, flexible model. You feed a data point in by rotating a qubit based on that point's value, then apply a second rotation controlled by an adjustable parameter, similar to a weight in classical machine learning. Measuring the qubit afterward gives a result you can interpret as a prediction, one category or the other.

Training means trying different values for that adjustable parameter and checking which one makes the circuit's predictions match the correct answers most often across your training examples. This project uses a simple brute force search over possible parameter values instead of full gradient-based training, so you can see the entire learning process happen directly, without hidden optimization steps.

Step by Step Instructions

Set up your notebook.

Go to Google Colab at colab.research.google.com and create a new notebook.

Install Qiskit.

Type the following into a code cell and run it:

pip install qiskit qiskit-aer

Create a small, simple dataset.

We will use single numbers as data points, values below 0.5 belong to category 0, values above 0.5 belong to category 1.

import numpy as np

data_points = [0.1, 0.2, 0.3, 0.4, 0.6, 0.7, 0.8, 0.9]

labels =        [0,   0,   0,   0,   1,   1,   1,   1]

Build a function that runs the quantum circuit for one data point and one parameter.

from qiskit import QuantumCircuit

from qiskit_aer import AerSimulator

simulator = AerSimulator()

def quantum_predict(data_point, theta):

qc = QuantumCircuit(1, 1)

qc.ry(data_point * np.pi, 0)

qc.ry(theta, 0)

qc.measure(0, 0)

result = simulator.run(qc, shots=200).result()

counts = result.get_counts()

prediction = 1 if counts.get('1', 0) > counts.get('0', 0) else 0

return prediction

Build a function that scores how well a given theta performs across the whole dataset.

def score_theta(theta):

correct = 0

for point, true_label in zip(data_points, labels):

prediction = quantum_predict(point, theta)

if prediction == true_label:

correct += 1

return correct / len(data_points)

Train by testing many possible theta values and keeping the best one.

best_theta = 0

best_score = 0

for theta in np.linspace(0, np.pi, 30):

score = score_theta(theta)

if score > best_score:

best_score = score

best_theta = theta

print("Best theta found:", best_theta)

print("Training accuracy:", best_score)

Run it and check your results.

Training accuracy should land close to 100 percent, since this dataset was deliberately built to be easy to separate. The best_theta value printed is the trained parameter your tiny quantum model learned on its own, purely by testing which angle classified the most training points correctly.

Test the trained model on brand new data points it has never seen.

test_points = [0.15, 0.35, 0.55, 0.75, 0.95]

expected =     [0,    0,    1,    1,    1]

for point, expected_label in zip(test_points, expected):

prediction = quantum_predict(point, best_theta)

print(f"Point: {point}, Predicted: {prediction}, Expected: {expected_label}")

Check how well it generalizes.

Your trained model should correctly classify most or all of these new points too, confirming it actually learned a genuine underlying pattern in the data, rather than just memorizing the specific training examples.

Stretch step, make the boundary harder.

Add a few tricky data points very close to 0.5 on both sides, retrain by rerunning step 6, and see whether your model's accuracy drops, giving you a feel for how classifiers, quantum or classical, struggle near decision boundaries.

Congratulations, you just built and trained a working quantum classifier from scratch, touching the same core ideas, encoding data into circuits, adjustable parameters, and measurement-based predictions, that real quantum machine learning research is actively exploring today.

Fun Fact:

Quantum machine learning is still a young and actively debated research field, some real world studies have found that certain quantum models can learn patterns using exponentially fewer resources than classical models in specific narrow cases, but researchers are still working out exactly which practical problems, if any, quantum machine learning will meaningfully outperform classical machine learning on.



Next
Next

The Three-Way Bond: Building a GHZ Entangled State