View on GitHub
Open this notebook in GitHub to run it yourself
Quantum Oracle Sketching is a data-loading algorithm introduced by Zhao et al. [1]. It provides the missing link between classical data access and the coherent oracle queries on which many powerful quantum algorithms are built. The algorithm approximates the desired oracle on the fly from a stream of classical samples , applying an incremental data-dependent rotation per sample and discarding each sample once consumed. At no point is the dataset stored in either classical or quantum memory. The result is an exponential advantage in space complexity over any classical learner; and when the data distribution varies in time while the learner stays fixed, a super-polynomial advantage in sample complexity is obtained as well: a classical machine requires the number of data examples to scale super-polynomially with the data size , while a quantum learner only needs an amount of data that scales linearly, achieving matching accuracy and probabilistic performance. The algorithm treats the following problem:Extensions: Here we consider a Boolean function. Nevertheless, the method can be generalized for multi-bit Boolean output real-valued functions (), state preparation (input , encoding the index and value of a vector element) oracles, and block-encodings of matrices (input is a tuple , encoding the elements of a matrix ). can then be used as a drop-in subroutine inside any quantum query algorithm. Complexity
- Input: classical data samples , where each is drawn independently from a (possibly time-varying) distribution , and is the target Boolean function ().
- Output: A quantum unitary on qubits that approximates the corresponding coherent query oracle , the phase oracle , or a real valued phase to error in diamond distance.
- Sample complexity: A single -accurate oracle sketch requires classical samples. To support an arbitrary quantum algorithm that makes queries to total error , each query is sketched to error , giving
The quadratic dependence on is unavoidable, mirroring the Born-rule relationship between quantum amplitudes and probabilities.
- Space complexity: qubits - the quantum machine never stores the dataset, only its current quantum state.
- Time complexity: in the data-loading stage (one constant-depth controlled gate per sample); subsequent processing of each sample requires only time. Here the overscript tilde designates possible hidden logarithmic dependencies in .
- Classical lower bound (dynamic case): Any classical machine of size that matches the quantum prediction accuracy on a time-varying distribution, requires a super-polynomial number of samples.
Keywords: Quantum Learning Theory, Quantum Machine Learning (QML), Streaming Algorithms
Introduction
Most quantum speedup over a classical baseline is stated relative to an oracle, a unitary that encodes the input function and acts in superposition. Grover’s search assumes a phase oracle ; the HHL linear-system solver [5], quantum singular-value transformation (QSVT) [4], and most quantum machine-learning routines rely on a block-encoding oracle for the input matrix; amplitude estimation and quantum walks similarly assume coherent access to the input. This coherent access, the ability to evaluate on a superposition of inputs in a single query, is exactly what enables the quadratic and exponential speedups, without it, an in-depth analysis shows that the algorithms collapse to their classical counterparts. Yet real-world classical data, produced one sample at a time by experiments, sensors, or users, does not natively support such queries. The standard remedy is to load the entire dataset into a quantum random-access memory (QRAM [3]), but its fault-tolerance overhead often exceeds the quantum advantage it enables, leaving the most powerful quantum algorithms without a practical interface to massive classical data. Quantum oracle sketching is the algorithm that closes this gap: it constructs an approximate on the fly from a stream of classical samples, with no QRAM and no full dataset ever held in memory.The streaming-learning framework
Quantum oracle sketching is proposed in the context of a specific online-learning model. The framework sets the rules under which both the quantum and the classical baselines operate, and it explains why the comparison between the two is well-posed. Setup.- A data-generating process emits a sequence of classical samples .
- A learner of size (which can be the number of logical qubits composing the quantum learner, or the number of floating-point words for a classical one) observes the samples one at a time.
- After samples, the learner is asked to predict, classify, or compress some property of the underlying process (not of the empirical sample!). Examples: the label of a held-out test feature vector (classification), the value of a quadratic form (linear systems), or the projection of a test point onto the top principal direction (PCA).
- Refreshing time : the timescale beyond which samples become effectively uncorrelated.
- Repetition number : the maximum expected number of times any single sample is repeated within a window of steps.
- Quantum oracle sketching consumes the stream of classical samples and produces an approximate oracle unitary (the subject of the present notebook).
- A quantum query algorithm
- Grover, QSVT [4], HHL [5], amplitude estimation, etc. - uses as a black-box subroutine to prepare a target state that encodes the desired property of .
- Interferometric classical shadows - a sign-preserving variant of the Classical Shadow Tomography protocol [2].
Implementation of the Oracle Sketching Algorithm
Oracle Sketching of a Boolean Function and Uniform Distribution
We aim to implement the Boolean phase oracle given only random classical samples for , where each is drawn uniformly from . Per-sample rotation. For each sample we apply a small data-dependent phase rotation that fires only on the basis state : Coherent accumulation. Because every is diagonal in the computational basis, all factors commute and the product collapses cleanly: where is the empirical frequency of basis label . Concentration. As grows, by the law of large numbers. Picking makes and Sample complexity. Ref. [1] shows the diamond-distance error decays as , so reaching error costs samples. Each sample is processed once and immediately discarded. Comparison to generic random Hamiltonian simulation. The scaling above is not what a naive random-rotation strategy would deliver. If one instead applied a sequence of small phases driven by an arbitrary Hamiltonian with - hoping the sequence approximates , randomized-Hamiltonian-simulation results show the operator-norm error compounds to That extra factor of would consume the entire quantum advantage. To support queries with combined error , each individual query must be sketched to error , so per-query naive simulation needs samples and the total becomes versus the achieved here - a factor- blow-up that would take -sample tasks straight back into the regime where any classical machine of size wins. The improvement to comes from a crucial piece of structure: each phases only the one-dimensional subspace , and distinct values of label mutually orthogonal subspaces, so per-sample errors live in disjoint Hilbert-space blocks rather than compounding across the full register. Quantum oracle sketching is, in this sense, a carefully engineered non-generic instance of randomized Hamiltonian simulation, designed precisely to dodge the trap. We begin by importing the required python modules and define the size of the problem and the truth table, defining .Output:
apply_basis_phase realizes by controlling on the equality predicate qvar == x_int; the body phase(theta) becomes a relative phase on the controlled subspace and the identity elsewhere, which is exactly .
The quantum_oracle_sketch_boolean function iterates over the samples of x_samples, leading to an approximate application of .
quantum_oracle_sketch_boolean for one random sample sequence of samples, then run the inverse Bell preparation (CX followed by H on qubit 0) so that the Bell basis is rotated back to the computational basis: in particular (i.e. qvar = 1).
Measuring the register in the computational basis with sample(...) function makes the fidelity equal to the probability of observing qvar = 1.
The single-realization error scales as , so the fidelity gap from is of order on average over realizations, with typical per-shot fluctuations of order .
Output:
Verification of the sample-complexity scaling
In order to verify the sample complexity, we build the sketched unitary classically by collecting random samples and assembling the diagonal matrix from eq. (2). Following, we compare the results to the theory and evaluate the scaling constants (hidden by the big-O notation). Two scalings of the error to the ideal oracle are worth keeping distinct:- Single-realization operator-norm error for one random data sample is dominated by the variance of the empirical frequencies .
- Random-unitary channel - the actual object that gets used inside any quantum query algorithm - has diamond-distance error to .
Output:

