Quantum Machine Learning: Writing Data into Qubits

In the first half of the series we saw how quantum computers think: superposition, entanglement, interference. Now we bring those tools into the most popular corner of AI: machine learning.
But first we need to answer a basic question: how do you hand a data table, an image or a sequence of measurements to a quantum computer? Quantum computers don't read Excel files; they only understand qubit states. Turning classical data into a quantum state is called encoding, and a quantum model's success depends heavily on this step.
In this post we'll try the three main approaches with PennyLane.
1) Angle encoding: write data as rotation angles
Remember the Ry(θ) gate from the first post — the one that swings the arrow around the Bloch sphere? The idea is simple: use the data value directly as the angle.
For a data point with two features, x = (x₁, x₂):
@qml.qnode(dev1)
def angle_encode_bloch(x):
qml.RY(x[0], wires=0) # x₁ → latitude
qml.RZ(x[1], wires=0) # x₂ → longitude
return [qml.expval(qml.PauliX(0)),
qml.expval(qml.PauliY(0)),
qml.expval(qml.PauliZ(0))]Each data point becomes a point on the sphere's surface. In the cover animation you can watch the points of the "moons" dataset fly onto the sphere.
Pros: simple, shallow circuits that suit today's noisy hardware.
Cons: you need one qubit (or one gate) per feature.
2) Amplitude encoding: exponential compression
This method uses the most fascinating property of quantum systems: n qubits carry 2ⁿ amplitudes. So you can fit 8 numbers into just 3 qubits, and a million numbers into only 20!
data = [3, 1, 4, 1, 5, 9, 2, 6]
qml.AmplitudeEmbedding(data, wires=range(3), normalize=True)The measurement probabilities are proportional to the squared data: the largest value, 9, is the most common outcome with 47% probability.
Pros: exponential compactness.
Cons: the circuit that prepares this state is usually very deep; the cost of "loading the data" can eat the gain.
3) Feature maps: enrich data with entanglement
The third approach doesn't just write the data — it links the features together through entanglement. The popular "ZZ feature map" writes each feature into a rotation, then adds an interaction term like (π − x₁)(π − x₂) through an entangling gate between two qubits.
The result places the data in a very high-dimensional space, much like the "kernel trick" in classical machine learning — a space that classical computers may struggle to simulate.
Quantum similarity: how close are two points?
Once data points are quantum states, we can measure their similarity by the overlap of their states:
nearby points F(a, b) = 0.917
distant points F(a, c) = 0.020Nearby data points map to almost the same quantum state; distant ones to nearly "orthogonal" states. This similarity measure is exactly the quantum kernel we'll meet in the last post of the series.
Which one should you choose?
Few features and a shallow circuit: angle encoding.
Many features squeezed into few qubits: amplitude encoding.
Complex relationships between features: feature maps.
Remember: a good encoding matters as much as a good model. The wrong encoding can make even the best quantum algorithm useless.
Summary
Step one of quantum ML is encoding classical data as a quantum state.
Angle encoding is simple and shallow; amplitude encoding is exponentially compact but costly.
Feature maps use entanglement to lift data into a high-dimensional space.
The overlap between quantum states is a natural similarity measure.
All the code for this post is on GitHub — download it, run it and play with the parameters.
Next up: A Quantum Circuit That Learns — we'll train a real quantum classifier on encoded data.



Comments