QuantumCore

Lesson 35 · 70 min · core

Basis Encoding

Loading progress…

Basis encoding stores a classical bit string x∈{0,1}nx\in\{0,1\}^n as the computational basis state ∣x⟩=⨂jXxj∣0⟩\lvert x\rangle=\bigotimes_jX^{x_j}\lvert0\rangle of nn qubits. This lesson proves by induction that this state is the basis vector ek(x)e_{k(x)} with the big-endian index k(x)=∑jxj2n−1−jk(x)=\sum_jx_j2^{n-1-j}, counts its cost (nn qubits, at most nn gates, depth 11), encodes real numbers in fixed point with truncation error at most 2−τ2^{-\tau}, derives when the dataset superposition M−1/2∑m∣x(m)⟩M^{-1/2}\sum_m\lvert x^{(m)}\rangle is normalised and prepares it with O(Mn)O(Mn) gates, discusses the qRAM assumption, and proves that basis encoding induces the kernel δxx′\delta_{xx'}, which cannot generalise to unseen inputs.

Prerequisites: The Qubit, Pauli Gates & the Pauli Algebra

Objectives and key results
  • Prove by induction on nn that ⨂j=0n−1Xxj∣0⟩=ek(x)\bigotimes_{j=0}^{n-1}X^{x_j}\lvert0\rangle=e_{k(x)} with k(x)=∑jxj2n−1−jk(x)=\sum_jx_j2^{n-1-j} (qubit 0 = most significant bit).

  • Count the resources of basis encoding: nn qubits, ∣x∣≤n\lvert x\rvert\le n XX gates, depth 11.

  • Encode a real number in [0,1)[0,1) with τ\tau fixed-point bits, prove the truncation bound 0≤x−x~<2−τ0\le x-\tilde x<2^{-\tau}, and size a register of dτd\tau qubits for a dd-dimensional vector.

  • Show that M−1/2∑m∣x(m)⟩M^{-1/2}\sum_m\lvert x^{(m)}\rangle is normalised if and only if the patterns are distinct, and prepare it with an incremental flag-qubit procedure using O(Mn)O(Mn) gates.

  • State the qRAM assumption behind O(log⁡M)O(\log M) data access and its hardware caveats.

  • Prove that the basis-encoding fidelity kernel is δxx′\delta_{xx'}, so kernel ridge regression predicts only its bias at every unseen input.

definition · Basis encoding
x∈{0,1}n ⟼ ∣x⟩=⨂j=0n−1Xxj∣0⟩=ek(x),k(x)=∑j=0n−1xj 2 n−1−jx\in\{0,1\}^n\ \longmapsto\ \lvert x\rangle=\bigotimes_{j=0}^{n-1}X^{x_j}\lvert0\rangle=e_{k(x)},\qquad k(x)=\sum_{j=0}^{n-1}x_j\,2^{\,n-1-j}
formula · Fixed-point truncation error
x~=∑k=1τbk2−k=⌊2τx⌋2τ,0≤x−x~<2−τ\tilde x=\sum_{k=1}^{\tau}b_k2^{-k}=\frac{\lfloor2^\tau x\rfloor}{2^\tau},\qquad0\le x-\tilde x<2^{-\tau}
theorem · Dataset superposition
∥1M∑m=0M−1∣x(m)⟩∥2=1M∑xcx2=1  ⟺  x(0),…,x(M−1) distinct\Big\lVert\tfrac1{\sqrt M}\sum_{m=0}^{M-1}\lvert x^{(m)}\rangle\Big\rVert^2=\frac1M\sum_xc_x^2=1\iff x^{(0)},\dots,x^{(M-1)}\ \text{distinct}
algorithm · Incremental preparation
θm=2arcsin⁡1M−m,gates≤1+M(5n−1)=O(Mn),2n qubits\theta_m=2\arcsin\frac1{\sqrt{M-m}},\qquad\text{gates}\le1+M(5n-1)=O(Mn),\qquad2n\ \text{qubits}
theorem · Sparsity of the basis-encoding kernel
k(x,x′)=∣⟨x∣x′⟩∣2=δxx′,K=IM,f(x∗)=yˉ  for every unseen x∗k(x,x')=\lvert\langle x\vert x'\rangle\rvert^2=\delta_{xx'},\qquad K=I_M,\qquad f(x_\ast)=\bar y\ \ \text{for every unseen }x_\ast

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 nn bits of xx are copied one-to-one onto nn 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 x=x0x1⋯xn−1∈{0,1}nx=x_0x_1\cdots x_{n-1}\in\{0,1\}^n. Its basis encoding is the nn-qubit state ∣x⟩:=∣x0⟩⊗∣x1⟩⊗⋯⊗∣xn−1⟩∈(C2)⊗n≅C2n,\lvert x\rangle:=\lvert x_0\rangle\otimes\lvert x_1\rangle\otimes\cdots\otimes\lvert x_{n-1}\rangle\in(\mathbb C^2)^{\otimes n}\cong\mathbb C^{2^n}, in which qubit jj carries bit xjx_j. It is prepared from ∣0n⟩\lvert0^n\rangle by the unitary Ux:=⨂j=0n−1XxjU_x:=\bigotimes_{j=0}^{n-1}X^{x_j} with X0=IX^0=I and X1=XX^1=X, i.e. by one Pauli-XX gate on every qubit whose bit equals 11.

Definition

Big-endian index map

The index of x∈{0,1}nx\in\{0,1\}^n is k(x)=∑j=0n−1xj 2 n−1−j∈{0,1,…,2n−1},k(x)=\sum_{j=0}^{n-1}x_j\,2^{\,n-1-j}\in\{0,1,\dots,2^n-1\}, so qubit 00 is the leftmost symbol of the ket and the most significant bit of the index. We write eke_k for the kk-th standard basis vector of C2n\mathbb C^{2^n} (entries counted from 00). Because every integer in {0,…,2n−1}\{0,\dots,2^n-1\} has exactly one nn-digit binary representation, x↦k(x)x\mapsto k(x) is a bijection {0,1}n→{0,…,2n−1}\{0,1\}^n\to\{0,\dots,2^n-1\}.

Theorem

The basis-encoding circuit prepares e_k(x)

For every n≥1n\ge1 and every x∈{0,1}nx\in\{0,1\}^n, (⨂j=0n−1Xxj)∣0n⟩=∣x0⟩⊗⋯⊗∣xn−1⟩=ek(x)∈C2n.\Big(\bigotimes_{j=0}^{n-1}X^{x_j}\Big)\lvert0^n\rangle=\lvert x_0\rangle\otimes\cdots\otimes\lvert x_{n-1}\rangle=e_{k(x)}\in\mathbb C^{2^n}. Consequently the 2n2^n encoded states {∣x⟩}x∈{0,1}n\{\lvert x\rangle\}_{x\in\{0,1\}^n} are exactly the computational basis of C2n\mathbb C^{2^n}.

Proof (8 steps)
  1. Step 1:
    n=1:X0∣0⟩=∣0⟩=(10)=e0,X1∣0⟩=(0110)(10)=(01)=∣1⟩=e1n=1:\quad X^0\lvert0\rangle=\lvert0\rangle=\begin{pmatrix}1\\0\end{pmatrix}=e_0,\qquad X^1\lvert0\rangle=\begin{pmatrix}0&1\\1&0\end{pmatrix}\begin{pmatrix}1\\0\end{pmatrix}=\begin{pmatrix}0\\1\end{pmatrix}=\lvert1\rangle=e_1
    Base case: for one qubit k(x0)=x0k(x_0)=x_0, and X∣0⟩=∣1⟩X\lvert0\rangle=\lvert1\rangle by direct matrix multiplication.
  2. Step 2:
    Hypothesis: (⨂j=0n−2Xxj)∣0n−1⟩=ek(x′)∈C2n−1,x′:=x0x1⋯xn−2\text{Hypothesis: }\Big(\bigotimes_{j=0}^{n-2}X^{x_j}\Big)\lvert0^{n-1}\rangle=e_{k(x')}\in\mathbb C^{2^{n-1}},\qquad x':=x_0x_1\cdots x_{n-2}
    Induction step: assume the claim for n−1≥1n-1\ge1 qubits and let x′x' be xx without its last bit.
  3. Step 3:
    (⨂j=0n−1Xxj)∣0n⟩=((⨂j=0n−2Xxj)⊗Xxn−1)(∣0n−1⟩⊗∣0⟩)=(⨂j=0n−2Xxj)∣0n−1⟩⊗Xxn−1∣0⟩\Big(\bigotimes_{j=0}^{n-1}X^{x_j}\Big)\lvert0^n\rangle=\Big(\Big(\bigotimes_{j=0}^{n-2}X^{x_j}\Big)\otimes X^{x_{n-1}}\Big)\big(\lvert0^{n-1}\rangle\otimes\lvert0\rangle\big)=\Big(\bigotimes_{j=0}^{n-2}X^{x_j}\Big)\lvert0^{n-1}\rangle\otimes X^{x_{n-1}}\lvert0\rangle
    Group the first n−1n-1 factors, write ∣0n⟩=∣0n−1⟩⊗∣0⟩\lvert0^n\rangle=\lvert0^{n-1}\rangle\otimes\lvert0\rangle, and use the mixed-product rule (A⊗B)(u⊗v)=Au⊗Bv(A\otimes B)(u\otimes v)=Au\otimes Bv.
  4. Step 4:
    =ek(x′)⊗exn−1=e_{k(x')}\otimes e_{x_{n-1}}
    Induction hypothesis for the first factor, base case for the last.
  5. Step 5:
    (u⊗v)2i+b=ui vb ⇒ ea⊗eb=e2a+b(a<2n−1, b∈{0,1})(u\otimes v)_{2i+b}=u_i\,v_b\ \Rightarrow\ e_a\otimes e_b=e_{2a+b}\qquad(a<2^{n-1},\ b\in\{0,1\})
    Definition of the Kronecker product of a 2n−12^{n-1}-vector with a 22-vector: entry 2i+b2i+b is uivbu_iv_b, which is 11 only for i=ai=a, bb the given bit.
  6. Step 6:
    k(x)=∑j=0n−1xj2n−1−j=2∑j=0n−2xj2n−2−j+xn−1=2 k(x′)+xn−1k(x)=\sum_{j=0}^{n-1}x_j2^{n-1-j}=2\sum_{j=0}^{n-2}x_j2^{n-2-j}+x_{n-1}=2\,k(x')+x_{n-1}
    Split off the last term and factor 22 out of the others (Horner's rule).
  7. Step 7:
    (⨂j=0n−1Xxj)∣0n⟩=e2k(x′)+xn−1=ek(x)\Big(\bigotimes_{j=0}^{n-1}X^{x_j}\Big)\lvert0^n\rangle=e_{2k(x')+x_{n-1}}=e_{k(x)}
    Combine the last three lines; by induction the claim holds for all n≥1n\ge1.
  8. Step 8:
    {∣x⟩}x∈{0,1}n={ek(x)}x={e0,…,e2n−1}\{\lvert x\rangle\}_{x\in\{0,1\}^n}=\{e_{k(x)}\}_{x}=\{e_0,\dots,e_{2^n-1}\}
    x↦k(x)x\mapsto k(x) is a bijection onto {0,…,2n−1}\{0,\dots,2^n-1\}, so the encoded states are all standard basis vectors, each exactly once.

∎

Proposition

Cost of basis encoding

Preparing ∣x⟩\lvert x\rangle from ∣0n⟩\lvert0^n\rangle uses nn qubits, exactly ∣x∣=∑jxj≤n\lvert x\rvert=\sum_jx_j\le n gates (the Hamming weight of xx), and depth 11 (depth 00 for x=0nx=0^n). Moreover Ux∣x′⟩=∣x⊕x′⟩U_x\lvert x'\rangle=\lvert x\oplus x'\rangle for all x′x', and Ux2=IU_x^2=I.

Proof (5 steps)
  1. Step 1:
    Ux=⨂jXxj contains one X per index j with xj=1 ⇒ #gates=∑jxj=∣x∣≤nU_x=\bigotimes_jX^{x_j}\ \text{contains one }X\ \text{per index }j\ \text{with }x_j=1\ \Rightarrow\ \#\text{gates}=\sum_jx_j=\lvert x\rvert\le n
    The factors with xj=0x_j=0 are identities and need no gate.
  2. Step 2:
    XjXj′=Xj′Xj  (j≠j′) ⇒ all X gates fit into one moment: depth 1X_jX_{j'}=X_{j'}X_j\ \ (j\ne j')\ \Rightarrow\ \text{all }X\text{ gates fit into one moment: depth }1
    Gates on disjoint qubits commute and can be executed in parallel, so the circuit has a single layer.
  3. Step 3:
    Xxj∣xj′⟩=∣xj⊕xj′⟩X^{x_j}\lvert x'_j\rangle=\lvert x_j\oplus x'_j\rangle
    For xj=0x_j=0 nothing changes; for xj=1x_j=1, XX swaps ∣0⟩↔∣1⟩\lvert0\rangle\leftrightarrow\lvert1\rangle, which is addition of 11 modulo 22.
  4. Step 4:
    Ux∣x′⟩=⨂jXxj∣xj′⟩=⨂j∣xj⊕xj′⟩=∣x⊕x′⟩U_x\lvert x'\rangle=\bigotimes_jX^{x_j}\lvert x'_j\rangle=\bigotimes_j\lvert x_j\oplus x'_j\rangle=\lvert x\oplus x'\rangle
    Mixed-product rule on the product state ∣x′⟩=⨂j∣xj′⟩\lvert x'\rangle=\bigotimes_j\lvert x'_j\rangle.
  5. Step 5:
    Ux2∣x′⟩=∣x⊕x⊕x′⟩=∣x′⟩  ∀x′ ⇒ Ux2=IU_x^2\lvert x'\rangle=\lvert x\oplus x\oplus x'\rangle=\lvert x'\rangle\ \ \forall x'\ \Rightarrow\ U_x^2=I
    x⊕x=0nx\oplus x=0^n; an operator that fixes every basis vector is the identity. In particular Ux†=UxU_x^\dagger=U_x, so the same circuit also un-computes the encoding.

∎

Worked example: x = 1011#

Derivation

The state |1011⟩ as a vector of C¹⁶

  1. Step 1:
    x=1011: x0=1, x1=0, x2=1, x3=1 ⇒ Ux=X⊗I⊗X⊗Xx=1011:\ x_0=1,\ x_1=0,\ x_2=1,\ x_3=1\ \Rightarrow\ U_x=X\otimes I\otimes X\otimes X
    One XX on qubits 00, 22 and 33; qubit 11 is left alone. Three gates, depth 11.
  2. Step 2:
    k(1011)=1⋅23+0⋅22+1⋅21+1⋅20=8+0+2+1=11k(1011)=1\cdot2^3+0\cdot2^2+1\cdot2^1+1\cdot2^0=8+0+2+1=11
    Big-endian index map: qubit 00 is weighted by 2n−1=82^{n-1}=8.
  3. Step 3:
    ∣1⟩⊗∣0⟩=(01)⊗(10)=(0010)=e2\lvert1\rangle\otimes\lvert0\rangle=\begin{pmatrix}0\\1\end{pmatrix}\otimes\begin{pmatrix}1\\0\end{pmatrix}=\begin{pmatrix}0\\0\\1\\0\end{pmatrix}=e_2
    First two qubits: e1⊗e0=e2⋅1+0e_1\otimes e_0=e_{2\cdot1+0}.
  4. Step 4:
    e2⊗∣1⟩=e2⋅2+1=e5∈C8,e5⊗∣1⟩=e2⋅5+1=e11∈C16e_2\otimes\lvert1\rangle=e_{2\cdot2+1}=e_5\in\mathbb C^8,\qquad e_5\otimes\lvert1\rangle=e_{2\cdot5+1}=e_{11}\in\mathbb C^{16}
    Append qubits 22 and 33 with the rule ea⊗eb=e2a+be_a\otimes e_b=e_{2a+b} (the induction step of the theorem).
  5. Step 5:
    ∣1011⟩=(0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0)T\lvert1011\rangle=(0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0)^{\mathsf T}
    The full amplitude vector: a single 11 at position 1111 (counting from 00) and 1515 zeros. Measuring returns 10111011 with probability 11.
  6. Step 6:
    reversed reading: 1101 ↦ 1⋅8+1⋅4+0⋅2+1=13≠11\text{reversed reading: }1101\ \mapsto\ 1\cdot8+1\cdot4+0\cdot2+1=13\ne11
    Reading the bits in the opposite order (little-endian) addresses the basis vector e13e_{13}, a different state orthogonal to e11e_{11} — the most common mistake in the lab.

Result

∣1011⟩=X⊗I⊗X⊗X ∣0000⟩=e11∈C16\boxed{\lvert1011\rangle=X\otimes I\otimes X\otimes X\,\lvert0000\rangle=e_{11}\in\mathbb C^{16}}

Circuit · 4 qubits

Quantum circuit on 4 qubitsX on q0; X on q2; X on q3q0q1q2q3XXX

Basis encoding of x=1011x=1011: XX on qubits 00, 22 and 33. The statevector has the single amplitude 11 at index 11=∣1011⟩11=\lvert1011\rangle.

∣ψ⟩=1∣1011⟩\lvert\psi\rangle = 1\lvert 1011\rangle

Quick check

With qubit 00 the most significant bit, what is the index kk of the basis state ∣0110⟩\lvert0110\rangle in C16\mathbb C^{16}?

tolerance ±1e-9
TypeScriptrunnable
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 ∣1011⟩\lvert1011\rangle as an explicit Kronecker product of the one-qubit vectors Xxj∣0⟩X^{x_j}\lvert0\rangle, exactly following the induction in the theorem.

Pythonrunnable
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 x∈[0,1)x\in[0,1) has a binary expansion x=∑k≥1bk2−kx=\sum_{k\ge1}b_k2^{-k} with digits bk=⌊2kx⌋ mod 2∈{0,1}b_k=\lfloor2^kx\rfloor\bmod2\in\{0,1\} (the expansion that does not end in infinitely many 11s). With τ\tau bits of precision we keep the first τ\tau digits, x~=∑k=1τbk2−k=0.b1b2⋯bτ (base 2),\tilde x=\sum_{k=1}^{\tau}b_k2^{-k}=0.b_1b_2\cdots b_\tau\ (\text{base }2), and basis-encode the string b1b2⋯bτb_1b_2\cdots b_\tau on τ\tau qubits, b1b_1 on the most significant qubit. The digits are produced by repeated doubling: r0=xr_0=x, bk=⌊2rk−1⌋b_k=\lfloor2r_{k-1}\rfloor, rk=2rk−1−bk∈[0,1)r_k=2r_{k-1}-b_k\in[0,1).

Theorem

Truncation error of τ-bit fixed point

For every x∈[0,1)x\in[0,1) and τ≥1\tau\ge1, the truncation x~=∑k=1τbk2−k\tilde x=\sum_{k=1}^{\tau}b_k2^{-k} satisfies x~=⌊2τx⌋/2τ\tilde x=\lfloor2^\tau x\rfloor/2^\tau and 0≤x−x~<2−τ.0\le x-\tilde x<2^{-\tau}. Hence τ=⌈log⁡2(1/ε)⌉\tau=\lceil\log_2(1/\varepsilon)\rceil bits guarantee an error below ε\varepsilon for every x∈[0,1)x\in[0,1), and fewer bits do not. (Rounding to the nearest multiple of 2−τ2^{-\tau} instead of truncating halves the bound to 2−τ−12^{-\tau-1}, at the price of one extra bit for values that round up to 11.)

Proof (6 steps)
  1. Step 1:
    x−x~=∑k=τ+1∞bk2−k≥0x-\tilde x=\sum_{k=\tau+1}^{\infty}b_k2^{-k}\ge0
    Subtract the first τ\tau terms from the full expansion; every remaining term is non-negative.
  2. Step 2:
    ∑k=τ+1∞bk2−k≤∑k=τ+1∞2−k\sum_{k=\tau+1}^{\infty}b_k2^{-k}\le\sum_{k=\tau+1}^{\infty}2^{-k}
    Each digit satisfies bk≤1b_k\le1.
  3. Step 3:
    ∑k=τ+1∞2−k=2−(τ+1)∑i=0∞2−i=2−(τ+1)⋅11−12=2−τ\sum_{k=\tau+1}^{\infty}2^{-k}=2^{-(\tau+1)}\sum_{i=0}^{\infty}2^{-i}=2^{-(\tau+1)}\cdot\frac{1}{1-\frac12}=2^{-\tau}
    Geometric series with first term 2−(τ+1)2^{-(\tau+1)} and ratio 12\tfrac12.
  4. Step 4:
    x−x~=2−τ  ⟺  bk=1  ∀k>τx-\tilde x=2^{-\tau}\iff b_k=1\ \ \forall k>\tau
    Equality in the digit-wise bound requires every tail digit to be 11, which the chosen expansion excludes; hence the inequality is strict: x−x~<2−τx-\tilde x<2^{-\tau}.
  5. Step 5:
    2τx=∑k=1τbk2τ−k+2τ(x−x~),0≤2τ(x−x~)<1 ⇒ ⌊2τx⌋=2τx~2^\tau x=\sum_{k=1}^{\tau}b_k2^{\tau-k}+2^\tau(x-\tilde x),\qquad0\le2^\tau(x-\tilde x)<1\ \Rightarrow\ \lfloor2^\tau x\rfloor=2^\tau\tilde x
    Multiply by 2τ2^\tau: the first sum is an integer and the remainder lies in [0,1)[0,1), so it is the fractional part.
  6. Step 6:
    sup⁡x∈[0,1)(x−x~)=2−τ ⇒ (x−x~<ε  ∀x  ⟺  2−τ≤ε  ⟺  τ≥log⁡21ε)\sup_{x\in[0,1)}(x-\tilde x)=2^{-\tau}\ \Rightarrow\ \big(x-\tilde x<\varepsilon\ \ \forall x\iff2^{-\tau}\le\varepsilon\iff\tau\ge\log_2\tfrac1\varepsilon\big)
    The supremum is approached by x=1−2−Kx=1-2^{-K} with K→∞K\to\infty, whose error is 2−τ−2−K2^{-\tau}-2^{-K}; so the guarantee holds exactly when 2−τ≤ε2^{-\tau}\le\varepsilon, i.e. τ≥⌈log⁡2(1/ε)⌉\tau\ge\lceil\log_2(1/\varepsilon)\rceil by monotonicity of log⁡2\log_2. For example ε=10−3\varepsilon=10^{-3}: log⁡21000≈9.97\log_2 1000\approx9.97, so τ=10\tau=10 and 2−10=11024<10−32^{-10}=\tfrac1{1024}<10^{-3}, whereas 2−9=1512>10−32^{-9}=\tfrac1{512}>10^{-3}.

∎

Derivation

Example: x = 0.7 with τ = 5 bits

  1. Step 1:
    2⋅0.7=1.4⇒b1=1, r1=0.4;2⋅0.4=0.8⇒b2=0, r2=0.82\cdot0.7=1.4\Rightarrow b_1=1,\ r_1=0.4;\qquad2\cdot0.4=0.8\Rightarrow b_2=0,\ r_2=0.8
    Doubling algorithm, first two digits.
  2. Step 2:
    2⋅0.8=1.6⇒b3=1, r3=0.6;2⋅0.6=1.2⇒b4=1, r4=0.2;2⋅0.2=0.4⇒b5=02\cdot0.8=1.6\Rightarrow b_3=1,\ r_3=0.6;\qquad2\cdot0.6=1.2\Rightarrow b_4=1,\ r_4=0.2;\qquad2\cdot0.2=0.4\Rightarrow b_5=0
    Digits three to five.
  3. Step 3:
    x~=0.101102=12+18+116=1116=0.6875=⌊32⋅0.7⌋32=2232\tilde x=0.10110_2=\tfrac12+\tfrac18+\tfrac1{16}=\tfrac{11}{16}=0.6875=\frac{\lfloor32\cdot0.7\rfloor}{32}=\frac{22}{32}
    Sum the kept digits; this agrees with ⌊2τx⌋/2τ\lfloor2^\tau x\rfloor/2^\tau since 32⋅0.7=22.432\cdot0.7=22.4.
  4. Step 4:
    x−x~=0.7−0.6875=0.0125<2−5=0.03125x-\tilde x=0.7-0.6875=0.0125<2^{-5}=0.03125
    The truncation bound of the theorem.
  5. Step 5:
    0.7 ⟼ ∣10110⟩=e22∈C320.7\ \longmapsto\ \lvert10110\rangle=e_{22}\in\mathbb C^{32}
    Basis-encode the digit string with b1b_1 on qubit 00: index 16+4+2=22=⌊25x⌋16+4+2=22=\lfloor2^5x\rfloor.

Result

0.7≈0.101102=0.6875,∣x−x~∣=0.0125<2−5\boxed{0.7\approx0.10110_2=0.6875,\qquad\lvert x-\tilde x\rvert=0.0125<2^{-5}}
Proposition

Register size for a real vector

Encoding v=(v0,…,vd−1)∈[0,1)d\mathbf v=(v_0,\dots,v_{d-1})\in[0,1)^d with τ\tau bits per component uses dτd\tau qubits, at most dτd\tau XX gates and depth 11; component jj occupies qubits jτ,…,jτ+τ−1j\tau,\dots,j\tau+\tau-1, and the total index is K=∑j=0d−1⌊2τvj⌋ 2τ(d−1−j)K=\sum_{j=0}^{d-1}\lfloor2^\tau v_j\rfloor\,2^{\tau(d-1-j)}. Every component is reproduced to within 2−τ2^{-\tau}.

Proof (4 steps)
  1. Step 1:
    ∣v~⟩=∣v~0⟩⊗∣v~1⟩⊗⋯⊗∣v~d−1⟩,∣v~j⟩∈(C2)⊗τ\lvert\tilde{\mathbf v}\rangle=\lvert\tilde v_0\rangle\otimes\lvert\tilde v_1\rangle\otimes\cdots\otimes\lvert\tilde v_{d-1}\rangle,\qquad\lvert\tilde v_j\rangle\in(\mathbb C^2)^{\otimes\tau}
    Concatenate the dd bit strings of length τ\tau; the register is the tensor product of dd blocks of τ\tau qubits, i.e. dτd\tau qubits.
  2. Step 2:
    #gates=∑j∣b(vj)∣≤dτ,depth=1\#\text{gates}=\sum_j\lvert b(v_j)\rvert\le d\tau,\qquad\text{depth}=1
    Apply the cost proposition to the concatenated string of length dτd\tau.
  3. Step 3:
    K=∑j=0d−1∑k=1τbj,k 2 dτ−1−(jτ+k−1)=∑j=0d−12τ(d−1−j)∑k=1τbj,k2τ−kK=\sum_{j=0}^{d-1}\sum_{k=1}^{\tau}b_{j,k}\,2^{\,d\tau-1-(j\tau+k-1)}=\sum_{j=0}^{d-1}2^{\tau(d-1-j)}\sum_{k=1}^{\tau}b_{j,k}2^{\tau-k}
    Digit bj,kb_{j,k} sits on qubit jτ+k−1j\tau+k-1; apply the index map with n=dτn=d\tau and factor 2τ(d−1−j)2^{\tau(d-1-j)}.
  4. Step 4:
    ∑k=1τbj,k2τ−k=2τv~j=⌊2τvj⌋\sum_{k=1}^{\tau}b_{j,k}2^{\tau-k}=2^\tau\tilde v_j=\lfloor2^\tau v_j\rfloor
    The inner sum is the integer of the τ\tau-bit block (fixed-point theorem); the error bound applies to every component separately.

∎

Other ranges and signs. A feature in [a,b)[a,b) is first rescaled to (v−a)/(b−a)∈[0,1)(v-a)/(b-a)\in[0,1), which multiplies the error bound by b−ab-a. Signed values in [−1,1)[-1,1) use two's complement: with τ\tau bits s b1⋯bτ−1s\,b_1\cdots b_{\tau-1} the value is −s+∑k=1τ−1bk2−k-s+\sum_{k=1}^{\tau-1}b_k2^{-k}, the sign bit carrying weight −1-1; for example −0.25=−1+12+14-0.25=-1+\tfrac12+\tfrac14 is 11101110 with τ=4\tau=4, and the resolution is 2−(τ−1)2^{-(\tau-1)} 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 33 real features, each basis-encoded with 55 fixed-point bits. How many qubits does the register need?

tolerance ±1e-9

Quick check

What is the smallest number τ\tau of fixed-point bits that guarantees a truncation error below 10−310^{-3} for every x∈[0,1)x\in[0,1)?

tolerance ±1e-9

Encoding a whole dataset in superposition#

Definition

Dataset superposition

Given MM patterns x(0),…,x(M−1)∈{0,1}nx^{(0)},\dots,x^{(M-1)}\in\{0,1\}^n, the dataset state is ∣D⟩=1M∑m=0M−1∣x(m)⟩.\lvert D\rangle=\frac1{\sqrt M}\sum_{m=0}^{M-1}\lvert x^{(m)}\rangle . It stores the whole training set in one nn-qubit register: measuring it returns each stored pattern with probability 1/M1/M. For a pattern xx let cx=#{m:x(m)=x}c_x=\#\{m:x^{(m)}=x\} be its multiplicity, so ∑xcx=M\sum_xc_x=M.

Theorem

The dataset state is normalised iff the patterns are distinct

⟨D∣D⟩=1M∑xcx2≥1\langle D\vert D\rangle=\frac1M\sum_xc_x^2\ge1, with equality if and only if the patterns x(0),…,x(M−1)x^{(0)},\dots,x^{(M-1)} are pairwise distinct. With repetitions the normalised state is ∑xcx∑ycy2∣x⟩\sum_x\frac{c_x}{\sqrt{\sum_yc_y^2}}\lvert x\rangle.

Proof (6 steps)
  1. Step 1:
    ⟨D∣D⟩=1M∑m=0M−1∑m′=0M−1⟨x(m)∣x(m′)⟩\langle D\vert D\rangle=\frac1M\sum_{m=0}^{M-1}\sum_{m'=0}^{M-1}\langle x^{(m)}\vert x^{(m')}\rangle
    Expand the inner product bilinearly (all coefficients are real and equal to M−1/2M^{-1/2}).
  2. Step 2:
    ⟨x∣x′⟩=δxx′ ⇒ ⟨D∣D⟩=1M #{(m,m′):x(m)=x(m′)}\langle x\vert x'\rangle=\delta_{xx'}\ \Rightarrow\ \langle D\vert D\rangle=\frac1M\,\#\{(m,m'):x^{(m)}=x^{(m')}\}
    Encoded states are orthonormal (they are distinct standard basis vectors by the basis-encoding theorem).
  3. Step 3:
    #{(m,m′):x(m)=x(m′)}=∑x#{m:x(m)=x}2=∑xcx2\#\{(m,m'):x^{(m)}=x^{(m')}\}=\sum_x\#\{m:x^{(m)}=x\}^2=\sum_xc_x^2
    Group the ordered pairs by their common pattern xx; there are cx⋅cxc_x\cdot c_x pairs for each xx.
  4. Step 4:
    cx∈Z≥0 ⇒ cx2≥cx ⇒ ∑xcx2≥∑xcx=Mc_x\in\mathbb Z_{\ge0}\ \Rightarrow\ c_x^2\ge c_x\ \Rightarrow\ \sum_xc_x^2\ge\sum_xc_x=M
    For a non-negative integer, c2−c=c(c−1)≥0c^2-c=c(c-1)\ge0, with equality iff c∈{0,1}c\in\{0,1\}.
  5. Step 5:
    ⟨D∣D⟩=1  ⟺  cx∈{0,1} ∀x  ⟺  x(m) pairwise distinct\langle D\vert D\rangle=1\iff c_x\in\{0,1\}\ \forall x\iff x^{(m)}\ \text{pairwise distinct}
    Equality holds term by term iff no pattern occurs twice.
  6. Step 6:
    1M∑m∣x(m)⟩=1M∑xcx∣x⟩ ⟶ ∑xcx∣x⟩∑ycy2\frac1{\sqrt M}\sum_m\lvert x^{(m)}\rangle=\frac1{\sqrt M}\sum_xc_x\lvert x\rangle\ \longrightarrow\ \frac{\sum_xc_x\lvert x\rangle}{\sqrt{\sum_yc_y^2}}
    Collect equal kets and divide by the norm ∑ycy2/M\sqrt{\sum_yc_y^2/M}. Its Born probabilities cx2/∑ycy2c_x^2/\sum_yc_y^2 are not the empirical frequencies cx/Mc_x/M; to sample with frequencies one uses amplitudes cx/M\sqrt{c_x/M} instead.

∎

An incremental preparation procedure. Ventura and Martinez (2000) proposed to build ∣D⟩\lvert D\rangle one pattern at a time: a flag qubit marks an "active" branch that still carries the not-yet-distributed amplitude, and in step mm a controlled rotation splits off the share 1/(M−m)\sqrt{1/(M-m)} of the active branch and freezes it as a stored pattern. We use the following streamlined variant on 2n2n qubits: data qubits 0,…,n−10,\dots,n-1 (initially ∣0n⟩\lvert0^n\rangle), a flag qubit ff, and n−1n-1 ancillas for the multi-controlled rotation. Set x(−1):=0nx^{(-1)}:=0^n and θm:=2arcsin⁡(1/M−m)\theta_m:=2\arcsin\big(1/\sqrt{M-m}\big).

  1. Apply XX to the flag, giving ∣0n⟩∣1⟩f\lvert0^n\rangle\lvert1\rangle_f.
  2. For m=0,1,…,M−1m=0,1,\dots,M-1:
    • Load — for every jj with xj(m)≠xj(m−1)x^{(m)}_j\ne x^{(m-1)}_j apply CNOT(f→j)\mathrm{CNOT}(f\to j); this rewrites the data register of the active (flag =1=1) branch from x(m−1)x^{(m-1)} to x(m)x^{(m)}.
    • Split — apply RY(−θm)R_Y(-\theta_m) to the flag, controlled on the data register being equal to x(m)x^{(m)} (negate the zero-controls with XX gates, compute the AND of the nn data qubits into the last ancilla with a ladder of n−1n-1 Toffoli gates, apply a controlled RYR_Y from that ancilla, then uncompute the ladder and the XX gates).
  3. The output is ∣D⟩⊗∣0⟩f⊗∣0n−1⟩\lvert D\rangle\otimes\lvert0\rangle_f\otimes\lvert0^{n-1}\rangle.
Theorem

Correctness of the incremental preparation

For pairwise distinct x(0),…,x(M−1)∈{0,1}nx^{(0)},\dots,x^{(M-1)}\in\{0,1\}^n the procedure above outputs ∣D⟩⊗∣0⟩f\lvert D\rangle\otimes\lvert0\rangle_f (ancillas in ∣0n−1⟩\lvert0^{n-1}\rangle). Before the load of iteration mm the state is ∣Ψm⟩=1M∑k<m∣x(k)⟩∣0⟩f+M−mM ∣x(m−1)⟩∣1⟩f.\lvert\Psi_m\rangle=\frac1{\sqrt M}\sum_{k<m}\lvert x^{(k)}\rangle\lvert0\rangle_f+\sqrt{\frac{M-m}{M}}\,\lvert x^{(m-1)}\rangle\lvert1\rangle_f .

Proof (10 steps)
  1. Step 1:
    ∣Ψ0⟩=∣0n⟩∣1⟩f=MM ∣x(−1)⟩∣1⟩f\lvert\Psi_0\rangle=\lvert0^n\rangle\lvert1\rangle_f=\sqrt{\tfrac MM}\,\lvert x^{(-1)}\rangle\lvert1\rangle_f
    After step 1 the invariant holds for m=0m=0: the stored sum is empty and x(−1)=0nx^{(-1)}=0^n.
  2. Step 2:
    ∏j: xj(m)≠xj(m−1)CNOT(f→j)=∣0⟩⟨0∣f⊗I+∣1⟩⟨1∣f⊗Ux(m−1)⊕x(m)\prod_{j:\,x^{(m)}_j\ne x^{(m-1)}_j}\mathrm{CNOT}(f\to j)=\lvert0\rangle\langle0\rvert_f\otimes I+\lvert1\rangle\langle1\rvert_f\otimes U_{x^{(m-1)}\oplus x^{(m)}}
    Each CNOT acts as the identity when the flag is 00 and as XjX_j when it is 11; the product of the XjX_j is the Pauli string Ux(m−1)⊕x(m)U_{x^{(m-1)}\oplus x^{(m)}}.
  3. Step 3:
    Load ∣Ψm⟩=1M∑k<m∣x(k)⟩∣0⟩f+M−mM ∣x(m)⟩∣1⟩f\text{Load}\,\lvert\Psi_m\rangle=\frac1{\sqrt M}\sum_{k<m}\lvert x^{(k)}\rangle\lvert0\rangle_f+\sqrt{\frac{M-m}{M}}\,\lvert x^{(m)}\rangle\lvert1\rangle_f
    Stored terms have flag 00 and are unchanged; on the active term Ux(m−1)⊕x(m)∣x(m−1)⟩=∣x(m)⟩U_{x^{(m-1)}\oplus x^{(m)}}\lvert x^{(m-1)}\rangle=\lvert x^{(m)}\rangle by the cost proposition.
  4. Step 4:
    Cm=(I−Pm)⊗If+Pm⊗RY(−θm),Pm=∣x(m)⟩⟨x(m)∣,Pm∣x(k)⟩=0  (k<m)C_m=(I-P_m)\otimes I_f+P_m\otimes R_Y(-\theta_m),\qquad P_m=\lvert x^{(m)}\rangle\langle x^{(m)}\rvert,\qquad P_m\lvert x^{(k)}\rangle=0\ \ (k<m)
    The split is the rotation controlled on the data register equal to x(m)x^{(m)}. Distinctness gives x(k)≠x(m)x^{(k)}\ne x^{(m)}, so every stored term lies in the kernel of PmP_m and is untouched.
  5. Step 5:
    RY(−θ)∣1⟩=(cos⁡θ2sin⁡θ2−sin⁡θ2cos⁡θ2)(01)=sin⁡θ2∣0⟩+cos⁡θ2∣1⟩R_Y(-\theta)\lvert1\rangle=\begin{pmatrix}\cos\frac\theta2&\sin\frac\theta2\\-\sin\frac\theta2&\cos\frac\theta2\end{pmatrix}\begin{pmatrix}0\\1\end{pmatrix}=\sin\tfrac\theta2\lvert0\rangle+\cos\tfrac\theta2\lvert1\rangle
    Second column of RY(−θ)R_Y(-\theta), using RY(θ)=(cos⁡θ2−sin⁡θ2sin⁡θ2cos⁡θ2)R_Y(\theta)=\begin{pmatrix}\cos\frac\theta2&-\sin\frac\theta2\\\sin\frac\theta2&\cos\frac\theta2\end{pmatrix}.
  6. Step 6:
    sin⁡θm2=1M−m,cos⁡θm2=1−1M−m=M−m−1M−m\sin\tfrac{\theta_m}2=\frac1{\sqrt{M-m}},\qquad\cos\tfrac{\theta_m}2=\sqrt{1-\frac1{M-m}}=\sqrt{\frac{M-m-1}{M-m}}
    Definition of θm\theta_m; θm/2∈(0,π2]\theta_m/2\in(0,\tfrac\pi2], so the cosine is non-negative.
  7. Step 7:
    M−mM⋅1M−m=1M,M−mM⋅M−m−1M−m=M−m−1M\sqrt{\tfrac{M-m}{M}}\cdot\frac1{\sqrt{M-m}}=\frac1{\sqrt M},\qquad\sqrt{\tfrac{M-m}{M}}\cdot\sqrt{\tfrac{M-m-1}{M-m}}=\sqrt{\tfrac{M-m-1}{M}}
    Multiply the active amplitude by the two rotation coefficients: exactly the share 1/M1/\sqrt M is split off.
  8. Step 8:
    Cm Load ∣Ψm⟩=1M∑k≤m∣x(k)⟩∣0⟩f+M−(m+1)M ∣x(m)⟩∣1⟩f=∣Ψm+1⟩C_m\,\text{Load}\,\lvert\Psi_m\rangle=\frac1{\sqrt M}\sum_{k\le m}\lvert x^{(k)}\rangle\lvert0\rangle_f+\sqrt{\frac{M-(m+1)}{M}}\,\lvert x^{(m)}\rangle\lvert1\rangle_f=\lvert\Psi_{m+1}\rangle
    The new flag-00 term joins the stored sum; the invariant holds for m+1m+1.
  9. Step 9:
    ∣ΨM⟩=1M∑k<M∣x(k)⟩∣0⟩f+0=∣D⟩∣0⟩f\lvert\Psi_M\rangle=\frac1{\sqrt M}\sum_{k<M}\lvert x^{(k)}\rangle\lvert0\rangle_f+0=\lvert D\rangle\lvert0\rangle_f
    At m=Mm=M the active amplitude (M−M)/M\sqrt{(M-M)/M} vanishes (the last rotation is θM−1=2arcsin⁡1=π\theta_{M-1}=2\arcsin1=\pi, which maps ∣1⟩↦∣0⟩\lvert1\rangle\mapsto\lvert0\rangle).
  10. Step 10:
    CCX ladder⋅CRY⋅CCX ladder−1 restores the ancillas to ∣0n−1⟩\mathrm{CCX}\ \text{ladder}\cdot\mathrm{CR}_Y\cdot\mathrm{CCX}\ \text{ladder}^{-1}\ \text{restores the ancillas to}\ \lvert0^{n-1}\rangle
    After the zero-controls are negated, the ladder writes the AND of data bits 0,…,k0,\dots,k into ancilla kk, so the last ancilla is 11 exactly on the branch whose data register equals x(m)x^{(m)}; the controlled RYR_Y does not touch the data or ancilla qubits, and the reversed ladder (Toffoli is self-inverse) restores every ancilla to 00 on every branch, so the ancillas factor out.

∎

Proposition

Gate count O(Mn)

The procedure uses 2n2n qubits and at most 1+M(5n−1)1+M(5n-1) gates from {X,CNOT,CCX,CRY}\{X,\mathrm{CNOT},\mathrm{CCX},\mathrm{CR}_Y\}, hence O(Mn)O(Mn) gates; compiled to one- and two-qubit gates the count is still O(Mn)O(Mn).

Proof (5 steps)
  1. Step 1:
    qubits=n⏟data+1⏟flag+n−1⏟ancillas=2n\text{qubits}=\underbrace{n}_{\text{data}}+\underbrace{1}_{\text{flag}}+\underbrace{n-1}_{\text{ancillas}}=2n
    One ancilla per Toffoli of the AND ladder (none for n=1n=1, where the single data qubit controls the rotation directly).
  2. Step 2:
    #CNOTm=dH(x(m−1),x(m))≤n\#\mathrm{CNOT}_m=d_H\big(x^{(m-1)},x^{(m)}\big)\le n
    Load step: one CNOT per differing bit (Hamming distance).
  3. Step 3:
    #Xm=2(n−∣x(m)∣)≤2n,#CCXm=2(n−1),#CRY,m=1\#X_m=2\big(n-\lvert x^{(m)}\rvert\big)\le2n,\qquad\#\mathrm{CCX}_m=2(n-1),\qquad\#\mathrm{CR}_{Y,m}=1
    Split step: negate and restore the zero-controls, compute and uncompute the ladder, one controlled rotation.
  4. Step 4:
    total≤1+∑m=0M−1(n+2n+2(n−1)+1)=1+M(5n−1)=O(Mn)\text{total}\le1+\sum_{m=0}^{M-1}\big(n+2n+2(n-1)+1\big)=1+M(5n-1)=O(Mn)
    Add the initial XX on the flag and sum over the MM iterations.
  5. Step 5:
    CCX=6 CNOT+9 one-qubit gates,CRY(θ)=RY(θ2) CNOT RY(−θ2) CNOT\mathrm{CCX}=6\ \mathrm{CNOT}+9\ \text{one-qubit gates},\qquad\mathrm{CR}_Y(\theta)=R_Y(\tfrac\theta2)\,\mathrm{CNOT}\,R_Y(-\tfrac\theta2)\,\mathrm{CNOT}
    Standard decompositions: the Toffoli into 66 CNOTs, 22 Hadamards and 77 TT/T†T^\dagger gates (cf. Nielsen–Chuang Fig. 4.9), and the controlled rotation into 22 CNOTs and 22 single-qubit rotations (Barenco et al. 1995). Each gate is replaced by a constant number of elementary gates, so the elementary count is also O(Mn)O(Mn).

∎

Derivation

Example: D = {01, 10, 11} (M = 3, n = 2)

  1. Step 1:
    θ0=2arcsin⁡13≈1.230959,θ1=2arcsin⁡12=π2,θ2=2arcsin⁡1=π\theta_0=2\arcsin\tfrac1{\sqrt3}\approx1.230959,\qquad\theta_1=2\arcsin\tfrac1{\sqrt2}=\tfrac\pi2,\qquad\theta_2=2\arcsin1=\pi
    Split angles for M−m=3,2,1M-m=3,2,1.
  2. Step 2:
    ∣00⟩∣1⟩f→load 01∣01⟩∣1⟩f→split13∣01⟩∣0⟩f+23 ∣01⟩∣1⟩f\lvert00\rangle\lvert1\rangle_f\xrightarrow{\text{load }01}\lvert01\rangle\lvert1\rangle_f\xrightarrow{\text{split}}\tfrac1{\sqrt3}\lvert01\rangle\lvert0\rangle_f+\sqrt{\tfrac23}\,\lvert01\rangle\lvert1\rangle_f
    Iteration 00: one CNOT (bit 11 differs), then sin⁡θ02=13\sin\tfrac{\theta_0}2=\tfrac1{\sqrt3}, cos⁡θ02=23\cos\tfrac{\theta_0}2=\sqrt{\tfrac23}.
  3. Step 3:
    →load 1013∣01⟩∣0⟩f+23 ∣10⟩∣1⟩f→split13(∣01⟩+∣10⟩)∣0⟩f+13∣10⟩∣1⟩f\xrightarrow{\text{load }10}\tfrac1{\sqrt3}\lvert01\rangle\lvert0\rangle_f+\sqrt{\tfrac23}\,\lvert10\rangle\lvert1\rangle_f\xrightarrow{\text{split}}\tfrac1{\sqrt3}\big(\lvert01\rangle+\lvert10\rangle\big)\lvert0\rangle_f+\tfrac1{\sqrt3}\lvert10\rangle\lvert1\rangle_f
    Iteration 11: two CNOTs (01⊕10=1101\oplus10=11) act only on the flag-11 branch; the split multiplies 2/3\sqrt{2/3} by 12\tfrac1{\sqrt2} twice.
  4. Step 4:
    →load 1113(∣01⟩+∣10⟩)∣0⟩f+13∣11⟩∣1⟩f→split13(∣01⟩+∣10⟩+∣11⟩)∣0⟩f\xrightarrow{\text{load }11}\tfrac1{\sqrt3}\big(\lvert01\rangle+\lvert10\rangle\big)\lvert0\rangle_f+\tfrac1{\sqrt3}\lvert11\rangle\lvert1\rangle_f\xrightarrow{\text{split}}\tfrac1{\sqrt3}\big(\lvert01\rangle+\lvert10\rangle+\lvert11\rangle\big)\lvert0\rangle_f
    Iteration 22: one CNOT, and RY(−π)∣1⟩=∣0⟩R_Y(-\pi)\lvert1\rangle=\lvert0\rangle moves the whole remaining amplitude to flag 00.
  5. Step 5:
    #gates=1+(1+2+2+1)+(2+2+2+1)+(1+0+2+1)=18≤1+3(5⋅2−1)=28\#\text{gates}=1+(1+2+2+1)+(2+2+2+1)+(1+0+2+1)=18\le1+3(5\cdot2-1)=28
    Count per iteration: CNOTs, zero-control XX gates, the two Toffolis of the one-step ladder, one controlled RYR_Y.

Result

∣D⟩∣0⟩f∣0⟩a=13(∣0100⟩+∣1000⟩+∣1100⟩)\boxed{\lvert D\rangle\lvert0\rangle_f\lvert0\rangle_a=\tfrac1{\sqrt3}\big(\lvert0100\rangle+\lvert1000\rangle+\lvert1100\rangle\big)}

Circuit · 4 qubits

Quantum circuit on 4 qubitsX on q2; X on q1 controlled by q2; X on q0; X on q3 controlled by q0, q1; RY(-1.23) on q2 controlled by q3; X on q3 controlled by q0, q1; X on q0; X on q0 controlled by q2; X on q1 controlled by q2; X on q1; X on q3 controlled by q0, q1; RY(-π/2) on q2 controlled by q3; X on q3 controlled by q0, q1; X on q1; X on q1 controlled by q2; X on q3 controlled by q0, q1; RY(-π) on q2 controlled by q3; X on q3 controlled by q0, q1q0q1q2q3XXRY(-1.23)XXRY(-π/2)XRY(-π)

Incremental preparation of 13(∣01⟩+∣10⟩+∣11⟩)\tfrac1{\sqrt3}(\lvert01\rangle+\lvert10\rangle+\lvert11\rangle): data qubits 0,10,1, flag qubit 22, ancilla qubit 33. The statevector has amplitude 13\tfrac1{\sqrt3} at indices 44, 88 and 1212 (data 0101, 1010, 1111 with flag and ancilla back in ∣0⟩\lvert0\rangle).

∣ψ⟩=13∣0100⟩+13∣1000⟩+13∣1100⟩\lvert\psi\rangle = \tfrac{1}{\sqrt{3}}\lvert 0100\rangle + \tfrac{1}{\sqrt{3}}\lvert 1000\rangle + \tfrac{1}{\sqrt{3}}\lvert 1100\rangle

The qRAM assumption#

Definition

Quantum random-access memory (qRAM)

A qRAM holding M=2aM=2^a classical nn-bit words x(0),…,x(M−1)x^{(0)},\dots,x^{(M-1)} is a device implementing, for every address superposition, UqRAM∑i=0M−1αi∣i⟩A∣0n⟩D=∑i=0M−1αi∣i⟩A∣x(i)⟩D,U_{\mathrm{qRAM}}\sum_{i=0}^{M-1}\alpha_i\lvert i\rangle_A\lvert0^n\rangle_D=\sum_{i=0}^{M-1}\alpha_i\lvert i\rangle_A\lvert x^{(i)}\rangle_D, with an address register AA of a=log⁡2Ma=\log_2M qubits and a data register DD of nn qubits. The qRAM assumption is that one such query costs time O(log⁡M)O(\log M) (after the memory has been loaded once), independent of how the words are distributed.

Derivation

One query prepares the indexed dataset state

  1. Step 1:
    H⊗a∣0a⟩=1M∑i=0M−1∣i⟩AH^{\otimes a}\lvert0^a\rangle=\frac1{\sqrt M}\sum_{i=0}^{M-1}\lvert i\rangle_A
    Each HH maps ∣0⟩↦12(∣0⟩+∣1⟩)\lvert0\rangle\mapsto\tfrac1{\sqrt2}(\lvert0\rangle+\lvert1\rangle); the product over aa qubits is the uniform superposition over all 2a=M2^a=M addresses.
  2. Step 2:
    UqRAM(1M∑i∣i⟩A∣0n⟩D)=1M∑i=0M−1∣i⟩A∣x(i)⟩DU_{\mathrm{qRAM}}\Big(\frac1{\sqrt M}\sum_i\lvert i\rangle_A\lvert0^n\rangle_D\Big)=\frac1{\sqrt M}\sum_{i=0}^{M-1}\lvert i\rangle_A\lvert x^{(i)}\rangle_D
    Linearity of the query with αi=1/M\alpha_i=1/\sqrt M.
  3. Step 3:
    cost=a Hadamards+1 query=O(log⁡M) vs. O(Mn) gates for the incremental circuit\text{cost}=a\ \text{Hadamards}+1\ \text{query}=O(\log M)\ \text{vs.}\ O(Mn)\ \text{gates for the incremental circuit}
    This exponential gap in MM is entirely due to the assumed device.
  4. Step 4:
    1M∑i∣i⟩∣x(i)⟩≠∣D⟩⊗∣χ⟩ for any ∣χ⟩ (M≥2, distinct x(i))\frac1{\sqrt M}\sum_i\lvert i\rangle\lvert x^{(i)}\rangle\ne\lvert D\rangle\otimes\lvert\chi\rangle\ \text{for any }\lvert\chi\rangle\ (M\ge2,\ \text{distinct } x^{(i)})
    The address stays entangled with the data: tracing out AA leaves the mixture 1M∑i∣x(i)⟩⟨x(i)∣\tfrac1M\sum_i\lvert x^{(i)}\rangle\langle x^{(i)}\rvert, not ∣D⟩⟨D∣\lvert D\rangle\langle D\rvert. Removing the address requires uncomputing ii from x(i)x^{(i)}, a separate reversible circuit.

Bucket-brigade architecture. Giovannetti, Lloyd and Maccone (2008) proposed to realise UqRAMU_{\mathrm{qRAM}} with a binary tree of M−1M-1 three-level routers (states wait, left, right) whose MM 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 a=log⁡2Ma=\log_2M address qubits a single path from the root to cell ii is open (in superposition over ii). A bus qubit then travels down, copies the cell content, and returns, after which the routers are reset in reverse order. Only O(log⁡M)O(\log M) routers are activated in each branch of the superposition, compared with O(M)O(M) 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 x↦∣ϕ(x)⟩x\mapsto\lvert\phi(x)\rangle into a Hilbert space defines the fidelity kernel k(x,x′)=∣⟨ϕ(x)∣ϕ(x′)⟩∣2k(x,x')=\lvert\langle\phi(x)\vert\phi(x')\rangle\rvert^2 (studied in depth in the quantum-kernel module). For training inputs x(0),…,x(M−1)x^{(0)},\dots,x^{(M-1)} the Gram matrix is Kmm′=k(x(m),x(m′))K_{mm'}=k(x^{(m)},x^{(m')}). Kernel ridge regression with bias fits f(x)=∑mαmk(x,x(m))+bf(x)=\sum_{m}\alpha_mk(x,x^{(m)})+b by minimising J(α,b)=∑m(ym−f(x(m)))2+λ αTKαJ(\boldsymbol\alpha,b)=\sum_m\big(y_m-f(x^{(m)})\big)^2+\lambda\,\boldsymbol\alpha^{\mathsf T}K\boldsymbol\alpha with λ>0\lambda>0.

Theorem

Sparsity of the basis-encoding kernel

For basis encoding ∣ϕ(x)⟩=∣x⟩\lvert\phi(x)\rangle=\lvert x\rangle: (i) ⟨x∣x′⟩=δxx′\langle x\vert x'\rangle=\delta_{xx'}; (ii) k(x,x′)=δxx′k(x,x')=\delta_{xx'}; (iii) on distinct training inputs K=IMK=I_M; (iv) kernel ridge regression gives b=yˉ=1M∑mymb=\bar y=\tfrac1M\sum_my_m, αm=ym−yˉ1+λ\alpha_m=\frac{y_m-\bar y}{1+\lambda}, and the prediction at every input x∗x_\ast outside the training set is f(x∗)=yˉf(x_\ast)=\bar y, independent of x∗x_\ast.

Proof (9 steps)
  1. Step 1:
    ⟨x∣x′⟩=∏j=0n−1⟨xj∣xj′⟩=∏j=0n−1δxjxj′=δxx′\langle x\vert x'\rangle=\prod_{j=0}^{n-1}\langle x_j\vert x'_j\rangle=\prod_{j=0}^{n-1}\delta_{x_jx'_j}=\delta_{xx'}
    The inner product of product states factorises, and ⟨0∣1⟩=0\langle0\vert1\rangle=0, ⟨b∣b⟩=1\langle b\vert b\rangle=1; the product is 11 iff all bits agree.
  2. Step 2:
    k(x,x′)=∣δxx′∣2=δxx′,Kmm′=δx(m)x(m′)=δmm′k(x,x')=\lvert\delta_{xx'}\rvert^2=\delta_{xx'},\qquad K_{mm'}=\delta_{x^{(m)}x^{(m')}}=\delta_{mm'}
    Square the modulus; for pairwise distinct training inputs only the diagonal survives, so K=IMK=I_M.
  3. Step 3:
    J(α,b)=∥y−α−b1∥2+λ∥α∥2J(\boldsymbol\alpha,b)=\lVert\mathbf y-\boldsymbol\alpha-b\mathbf 1\rVert^2+\lambda\lVert\boldsymbol\alpha\rVert^2
    With K=IK=I the training predictions are f(x(m))=αm+bf(x^{(m)})=\alpha_m+b and the regulariser is λαTα\lambda\boldsymbol\alpha^{\mathsf T}\boldsymbol\alpha.
  4. Step 4:
    ∇2J=2((1+λ)IM11TM),M−1T11+λ=Mλ1+λ>0\nabla^2J=2\begin{pmatrix}(1+\lambda)I_M&\mathbf 1\\\mathbf 1^{\mathsf T}&M\end{pmatrix},\qquad M-\frac{\mathbf 1^{\mathsf T}\mathbf 1}{1+\lambda}=\frac{M\lambda}{1+\lambda}>0
    The Hessian is constant; its upper block is positive definite and its Schur complement is positive for λ>0\lambda>0, so JJ is strictly convex and its unique stationary point is the global minimum.
  5. Step 5:
    ∂J∂b=−2 1T(y−α−b1)=0 ⇒ b=1M∑m(ym−αm)=yˉ−αˉ\frac{\partial J}{\partial b}=-2\,\mathbf 1^{\mathsf T}(\mathbf y-\boldsymbol\alpha-b\mathbf 1)=0\ \Rightarrow\ b=\frac1M\sum_m(y_m-\alpha_m)=\bar y-\bar\alpha
    Stationarity in the bias, with αˉ=1M∑mαm\bar\alpha=\tfrac1M\sum_m\alpha_m.
  6. Step 6:
    ∂J∂α=−2(y−α−b1)+2λα=0 ⇒ α=y−b11+λ\frac{\partial J}{\partial\boldsymbol\alpha}=-2(\mathbf y-\boldsymbol\alpha-b\mathbf 1)+2\lambda\boldsymbol\alpha=0\ \Rightarrow\ \boldsymbol\alpha=\frac{\mathbf y-b\mathbf 1}{1+\lambda}
    Stationarity in the coefficients.
  7. Step 7:
    αˉ=yˉ−b1+λ,b=yˉ−yˉ−b1+λ ⇒ λb=λyˉ ⇒ b=yˉ,αm=ym−yˉ1+λ\bar\alpha=\frac{\bar y-b}{1+\lambda},\quad b=\bar y-\frac{\bar y-b}{1+\lambda}\ \Rightarrow\ \lambda b=\lambda\bar y\ \Rightarrow\ b=\bar y,\quad\alpha_m=\frac{y_m-\bar y}{1+\lambda}
    Average the second condition, insert into the first, multiply by 1+λ1+\lambda and cancel; λ>0\lambda>0.
  8. Step 8:
    x∗∉{x(0),…,x(M−1)} ⇒ k(x∗,x(m))=0 ∀m ⇒ f(x∗)=∑mαm⋅0+b=yˉx_\ast\notin\{x^{(0)},\dots,x^{(M-1)}\}\ \Rightarrow\ k(x_\ast,x^{(m)})=0\ \forall m\ \Rightarrow\ f(x_\ast)=\sum_m\alpha_m\cdot0+b=\bar y
    The kernel vanishes between the unseen input and every training input, so only the bias remains — the same constant for every unseen x∗x_\ast.
  9. Step 9:
    f(x(m))=αm+yˉ=yˉ+ym−yˉ1+λ →λ→0 ymf(x^{(m)})=\alpha_m+\bar y=\bar y+\frac{y_m-\bar y}{1+\lambda}\ \xrightarrow{\lambda\to0}\ y_m
    On the training set the model shrinks the labels towards their mean and interpolates them as λ→0\lambda\to0: it memorises the data and predicts nothing about new inputs.

∎

Corollary

A fixed unitary cannot repair the kernel

For any data-independent unitary WW, the feature map x↦W∣x⟩x\mapsto W\lvert x\rangle has the same fidelity kernel δxx′\delta_{xx'}. A soft-margin SVM with this kernel likewise assigns the same label sign(b)\mathrm{sign}(b) to every unseen input.

Proof (3 steps)
  1. Step 1:
    ⟨x∣W†W∣x′⟩=⟨x∣x′⟩=δxx′\langle x\vert W^\dagger W\vert x'\rangle=\langle x\vert x'\rangle=\delta_{xx'}
    W†W=IW^\dagger W=I: a unitary preserves inner products, so the kernel is unchanged.
  2. Step 2:
    g(x∗)=sign(∑mαmymk(x∗,x(m))+b)=sign(b)g(x_\ast)=\mathrm{sign}\Big(\sum_m\alpha_my_mk(x_\ast,x^{(m)})+b\Big)=\mathrm{sign}(b)
    The SVM decision function contains the kernel only through k(x∗,x(m))=0k(x_\ast,x^{(m)})=0 for unseen x∗x_\ast.
  3. Step 3:
    inductive bias must come from k(x,x′)≠0 for x≠x′\text{inductive bias must come from }k(x,x')\ne0\ \text{for}\ x\ne x'
    To generalise, similar inputs must have overlapping feature states. Encodings with continuous parameters provide exactly this: amplitude encoding gives k=∣⟨x^∣x^′⟩∣2k=\lvert\langle\hat x\vert\hat x'\rangle\rvert^2 for normalised vectors, and angle encoding gives k=∏jcos⁡2xj−xj′2k=\prod_j\cos^2\tfrac{x_j-x'_j}2 (next lessons).

∎

Quick check

Four distinct bit strings with labels y=(3,−1,4,2)\mathbf y=(3,-1,4,2) are basis-encoded and fitted by kernel ridge regression with bias and λ=0.5\lambda=0.5. What does the model predict for a fifth, unseen bit string?

tolerance ±0.000001

Quick check

What is the fidelity-kernel Gram matrix of the basis-encoded strings 000000, 011011, 101101, 110110?

Select one answer

Comparison of encodings. The table summarises the three encodings of this module for an input of dimension NN (a bit string of length NN, or a real vector with NN components):

EncodingQubitsState-preparation gatesFidelity kernel
Basis (τ\tau bits per component)NτN\tau≤Nτ\le N\tau, depth 11δxx′\delta_{xx'}
Amplitude⌈log⁡2N⌉\lceil\log_2N\rceilO(N)O(N) for generic inputs∣⟨x^∣x^′⟩∣2\lvert\langle\hat x\vert\hat x'\rangle\rvert^2
Angle (RYR_Y per feature)NNNN, depth 11∏jcos⁡2xj−xj′2\prod_j\cos^2\tfrac{x_j-x'_j}{2}

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

  1. 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
  2. D. Ventura and T. Martinez, Quantum associative memory, Information Sciences 124, 273–296 (2000).doi:10.1016/S0020-0255(99)00101-2
  3. V. Giovannetti, S. Lloyd and L. Maccone, Quantum random access memory, Physical Review Letters 100, 160501 (2008).doi:10.1103/PhysRevLett.100.160501
  4. S. Aaronson, Read the fine print, Nature Physics 11, 291–293 (2015).doi:10.1038/nphys3272
  5. 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
  6. 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-graded

    Prepare the basis encoding ∣1011⟩=X⊗I⊗X⊗X ∣0000⟩\lvert1011\rangle=X\otimes I\otimes X\otimes X\,\lvert0000\rangle of the bit string x=1011x=1011 on four qubits, using at most three XX gates. Qubit 0 is the leftmost (most significant) bit, so the target is the basis vector with index k=8+2+1=11k=8+2+1=11 of C16\mathbb C^{16}.

    Open lab