Qiskit / qiskit-ibm-runtime

IBM Client for Qiskit Runtime
https://docs.quantum.ibm.com/api/qiskit-ibm-runtime
Apache License 2.0
149 stars 154 forks source link

Backend specified on job submitted as part of a session is not honored #627

Closed mriedem closed 1 year ago

mriedem commented 1 year ago

Describe the bug

I've got a script which tries to create 5 jobs in a session, it submits the jobs via QiskitRuntimeService.run and the options on the jobs specify the alt_algiers backend. However, the jobs are run on the alt_canberra backend.

from qiskit_ibm_runtime import QiskitRuntimeService

QiskitRuntimeService.save_account(
    channel="ibm_cloud", token=apikey, instance=crn,
    url="https://test.cloud.ibm.com")
service = QiskitRuntimeService()
program_inputs = {'iterations': 1}
# Tag the job for the MCS flow
options = {
    "backend": "alt_algiers",
    "job_tags": ["proxy_mcs", "use_runtime_workers", "sessions"]
}

print('submitting 5 jobs in a session')
with Session(service=service) as session:
    for i in range(5):
        print(f'{i+1} submitting session job')
        job = session.run(program_id="hello-world",
                          options=options,
                          inputs=program_inputs)
        print(f"submitted job: {job.job_id()}")
        print(f"job results: {job.result()}")
    print(f'all jobs run for session {session.session_id}')

The first job created is cdru53vj3bs1jpmpdmfg which ran on alt_canberra, not alt_algiers.

Checking the server logs this is because it looks like the job was sent to the runtime API without a backend specified even though it's in the options passed to run().

My guess this has something to do with the Session constructor in the context manager and how it doesn't have backend supplied, so maybe that's overwriting what's passed to submit the job to run()? From the docs:

https://qiskit.org/documentation/partners/qiskit_ibm_runtime/stubs/qiskit_ibm_runtime.Session.html#qiskit_ibm_runtime.Session

image

I guess that was a surprise to me, is it intentional that it'd overwrite the backend specified in options to the run() method?

Steps to reproduce

see the script above

Expected behavior

I would expect that if the options passed to run() have a backend specified but the Session doesn't, then the backend from options is used when submitting the job rather than Session.backend overwriting it when Session.backend is None.

If this is working as designed and my script is invalid, I'd expect an error when trying to submit the first job in the session because the usage is wrong - an explicit error would be easier to sort out from a user perspective rather than wonder why the backend I specified wasn't used.

Suggested solutions

See above.

Additional Information

jyu00 commented 1 year ago

@kt474 We should either make Session.run() a private method or deprecate passing backend to Session.run() since session doesn't support cross backend.