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

[sc-69429] SPSA keyword argument bug fix #6027

Merged
merged 8 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@

<h3>Bug fixes 🐛</h3>

* Fixed a bug in `qml.SPSAOptimizer` that ignored keyword arguments in the objective function.
isaacdevlugt marked this conversation as resolved.
Show resolved Hide resolved

* `CircuitGraph` can now handle circuits with the same operation instance occuring multiple times.
[(#5907)](https://github.com/PennyLaneAI/pennylane/pull/5907)

Expand Down
4 changes: 3 additions & 1 deletion pennylane/optimize/spsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@
objective function output prior to the step.
"""
g = self.compute_grad(objective_fn, args, kwargs)

new_args = self.apply_grad(g, args)

self.k += 1
Expand Down Expand Up @@ -270,7 +271,8 @@
shots = Shots(objective_fn.device._raw_shot_sequence) # pragma: no cover
else:
shots = Shots(None)
if np.prod(objective_fn.func(*args).shape(objective_fn.device, shots)) > 1:

Check notice on line 274 in pennylane/optimize/spsa.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/optimize/spsa.py#L274

Trailing whitespace (trailing-whitespace)
if np.prod(objective_fn.func(*args, **kwargs).shape(objective_fn.device, shots)) > 1:
isaacdevlugt marked this conversation as resolved.
Show resolved Hide resolved
raise ValueError(
"The objective function must be a scalar function for the gradient "
"to be computed."
Expand Down
4 changes: 2 additions & 2 deletions tests/optimize/test_spsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ def cost(params):

@pytest.mark.usefixtures("use_legacy_opmath")
@pytest.mark.slow
def test_lighting_device_legacy_opmath(self):
def test_lightning_device_legacy_opmath(self):
"""Test SPSAOptimizer implementation with lightning.qubit device."""
coeffs = [0.2, -0.543, 0.4514]
obs = [
Expand Down Expand Up @@ -479,7 +479,7 @@ def cost_fun(params, num_qubits=1):
assert energy < init_energy

@pytest.mark.slow
def test_lighting_device(self):
def test_lightning_device(self):
isaacdevlugt marked this conversation as resolved.
Show resolved Hide resolved
"""Test SPSAOptimizer implementation with lightning.qubit device."""
coeffs = [0.2, -0.543, 0.4514]
obs = [
Expand Down
Loading