Skip to content
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

Update QNGOptimizer to handle deprecations #1834

Merged
merged 9 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions pennylane/optimize/qng.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# pylint: disable=too-many-branches
# pylint: disable=too-many-arguments

import numpy as np
from pennylane import numpy as np

import pennylane as qml
from pennylane.utils import _flatten, unflatten
Expand Down Expand Up @@ -192,13 +192,17 @@ def step_and_cost(

if recompute_tensor or self.metric_tensor is None:
if metric_tensor_fn is None:
metric_tensor_fn = qml.metric_tensor(qnode, diag_approx=self.diag_approx)

if self.diag_approx:
metric_tensor_fn = qml.metric_tensor(qnode, approx="diag")
else:
metric_tensor_fn = qml.metric_tensor(qnode)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@antalszava do you think we should update the QNG optimizer to match the change in the metric tensor?

That is,

class QNGOptimizer:

    def __init__(self, stepsize=0.01, approx="diag", lam=0):

?

I had forgotten the QNG optimizer also used this old terminology! I think it makes sense to match the metric tensor here, that way we can simply have

Suggested change
if self.diag_approx:
metric_tensor_fn = qml.metric_tensor(qnode, approx="diag")
else:
metric_tensor_fn = qml.metric_tensor(qnode)
metric_tensor_fn = qml.metric_tensor(qnode, approx=self.approx)


self.metric_tensor = metric_tensor_fn(*args, **kwargs)
self.metric_tensor += self.lam * np.identity(self.metric_tensor.shape[0])

g, forward = self.compute_grad(qnode, args, kwargs, grad_fn=grad_fn)
new_args = self.apply_grad(g, args)
new_args = np.array(self.apply_grad(g, args))
antalszava marked this conversation as resolved.
Show resolved Hide resolved

if forward is None:
forward = qnode(*args, **kwargs)
Expand Down
5 changes: 3 additions & 2 deletions tests/optimize/test_qng.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def gradient(params):
# check final cost
assert np.allclose(cost_fn(theta), -1.41421356, atol=tol, rtol=0)

def test_single_qubit_vqe_using_vqecost(self, tol):
def test_single_qubit_vqe_using_vqecost(self, tol, recwarn):
"""Test single-qubit VQE using ExpvalCost
has the correct QNG value every step, the correct parameter updates,
and correct cost after 200 steps"""
Expand All @@ -252,7 +252,7 @@ def gradient(params):
return np.array([da, db])

eta = 0.01
init_params = np.array([0.011, 0.012])
init_params = np.array([0.011, 0.012], requires_grad=True)
num_steps = 200

opt = qml.QNGOptimizer(eta)
Expand All @@ -275,3 +275,4 @@ def gradient(params):

# check final cost
assert np.allclose(cost_fn(theta), -1.41421356, atol=tol, rtol=0)
assert len(recwarn) == 0