Encoding data
In the last lesson the input entered the circuit through a gate , and nothing more was said about it. That step is called the encoding, or feature map. It turns out to matter more than the trainable gates do: the encoding fixes the set of functions a model can ever represent, and training only chooses among them.
Three ways to load data
Section titled “Three ways to load data”A classical data point is a list of numbers . There are three standard ways to turn it into a quantum state.
Basis encoding. Write the data in binary and prepare the matching basis state. The bit string 101 becomes , made with an gate on each qubit that should be 1. This is cheap and exact, but it uses one qubit per bit and makes no use of amplitudes.
Angle encoding. Use each number as the angle of a rotation, one qubit per feature:
This takes qubits and a single layer of gates. It is what almost all experiments on today’s hardware use. The resulting state is a product state, with no entanglement, unless more gates follow.
Amplitude encoding. Use the numbers themselves as amplitudes. After rescaling the vector to length 1,
Since qubits have amplitudes, this is extremely compact.
How many qubits does amplitude encoding need for a data point with 1024 features?
, so ten qubits have exactly enough amplitudes. Angle encoding would need 1024 qubits.
That compactness is the source of most claims that quantum computers could process huge datasets. It comes with a price that is easy to overlook.
| Encoding | Qubits for features | Gates to prepare | Catch |
|---|---|---|---|
| Basis | one per bit | one layer | uses no amplitudes |
| Angle | one layer | one qubit per feature | |
| Amplitude | about in general | the circuit is as large as the data |
A general state of qubits has independent amplitudes, and a circuit that sets them all needs a number of gates that grows like . So amplitude-encoding an arbitrary vector of a million numbers takes about a million gates, for every data point, every time the circuit is run. The qubit count is tiny but the running time is not. The usual way around this is to assume a device called quantum RAM, which would load data in superposition quickly. Nobody has built one at a useful size, and whether one can be built at a reasonable cost is an open question. Data with special structure can sometimes be loaded efficiently, but that has to be shown case by case.
Reading results back out has the same problem in reverse. As you saw in Entropy and distinguishability, qubits give up at most bits when measured.
The encoding decides what the model can say
Section titled “The encoding decides what the model can say”Now for the less obvious effect. Take the smallest model that has trainable gates on both sides of the encoding:
Here is the rotation about the vertical axis of the Bloch ball, which puts a relative phase between and . Follow the Bloch vector through the circuit:
- It starts at the north pole, .
- tilts it to .
- swings it around the vertical axis to .
- tilts it again, and the final height is the output.
The result is
Look at the shape of this. Whatever the two angles are, is a constant plus a multiple of . Training can change the two coefficients. It cannot produce , or a step, or anything else. The data entered through one gate, and one gate carries one frequency.
This is a general fact. For models built from rotation gates, the output is always a Fourier series in the data,
in which
- the frequencies that appear are fixed by the encoding gates,
- the coefficients are set by the trainable gates.
A single or gives the frequencies , and . To get more, the data has to go in more than once. Encoding again later in the circuit, with trainable gates in between, is called data re-uploading. With encodings in a row on one qubit the frequencies run up to .
Try it
Section titled “Try it”The simulator has one qubit, with encoded one, two or three times and a trainable between each. The bars show how much of each frequency is in your model’s output.
Solid: your model's output ⟨Z⟩. Dashed: the target.
Things to try:
- With one encoding, fit by hand. The formula above tells you which angles to aim for.
- Keep one encoding and switch the target to . Press Fit automatically. The best the optimiser can do is a flat line with an error of 0.71, because the model contains no frequency 2 to work with.
- Now encode twice and fit again. Watch the third bar appear.
- Try with two encodings, then with three.
Every trainable gate in this simulator is an , which keeps the number of sliders small but restricts the model to cosines. With gates among the trainable ones the model could shift the waves sideways as well.
A model with one encoding gate cannot fit data that varies as . What would let it?
The frequencies available are fixed by how the data enters the circuit. Trainable gates and longer training only adjust the coefficients of frequencies that are already there.
Multiplying the input by a constant, as in , also reaches . But it moves the single available frequency without adding any, so the model can then no longer produce .
What follows from this
Section titled “What follows from this”Choosing an encoding is choosing a model class. In classical machine learning this choice goes by names such as feature engineering or inductive bias, and it is where most of the skill lies. The same is true here. A model with a poor encoding cannot be rescued by training.
More frequencies are not automatically better. A model that can wiggle quickly can also fit noise. And the coefficients are not free: a small circuit can reach only some combinations of them.
Simple spectra invite classical competition. If a model’s output is a Fourier series with a modest number of terms, a classical computer can fit a series with the same frequencies directly. A quantum model can only be out of classical reach if its encoding produces very many frequencies with coefficients that are hard to compute. Whether such models are also trainable and useful is the question the rest of this track keeps returning to.
There is another way to look at an encoding: as a way of measuring how similar two data points are. That view leads to quantum kernels, the next lesson.
What is the main practical obstacle to amplitude encoding?
The qubit count is logarithmic, which is the attraction. The cost is in the circuit: about gates for a general state of qubits, unless the data has special structure or a quantum RAM exists.
Remember it
Section titled “Remember it”Name three ways to encode classical data in a quantum state.
Basis encoding (bit strings to basis states), angle encoding (features as rotation angles) and amplitude encoding (features as amplitudes).
How many qubits does amplitude encoding need for features, and what is the catch?
Only qubits. But preparing an arbitrary state takes about gates, unless the data has special structure or a quantum RAM exists.
As a function of the input , what kind of function does a circuit of rotation gates compute?
A Fourier series. The encoding gates fix which frequencies appear, and the trainable gates set the coefficients.
What is data re-uploading, and what does it buy?
Encoding the same input several times, with trainable gates in between. Each repetition adds higher frequencies to the functions the model can represent.
What functions of can , , on produce as ?
Only a constant plus a multiple of : .