Qiskit / qiskit-ibm-runtime

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

using old syntax gives cryptic error message #560

Closed kevinsung closed 2 years ago

kevinsung commented 2 years ago

Describe the bug Updating to 8bc659df1fecf2091908bde76a54fe95707aa1f1 broke my code. The error message says something about needing a backend even though it was explicitly passed. It should be improved.

Steps to reproduce

from qiskit import QuantumCircuit
from qiskit.primitives import Sampler as TerraSampler
from qiskit_ibm_runtime import Sampler as RuntimeSampler

bell = QuantumCircuit(2)
bell.h(0)
bell.cx(0, 1)
bell.measure_all()

result = RuntimeSampler().run([bell], shots=None, backend="ibmq_qasm_simulator").result()
print("Runtime result:")
print(result.quasi_dists)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In [1], line 10
      7 bell.cx(0, 1)
      8 bell.measure_all()
---> 10 result = RuntimeSampler().run([bell], shots=None, backend="ibmq_qasm_simulator").result()
     11 print("Runtime result:")
     12 print(result.quasi_dists)

File ~/projects/qiskit-ibm-runtime/qiskit_ibm_runtime/sampler.py:162, in Sampler.__init__(self, circuits, parameters, service, session, options, skip_transpilation)
    160 else:
    161     backend = session or backend
--> 162     self._session = get_default_session(service, backend)
    164 self._first_run = True
    165 self._circuits_map = {}

File ~/projects/qiskit-ibm-runtime/qiskit_ibm_runtime/session.py:255, in get_default_session(service, backend)
    249 if service is None:
    250     service = (
    251         backend.service
    252         if isinstance(backend, IBMBackend)
    253         else QiskitRuntimeService()
    254     )
--> 255 session = Session(service=service, backend=backend)
    256 if not _IN_SESSION_CM:
    257     _DEFAULT_SESSION = session

File ~/projects/qiskit-ibm-runtime/qiskit_ibm_runtime/session.py:93, in Session.__init__(self, service, backend, max_time)
     90 self._service = service or QiskitRuntimeService()
     92 if self._service.channel == "ibm_quantum" and not backend:
---> 93     raise ValueError('"backend" is required for ``ibm_quantum`` channel.')
     94 if isinstance(backend, IBMBackend):
     95     backend = backend.name

ValueError: "backend" is required for ``ibm_quantum`` channel.

Expected behavior Error message should be more clear.

Suggested solutions I may be wrong, but it's possible the Sampler should not be instantiated with a Session. If so, the error message should say that.

Additional Information

kevinsung commented 2 years ago

Never mind, I was confused. It's the call to RuntimeSampler which needs the backend.