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

VQE Runtime ignores any parameters of optimizer other than SPSA/QNSPSA #407

Closed
LenaPer opened this issue Jul 4, 2022 · 10 comments · Fixed by Qiskit/qiskit#8381
Closed
Labels
bug Something isn't working priority: high High priority issue (must have for current release)

Comments

@LenaPer
Copy link

LenaPer commented Jul 4, 2022

Describe the bug
When running a vqe runtime through QiskitRuntimeService(channel='ibm_quantum') with an optimizer other than SPSA/QNSPSA, and looking at the logs (job.logs()), I noticed the optimizer is set to be SLSQP, even when I pass any other optimizer, it shows on the logs :
2022-07-04T09:37:35.046460730Z -- optimizer: <qiskit.algorithms.optimizers.slsqp.SLSQP object at 0x7fed33b72d60>
And later in the logs :

2022-07-04T09:37:35.046639714Z ===============================================================
2022-07-04T09:37:35.046645335Z Optimizer: SLSQP
2022-07-04T09:37:35.046650890Z -- method: slsqp
2022-07-04T09:37:35.046656375Z -- bounds_support_level: 2
2022-07-04T09:37:35.046661230Z -- gradient_support_level: 2
2022-07-04T09:37:35.046665764Z -- initial_point_support_level: 3
2022-07-04T09:37:35.046672463Z -- options: {'maxiter': 100, 'disp': False, 'ftol': 1e-06, 'eps': 1.4901161193847656e-08}
2022-07-04T09:37:35.046677546Z -- max_evals_grouped: 1
2022-07-04T09:37:35.046683473Z -- kwargs: {'tol': None}
2022-07-04T09:37:35.046689198Z ===============================================================

One of the job ids about this : cb1fncea4p74378e70v0

Steps to reproduce
The code to reproduce the error :

import qiskit
from qiskit.opflow import Z
from qiskit.circuit.library import TwoLocal
from qiskit_ibm_runtime import QiskitRuntimeService
from qiskit.algorithms.optimizers import COBYLA

service = QiskitRuntimeService(channel='ibm_quantum')

n = 2
operator = Z^Z
ansatz = TwoLocal(n, 'ry', 'cx', 'linear', reps=1)

runtime_inputs_cobyla = {
    'ansatz': ansatz,
    'operator': operator, 
    'optimizer': COBYLA(maxiter=1), 
}

options = {
    'backend_name': 'ibmq_qasm_simulator'
}

job_cobyla = service.run(
    program_id='vqe',
    options=options,
    inputs=runtime_inputs_cobyla,
)

Expected behavior
The right optimizer shows in the logs

Suggested solutions

Additional Information

  • qiskit-ibm-runtime version: 0.5.0
  • Python version: 3.9.12
  • qiskit version: 0.37
@LenaPer LenaPer added the bug Something isn't working label Jul 4, 2022
@rathishcholarajan
Copy link
Member

@Cryoris Could you please help check?

@LenaPer
Copy link
Author

LenaPer commented Jul 7, 2022

Weirdly enough without changing anything on my environment I notice now the optimiser is not ignored anymore, but I'm still seeing that when using any optimizer other than SPSA/QNSPSA, any parameters I pass to the optimizer gets ignored and it uses the default ones (looking at this via job.logs())

@LenaPer LenaPer changed the title VQE Runtime ignores any optimizer other than SPSA/QNSPSA VQE Runtime ignores any parameters of optimizer other than SPSA/QNSPSA Jul 7, 2022
@Cryoris
Copy link
Contributor

Cryoris commented Jul 8, 2022

Yeah we updated the VQE program and alongside fixed that the optimizers are ignored 🙂 Ok the lost settings are certainly a bug though, not quite sure where this originates from since the optimizers have a settings attribute which the runtime JSON encoder and decoders should use to reconstruct the object...

@LenaPer
Copy link
Author

LenaPer commented Jul 8, 2022

As an example, when I pass COBYLA(maxiter=1), it shows in the logs it still has the default parameters

2022-07-08T14:06:50.003629122Z Optimizer: COBYLA
2022-07-08T14:06:50.003629122Z -- method: cobyla
2022-07-08T14:06:50.003629122Z -- bounds_support_level: 1
2022-07-08T14:06:50.003629122Z -- gradient_support_level: 1
2022-07-08T14:06:50.003629122Z -- initial_point_support_level: 3
2022-07-08T14:06:50.003629122Z -- options: {'disp': False, 'maxiter': 1000, 'rhobeg': 1.0}
2022-07-08T14:06:50.003631508Z -- max_evals_grouped: 1
2022-07-08T14:06:50.003636219Z -- kwargs: {'tol': None}

@LenaPer
Copy link
Author

LenaPer commented Jul 13, 2022

Any updates on this?

@Cryoris
Copy link
Contributor

Cryoris commented Jul 13, 2022

Not yet -- I'll try having a look this week 😄

@Cryoris
Copy link
Contributor

Cryoris commented Jul 14, 2022

This issue seems to come from the encoding and decoding the optimizer which reset the settings. For example, the following serialization and deserialization of COBYLA

import json
from qiskit.algorithms.optimizers import COBYLA
from qiskit.providers.ibmq.runtime import RuntimeEncoder, RuntimeDecoder

obj = COBYLA(maxiter=1)
print("Original:\n", obj.settings)
dumped = json.dumps(obj, cls=RuntimeEncoder)
loaded = json.loads(dumped, cls=RuntimeDecoder)
print("Loaded:\n", loaded.settings)

prints

Original:
 {'max_evals_grouped': 1, 'options': {'maxiter': 1, 'disp': False, 'rhobeg': 1.0}, 'tol': None}
Loaded:
 {'max_evals_grouped': 1, 'options': {'maxiter': 1000, 'disp': False, 'rhobeg': 1.0}, 'tol': None}

It seems this comes from the problem that SciPy-based optimizers cannot be reconstructed from their own settings

>>> COBYLA(**obj.settings).settings
{'max_evals_grouped': 1, 'options': {'maxiter': 1000, 'disp': False, 'rhobeg': 1.0}, 'tol': None}

which is an assumption we have with the runtime encoder and decoders.

@Cryoris
Copy link
Contributor

Cryoris commented Jul 14, 2022

Ok something really funky is going on (keep track of maxiter)

>>> from qiskit.algorithms.optimizers import COBYLA
>>> one_iter_optimizer = COBYLA(maxiter=1)
>>> one_iter_optimizer.settings
{'max_evals_grouped': 1, 'options': {'maxiter': 1, 'disp': False, 'rhobeg': 1.0}, 'tol': None}
>>> COBYLA(**one_iter_optimizer.settings)
<qiskit.algorithms.optimizers.cobyla.COBYLA object at 0x7f82100709d0>
>>> one_iter_optimizer.settings
{'max_evals_grouped': 1, 'options': {'maxiter': 1000, 'disp': False, 'rhobeg': 1.0}, 'tol': None}

@Cryoris
Copy link
Contributor

Cryoris commented Jul 14, 2022

Qiskit/qiskit/pull/8343 (plus backport and update of the runtime images) should fix this 🙂

@rathishcholarajan rathishcholarajan added the priority: high High priority issue (must have for current release) label Jul 20, 2022
@kt474
Copy link
Member

kt474 commented Feb 14, 2023

This issue has been resolved

@kt474 kt474 closed this as completed Feb 14, 2023
blakejohnson pushed a commit to blakejohnson/qiskit-ibm-runtime that referenced this issue May 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working priority: high High priority issue (must have for current release)
Projects
None yet
4 participants