pennylane
pennylane copied to clipboard
Make the default shift size `h` of `finite_diff` and `spsa_grad` depend on shots
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. For shots=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
and spsa_grad
that sets the default value of h
to 0.1
when the differentiated tape has shots!=None
. If shots=None
, the previous default values (1e-7
for finite_diff
, 1e-5
for spsa_grad
) are maintained, and of course a user-specified h
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 to finite_diff
, no h
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
or strategy
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]
Codecov Report
All modified and coverable lines are covered by tests :white_check_mark:
Project coverage is 99.68%. Comparing base (
10d59e7
) to head (1f7834d
). Report is 409 commits behind head on master.
Additional details and impacted files
@@ Coverage Diff @@
## master #5568 +/- ##
==========================================
- Coverage 99.69% 99.68% -0.01%
==========================================
Files 410 410
Lines 38247 37966 -281
==========================================
- Hits 38130 37848 -282
- Misses 117 118 +1
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
It was decided not to implement this change. Good to close @trbromley @co9olguy ?