Extensions
We now extend the basic sketching algorithm to the case where the data points are sampled from a non-uniform distribution. First, we analyze the case where the probability distribution, , is known. In practice, this case is scalable only when can be defined in terms of parameters; otherwise, the memory required to store all the probabilities would ruin the exponential memory advantage. In the second extension we consider an unknown probability distribution, where only the range of probabilities is known. We present an implementation of the two cases and test the latter for a two-qubit operation.Non-uniform known probability distribution
The construction above assumed the samples are drawn uniformly from , which let us pick the global scaling so that . When the samples are drawn from a known, possibly non-uniform distribution on with everywhere, the same product-and-concentration argument carries through provided we rescale per sample: so that the empirical-frequency limit still gives on each active basis state. The sample complexity becomes ; the worst-case rescaling appears as an effective factor (the maximum value of for ) that reduces to for the uniform case. Below we present a generalization ofquantum_oracle_sketch_boolean by accepting a per-sample angle thetas[t] = π / (p(x_samples[t]) · M); the existing apply_basis_phase primitive then realizes each . x_samples is assumed pre-filtered to active samples ().
The function reduces to the uniform Boolean qfunc when (all angles equal ).
Extension to an unknown probability distribution (via QSVT)
When is not known in advance the per-sample angle is unavailable. Suppose, however, that we know bounds on the support of : on the support, with condition number . The construction of Zhao et al. handles this case in three stages. Sketch with the -scaling. Pick . Quantum oracle sketching then approximates The eigenvalues of are on the subspace and lie in on the subspace. Expose with a Hadamard test. Introduce one ancilla qubit and the gate . The unitary which satisfies So the spectrum of the block-encoded operator is on and on . Crucially, this is a Hermitian operator with eigenvalues in - the natural domain for a real QSVT polynomial. Apply a threshold polynomial via QSVT. Use the QSVT polynomial of Gilyén–Su–Low–Wiebe, [6] (Polynomial approximation for a threshold function) with threshold . It is an even real polynomial satisfying- on ,
- on (where the entries sit),
- on (where the entries sit),
- degree .
hadamard_test_W, which performs a block encoding of , and proj_W_block, which is used as both projectors of the QSVT algorithm.

Output:
Verification on a 2-qubit Bell state via Classiq execution
With the ideal oracle is , the same phase on the most-significant-bit subspace used in the Boolean cell above, so again maps to . The unknown- pipeline replaces the direct sketched oracle by the QSVT polynomial block-encoded in , which approximates inside the ancilla subspace. We prepare , apply the full QSVT chain, and then run the inverse Bell preparation so that (qvar = 1). Post-selecting on the QSVT block (aux_W = qsvt_aux = 0) and measuring qvar gives the in-block fidelity .
Output:
Output:
Output:
Output: