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

Update from_id() #1311

Merged
merged 14 commits into from Apr 9, 2024
26 changes: 24 additions & 2 deletions qiskit_ibm_runtime/session.py
Expand Up @@ -20,6 +20,7 @@
from qiskit_ibm_provider.utils.converters import hms_to_seconds

from qiskit_ibm_runtime import QiskitRuntimeService
from .exceptions import IBMInputValueError
from .runtime_job import RuntimeJob
from .utils.result_decoder import ResultDecoder
from .ibm_backend import IBMBackend
Expand Down Expand Up @@ -301,15 +302,36 @@ def from_id(
session_id: the id of the session to be created. This must be an already
existing session id.
service: instance of the ``QiskitRuntimeService`` class.
kt474 marked this conversation as resolved.
Show resolved Hide resolved
backend: instance of :class:`qiskit_ibm_runtime.IBMBackend` class or
string name of backend.
backend (DEPRECATED): instance of :class:`qiskit_ibm_runtime.IBMBackend` class or
string name of backend. Either ``service`` or ``backend`` must be given.

Raises:
IBMInputValueError: If given `session_id` does not exist. or the backend passed in does
not match the original session backend.
kt474 marked this conversation as resolved.
Show resolved Hide resolved

Returns:
A new Session with the given ``session_id``

"""
if backend:
deprecate_arguments("backend", "0.15.0", "Sessions do not support multiple backends.")
kt474 marked this conversation as resolved.
Show resolved Hide resolved
if isinstance(backend, IBMBackend):
backend = backend.name

if service:
response = service._api_client.session_details(session_id)
if response:
session_backend = response.get("backend_name")
if backend and backend != session_backend:
raise IBMInputValueError(
f"The session_id {session_id} was created with backend {session_backend}, "
f"but backend {backend} was given."
)
kt474 marked this conversation as resolved.
Show resolved Hide resolved
backend = session_backend
else:
kt474 marked this conversation as resolved.
Show resolved Hide resolved
raise IBMInputValueError(f"The session_id {session_id} does not exist.")
if not service and not backend:
raise IBMInputValueError("Either service or backend must be given.")
kt474 marked this conversation as resolved.
Show resolved Hide resolved

session = cls(service, backend)
session._session_id = session_id
Expand Down