|q⟩ Bad Qubits

intermediate · Programming · Quantum Machine Learning (Intro)

Encoding Classical Data

Before a quantum computer can process classical data, that data must be encoded into a quantum state. This translation step — sometimes called a feature map or data embedding — is one of the most important design choices in quantum machine learning (QML). Different encodings trade off circuit depth, expressibility, and how much classical information fits into the state.

Why encoding matters

A classical dataset is a list of real-valued feature vectors. A quantum circuit operates on qubits, which are complex unit vectors in Hilbert space. Encoding bridges the two worlds by choosing a map

ϕ:xRd    ϕ(x)H.\phi : \mathbf{x} \in \mathbb{R}^d \;\longrightarrow\; |\phi(\mathbf{x})\rangle \in \mathcal{H}.

The encoded state ϕ(x)|\phi(\mathbf{x})\rangle is then processed by a parameterized quantum circuit (the variational ansatz) whose output is measured to produce a prediction. The encoding determines which transformations of the data are "easy" for the quantum circuit to implement and which kernel functions become efficiently computable.

Angle encoding

The simplest and most hardware-friendly strategy is angle encoding: assign one qubit per feature and rotate that qubit by an angle equal to the feature value. For a single feature xRx \in \mathbb{R} and the RyR_y rotation gate,

Ry(θ)=(cos(θ/2)sin(θ/2)sin(θ/2)cos(θ/2)),R_y(\theta) = \begin{pmatrix} \cos(\theta/2) & -\sin(\theta/2) \\ \sin(\theta/2) & \cos(\theta/2) \end{pmatrix},

the encoded single-qubit state starting from 0|0\rangle is

ϕ(x)=Ry(x)0=cos ⁣(x2)0+sin ⁣(x2)1.|\phi(x)\rangle = R_y(x)|0\rangle = \cos\!\left(\tfrac{x}{2}\right)|0\rangle + \sin\!\left(\tfrac{x}{2}\right)|1\rangle.

For a dd-feature vector x=(x0,x1,,xd1)\mathbf{x} = (x_0, x_1, \ldots, x_{d-1}) we use dd qubits and apply Ry(xj)R_y(x_j) to qubit jj independently:

ϕ(x)=j=0d1Ry(xj)0.|\phi(\mathbf{x})\rangle = \bigotimes_{j=0}^{d-1} R_y(x_j)|0\rangle.

Because the qubits are not entangled, the full state is a product state. The Born rule then tells us that the probability of measuring qubit jj in state 1|1\rangle is

Pj(1)=sin2 ⁣(xj2),P_j(1) = \sin^2\!\left(\tfrac{x_j}{2}\right),

which varies smoothly with xjx_j. This is directly useful: a downstream parameterized layer can learn to weight these probabilities differently for different classes.

Expressibility and limitations

Angle encoding with a single RyR_y rotation per qubit traces a great circle on the Bloch sphere (all amplitudes remain real; no relative phase is introduced). Extending to two angles per qubit — for example Rz(ϕ)Ry(θ)R_z(\phi)R_y(\theta) — reaches any point on the Bloch sphere, but doubles the number of parameters and the circuit depth. The single-rotation variant used here sacrifices that extra expressibility in exchange for simplicity. Either way, angle encoding requires as many qubits as features. For large datasets (millions of features) that is impractical on near-term devices. Amplitude encoding addresses this by storing 2n2^n features in nn qubits, but at the cost of a deep state-preparation circuit. For the moderate-size problems that current quantum hardware can address, angle encoding is the standard starting point.

Try it

Encode the two-feature vector (x0,x1)=(π/3,π/4)(x_0, x_1) = (\pi/3, \pi/4) into a two-qubit state using angle encoding. Apply Ry(x0)R_y(x_0) to qubit 0 and Ry(x1)R_y(x_1) to qubit 1. The grader checks the full statevector.

Run your code to see the quantum state.

After a correct run you should see qubit 0 in the state cos(π/6)0+sin(π/6)1\cos(\pi/6)|0\rangle + \sin(\pi/6)|1\rangle and qubit 1 in cos(π/8)0+sin(π/8)1\cos(\pi/8)|0\rangle + \sin(\pi/8)|1\rangle. The measurement probability for qubit 0 in 1|1\rangle is sin2(π/6)=1/4\sin^2(\pi/6) = 1/4, and for qubit 1 it is sin2(π/8)0.146\sin^2(\pi/8) \approx 0.146.

Sign in on the full site to ask questions and join the discussion.