1. Quick check §
How many qubits and gates does basis encoding of a 4-bit string need?
Hint 1
One qubit per bit; X for each 1.
Worked solution
Four qubits and one X gate per 1 in the string: for 1011, three X gates.
Quantum Machine Learning · Chapter 02
What does it cost to turn a classical data point into a quantum state?
Starting point: Chapter 1 and the state-vector formalism.
Anchor example: Three encodings of the same four-dimensional vector.
Method: Count qubits, gates, and preparation cost for each encoding.
Watch for: An encoding is a modelling choice, not a neutral step.
This lesson includes explanations, worked examples, and problems with solutions. Results quoted from the literature are identified as theorems with their hypotheses.
Every quantum model begins with a decision that is usually made in a single line of code and rarely justified: how the classical data become a quantum state. That decision fixes the geometry of the problem—which points are close, which functions are reachable, how many frequencies the model can represent—and it fixes the cost of the first step of every training iteration. This chapter treats encodings as modelling choices with measurable consequences rather than as neutral plumbing.
Three families cover almost everything in use. They differ in how many features a qubit can carry, in what they cost to prepare, and in which classical geometry survives the trip into Hilbert space.
The simplest embedding maps a bit string to a computational basis state:
\[x=(b_1,\dots,b_n)\in\{0,1\}^n\ \longmapsto\ |x\rangle=|b_1\cdots b_n\rangle .\]It uses one qubit per bit and costs a layer of \(X\) gates. It is exact—measuring in the computational basis returns the input—and it preserves Hamming geometry: the distance between two encoded strings is the number of positions in which they differ.
Its limitation is equally clear. Continuous features must be discretised first, and the discretisation is lossy in a way that is invisible to the model. More importantly, basis encoding gives no cheap way to evaluate a function of the whole string: a circuit that acts on \(|x\rangle\) sees a single computational basis state, so any superposition that makes the computation parallel must be created independently of the data.
Basis encoding is nevertheless the right choice for genuinely discrete problems—combinatorial inputs, Boolean formulas, or the oracle models used to prove query-complexity separations. It is the wrong choice when the data come from a continuous measurement.
Encode the three-bit string \(101\) and the uniform superposition of all three-bit strings. How many gates does each require?
The string needs \(X\) on qubits 1 and 3, giving \(|101\rangle\): two gates. The uniform superposition is \(H^{\otimes3}|000\rangle=\frac{1}{\sqrt8}\sum_{x}|x\rangle\): three gates, and it is independent of any data. The contrast is the point. Data-dependent superpositions—the states that would let one circuit act on many inputs "at once"—are not produced by basis encoding; producing them requires a preparation whose cost scales with the number of amplitudes, which is the subject of Section 2.2.
Amplitude encoding stores a real vector in the amplitudes of a state:
\[x\in\mathbb R^{2^n}\ \longmapsto\ |\psi(x)\rangle=\frac{1}{\|x\|}\sum_{i=0}^{2^n-1}x_i\,|i\rangle .\]It is exponentially compressive: \(n\) qubits carry \(2^n\) numbers. It also preserves Euclidean geometry in a precise sense. For two unit vectors, the squared distance between the states is
\[\bigl\||\psi(x)\rangle-|\psi(x')\rangle\bigr\|^2=2-2\langle\psi(x)|\psi(x')\rangle,\]so inner products of encoded states are inner products of normalised data vectors. A quantum computer that can estimate overlaps is therefore computing a linear kernel on the original features.
Two costs come with the compression. The norm is discarded, and recovering it needs either a classical record or extra qubits. And preparing an arbitrary \(2^n\)-amplitude state requires \(\Theta(2^n)\) gates in the worst case, because a general \(n\)-qubit state has \(2^{n+1}-2\) real parameters and each gate supplies only a constant number. The compression is real; the preparation is where the exponential reappears.
Encode \(x=(1,1,1,1)\) and then \(x=(1,2,3,4)\) in two qubits. Which preparation is free?
The first vector is already normalised up to a factor, giving \(\tfrac12(1,1,1,1)=H^{\otimes2}|00\rangle\): two Hadamard gates, no data dependence beyond a check that the entries are equal. The second gives \(\frac{1}{\sqrt{30}}(1,2,3,4)\), which is a generic two-qubit state with three independent parameters; it needs a general preparation—two or three two-qubit gates with computed angles. The example scales the wrong way for the optimistic reading: the "free" case is a special symmetric vector, and the generic case is the norm. An algorithm that assumes cheap amplitude preparation is assuming structure in the data, and that assumption belongs in the ledger.
The third family uses features as rotation angles. With one feature per qubit,
\[x\in\mathbb R^{n}\ \longmapsto\ |\psi(x)\rangle=\bigotimes_{i=1}^{n}R_y(x_i)\,|0\rangle ,\]which costs \(n\) gates for \(n\) features—linear, and cheap. The state is a product state, so it carries no entanglement; the model can still be nonlinear because the amplitudes are trigonometric functions of the data.
Entangling gates interleaved with rotations turn this into a feature map. A single layer
\[U_\phi(x)=U_{\text{ent}}\Bigl(\bigotimes_i R_z(x_i)\Bigr)H^{\otimes n}\]already produces states that are not product states, and the resulting kernel is no longer a product of univariate functions. Repeating the block \(L\) times creates frequencies up to \(\pm L\) in each feature, which is the mechanism behind the Fourier series of Chapter 1.
Compute the kernel of the product-state angle encoding for two features with \(x=(0,0)\) and \(x'=(\pi,0)\).
For a product state the overlap factorises: \(\langle\psi(x)|\psi(x')\rangle=\prod_i\cos\bigl((x_i-x'_i)/2\bigr)\). Here it is \(\cos(0)\cos(\pi/2)=0\), so \(K=0\). The two points are orthogonal in Hilbert space even though their Euclidean distance is \(\pi\). That is the encoding at work: the geometry of the features has been replaced by the geometry of rotations, and a distance of \(\pi\) in one feature is the maximum separating distance. Two features differing by \(2\pi\) would be indistinguishable—the trap examined in Section 2.2.3.
State preparation is the step most often assumed away. This section separates the cases where it is genuinely cheap from the cases where an oracle is being assumed, and then asks what the encoding does to distances.
There are only four honest routes to a quantum state that encodes classical data:
| Route | Cost | Assumption |
|---|---|---|
| The data are already quantum | Sample supplied by the experiment | No classical vector exists to load |
| Structured preparation | Polynomial in \(n\) | The vector has structure—uniform, sparse, low rank, or generated by a known circuit |
| Quantum memory (QRAM) | Polylogarithmic queries | A hardware device that returns amplitudes on demand, with its own error and scaling costs |
| Generic preparation | \(\Theta(2^n)\) gates | None; this is the honest baseline |
Most algorithms in the literature are stated with the third route. That is legitimate as complexity theory, because the oracle is part of the problem statement, but it is not a claim about hardware. When a paper says "assume access to a quantum random-access memory", the right response is to ask what the memory costs to build, how its errors propagate, and whether the claimed advantage survives when those costs are included.
How many gates are needed to prepare an arbitrary 12-qubit state, and how many for the uniform superposition? Express both as a ratio.
A general 12-qubit state has \(2^{13}-2=8190\) real parameters, so a gate-by-gate preparation needs on the order of \(10^{3}\) two-qubit gates. The uniform superposition needs 12 Hadamards. The ratio is roughly \(700:1\) at this size and doubles with every added qubit. This is why "amplitude encoding of a \(2^{12}\)-dimensional feature vector" is cheap only when the vector has a short description—a fact the encoding notation hides.
Amplitude encoding maps a vector to a ray, so the encoding is invariant under positive rescaling: \(x\) and \(cx\) give the same state for \(c>0\). Two consequences follow.
First, information is lost. If the norm carries signal—it often does, for example when it measures an intensity or a concentration—it must be encoded separately, in extra qubits or a classical side channel. Second, distances become angular. For unit vectors the state distance and the feature distance agree, but for vectors of different lengths the comparison is between directions only, and two points can be far apart in Euclidean terms while mapping to nearby states.
Angle encoding has a different pathology: periodicity. Since \(R_y(\alpha+2\pi)=-R_y(\alpha)\), the state is unchanged when a feature is shifted by \(2\pi\). Feature values therefore have to be rescaled into an interval narrower than \(2\pi\); if they are not, distinct data points receive identical states and no model can separate them. A rescaling that maps a large range into \([0,2\pi)\) compresses the far end of the distribution, which is a modelling decision with consequences for the kernel.
A feature ranges over \([0,100]\). What goes wrong with the naive map \(x\mapsto R_y(x)\), and what is a reasonable repair?
The map is \(2\pi\)-periodic, so the range wraps about sixteen times: \(x=0\), \(x=2\pi\), \(x=4\pi\) and so on all give the same state, and points differing by about \(6.28\) become indistinguishable. A repair is an affine rescaling \(x\mapsto 2\pi x/100\), which makes the map injective on the range but compresses all separations by a factor of about \(16\). The compressed map is not wrong, but it changes the kernel: differences that were large in the data become small in Hilbert space. Recording the scale in the model specification is part of the specification, not an implementation detail.
Distances are the clearest way to compare encodings, because a kernel is a function of distance in feature space and the encoding changes what "distance" means.
This figure is the practical reason to state the encoding explicitly in every experiment. A model that appears to ignore a feature may simply be seeing the feature through a periodic map whose period is shorter than the data range, and a kernel that appears to separate classes may be relying on an aliasing artefact.
For two points at feature difference \(\Delta\), find the kernel value at \(\Delta=\pi/2\) and at \(\Delta=2\pi\), for the angle encoding and for an RBF kernel with bandwidth \(\sigma=1\).
Angle encoding: \(K=\cos^2(\Delta/2)\), so at \(\Delta=\pi/2\) we get \(\cos^2(\pi/4)=1/2\), and at \(\Delta=2\pi\) we get \(\cos^2(\pi)=1\). RBF: \(\exp(-\Delta^2/2)\), so at \(\Delta=\pi/2\) it is \(\exp(-\pi^2/8)\approx0.29\), and at \(2\pi\) it is \(\exp(-2\pi^2)\approx3\times10^{-9}\). The RBF says the far point is unrelated; the angle encoding says it is identical to the origin. Both are legitimate kernels, and the difference is entirely a property of the encoding.
An encoding plus an inner product is a kernel. Making that explicit turns encoding choices into statements about similarity, which is the language Chapter 3 uses to compare quantum and classical models on equal terms.
Given an encoding, define
\[K(x,x')=\bigl|\langle\psi(x)|\psi(x')\rangle\bigr|^2=\bigl|\langle 0|U_\phi(x')^{\dagger}U_\phi(x)|0\rangle\bigr|^2 .\]This is a positive semi-definite function of the pair, because it is a Gram matrix of state vectors, and it is computable on a quantum device by the overlap circuit of Section 3.2. For the product-state angle encoding it factorises:
\[K(x,x')=\prod_{i=1}^{n}\cos^2\!\Bigl(\frac{x_i-x_i'}{2}\Bigr),\]which is a product of univariate functions—a classical kernel in disguise. This is worth internalising, because it explains a recurring result: circuits without entanglement, or with entanglement that only commutes with the encoding, produce kernels that a classical computer can evaluate in time linear in \(n\). Entanglement is necessary for the kernel to be hard to compute, though as Chapter 3 shows it is not sufficient for the kernel to be useful.
Show that the product-state kernel equals one whenever \(x_i-x_i'\in2\pi\mathbb Z\) for every feature, and interpret the result.
Each factor is \(\cos^2(k\pi)=1\) for integer \(k\), so the product is one and the two points are indistinguishable to any model built on this kernel—regardless of how far apart they are in the original units. With more than one feature the ambiguity compounds: any vector difference whose components are multiples of \(2\pi\) is invisible. This is an aliasing effect, not a limitation of quantum hardware, and it is removed only by choosing a different encoding or by rescaling the data so the range is smaller than the period.
The standard construction alternates a data-dependent rotation layer with a fixed entangling layer:
\[U_\phi(x)=\prod_{\ell=1}^{L}\Bigl[U_{\text{ent}}\prod_{i=1}^{n}R_z(x_i)\Bigr]H^{\otimes n}.\]Each factor \(R_z(x_i)\) contributes a frequency of \(1\) in feature \(i\); the entangling layers mix features, so the composed map contains products such as \(x_ix_j\) inside phases. A widely used variant replaces the rotation layer with entangling rotations \(\exp(i x_i x_j Z_iZ_j)\) for each pair, at a cost of \(O(n^2)\) gates per layer rather than \(O(n)\).
The frequency content is what makes the map a model rather than a convenience. With \(L\) repetitions of a Pauli-generated encoding, the reachable functions are trigonometric polynomials whose spectra in each feature are contained in \(\{-L,\dots,L\}\). More layers buy higher frequencies, and therefore the ability to fit more oscillatory targets—at the cost of more parameters and, as Chapter 6 shows, flatter gradients.
How many distinct frequencies in a single feature are available with \(L=2\) encoding layers, and which ones?
Each encoding layer contributes one unit of frequency in either direction, and the generator's eigenvalues are integers, so with two layers the accessible set is \(\{-2,-1,0,1,2\}\): five frequencies, of which the symmetric pair \(\pm k\) corresponds to the same period. A single layer would give only \(\{-1,0,1\}\). The count grows linearly in \(L\), which is why adding layers is the standard way to increase expressivity—and why the parameter count grows with it.
Because the map fixes the kernel, choosing it is choosing a prior about which points should be considered similar. Three consequences deserve to be stated plainly.
Symmetries should match. If the task is invariant under a permutation of features, a map that treats features asymmetrically wastes capacity and data. Encoding a symmetry into the circuit is the quantum counterpart of using a convolutional architecture.
Expressivity trades against concentration. A richer map produces states that are, on average, more nearly orthogonal—the kernel concentrates, and the model becomes insensitive to differences between points. This is the subject of Section 3.2.3 and the reason "more expressivity" is not an unconditional good.
The map must be fixed before the test set is touched. Choosing the map, its depth, and its rescaling by observing test performance is fitting on the test set. The selection is part of the model and belongs to the training protocol.
With these three commitments, the encoding is specified rather than inherited from a tutorial, and the comparisons of Chapter 3 become meaningful.
These sources derive the Fourier structure of encoded circuits, define quantum feature spaces, and analyse the preparation cost that Section 2.2 treats as a ledger entry.
These exercises draw on the whole chapter. Attempt each question before opening a hint, explain your reasoning, then compare it with the worked solution.
How many qubits and gates does basis encoding of a 4-bit string need?
One qubit per bit; X for each 1.
Four qubits and one X gate per 1 in the string: for 1011, three X gates.
Write the amplitude-encoded state for x=(1,1,1,1) and give a circuit that prepares it.
Normalise, then recognise the uniform superposition.
The state is (1,1,1,1)/2, which is H⊗H|00⟩. Two Hadamard gates prepare it, and no data-dependent angles are needed.
How many real parameters does a general 5-qubit state have, and what does that imply for preparation?
A general n-qubit pure state has 2^(n+1) − 2 real parameters.
2^6 − 2 = 62 parameters. Each gate supplies a constant number, so preparing an arbitrary 5-qubit state needs on the order of 60 gates; for n qubits the count scales as 2^n.
For the product-state angle encoding, compute K between x=(π/2, 0) and x'=(0, 0).
The overlap factorises into cos((x_i − x'_i)/2).
cos(π/4)·cos(0) = 0.7071, so K = 0.5. The second feature is identical in both points and contributes a factor of one.
Angle encoding maps every real feature value to a distinct quantum state, so it loses no information. Correct this.
Check the period of a rotation.
No. R_y(α+2π) = −R_y(α), so the state is unchanged when a feature shifts by 2π. Values differing by 2π are indistinguishable, and a feature ranging over [0,10π] wraps five times.
Encode x=(3,4) and x=(6,8) by amplitude encoding. What is lost?
Normalise both vectors.
Both give (0.6, 0.8): the norm is discarded, so positive rescalings produce the same state. If the norm carries information it must be encoded separately.
Two encoded states have fidelity F = 0.5. What is the squared Euclidean distance between them, and what does that say about the encoding?
Use ‖ψ−ψ'‖² = 2 − 2F for unit vectors.
‖ψ−ψ'‖² = 2 − 1 = 1. For amplitude encoding this equals the squared distance between the corresponding normalised feature vectors, so the encoding preserves Euclidean geometry up to the norm.
With L=3 encoding layers of a Pauli-generated map, list the accessible frequencies in one feature.
Each layer adds one unit of frequency in either direction.
The integers from −3 to 3: seven frequencies, of which ±1, ±2, ±3 correspond to three distinct periods. Adding layers increases the count linearly.
A dataset has 1024 features per point. Compare the gate count of amplitude encoding with that of angle encoding at equal input size.
Count qubits for amplitude encoding, then count rotations for angle encoding.
Amplitude encoding uses 10 qubits and up to about 1024 two-qubit gates in the worst case; angle encoding uses 1024 qubits and 1024 rotations. Amplitude encoding is cheaper in qubits and comparable in gates here, but its worst-case preparation is exponential in qubits while its feature count is exponential too—so the crossover depends on the ratio of features to qubits.
Choose an encoding for a task with 8 continuous features in [0,1] and 500 samples, and justify the choice in three sentences.
Consider cost, geometry, and periodicity.
Angle encoding with 8 qubits fits the data range inside one period, costs eight rotations per layer, and gives a kernel that is cheap to compute but classically simulable at this size; if a classically hard kernel is needed, add entangling layers and accept the deeper circuit; amplitude encoding would need only 3 qubits but its preparation cost dominates at 500 samples.