-
Notifications
You must be signed in to change notification settings - Fork 604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make the default shift size h
of finite_diff
and spsa_grad
depend on shots
#5568
Conversation
>>> qml.gradients.finite_diff(circuit, h=10e-2)(params) | ||
>>> qml.gradients.finite_diff(circuit, h=0.1)(params) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a change.
@@ -787,6 +779,7 @@ def test_var_expectation_values(self, approx_order, strategy, validate): | |||
def test_prob_expectation_values(self, approx_order, strategy, validate): | |||
"""Tests correct output shape and evaluation for a tape | |||
with prob and expval outputs""" | |||
np.random.seed(239) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Failed randomly while testing locally, so I add a seed.
Codecov ReportAll modified and coverable lines are covered by tests ✅
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 ☔ View full report in Codecov by Sentry. |
It was decided not to implement this change. Good to close @trbromley @co9olguy ? |
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]