|q⟩ Bad Qubits

intermediate · Programming · Quantum Machine Learning (Intro)

Angle and Amplitude Encoding

Every quantum machine learning (QML) algorithm must begin by loading classical data into a quantum state. The choice of encoding determines how the data's structure is represented, what circuits are needed, and — ultimately — whether any quantum advantage is achievable. Two strategies appear in almost every QML paper: angle encoding and amplitude encoding.

Angle encoding

In angle encoding each feature xiRx_i \in \mathbb{R} is stored in the rotation angle of a single qubit. Applying Ry(xi)R_y(x_i) to 0|0\rangle gives

Ry(xi)0=cos ⁣(xi2)0+sin ⁣(xi2)1.R_y(x_i)|0\rangle = \cos\!\left(\tfrac{x_i}{2}\right)|0\rangle + \sin\!\left(\tfrac{x_i}{2}\right)|1\rangle.

A feature vector x=(x0,x1,,xn1)\mathbf{x} = (x_0, x_1, \ldots, x_{n-1}) is loaded into nn qubits with one RyR_y gate per feature:

ψ(x)=i=0n1Ry(xi)0.|\psi(\mathbf{x})\rangle = \bigotimes_{i=0}^{n-1} R_y(x_i)|0\rangle.

The resulting state is a product state — no entanglement is introduced during the encoding step itself. The circuit depth is O(n)O(n) and each gate acts on a single qubit, making angle encoding hardware-friendly and straightforward to implement.

The cost is that you need one qubit per feature. For a dataset with n=100n = 100 features you need a 100-qubit device.

Amplitude encoding

Amplitude encoding takes a different approach: it packs an entire classical vector into the amplitudes of a single quantum state. Given a normalised vector xRN\mathbf{x} \in \mathbb{R}^N with x=1\|\mathbf{x}\| = 1, the encoded state is

ψ(x)=i=0N1xii.|\psi(\mathbf{x})\rangle = \sum_{i=0}^{N-1} x_i |i\rangle.

Here N=2nN = 2^n amplitudes fit into nn qubits, so amplitude encoding is exponentially more compact in qubit count: 100 features need only log2100=7\lceil\log_2 100\rceil = 7 qubits.

The catch is preparation cost. Building an arbitrary nn-qubit state from 0n|0\rangle^{\otimes n} generally requires a circuit of depth O(2n)O(2^n) — the exponential saving in qubits can be cancelled by an exponential overhead in gates unless the data vector has special structure (e.g. a quantum random-access memory is available).

Side-by-side comparison

| Property | Angle encoding | Amplitude encoding | |---|---|---| | Qubits required | nn (one per feature) | log2n\lceil\log_2 n\rceil | | Circuit depth | O(n)O(n) | O(n)O(n) in general | | Entanglement during load | None (product state) | Required | | Normalisation needed | No (per-feature) | Yes (x=1\|\mathbf{x}\|=1) | | Hardware-friendly | Yes | Challenging on NISQ |

Neither strategy is universally superior. For near-term devices with limited depth, angle encoding is practical. For future fault-tolerant processors where state preparation is cheap, amplitude encoding can exploit the exponential compression.

Try it

Angle-encode the feature vector x=(π/3,π/4)\mathbf{x} = (\pi/3,\, \pi/4) into a 2-qubit circuit by applying Ry(π/3)R_y(\pi/3) to qubit 0 and Ry(π/4)R_y(\pi/4) to qubit 1. The grader checks the resulting statevector.

Run your code to see the quantum state.

After running, inspect the amplitudes. Qubit 0 has cos(π/6)0.866\cos(\pi/6) \approx 0.866 on 0|0\rangle and sin(π/6)=0.5\sin(\pi/6) = 0.5 on 1|1\rangle; qubit 1 has cos(π/8)0.924\cos(\pi/8) \approx 0.924 and sin(π/8)0.383\sin(\pi/8) \approx 0.383. The full two-qubit product state combines these via the tensor product.

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