Lesson 37 · 80 min · core
Angle & Phase Encoding
Angle encoding writes each real feature into the rotation angle of one qubit, , so that and . This lesson derives these expectation values by explicit matrix algebra for and encodings, the Bloch vector of dense angle encoding (two features per qubit) and of phase encoding ( then ), and the product kernel . It then proves the Fourier-series view of Schuld, Sweke and Meyer: a model whose data enters through Pauli rotations re-uploaded times is a real trigonometric polynomial with frequencies , which explains why data re-uploading is the route to richer models.
Prerequisites: Basis Encoding, Rotation Gates & ZYZ Euler Decomposition
Objectives and key results
Encode with one (or ) per qubit and derive , , (and , , for ) by explicit matrix algebra.
Encode two features per qubit with and derive the Bloch vector .
Derive the state, Bloch vector and expectation values of phase encoding .
Prove that product , and phase encodings induce the kernel , and explain its periodicity and its data-scaling bandwidth.
Prove that a model with re-uploaded Pauli encodings is a real Fourier series with , and use it to explain data re-uploading.
- definition · Angle encoding
- formula · Dense angle encoding
- formula · Phase encoding
- theorem · Product angle kernel
- theorem · Fourier spectrum of re-uploading models
Angle encoding with single-qubit rotations#
Basis encoding (previous lesson) turns a bit string into a computational basis state, and its kernel cannot relate two different inputs. Real-valued features call for an encoding that depends continuously on the data. The simplest one uses the feature itself as a rotation angle: feature rotates qubit , so features occupy qubits, the circuit has single-qubit gates and depth , and nearby inputs give nearby states. This angle encoding (also called qubit encoding or rotation encoding) is the first layer of most variational classifiers and quantum-kernel models.
Definition
Angle encoding
Let . The angle encoding of is the -qubit product state prepared from by the depth-one circuit . Replacing by gives the angle encoding. Both use qubits and single-qubit gates, and no entangling gate.
Pauli expectation values of a qubit from its amplitudes
Let with . Then , and , and the Bloch vector has length .
Proof (6 steps)
- Step 1:swaps the two amplitudes; the inner product with is a number plus its complex conjugate.
- Step 2:Matrix–vector product with , then the inner product with .
- Step 3:For any complex , ; apply it to and use .
- Step 4:flips the sign of the amplitude.
- Step 5:.
- Step 6:The identity with , , and normalisation.
∎
Local observables on product states
If with normalised factors and is a single-qubit operator on qubit (identity elsewhere), then .
Proof (3 steps)
- Step 1:Mixed-product rule , factor by factor.
- Step 2:The inner product of two product vectors is the product of the factor inner products.
- Step 3:Every factor is normalised, so only the factor of qubit remains.
∎
State and expectation values of R_Y angle encoding
For every , and for every qubit : , , . The Bloch vector of qubit is : it lies in the -plane at polar angle .
Proof (6 steps)
- Step 1:A matrix applied to returns its first column.
- Step 2:Mixed-product rule; this proves the product form of .
- Step 3:By the product-state lemma only qubit matters; the amplitudes are real, and .
- Step 4:Double-angle formula .
- Step 5:With real amplitudes is real, so , in agreement with the lemma.
- Step 6:Collect the three components; this is the point of the Bloch sphere with polar angle and azimuth (for ).
∎
Expectation values of R_X angle encoding
, and in this state , , . The Bloch vector rotates in the -plane.
Proof (6 steps)
- Step 1:First column of .
- Step 2:Conjugate transpose; are real.
- Step 3:.
- Step 4:swaps the amplitudes: .
- Step 5:with , , then the double-angle formula.
- Step 6:For the data moves the Bloch vector in the -plane, so the -component carries no information about ; for it is the -component that vanishes.
∎
Worked example: x = (0.3, 1.2)
- Step 1:Half-angles of the two features, to six decimals.
- Step 2:Expand the tensor product; qubit is the left symbol of each ket.
- Step 3:The four real amplitudes in the order ; their squares sum to .
- Step 4:The theorem applied qubit by qubit; these are exactly the values the lab grader expects at its first datum.
Result
Circuit · 2 qubits
Angle encoding of : on qubit and on qubit , one layer, no entangling gate. The statevector shows the four real amplitudes derived above.
Quick check
A single feature is encoded as . What is ?
Dense angle encoding: two features per qubit#
A pure qubit state has two real degrees of freedom, the polar and azimuthal angles of its Bloch vector, but angle encoding uses only one of them. Dense angle encoding (LaRose and Coyle 2020) uses both: one feature sets the polar angle with , the next one the azimuth with . This halves the number of qubits at the price of one more gate per qubit.
Definition
Dense angle encoding
For the dense angle encoding is the -qubit product state i.e. qubit is first rotated by and then by . A vector with an odd number of features is padded with one zero, so qubits and at most single-qubit gates suffice.
Bloch vector of dense angle encoding
Write and . Qubit of is with Bloch vector : polar angle , azimuth .
Proof (7 steps)
- Step 1:First column of , as in the angle-encoding theorem.
- Step 2:is diagonal and multiplies each amplitude by its phase.
- Step 3:Factor out ; this global phase has no physical effect.
- Step 4:With and ; and .
- Step 5:Pauli-expectation lemma with .
- Step 6:The phases have modulus , so , .
- Step 7:These are the spherical coordinates of the unit vector with polar angle and azimuth ; by the product-state lemma the other qubits do not affect .
∎
Kernel of dense angle encoding
For single-qubit pure states with Bloch vectors , . Hence dense angle encoding has the kernel
Proof (6 steps)
- Step 1:Bloch representation of a pure qubit state, with (lemma above).
- Step 2:Write the squared modulus as a trace of the two projectors.
- Step 3:Expand the product and use linearity of the trace.
- Step 4:Pauli matrices are traceless and .
- Step 5:Dot product of the two Bloch vectors from the theorem and .
- Step 6:The overlap of product states factorises over qubits; the global phases drop out of the modulus.
∎
Quick check
A data set has real features. How many qubits does dense angle encoding need, compared with plain angle encoding?
Phase encoding#
A diagonal gate applied to only multiplies it by a phase, so encoding into the relative phase requires a superposition first. Phase encoding applies a Hadamard and then the phase gate , which places the datum in the azimuth of a Bloch vector on the equator.
Definition
Phase encoding
For the phase encoding is It uses qubits and single-qubit gates in depth .
State, Bloch vector and expectation values of phase encoding
, its Bloch vector is , so , , . Moreover , so is the same physical state.
Proof (6 steps)
- Step 1:First column of .
- Step 2:The phase gate multiplies the amplitude by .
- Step 3:Euler's formula.
- Step 4:Pauli-expectation lemma; both amplitudes have modulus .
- Step 5:Multiply the diagonal entries of by .
- Step 6:The two preparations differ by the global phase , which no measurement can detect; the platform's QML engine implements phase encoding as .
∎
Circuit · 2 qubits
Phase encoding of : Hadamards create , then and write the features into the relative phases. All four amplitudes have modulus ; only their phases depend on the datum.
The kernel of a product angle encoding#
The fidelity kernel of a feature map measures how similar two encoded inputs are (it is studied in depth in the quantum-kernel module). For product encodings it can be computed in closed form, one qubit at a time.
Product angle kernel
Let with for every qubit, or for every qubit. Then for all Each factor is -periodic in , , and if and only if for every . Phase encoding has the same kernel.
Proof (8 steps)
- Step 1:The inner product of product states is the product of the single-qubit inner products, with .
- Step 2:is Hermitian, so .
- Step 3:Exponentials of multiples of the same operator commute and add their exponents.
- Step 4:is the top-left entry, which is for both and .
- Step 5:Combine the previous steps with and take the squared modulus of a real number.
- Step 6:Half-angle identity: each factor is a -periodic function of with values in , hence .
- Step 7:A product of numbers in equals only if every factor equals .
- Step 8:Phase encoding: and , so the per-qubit overlap is ; then .
∎
Data scaling sets the kernel bandwidth
- Step 1:Encoding the rescaled features with (this is the scale option of the angle feature map) multiplies every angle difference by .
- Step 2:From and with : .
- Step 3:Insert : and ; sum over .
- Step 4:Locally the angle kernel is a Gaussian (RBF) kernel with .
- Step 5:For every factor tends to , so the kernel flattens to . For large the first zero of each factor moves toward and the factor oscillates many times across the range of the data, so it no longer decreases with distance; over differences spread across many periods its mean is , and off-diagonal entries are typically of order . With many qubits distinct training points are then nearly orthogonal, (memorisation, as in basis encoding), and the kernel has lost its notion of locality. Because each factor is -periodic in , has no limit as ; the effect is this loss of locality, not convergence. The scale is a hyperparameter, exactly like the width of an RBF kernel.
Quick check
Compute the angle-encoding kernel for and (four decimals).
import { Statevector, expectationPauli } from '@/lib/quantum-simulator';
// Angle encoding |phi(x)> = RY(x0)|0> (x) RY(x1)|0>; qubit 0 is the most significant bit,
// so the Pauli word 'ZI' is Z on qubit 0 and 'IX' is X on qubit 1.
const encode = (gate, x) => {
const sv = Statevector.zero(x.length);
x.forEach((xj, j) => sv.applyGate(gate, [j], [xj]));
return sv;
};
const fmt = (v) => (Math.abs(v) < 1e-12 ? 0 : v).toFixed(6);
const ev = (sv, ops) => fmt(expectationPauli(sv, { coeff: 1, ops }));
const x = [0.3, 1.2];
const phi = encode('RY', x);
for (const ops of ['ZI', 'XI', 'YI', 'IZ', 'IX']) console.log('<' + ops + '> = ' + ev(phi, ops));
const viaRx = encode('RX', x);
console.log('RX: <ZI> = ' + ev(viaRx, 'ZI') + ', <YI> = ' + ev(viaRx, 'YI') + ', <XI> = ' + ev(viaRx, 'XI'));
// Fidelity kernel k(x, x') = |<phi(x')|phi(x)>|^2 against the product formula.
const xp = [1.0, 0.4];
const k = phi.fidelity(encode('RY', xp));
const product = x.reduce((p, xj, j) => p * Math.cos((xj - xp[j]) / 2) ** 2, 1);
console.log('k(x, xp) = ' + k.toFixed(10) + ', product formula = ' + product.toFixed(10));Expected output
<ZI> = 0.955336 <XI> = 0.295520 <YI> = 0.000000 <IZ> = 0.362358 <IX> = 0.932039 RX: <ZI> = 0.955336, <YI> = -0.295520, <XI> = 0.000000 k(x, xp) = 0.7486048950, product formula = 0.7486048950
The same computation in NumPy builds the three encoded states of the data set with Kronecker products, forms the Gram matrix , checks it against the product formula, and confirms that it is positive definite.
import numpy as np
def ry(t):
c, s = np.cos(t / 2), np.sin(t / 2)
return np.array([[c, -s], [s, c]])
def encode(x):
psi = np.array([1.0])
for xj in x:
psi = np.kron(psi, ry(xj) @ np.array([1.0, 0.0]))
return psi
Z = np.array([[1.0, 0.0], [0.0, -1.0]])
X = np.array([[0.0, 1.0], [1.0, 0.0]])
I2 = np.eye(2)
data = np.array([[0.3, 1.2], [1.0, 0.4], [2.5, -0.7]])
psi = encode(data[0])
print(f"<Z0> = {psi @ np.kron(Z, I2) @ psi:.6f} cos(x0) = {np.cos(data[0, 0]):.6f}")
print(f"<X1> = {psi @ np.kron(I2, X) @ psi:.6f} sin(x1) = {np.sin(data[0, 1]):.6f}")
states = np.array([encode(x) for x in data])
K = np.abs(states @ states.T) ** 2
delta = data[:, None, :] - data[None, :, :]
K_formula = np.prod(np.cos(delta / 2) ** 2, axis=2)
print(np.round(K, 6))
print(bool(np.allclose(K, K_formula)))
print(f"min eigenvalue of K: {np.linalg.eigvalsh(K).min():.6f}")Expected output
<Z0> = 0.955336 cos(x0) = 0.955336 <X1> = 0.932039 sin(x1) = 0.932039 [[1. 0.748605 0.069616] [0.748605 1. 0.389105] [0.069616 0.389105 1. ]] True min eigenvalue of K: 0.183300
The Fourier view of angle-encoded models#
A variational model does not stop at the encoding: trainable unitaries act before and after it, and a measurement turns the state into a number. Schuld, Sweke and Meyer (2021) showed that the functions such a model can represent are determined by the eigenvalues of the encoding generator: the model is always a Fourier series, and its frequencies are differences of those eigenvalues. We prove this in full for one input feature.
Definition
Quantum model with data re-uploading
Fix a Hermitian encoding generator on and the encoding gate for . For trainable unitaries and a Hermitian observable , the quantum model with uploads is Angle encoding is the case (or ), i.e. for a Pauli matrix . The FourierSpectrum widget below simulates the one-qubit instance , or , .
The encoding gate is diagonal in the eigenbasis of its generator
If is a spectral decomposition (orthonormal eigenbasis , real eigenvalues ), then .
Proof (4 steps)
- Step 1:Spectral theorem for the Hermitian matrix .
- Step 2:Induction on : by orthonormality; by completeness.
- Step 3:Definition of the matrix exponential by its absolutely convergent power series, then exchange of the finite sum over with the series.
- Step 4:The inner series is the scalar exponential.
∎
One upload: the model is a Fourier series
For , with frequency spectrum and coefficients , where . For a Pauli encoding the spectrum is , so .
Proof (6 steps)
- Step 1:Insert the eigen-expansion of from the lemma; the scalar phases move to the front.
- Step 2:Conjugate transpose: because and are real.
- Step 3:Bilinearity of the inner product.
- Step 4:Group the terms by the value of the exponent .
- Step 5:An eigenvalue of satisfies ; tracelessness forces one eigenvalue of each sign.
- Step 6:All differences of the two eigenvalues; hence .
∎
L uploads: spectrum {-L, ..., L}
For uploads, with multi-indices and , with . For a Pauli encoding , , which has elements: is a trigonometric polynomial of degree at most .
Proof (7 steps)
- Step 1:Replace each of the encoding gates by its eigen-expansion (lemma).
- Step 2:Multiply out the sums (distributivity of the matrix product) and collect the scalar phases.
- Step 3:Apply to and use the definitions of and .
- Step 4:Same computation as for one upload, with the bra ; group the terms by .
- Step 5:Pauli case: each of the eigenvalues is ; let count the positive ones.
- Step 6:The constant cancels; a difference of two integers in lies in .
- Step 7:Every value is attained: choose with all eigenvalues and with exactly of them equal to . Hence .
∎
Hermitian symmetry of the coefficients
The coefficients satisfy for every , in particular . Consequently is real for every real .
Proof (5 steps)
- Step 1:and .
- Step 2:Conjugate term by term and relabel ; the condition becomes . Note that .
- Step 3:Special case of the symmetry.
- Step 4:Pair each positive frequency with its negative; .
- Step 5:Multiply by and keep the real part.
∎
Worked example: L = 1, W = I, R_X encoding, M = Z
- Step 1:Eigenbasis of the generator: and .
- Step 2:.
- Step 3:maps to .
- Step 4:only for , only for , and for and .
- Step 5:Euler's formula. This is exactly for encoding derived in the first section, and as required by the symmetry.
Result
With and every trainable block frozen at the identity, the widget shows and the two bars of the example. The next widget re-uploads three times with randomly drawn blocks : the bars now occupy and never appear beyond , whatever the seed.
The spectrum is an upper bound; the trainable blocks set the coefficients
(i) With , and , the -upload model is : only are nonzero. (ii) Encoding once on each of qubits in parallel, , gives the same spectrum as sequential uploads on one qubit.
Proof (5 steps)
- Step 1:Identity blocks leave consecutive rotations about the same axis, whose angles add.
- Step 2:The proposition with angle : of the available frequencies only carry weight. The spectrum says which frequencies can appear; the blocks decide their coefficients.
- Step 3:The operators act on different qubits and commute, so the product of exponentials is the exponential of the sum: the generator is .
- Step 4:The product basis of -eigenvectors diagonalises the sum; with positive signs the eigenvalue is .
- Step 5:The same eigenvalue set as in the sequential case, so the one-upload theorem with this gives the same spectrum: width (qubits) can be traded for depth (uploads).
∎
Quick check
A one-qubit model re-uploads four times through , with trainable blocks between the uploads. How many frequencies does its spectrum contain?
Quick check
Take uploads of on one qubit with every trainable block equal to , and measure . Which Fourier coefficients of are nonzero?
Data re-uploading and expressivity#
A single upload cannot represent cos 2x
Every one-upload Pauli model is of the form with real . In particular no choice of gives ; more generally a model with uploads cannot represent a function with a nonzero Fourier coefficient at a frequency .
Proof (5 steps)
- Step 1:The one-upload theorem gives ; the symmetry proposition turns the series into this real form.
- Step 2:For the integrand is a full number of periods of a complex exponential and integrates to ; for it is .
- Step 3:Project the model onto using the orthogonality relation.
- Step 4:The same projection of the target function.
- Step 5:Equal functions have equal Fourier coefficients. The same argument with excludes every frequency .
∎
Re-uploading as the route to richer models. Pérez-Salinas, Cervera-Lierta, Gil-Fuster and Latorre (2020) proposed to re-upload the data many times, interleaving encoding gates with trainable gates, and showed that even a single qubit then becomes a flexible classifier. The spectrum theorem explains why: each upload enlarges the frequency set by one, , so the model class grows from degree-one trigonometric polynomials to degree- ones. Their result can be stated as follows.
Statement (Pérez-Salinas et al. 2020). A single qubit processed by layers , in which the data is re-uploaded in every layer and are trainable, can approximate a broad class of classification boundaries, with an expressive power that grows with the number of layers; adding qubits and entanglement reduces the number of layers needed.
Two facts proved in this lesson make the statement precise in one dimension. First, the degree bound: with uploads only frequencies exist, so re-uploading is necessary to fit high-frequency structure. Second, the coefficient picture: the coefficients are functions of the trainable blocks. Schuld, Sweke and Meyer show that when the trainable blocks are arbitrary unitaries on a large enough register and the observable is arbitrary, the coefficients of any real trigonometric polynomial with frequencies in the spectrum can be realised, up to a normalisation imposed by (a single qubit with reaches only a subset of them); combined with the Weierstrass approximation theorem for trigonometric polynomials (every continuous -periodic function is a uniform limit of them), re-uploading models with growing are universal approximators for periodic functions. For features the same expansion, applied to every encoding gate, gives frequency vectors with when feature is uploaded times; a product encoding without re-uploading measured in gives, for example, , which contains only the frequency vectors .
The following NumPy experiment fixes four blocks , evaluates of the re-uploading model at equally spaced points, and computes the coefficients with the FFT (exact here, because is a trigonometric polynomial of degree ). Each row lists for and checks : the support grows exactly to .
import numpy as np
def rz(a):
return np.diag([np.exp(-0.5j * a), np.exp(0.5j * a)])
def ry(a):
c, s = np.cos(a / 2), np.sin(a / 2)
return np.array([[c, -s], [s, c]])
def rx(a):
c, s = np.cos(a / 2), np.sin(a / 2)
return np.array([[c, -1j * s], [-1j * s, c]])
def W(phi, theta, gamma):
return rz(gamma) @ ry(theta) @ rz(phi)
Z = np.diag([1.0, -1.0])
angles = [(0.4, 1.3, 2.2), (2.9, 0.7, 1.6), (1.1, 2.4, 0.3), (0.8, 1.9, 2.7)]
def f(x, L):
psi = W(*angles[0]) @ np.array([1.0, 0.0])
for l in range(1, L + 1):
psi = W(*angles[l]) @ (rx(x) @ psi)
return float(np.real(psi.conj() @ Z @ psi))
N = 16
xs = 2 * np.pi * np.arange(N) / N
for L in (1, 2, 3):
c = np.fft.fft([f(x, L) for x in xs]) / N
mags = np.abs([c[w % N] for w in range(-4, 5)])
hermitian = np.allclose([c[w % N] for w in range(1, 5)], np.conj([c[-w % N] for w in range(1, 5)]))
print(L, np.round(mags, 4), bool(hermitian))Expected output
1 [0. 0. 0. 0.3213 0.3547 0.3213 0. 0. 0. ] True 2 [0. 0. 0.1534 0.1432 0.509 0.1432 0.1534 0. 0. ] True 3 [0. 0.0253 0.1944 0.2717 0.1469 0.2717 0.1944 0.0253 0. ] True
References
- M. Schuld, R. Sweke and J. J. Meyer, Effect of data encoding on the expressive power of variational quantum-machine-learning models, Physical Review A 103, 032430 (2021).doi:10.1103/PhysRevA.103.032430
- A. Pérez-Salinas, A. Cervera-Lierta, E. Gil-Fuster and J. I. Latorre, Data re-uploading for a universal quantum classifier, Quantum 4, 226 (2020).doi:10.22331/q-2020-02-06-226
- R. LaRose and B. Coyle, Robust data encodings for quantum classifiers, Physical Review A 102, 032420 (2020) (dense angle encoding).doi:10.1103/PhysRevA.102.032420
- M. Schuld and F. Petruccione, Machine Learning with Quantum Computers, 2nd ed., Springer (2021), Ch. 4 "Representing Data on a Quantum Computer" and Ch. 5 "Variational Circuits as Machine Learning Models".doi:10.1007/978-3-030-83098-4
- M. Schuld and N. Killoran, Quantum machine learning in feature Hilbert spaces, Physical Review Letters 122, 040504 (2019).doi:10.1103/PhysRevLett.122.040504
- V. Havlíček, A. D. Córcoles, K. Temme, A. W. Harrow, A. Kandala, J. M. Chow and J. M. Gambetta, Supervised learning with quantum-enhanced feature spaces, Nature 567, 209–212 (2019).doi:10.1038/s41586-019-0980-2
Hands-on labs
Angle-encode a two-feature datum
1 step · auto-gradedOpen labWrite the angle-encoding feature map for a datum as a circuit that uses the data symbols , so that it works for every datum. The grader re-binds the symbols to three data points and checks and on both qubits.