Skip to content

Encoding data

In the last lesson the input xx entered the circuit through a gate RY(x)R_Y(x), 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.

A classical data point is a list of numbers x=(x1,…,xN)x = (x_1, \dots, x_N). 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 ∣101⟩\ket{101}, made with an XX 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:

∣φ(x)⟩=RY(x1)∣0⟩⊗RY(x2)∣0⟩⊗⋯⊗RY(xN)∣0⟩.\ket{\varphi(x)} = R_Y(x_1)\ket{0} \otimes R_Y(x_2)\ket{0} \otimes \cdots \otimes R_Y(x_N)\ket{0}.

This takes NN 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,

∣φ(x)⟩=∑i=1Nxi∣i⟩.\ket{\varphi(x)} = \sum_{i=1}^{N} x_i \ket{i}.

Since nn qubits have 2n2^n amplitudes, this is extremely compact.

Check

How many qubits does amplitude encoding need for a data point with 1024 features?

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 NN features Gates to prepare Catch
Basis one per bit one layer uses no amplitudes
Angle NN one layer one qubit per feature
Amplitude log⁡2N\log_2 N about NN in general the circuit is as large as the data

A general state of nn qubits has 2n2^n independent amplitudes, and a circuit that sets them all needs a number of gates that grows like 2n2^n. 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, nn qubits give up at most nn 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:

∣0⟩  →  RY(θ0)  →  RZ(x)  →  RY(θ1)  →  measure ⟨Z⟩.\ket{0} \;\to\; R_Y(\theta_0) \;\to\; R_Z(x) \;\to\; R_Y(\theta_1) \;\to\; \text{measure } \langle Z\rangle .

Here RZ(x)R_Z(x) is the rotation about the vertical axis of the Bloch ball, which puts a relative phase eixe^{ix} between ∣0⟩\ket{0} and ∣1⟩\ket{1}. Follow the Bloch vector (x,y,z)(x, y, z) through the circuit:

  1. It starts at the north pole, (0,0,1)(0, 0, 1).
  2. RY(θ0)R_Y(\theta_0) tilts it to (sin⁡θ0, 0, cos⁡θ0)(\sin\theta_0,\, 0,\, \cos\theta_0).
  3. RZ(x)R_Z(x) swings it around the vertical axis to (sin⁡θ0cos⁡x, sin⁡θ0sin⁡x, cos⁡θ0)(\sin\theta_0\cos x,\, \sin\theta_0\sin x,\, \cos\theta_0).
  4. RY(θ1)R_Y(\theta_1) tilts it again, and the final height is the output.

The result is

f(x)=⟨Z⟩=cos⁡θ0cos⁡θ1  −  sin⁡θ0sin⁡θ1 cos⁡x.f(x) = \langle Z\rangle = \cos\theta_0\cos\theta_1 \;-\; \sin\theta_0\sin\theta_1\,\cos x .

Look at the shape of this. Whatever the two angles are, ff is a constant plus a multiple of cos⁡x\cos x. Training can change the two coefficients. It cannot produce cos⁡2x\cos 2x, 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,

f(x)=∑ωcω eiωx,f(x) = \sum_{\omega} c_\omega\, e^{i\omega x},

in which

  • the frequencies ω\omega that appear are fixed by the encoding gates,
  • the coefficients cωc_\omega are set by the trainable gates.

A single RZ(x)R_Z(x) or RY(x)R_Y(x) gives the frequencies −1-1, 00 and 11. To get more, the data has to go in more than once. Encoding xx again later in the circuit, with trainable gates in between, is called data re-uploading. With LL encodings in a row on one qubit the frequencies run up to LL.

The simulator has one qubit, with xx encoded one, two or three times and a trainable RYR_Y between each. The bars show how much of each frequency is in your model’s output.

SimulatorWhat can one qubit fit?
Times x is encoded
Target

Solid: your model's output ⟨Z⟩. Dashed: the target.

Things to try:

  1. With one encoding, fit cos⁡x\cos x by hand. The formula above tells you which angles to aim for.
  2. Keep one encoding and switch the target to cos⁡2x\cos 2x. 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.
  3. Now encode xx twice and fit again. Watch the third bar appear.
  4. Try cos⁡3x\cos 3x with two encodings, then with three.

Every trainable gate in this simulator is an RYR_Y, which keeps the number of sliders small but restricts the model to cosines. With RZR_Z gates among the trainable ones the model could shift the waves sideways as well.

Check

A model with one encoding gate cannot fit data that varies as cos⁡4x\cos 4x. What would let it?

Multiplying the input by a constant, as in RZ(4x)R_Z(4x), also reaches cos⁡4x\cos 4x. But it moves the single available frequency without adding any, so the model can then no longer produce cos⁡x\cos x.

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.

Check

What is the main practical obstacle to amplitude encoding?

RecallAnswer from memory, then check
LessonFinish this lesson