Skip to main content

View on GitHub

Open this notebook in GitHub to run it yourself
Given a scalar function of dd dimensions, f(x1,,xd)f(x_1, \ldots, x_d), computing its gradient at a specific point is often essential. On a classical computer, this requires at least d+1d+1 queries to ff. This notebook presents a quantum algorithm that computes the gradient with a single query, based on the paper by S. P. Jordan [1]. This represents a dramatic speedup for high-dimensional functions, where function evaluation is typically the dominant computational cost.
  • Input: A black-box function ff of dd dimensions.
  • Promise: The function is smooth and has bounded gradient: f<fmax|\nabla f|<\nabla f_{\text{max}}.
  • Output: The gradient f\nabla f at the origin with nn bits of precision, encoded on a quantum register.
Complexity: In black-box query complexity, the classical algorithm requires at least d+1d+1 queries to ff, while the quantum algorithm requires only one.
Keywords: Foundational quantum algorithms, Gradient, Function evaluation, Oracle problem, Quantum Fourier Transform (QFT)
The core idea is an in-place computation: first, the function is encoded directly into the phases of the coordinate superposition state. Then, an inverse QFT is applied to overwrite that exact same coordinate register with the gradient, extracting it as a measurable outcome without needing a separate output register. We demonstrate this step by step - first with a simplified version to build intuition, then with refinements for the complete algorithm. Quantum Circuit

Introduction

Initialization

Simplified Explanation of the Algorithm

The algorithm has two main steps:
  1. Encode the function into the phases of the coordinate register’s superposition
  2. Apply an inverse QFT to transform the coordinate register directly into the measurable gradient state
In detail:
  1. We define an interval ll with NN points around the origin where we estimate the gradient, establishing a resolution dx=l/N\mathrm{dx}=l/N.
To calculate the gradient at an arbitrary point x0x_0 instead of the origin, we can define an auxiliary function f~(x)=f(x+x0)\tilde{f}(x) = f(x + x_0) and evaluate its gradient at the origin. For each dimension (xix_i), we create the superposition δi=l+l+dx+l+2dxldx|\delta_i\rangle=|-l\rangle+|-l+\mathrm{dx}\rangle+|-l+2\mathrm{dx}\rangle\cdots|l-\mathrm{dx}\rangle using Hadamard gates on 0n|0\rangle^{\otimes n}. The actual encoding on qubits is discussed later. For simplicity, we consider a single dimension. For d>1d > 1, the state δ|\delta\rangle is a tensor product over all dimensions: δ=iδi|\delta\rangle=\prod_i|\delta_i\rangle
  1. Using a phase oracle or the phase kickback technique, we encode the function as phases:
ei2πf(l)l+ei2πf(l+dx)l+dx+ei2πf(l+2dx)l+2dxei2πf(ldx)ldxe^{i\cdot2\pi f(-l)}|-l\rangle + e^{i\cdot2\pi f(-l+\mathrm{dx})}|-l+\mathrm{dx}\rangle + e^{i\cdot2\pi f(-l+2\mathrm{dx})}|-l+2\mathrm{dx}\rangle \cdots e^{i\cdot2\pi f(l-\mathrm{dx})}|l-\mathrm{dx}\rangle
  1. For a sufficiently small interval, we approximate f(x)f(0)+xf(0)f(x)\approx f(0)+x\cdot f'(0), allowing us to factor the state:
