Make the default shift size h
of finite_diff
and spsa_grad
depend on shots
#5568
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context:
Finite differences use a shift parameter
h
by which input parameters are shifted. The optimal choice of this parameter (bias-variance tradeoff) is not known a priori, but it is well-known that analytic and shot-based computations have very different optimal settings.The current default
1e-7
is a poor choice for shot-based evaluation because of the large variance incurred by sampling. Forshots=None
it is a reasonable setting.The same holds for
spsa_grad
, which uses a finite difference recipe under the hood as well.Description of the Change:
Introduce helper functions to
finite_diff
andspsa_grad
that sets the default value ofh
to0.1
when the differentiated tape hasshots!=None
. Ifshots=None
, the previous default values (1e-7
forfinite_diff
,1e-5
forspsa_grad
) are maintained, and of course a user-specifiedh
overwrites these default values.Benefits:
Reasonable results for the gradient transforms when using shots and not specifying
h
manually.In particular, when
param_shift
falls back tofinite_diff
, noh
value can be specified.Possible Drawbacks:
Defaults are set arbitrary, but so they were before.
Related GitHub Issues:
#904
Regarding this issue: A way more involved relationship between shots and shift size is proposed in the discussion there.
Given that unknown parameters would be needed to implement the equation suggested there, and that it only holds for a particular finite difference recipe (and thus not for other
approx_order
,n
orstrategy
settings), it seems infeasible to implement such a complex heuristic. This PR is a coarse-grained fix preventing PennyLane from running almost-surely unreasonable circuits by default.@glassnotes @co9olguy
[sc-4095]