Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacdevlugt committed Jul 22, 2024
1 parent 3ad15d7 commit 50ae330
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
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.

* `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 @@ def step_and_cost(self, objective_fn, *args, **kwargs):
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 @@ def compute_grad(self, objective_fn, args, kwargs):
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:
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):
"""Test SPSAOptimizer implementation with lightning.qubit device."""
coeffs = [0.2, -0.543, 0.4514]
obs = [
Expand Down

0 comments on commit 50ae330

Please sign in to comment.