ConceptIntermediatequantum machine learningLesson 2

Data Encoding

Data encoding maps a classical vector $x$ to a quantum state $|\psi(x)\rangle$ via an $x$-dependent circuit; the main schemes are basis, amplitude and angle encoding.

Before a quantum model can process classical data, the data must be encoded in a quantum state. This is called data encoding or loading, and it is an essential part of the model rather than a mere preliminary step, because it determines the space of functions the model can represent.

Basis encoding: a bit string $b$ is represented by the basis state $|b\rangle$. It is simple and suits binary data, but it needs one qubit per bit.

Amplitude encoding: a vector $x\in\mathbb{R}^N$ is normalised and loaded into the amplitudes: $|\psi(x)\rangle=\tfrac{1}{\|x\|}\sum_{i}x_i|i\rangle$. It needs only $\lceil\log_2N\rceil$ qubits, which is tempting, but preparing such a general state requires, in the general case, a circuit with a number of gates of order $N$, so the exponential saving in qubits may be lost to the cost of preparation. This is one of the most important practical limits of many proposed algorithms.

Angle encoding: each feature is encoded in a rotation angle on a separate qubit, such as $$R_Y(x_i)|0\rangle=\cos\tfrac{x_i}{2}|0\rangle+\sin\tfrac{x_i}{2}|1\rangle.$$ It needs one qubit per feature and a constant-depth circuit, so it suits NISQ devices. Because rotations are periodic, it is advisable to scale the features to a range such as $[0,\pi]$, as the lab does in the Iris experiment, so that different values do not coincide.

More complex feature maps, such as the ZZ map, use entangling gates whose phase depends on the product of two features, introducing non-linear interactions between features. The encoding can also be repeated between trainable layers (data re-uploading), increasing the frequencies the model can represent.

A general rule: the resulting quantum model is a function of $x$ largely determined by the encoding map. If a feature is encoded with a single Pauli rotation, any expectation value is a first-degree trigonometric polynomial in that feature, i.e. of the form $a+b\cos x+c\sin x$, whatever trainable circuit follows. So choosing the encoding is a design decision as important as choosing the architecture of a classical neural network.

In the lab you can compare angle encoding with the ZZ map on the same datasets to see the effect of encoding directly.

Mathematical formula

$$|\psi(x)\rangle=\bigotimes_{i}R_Y(x_i)|0\rangle,\qquad |\psi_{\text{amp}}(x)\rangle=\frac{1}{\|x\|}\sum_i x_i|i\rangle$$

Worked example

  1. Amplitude encoding of the vector $x=(3,4)$: $\|x\|=5$, so $|\psi\rangle=0.6|0\rangle+0.8|1\rangle$, and the measurement probabilities are $0.36$ and $0.64$. One qubit is enough for a two-dimensional vector.
  1. Angle encoding of the feature $x=\pi/2$: $R_Y(\pi/2)|0\rangle=\cos\tfrac{\pi}{4}|0\rangle+\sin\tfrac{\pi}{4}|1\rangle=|+\rangle$, so $\langle Z\rangle=\cos x=0$.
  1. For a two-feature sample $(x_1,x_2)=(0,\pi)$ with angle encoding: $R_Y(\pi)|0\rangle=|1\rangle$ for qubit 1 and $|0\rangle$ for qubit 0, so the state is $|10\rangle$ in the order $q_1q_0$.

Test your understanding

The quiz needs JavaScript.

Related topics

References

  1. Maria Schuld; Francesco Petruccione (2021). Machine Learning with Quantum Computers. Springer (Quantum Science and Technology) doi:10.1007/978-3-030-83098-4
  2. Xanadu (2025). What is quantum machine learning?. PennyLane pennylane.ai
  3. IBM Quantum (2025). Quantum Machine Learning (course). IBM Quantum Learning quantum.cloud.ibm.com