ei2πf(0)(ei2π(l)f(0)l+ei2π(l+dx)f(0)l+dx+ei2π(l+2dx)f(0)l+2dxei2π(ldx)f(0)ldx)=ei2πf(0)lldxei2πjf(0)j\begin{aligned} & e^{i\cdot2\pi f(0)}\left(e^{i\cdot2\pi (-l)\cdot f'(0)}\lvert-l\rangle + e^{i\cdot2\pi (-l+\mathrm{dx})f'(0)}\lvert-l+\mathrm{dx}\rangle + e^{i\cdot2\pi (-l+2\mathrm{dx})f'(0)}\lvert-l+2\mathrm{dx}\rangle \cdots e^{i\cdot2\pi (l-\mathrm{dx})f'(0)}\lvert l-\mathrm{dx}\rangle\right) \\ &= e^{i\cdot2\pi f(0)}\sum_{-l}^{l-\mathrm{dx}} e^{i\cdot2\pi jf'(0)}|j\rangle \end{aligned}
  1. This state is exactly the QFT of the computational basis state f(0)|f'(0)\rangle.
Applying the inverse QFT yields: f(0)|f'(0)\rangle
  1. Measure the register to obtain f(0)f'(0) (or f\nabla f for multi-dimensional functions).

Full Explanation of the Algorithm

The simplified explanation captures the key idea but omits two important details: handling negative values and fractional representation. When estimating the gradient, we analyze an interval ll around the origin. The state δi|\delta_i\rangle represents NN equally spaced points in this interval, normalized as: x=lNδx=\frac{l}{N}\delta With signed δ\delta ranging from N/2-N/2 to N/21N/2-1: δ=N/2\delta=-N/2 is the leftmost point (l/2-l/2), δ=0\delta=0 is the origin, and δ=N/21\delta=N/2-1 is the rightmost point (l/2l/2)*. We also normalize the output. Assuming the gradient is bounded between m/2-m/2 and m/2m/2, we represent those values using the NN states: f=mNδmeasured\nabla f=\frac{m}{N}\delta_{measured} When applying the algorithm, we choose ll and mm based on prior knowledge of f(x)f(x). The value NN determines the final resolution. ll must be small enough to keep the function approximately linear, and mm must exceed the maximum expected gradient magnitude while maintaining sufficient resolution. Using these two normalizations, we modify the algorithm:
  1. In step 2, instead of applying f(δ)f(\delta), we apply Nmlf(lNδ)\frac{N}{ml}f\left(\frac{l}{N}\delta\right).
  2. In step 5, the signed measurement directly gives Nmf\frac{N}{m}\nabla f, so f=mNδmeasured\nabla f = \frac{m}{N}\delta_{measured}.
Using a signed register means the measured δmeasured\delta_{measured} is already the correct signed value with no further adjustment needed. * The rightmost point is actually l/2l/Nl/2-l/N rather than l/2l/2, because the center is at the origin and NN must be even. However, this distinction does not affect the conceptual understanding.

Parameter Selection

We need to select appropriate values for ll, mm, and NN. We use the following notation:
  • fmax\nabla f_{\text{max}} - bound on the gradient magnitude: f<fmax|\nabla f|<\nabla f_{\text{max}}
  • ϵ\epsilon - desired accuracy: festf<ϵ|\nabla f_{\text{est}}-\nabla f| < \epsilon
  • dd - dimensionality of ff
  • D2D_2 - bound on the second derivative of ff near the origin

Selecting ll

Choose ll to ensure the function remains approximately linear. Similar to classical numerical differentiation, this interval must be sufficiently small. To keep gradient variation within accuracy ϵ\epsilon, we require fmaxfminϵ\nabla f_{\text{max}} - \nabla f_{\text{min}} \le \epsilon. For a single dimension, the second derivative is approximately f(x)(fmaxfmin)/lf''(x) \approx (\nabla f_{\text{max}} - \nabla f_{\text{min}}) / l. This gives l<ϵf(x)l < \frac{\epsilon}{|f''(x)|}. Using D2D_2 as the second derivative bound, we get l<ϵD2l < \frac{\epsilon}{D_2}. In multiple dimensions, we sum deviations as root-mean-square across all dd dimensions, introducing a factor of 1/d1/\sqrt{d}. To improve this bound, we scale by the uniform distribution variance, 112\frac{1}{12}. The final bound for ll is: l23ϵD2dl\leq\frac{2\sqrt{3}\epsilon}{D_2\sqrt{d}} For a one-dimensional quadratic function f(x)=ax2+bx+cf(x)=ax^2+bx+c, we need ll smaller than 12ϵ2a\frac{\sqrt{12}\epsilon}{2a}. Furthermore, in order to minimize the number of bits of precision to which ff must be evaluated, ll should be chosen as large as possible, subject to the constraint above. So ll should be chosen tightly. See Jordan’s paper [1] for a full derivation.

Selecting mm

The parameter mm bounds the gradient magnitude. Since the gradient is signed, we need m2fmaxm \ge 2\nabla f_{\text{max}}. The resolution of the result is mN\frac{m}{N}. For a given register size and accuracy ϵ\epsilon, the upper bound is m2Nϵm \le 2N\epsilon.

Selecting nn

N=2nN=2^n defines the result resolution. The step size between possible outcomes is mN\frac{m}{N}. To achieve accuracy ϵ\epsilon, we need Nm2ϵN\geq\frac{m}{2\epsilon}. Assuming tight mm selection, this gives a lower bound for the number of qubits: nlog2(fmaxϵ)n\geq\log_2\left(\frac{\nabla f_{\text{max}}}{\epsilon}\right)

Summary

Given fmax\nabla f_{\text{max}} and desired accuracy ϵ\epsilon, select parameters as:

Outline

This notebook covers:
  1. Theoretical foundation and parameter selection
  2. Implementation
  • Phase state preparation (phase kickback and direct methods)
  • Inverse QFT for gradient extraction
  • Examples with linear and non-linear functions
  1. Performance analysis
  2. Multi-dimensional examples (d>1d > 1)

Implementation

State Preparation

Phase Kickback

The first step is to prepare the state: δei2πNmlf(lNδ)δ\sum_{\delta}e^{i2\pi\frac{N}{ml}f(\frac{l}{N}\delta)}|\delta\rangle The paper assumes an oracle xf(x)|x\rangle \rightarrow |f(x)\rangle, which we use with the phase kickback technique to create this state. The next example demonstrates how this works. The phase kickback has three main steps:
  1. Apply Hadamard gates on δ|\delta\rangle to create a superposition of all sample points
  2. Initialize the ancilla to 1111...1|1111...1\rangle (in binary) and apply QFT
  3. Add f(δ)f(\delta) to the ancilla; the function value is “kicked back” as a phase
See the appendix for details.
Output:
Output:
Output:
Let’s examine the phase compared to the classical function value:
output The graph shows the original function (gray), classical values (blue), and quantum phase at sample points (orange). Results are displayed in both normalized coordinates (black axes) and original values (blue axes).

Direct Phase

While the phase kickback approach is sometimes well-suited for hardware implementations with a state oracle, it requires significant circuit overhead in simulation. A direct approach provides more efficient state preparation for our purposes. A simpler approach is to directly prepare the desired state: δei2πNmlf(lNδ)δ\sum_{\delta}e^{i2\pi\frac{N}{ml}f(\frac{l}{N}\delta)}|\delta\rangle The example below demonstrates this method. Compare the circuit width, depth, and gate counts to see the efficiency gain. In practice, the choice between methods depends on whether you have access to an efficient oracle for the function.
Output:
Output:
Output:
output The state is identical to the phase kickback method. Moving forward, we’ll use the direct method for its superior efficiency.

Quadratic Function

Non-linear functions are more interesting. In the next example, we explore a quadratic function. Note that the critical step occurs in the next phase when we apply the QFT. Here, the phases follow the function exactly without linearization. The QFT will extract the linear (gradient) component from these phase values.
Output:
Output:
Output:
output Decreasing ll narrows the sampling interval, making the function appear more linear within that region.
Output:
Output:
Output:
output

Full Algorithm

Implementation

The final step is to apply the inverse QFT to the coordinates. This extracts the gradient from the phases and produces a measurable state containing the normalized gradient value. We will first define a QFunc that preforms the algorithm and calculates the gradient of a given function ff, for single and multiple dimensions, and another function that transforms a given function (either Callable or QCallable) into a normalized phase oracle.

Linear Functions

We will see the first example on a linear function.
We switch from statevector simulation to standard simulation, which measures the final result rather than examining phase values directly.
Output:
Output:
output The measured gradient is always a multiple of the resolution m/Nm/N. In the previous example, the exact gradient -0.5 was a multiple of the resolution 0.25, so the success rate was 100%. When the gradient is not a multiple of the resolution, the result becomes a superposition of nearby states. The algorithm still provides a good approximation, as shown in the next example.
Output:
Output:
output
Output:

Quadratic Function

For non-linear functions, the interval ll must be chosen carefully. The algorithm requires the function to be approximately linear over the interval, which means ll must be sufficiently small. The next example demonstrates this dependency. With appropriate selection of ll, the function remains nearly linear over the sampling interval, yielding high success rates.
Output:
Output:
output Conversely, if ll is too large and the function deviates significantly from linearity, the success rate drops dramatically.
Output:
Output:
output

Performance Analysis

We can plot the success rate as a function of parameter choices. Consider a quadratic function f(x)=0.6x2+0.25x+0.1f(x)=0.6x^2+0.25x+0.1 with f(0)=0.25f'(0)=0.25, and target accuracy ϵ=0.2\epsilon=0.2. To determine valid parameter ranges, we need to use:
  • d=1d=1 (dimensionality)
  • D2=1.2D_2=1.2 (second derivative bound)
  • fmax\nabla f_{\max}: With l0.1l \approx 0.1, we have f[0.19,0.31]\nabla f \in [0.19, 0.31], so fmax=0.31\nabla f_{\max}=0.31
From the theory, the parameters must satisfy (with n=3n=3):
  • m20.31=0.62m\geq 2 \cdot 0.31 = 0.62
  • m2Nϵ=3.2m\leq 2 \cdot N \cdot \epsilon = 3.2
  • l0.28l\leq 0.28
In the next graph, we plot success rate versus mm with l=0.1l=0.1. The valid range [0.62,3.2][0.62, 3.2] is highlighted.
Output:
Next, we plot success rate versus ll with m=2m=2. The valid bound l0.28l\leq 0.28 is highlighted.

Multi-Dimensional Examples

The algorithm extends to multiple dimensions. In this example we will demonstrate a general quadratic coupled function: f(x,y)=ax2+by2+cxy+dx+ey+ff(x,y)=ax^2+by^2+cxy+dx+ey+f

Summary and Discussion

This notebook demonstrated Jordan’s quantum gradient estimation algorithm, which estimates f\nabla f at the origin using a single query to ff, compared to the classical lower bound of d+1d+1 queries.

Algorithm

  1. Prepare a uniform superposition over N=2nN = 2^n sample points using Hadamard gates.
  2. Encode ff as phases, either via phase kickback from a state oracle or directly using a phase oracle.
  3. Apply the inverse QFT to map the phase encoding onto a measurable computational basis state containing f\nabla f.

Parameter Selection

Potential Use Cases

The d+11d+1 \to 1 query reduction is most valuable when dd is large and each function evaluation is expensive. Potential applications include: Optimization, Root-finding, Functional minimization and PDEs and more.

Limitation: Higher-Order Derivatives

The algorithm cannot be applied recursively to compute second or higher-order derivatives of ff. The method requires a quantum oracle that evaluates f(x)f(x) coherently as a phase across all xx simultaneously. After running the circuit and measuring, the output is a single classical value f(0)\nabla f(0) - not a new quantum oracle for f(x)\nabla f(x) at arbitrary points xx. Estimating 2f\nabla^2 f by this approach would require such an oracle for f\nabla f, which the algorithm does not construct. In classical finite differences, second derivatives are obtained by calling ff at multiple points and differencing the first-derivative estimates. Replicating this quantumly would require separate oracle queries for each evaluation point, reducing the query complexity back to the classical O(d2)O(d^2) regime and eliminating the quantum advantage entirely.

Appendices

Appendix 1

  • Phase Kickback
We start with the state δ0|\delta\rangle|0\rangle where δ|\delta\rangle is a superposition created by the Hadamard gate for all the coordinates, hence: δ1=0N1δd=0N1δ1δd\sum_{\delta_1=0}^{N-1}\cdots\sum_{\delta_d=0}^{N-1}|\delta_1\rangle\cdots|\delta_d\rangle For convenience we will look at the d=1d=1 case, but the same procedure can be used for bigger dd. The ancilla starts at the ground state a=0|a\rangle=|0\rangle. We first apply bitwise X gate to create the state 1111|111\cdots1\rangle, which in the signed fractional QNum representation corresponds to the value 1/N0-1/N_0. Next, we apply QFT on the ancilla. Using the fact that ei2πa=1e^{i2\pi a} = 1 for integer aa, the QFT of N01|N_0-1\rangle simplifies to: 1N0a=0N01ei2πa(N01)/N0a=1N0a=0N01ei2πa/N0a\frac{1}{\sqrt{N_0}}\sum_{a=0}^{N_0-1}e^{i2\pi a(N_0-1)/N_0}|a\rangle = \frac{1}{\sqrt{N_0}}\sum_{a=0}^{N_0-1}e^{-i2\pi a/N_0}|a\rangle The full state after both Hadamard (on coordinates) and QFT (on ancilla) is: 1NN0δ=0N1a=0N01ei2πa/N0δa\frac{1}{\sqrt{N \cdot N_0}}\sum_{\delta=0}^{N-1}\sum_{a=0}^{N_0-1} e^{-i2\pi a/N_0}\,|\delta\rangle|a\rangle As discussed before, we assume that we have an oracle ff that applies the function on the state xf(x)|x\rangle \rightarrow |f(x)\rangle. We apply this function and add the normalized result fnorm(δ)=Nmlf ⁣(lNδ)f_\mathrm{norm}(\delta) = \frac{N}{ml}f\!\left(\frac{l}{N}\delta\right) to the ancilla register, mapping aa+fnorm(δ)|a\rangle \to |a + f_\mathrm{norm}(\delta)\rangle: 1NN0δ,aei2πa/N0δa+fnorm(δ)\frac{1}{\sqrt{N \cdot N_0}}\sum_{\delta,\,a} e^{-i2\pi a/N_0}\,|\delta\rangle\,|a + f_\mathrm{norm}(\delta)\rangle Substituting a=a+fnorm(δ)a' = a + f_\mathrm{norm}(\delta), i.e. a=afnorm(δ)a = a' - f_\mathrm{norm}(\delta): 1NN0δ,aei2π(afnorm(δ))/N0δa\frac{1}{\sqrt{N \cdot N_0}}\sum_{\delta,\,a'} e^{-i2\pi (a' - f_\mathrm{norm}(\delta))/N_0}\,|\delta\rangle\,|a'\rangle Separating the phase into two factors: =1Nδ=0N1ei2πfnorm(δ)/N0δ    1N0a=0N01ei2πa/N0aoriginal ancilla state= \frac{1}{\sqrt{N}}\sum_{\delta=0}^{N-1} e^{i2\pi f_\mathrm{norm}(\delta)/N_0}\,|\delta\rangle \;\otimes\; \underbrace{\frac{1}{\sqrt{N_0}}\sum_{a'=0}^{N_0-1} e^{-i2\pi a'/N_0}\,|a'\rangle}_{\text{original ancilla state}} The ancilla returns to its pre-oracle state and the function value has been “kicked back” as a phase onto the coordinate register. Since the ancilla is a fractional QNum with n0n_0 fractional bits, dividing by N0N_0 converts from the integer index back to the fractional value, so fnorm(δ)/N0fnorm(δ)f_\mathrm{norm}(\delta)/N_0 \to f_\mathrm{norm}(\delta) in the fractional encoding. The coordinate register is therefore left in the state: δ=0N1ei2πfnorm(δ)δ=δ=0N1ei2πNmlf ⁣(lNδ)δ\sum_{\delta=0}^{N-1} e^{i2\pi f_\mathrm{norm}(\delta)}\,|\delta\rangle = \sum_{\delta=0}^{N-1} e^{i2\pi \frac{N}{ml} f\!\left(\frac{l}{N}\delta\right)}\,|\delta\rangle This is exactly the desired phase state from Step 2 of the algorithm, and the rest of the algorithm proceeds identically to the Direct Phase approach.

References

[1]: Stephen P. Jordan. Fast Quantum Algorithm for Numerical Gradient Estimation. Physical Review Letters 95 (2005)