qml.gradients.finite_diff_coeffs¶
- finite_diff_coeffs(n, approx_order, strategy)[source]¶
Generate the finite difference shift values and corresponding term coefficients for a given derivative order, approximation accuracy, and strategy.
- Parameters
n (int) – Positive integer specifying the order of the derivative. For example,
n=1
corresponds to the first derivative,n=2
the second derivative, etc.approx_order (int) – Positive integer referring to the approximation order of the returned coefficients, e.g.,
approx_order=1
corresponds to the first-order approximation to the derivative.strategy (str) – One of
"forward"
,"center"
, or"backward"
. For the"forward"
strategy, the finite-difference shifts occur at the points x0,x0+h,x0+2h,…, where h is some small step size. The"backwards"
strategy is similar, but in reverse: x0,x0−h,x0−2h,…. Finally, the"center"
strategy results in shifts symmetric around the unshifted point: …,x0−2h,x0−h,x0,x0+h,x0+2h,….
- Returns
A
(2, N)
array. The first row corresponds to the coefficients, and the second row corresponds to the shifts.- Return type
array[float]
Example
>>> finite_diff_coeffs(n=1, approx_order=1, strategy="forward") array([[-1., 1.], [ 0., 1.]])
For example, this results in the linear combination:
−y(x0)+y(x0+h)hwhere h is the finite-difference step size.
More examples:
>>> finite_diff_coeffs(n=1, approx_order=2, strategy="center") array([[-0.5, 0.5], [-1. , 1. ]]) >>> finite_diff_coeffs(n=2, approx_order=2, strategy="center") array([[-2., 1., 1.], [ 0., -1., 1.]])
Details
Consider a function y(x). We wish to approximate the n-th derivative at point x0, y(n)(x0), by sampling the function at N<n distinct points:
y(n)(x0)≈N∑i=1ciy(xi)where ci are coefficients, and xi=x0+si are the points we sample the function at.
Consider the Taylor expansion of y(xi) around the point x0:
y(n)(x0)≈N∑i=1ciy(xi)=N∑i=1ci[y(x0)+y′(x0)(xi−x0)+12y″where s_i = x_i-x_0. For this approximation to be satisfied, we must therefore have
\begin{split}\sum_{i=1}^N s_i^j c_i = \begin{cases} j!, &j=n\\ 0, & j\neq n\end{cases}.\end{split}Thus, to determine the coefficients c_i \in \{c_1, \dots, c_N\} for particular shift values s_i \in \{s_1, \dots, s_N\} and derivative order n, we must solve this linear system of equations.