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

[BUG] SPSAOptimizer.step_and_cost ignores keyword arguments #6028

Closed
1 task done
isaacdevlugt opened this issue Jul 22, 2024 · 1 comment
Closed
1 task done

[BUG] SPSAOptimizer.step_and_cost ignores keyword arguments #6028

isaacdevlugt opened this issue Jul 22, 2024 · 1 comment
Labels
bug 🐛 Something isn't working

Comments

@isaacdevlugt
Copy link
Contributor

isaacdevlugt commented Jul 22, 2024

Expected behavior

Keyword arguments don't get ignored

Actual behavior

They get ignored.

Additional information

sc-69429

Source code

coeffs = [0.2, -0.543, 0.4514]
obs = [
    qml.PauliX(0) @ qml.PauliZ(1),
    qml.PauliZ(0) @ qml.Hadamard(2),
    qml.PauliX(3) @ qml.PauliZ(1),
]
H = qml.Hamiltonian(coeffs, obs)
num_qubits = 4
dev = qml.device("lightning.qubit", wires=num_qubits)

@qml.qnode(dev)
def cost_fun(params, num_qubits=1):
    print(num_qubits)
    qml.BasisState([1, 1, 0, 0], wires=range(num_qubits))
    for i in range(num_qubits):
        qml.CNOT(wires=[2, 3])
        qml.CNOT(wires=[2, 0])
        qml.CNOT(wires=[3, 1])
    return qml.expval(H)


init_params = np.random.normal(0, np.pi, (num_qubits,), requires_grad=True)
params = init_params

init_energy = cost_fun(init_params, num_qubits)

max_iterations = 100
opt = qml.SPSAOptimizer(maxiter=max_iterations)

params, energy = opt.step_and_cost(cost_fun, params, num_qubits=num_qubits)

Tracebacks

4
4
4
1 # this should be 4
4

System information

Name: PennyLane
Version: 0.37.0
Summary: PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
Home-page: https://github.com/PennyLaneAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /Users/isaac/.virtualenvs/pennylane-stable/lib/python3.11/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, packaging, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: PennyLane_Lightning

Platform info:           macOS-14.5-arm64-arm-64bit
Python version:          3.11.8
Numpy version:           1.26.4
Scipy version:           1.12.0
Installed devices:
- default.clifford (PennyLane-0.38.0.dev0)
- default.gaussian (PennyLane-0.38.0.dev0)
- default.mixed (PennyLane-0.38.0.dev0)
- default.qubit (PennyLane-0.38.0.dev0)
- default.qubit.autograd (PennyLane-0.38.0.dev0)
- default.qubit.jax (PennyLane-0.38.0.dev0)
- default.qubit.legacy (PennyLane-0.38.0.dev0)
- default.qubit.tf (PennyLane-0.38.0.dev0)
- default.qubit.torch (PennyLane-0.38.0.dev0)
- default.qutrit (PennyLane-0.38.0.dev0)
- default.qutrit.mixed (PennyLane-0.38.0.dev0)
- default.tensor (PennyLane-0.38.0.dev0)
- null.qubit (PennyLane-0.38.0.dev0)
- lightning.qubit (PennyLane_Lightning-0.37.0)

Existing GitHub issues

  • I have searched existing GitHub issues to make sure the issue does not already exist.
@isaacdevlugt isaacdevlugt added the bug 🐛 Something isn't working label Jul 22, 2024
isaacdevlugt added a commit that referenced this issue Jul 23, 2024
**Context:** `SPSAOptimizer`'s `step_and_cost` method was ignoring
keyword arguments in the objective function.

**Description of the Change:** `SPSAOptimizer.step_and_cost` no longer
ignores kwargs.

**Benefits:** Your keyword arguments are heard <3 

**Possible Drawbacks:** None

**Related github issue:**
#6028
@isaacdevlugt
Copy link
Contributor Author

Fixed by #6027

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant