Lesson 35 · 70 min · core
Basis Encoding
Basis encoding stores a classical bit string as the computational basis state of qubits. This lesson proves by induction that this state is the basis vector with the big-endian index , counts its cost ( qubits, at most gates, depth ), encodes real numbers in fixed point with truncation error at most , derives when the dataset superposition is normalised and prepares it with gates, discusses the qRAM assumption, and proves that basis encoding induces the kernel , which cannot generalise to unseen inputs.
Prerequisites: The Qubit, Pauli Gates & the Pauli Algebra
Objectives and key results
Prove by induction on that with (qubit 0 = most significant bit).
Count the resources of basis encoding: qubits, gates, depth .
Encode a real number in with fixed-point bits, prove the truncation bound , and size a register of qubits for a -dimensional vector.
Show that is normalised if and only if the patterns are distinct, and prepare it with an incremental flag-qubit procedure using gates.
State the qRAM assumption behind data access and its hardware caveats.
Prove that the basis-encoding fidelity kernel is , so kernel ridge regression predicts only its bias at every unseen input.
- definition · Basis encoding
- formula · Fixed-point truncation error
- theorem · Dataset superposition
- algorithm · Incremental preparation
- theorem · Sparsity of the basis-encoding kernel
Bit strings as computational basis states#
A quantum model can only process classical data after the data has been written into a quantum state. The simplest such feature map treats a bit string as the label of a computational basis state: the bits of are copied one-to-one onto qubits. Binary data (categorical features, integers, fixed-point reals) is already in this form, and many textbook algorithms (Grover search over a database, arithmetic circuits, the qRAM model) assume that inputs are presented this way. In this lesson we make the encoding precise, prove that the circuit realising it is correct, count its cost, and then show its main weakness as a machine-learning feature map.
Definition
Basis encoding
Let . Its basis encoding is the -qubit state in which qubit carries bit . It is prepared from by the unitary with and , i.e. by one Pauli- gate on every qubit whose bit equals .
Definition
Big-endian index map
The index of is so qubit is the leftmost symbol of the ket and the most significant bit of the index. We write for the -th standard basis vector of (entries counted from ). Because every integer in has exactly one -digit binary representation, is a bijection .
The basis-encoding circuit prepares e_k(x)
For every and every , Consequently the encoded states are exactly the computational basis of .
Proof (8 steps)
- Step 1:Base case: for one qubit , and by direct matrix multiplication.
- Step 2:Induction step: assume the claim for qubits and let be without its last bit.
- Step 3:Group the first factors, write , and use the mixed-product rule .
- Step 4:Induction hypothesis for the first factor, base case for the last.
- Step 5:Definition of the Kronecker product of a -vector with a -vector: entry is , which is only for , the given bit.
- Step 6:Split off the last term and factor out of the others (Horner's rule).
- Step 7:Combine the last three lines; by induction the claim holds for all .
- Step 8:is a bijection onto , so the encoded states are all standard basis vectors, each exactly once.
∎
Cost of basis encoding
Preparing from uses qubits, exactly gates (the Hamming weight of ), and depth (depth for ). Moreover for all , and .
Proof (5 steps)
- Step 1:The factors with are identities and need no gate.
- Step 2:Gates on disjoint qubits commute and can be executed in parallel, so the circuit has a single layer.
- Step 3:For nothing changes; for , swaps , which is addition of modulo .
- Step 4:Mixed-product rule on the product state .
- Step 5:; an operator that fixes every basis vector is the identity. In particular , so the same circuit also un-computes the encoding.
∎
Worked example: x = 1011#
The state |1011⟩ as a vector of C¹⁶
- Step 1:One on qubits , and ; qubit is left alone. Three gates, depth .
- Step 2:Big-endian index map: qubit is weighted by .
- Step 3:First two qubits: .
- Step 4:Append qubits and with the rule (the induction step of the theorem).
- Step 5:The full amplitude vector: a single at position (counting from ) and zeros. Measuring returns with probability .
- Step 6:Reading the bits in the opposite order (little-endian) addresses the basis vector , a different state orthogonal to — the most common mistake in the lab.
Result
Circuit · 4 qubits
Basis encoding of : on qubits , and . The statevector has the single amplitude at index .
Quick check
With qubit the most significant bit, what is the index of the basis state in ?
import { Statevector } from '@/lib/quantum-simulator';
// Basis-encode x = 1011 on 4 qubits: one X gate on every qubit j with x_j = 1.
// Convention: qubit 0 is the leftmost bit and the most significant bit of the index.
const bits = '1011';
const sv = Statevector.zero(bits.length);
bits.split('').forEach((b, j) => {
if (b === '1') sv.applyGate('X', [j]);
});
const amps = sv.amplitudes();
const nonzero = amps.flatMap((a, k) => (Math.hypot(a.re, a.im) > 1e-12 ? [k] : []));
console.log('nonzero amplitude index: ' + nonzero.join(', '));
console.log('k(x) = ' + bits.split('').reduce((k, b) => 2 * k + Number(b), 0));
console.log('amplitude: ' + amps[11].re + ' + ' + amps[11].im + 'i');
const reversed = bits.split('').reverse().join('');
console.log('reversed (little-endian) reading: ' + reversed + ' = ' + parseInt(reversed, 2));Expected output
nonzero amplitude index: 11 k(x) = 11 amplitude: 1 + 0i reversed (little-endian) reading: 1101 = 13
The same construction in NumPy builds as an explicit Kronecker product of the one-qubit vectors , exactly following the induction in the theorem.
import numpy as np
ket0 = np.array([1, 0])
X = np.array([[0, 1], [1, 0]])
bits = '1011'
psi = np.array([1])
for b in bits:
psi = np.kron(psi, X @ ket0 if b == '1' else ket0)
print(psi)
print(int(np.argmax(psi)), int(bits, 2), int(bits[::-1], 2))Expected output
[0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0] 11 11 13
Encoding real numbers in fixed point#
Definition
Fixed-point binary encoding
Every has a binary expansion with digits (the expansion that does not end in infinitely many s). With bits of precision we keep the first digits, and basis-encode the string on qubits, on the most significant qubit. The digits are produced by repeated doubling: , , .
Truncation error of τ-bit fixed point
For every and , the truncation satisfies and Hence bits guarantee an error below for every , and fewer bits do not. (Rounding to the nearest multiple of instead of truncating halves the bound to , at the price of one extra bit for values that round up to .)
Proof (6 steps)
- Step 1:Subtract the first terms from the full expansion; every remaining term is non-negative.
- Step 2:Each digit satisfies .
- Step 3:Geometric series with first term and ratio .
- Step 4:Equality in the digit-wise bound requires every tail digit to be , which the chosen expansion excludes; hence the inequality is strict: .
- Step 5:Multiply by : the first sum is an integer and the remainder lies in , so it is the fractional part.
- Step 6:The supremum is approached by with , whose error is ; so the guarantee holds exactly when , i.e. by monotonicity of . For example : , so and , whereas .
∎
Example: x = 0.7 with τ = 5 bits
- Step 1:Doubling algorithm, first two digits.
- Step 2:Digits three to five.
- Step 3:Sum the kept digits; this agrees with since .
- Step 4:The truncation bound of the theorem.
- Step 5:Basis-encode the digit string with on qubit : index .
Result
Register size for a real vector
Encoding with bits per component uses qubits, at most gates and depth ; component occupies qubits , and the total index is . Every component is reproduced to within .
Proof (4 steps)
- Step 1:Concatenate the bit strings of length ; the register is the tensor product of blocks of qubits, i.e. qubits.
- Step 2:Apply the cost proposition to the concatenated string of length .
- Step 3:Digit sits on qubit ; apply the index map with and factor .
- Step 4:The inner sum is the integer of the -bit block (fixed-point theorem); the error bound applies to every component separately.
∎
Other ranges and signs. A feature in is first rescaled to , which multiplies the error bound by . Signed values in use two's complement: with bits the value is , the sign bit carrying weight ; for example is with , and the resolution is because one bit is spent on the sign. Two's complement is what reversible arithmetic circuits (adders, multipliers) expect, so a basis-encoded register can be fed directly into quantum arithmetic.
Quick check
A data point has real features, each basis-encoded with fixed-point bits. How many qubits does the register need?
Quick check
What is the smallest number of fixed-point bits that guarantees a truncation error below for every ?
Encoding a whole dataset in superposition#
Definition
Dataset superposition
Given patterns , the dataset state is It stores the whole training set in one -qubit register: measuring it returns each stored pattern with probability . For a pattern let be its multiplicity, so .
The dataset state is normalised iff the patterns are distinct
, with equality if and only if the patterns are pairwise distinct. With repetitions the normalised state is .
Proof (6 steps)
- Step 1:Expand the inner product bilinearly (all coefficients are real and equal to ).
- Step 2:Encoded states are orthonormal (they are distinct standard basis vectors by the basis-encoding theorem).
- Step 3:Group the ordered pairs by their common pattern ; there are pairs for each .
- Step 4:For a non-negative integer, , with equality iff .
- Step 5:Equality holds term by term iff no pattern occurs twice.
- Step 6:Collect equal kets and divide by the norm . Its Born probabilities are not the empirical frequencies ; to sample with frequencies one uses amplitudes instead.
∎
An incremental preparation procedure. Ventura and Martinez (2000) proposed to build one pattern at a time: a flag qubit marks an "active" branch that still carries the not-yet-distributed amplitude, and in step a controlled rotation splits off the share of the active branch and freezes it as a stored pattern. We use the following streamlined variant on qubits: data qubits (initially ), a flag qubit , and ancillas for the multi-controlled rotation. Set and .
- Apply to the flag, giving .
- For :
- Load — for every with apply ; this rewrites the data register of the active (flag ) branch from to .
- Split — apply to the flag, controlled on the data register being equal to (negate the zero-controls with gates, compute the AND of the data qubits into the last ancilla with a ladder of Toffoli gates, apply a controlled from that ancilla, then uncompute the ladder and the gates).
- The output is .
Correctness of the incremental preparation
For pairwise distinct the procedure above outputs (ancillas in ). Before the load of iteration the state is
Proof (10 steps)
- Step 1:After step 1 the invariant holds for : the stored sum is empty and .
- Step 2:Each CNOT acts as the identity when the flag is and as when it is ; the product of the is the Pauli string .
- Step 3:Stored terms have flag and are unchanged; on the active term by the cost proposition.
- Step 4:The split is the rotation controlled on the data register equal to . Distinctness gives , so every stored term lies in the kernel of and is untouched.
- Step 5:Second column of , using .
- Step 6:Definition of ; , so the cosine is non-negative.
- Step 7:Multiply the active amplitude by the two rotation coefficients: exactly the share is split off.
- Step 8:The new flag- term joins the stored sum; the invariant holds for .
- Step 9:At the active amplitude vanishes (the last rotation is , which maps ).
- Step 10:After the zero-controls are negated, the ladder writes the AND of data bits into ancilla , so the last ancilla is exactly on the branch whose data register equals ; the controlled does not touch the data or ancilla qubits, and the reversed ladder (Toffoli is self-inverse) restores every ancilla to on every branch, so the ancillas factor out.
∎
Gate count O(Mn)
The procedure uses qubits and at most gates from , hence gates; compiled to one- and two-qubit gates the count is still .
Proof (5 steps)
- Step 1:One ancilla per Toffoli of the AND ladder (none for , where the single data qubit controls the rotation directly).
- Step 2:Load step: one CNOT per differing bit (Hamming distance).
- Step 3:Split step: negate and restore the zero-controls, compute and uncompute the ladder, one controlled rotation.
- Step 4:Add the initial on the flag and sum over the iterations.
- Step 5:Standard decompositions: the Toffoli into CNOTs, Hadamards and / gates (cf. Nielsen–Chuang Fig. 4.9), and the controlled rotation into CNOTs and single-qubit rotations (Barenco et al. 1995). Each gate is replaced by a constant number of elementary gates, so the elementary count is also .
∎
Example: D = {01, 10, 11} (M = 3, n = 2)
- Step 1:Split angles for .
- Step 2:Iteration : one CNOT (bit differs), then , .
- Step 3:Iteration : two CNOTs () act only on the flag- branch; the split multiplies by twice.
- Step 4:Iteration : one CNOT, and moves the whole remaining amplitude to flag .
- Step 5:Count per iteration: CNOTs, zero-control gates, the two Toffolis of the one-step ladder, one controlled .
Result
Circuit · 4 qubits
Incremental preparation of : data qubits , flag qubit , ancilla qubit . The statevector has amplitude at indices , and (data , , with flag and ancilla back in ).
The qRAM assumption#
Definition
Quantum random-access memory (qRAM)
A qRAM holding classical -bit words is a device implementing, for every address superposition, with an address register of qubits and a data register of qubits. The qRAM assumption is that one such query costs time (after the memory has been loaded once), independent of how the words are distributed.
One query prepares the indexed dataset state
- Step 1:Each maps ; the product over qubits is the uniform superposition over all addresses.
- Step 2:Linearity of the query with .
- Step 3:This exponential gap in is entirely due to the assumed device.
- Step 4:The address stays entangled with the data: tracing out leaves the mixture , not . Removing the address requires uncomputing from , a separate reversible circuit.
Bucket-brigade architecture. Giovannetti, Lloyd and Maccone (2008) proposed to realise with a binary tree of three-level routers (states wait, left, right) whose leaves are the memory cells. The address qubits are sent down the tree one after another; each one is absorbed by the first router still in wait and sets its direction, so after address qubits a single path from the root to cell is open (in superposition over ). A bus qubit then travels down, copies the cell content, and returns, after which the routers are reset in reverse order. Only routers are activated in each branch of the superposition, compared with switches in a fan-out design, which is why the scheme was argued to be far more tolerant of router decoherence.
Why basis encoding cannot generalise#
Definition
Fidelity kernel of a feature map
A feature map into a Hilbert space defines the fidelity kernel (studied in depth in the quantum-kernel module). For training inputs the Gram matrix is . Kernel ridge regression with bias fits by minimising with .
Sparsity of the basis-encoding kernel
For basis encoding : (i) ; (ii) ; (iii) on distinct training inputs ; (iv) kernel ridge regression gives , , and the prediction at every input outside the training set is , independent of .
Proof (9 steps)
- Step 1:The inner product of product states factorises, and , ; the product is iff all bits agree.
- Step 2:Square the modulus; for pairwise distinct training inputs only the diagonal survives, so .
- Step 3:With the training predictions are and the regulariser is .
- Step 4:The Hessian is constant; its upper block is positive definite and its Schur complement is positive for , so is strictly convex and its unique stationary point is the global minimum.
- Step 5:Stationarity in the bias, with .
- Step 6:Stationarity in the coefficients.
- Step 7:Average the second condition, insert into the first, multiply by and cancel; .
- Step 8:The kernel vanishes between the unseen input and every training input, so only the bias remains — the same constant for every unseen .
- Step 9:On the training set the model shrinks the labels towards their mean and interpolates them as : it memorises the data and predicts nothing about new inputs.
∎
A fixed unitary cannot repair the kernel
For any data-independent unitary , the feature map has the same fidelity kernel . A soft-margin SVM with this kernel likewise assigns the same label to every unseen input.
Proof (3 steps)
- Step 1:: a unitary preserves inner products, so the kernel is unchanged.
- Step 2:The SVM decision function contains the kernel only through for unseen .
- Step 3:To generalise, similar inputs must have overlapping feature states. Encodings with continuous parameters provide exactly this: amplitude encoding gives for normalised vectors, and angle encoding gives (next lessons).
∎
Quick check
Four distinct bit strings with labels are basis-encoded and fitted by kernel ridge regression with bias and . What does the model predict for a fifth, unseen bit string?
Quick check
What is the fidelity-kernel Gram matrix of the basis-encoded strings , , , ?
Comparison of encodings. The table summarises the three encodings of this module for an input of dimension (a bit string of length , or a real vector with components):
| Encoding | Qubits | State-preparation gates | Fidelity kernel |
|---|---|---|---|
| Basis ( bits per component) | , depth | ||
| Amplitude | for generic inputs | ||
| Angle ( per feature) | , depth |
Basis encoding is the right choice when the downstream circuit performs exact arithmetic or search on the data (oracles, quantum arithmetic, Grover over a database). As a feature map for learning it is too sparse; the next two lessons, Amplitude Encoding & State Preparation and Angle & Phase Encoding, construct encodings whose kernels vary smoothly with the input.
References
- M. Schuld and F. Petruccione, Machine Learning with Quantum Computers, 2nd ed., Springer (2021), Ch. 4 "Representing Data on a Quantum Computer" (basis, amplitude and angle encoding).doi:10.1007/978-3-030-83098-4
- D. Ventura and T. Martinez, Quantum associative memory, Information Sciences 124, 273–296 (2000).doi:10.1016/S0020-0255(99)00101-2
- V. Giovannetti, S. Lloyd and L. Maccone, Quantum random access memory, Physical Review Letters 100, 160501 (2008).doi:10.1103/PhysRevLett.100.160501
- S. Aaronson, Read the fine print, Nature Physics 11, 291–293 (2015).doi:10.1038/nphys3272
- E. Tang, A quantum-inspired classical algorithm for recommendation systems, Proceedings of the 51st Annual ACM Symposium on Theory of Computing, 217–228 (2019).doi:10.1145/3313276.3316310
- A. Barenco et al., Elementary gates for quantum computation, Physical Review A 52, 3457–3467 (1995).doi:10.1103/PhysRevA.52.3457
Hands-on labs
Basis-encode the bit string 1011
1 step · auto-gradedOpen labPrepare the basis encoding of the bit string on four qubits, using at most three gates. Qubit 0 is the leftmost (most significant) bit, so the target is the basis vector with index of .