From 8fa0472e15ea36341acd6de67efa54a9815354e8 Mon Sep 17 00:00:00 2001 From: Kevin Tian Date: Fri, 17 Nov 2023 10:38:21 -0500 Subject: [PATCH] Update session.from_id() (#1163) * update session.from_id() * add deprecation warning * add reno --- qiskit_ibm_runtime/session.py | 33 +++++++++++++++---- .../from-id-params-659a2adb727b9d18.yaml | 6 ++++ test/integration/test_session.py | 17 +++++----- 3 files changed, 42 insertions(+), 14 deletions(-) create mode 100644 releasenotes/notes/from-id-params-659a2adb727b9d18.yaml diff --git a/qiskit_ibm_runtime/session.py b/qiskit_ibm_runtime/session.py index b5f322639..b48c6e2af 100644 --- a/qiskit_ibm_runtime/session.py +++ b/qiskit_ibm_runtime/session.py @@ -23,6 +23,8 @@ from .runtime_program import ParameterNamespace from .program.result_decoder import ResultDecoder from .ibm_backend import IBMBackend +from .exceptions import IBMInputValueError +from .utils.deprecation import deprecate_arguments from .utils.default_session import set_cm_session @@ -282,25 +284,44 @@ def service(self) -> QiskitRuntimeService: def from_id( cls, session_id: str, - service: Optional[QiskitRuntimeService] = None, + service: QiskitRuntimeService, backend: Optional[Union[str, IBMBackend]] = None, ) -> "Session": """Construct a Session object with a given session_id Args: - session_id: the id of the session to be created. This can be an already + session_id: the id of the session to be created. This must be an already existing session id. service: instance of the ``QiskitRuntimeService`` class. backend: instance of :class:`qiskit_ibm_runtime.IBMBackend` class or string name of backend. + Raises: + IBMInputValueError: If given `session_id` does not exist. or the backend passed in does + not match the original session backend. + Returns: A new Session with the given ``session_id`` - """ - session = cls(service, backend) - session._session_id = session_id - return session + + if backend: + deprecate_arguments("backend", "0.15.0", "Sessions do not support multiple backends.") + if isinstance(backend, IBMBackend): + backend = backend.name + + 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." + ) + session = cls(service, session_backend) + session._session_id = session_id + return session + + raise IBMInputValueError(f"The session_id {session_id} does not exist.") def __enter__(self) -> "Session": set_cm_session(self) diff --git a/releasenotes/notes/from-id-params-659a2adb727b9d18.yaml b/releasenotes/notes/from-id-params-659a2adb727b9d18.yaml new file mode 100644 index 000000000..b7f556697 --- /dev/null +++ b/releasenotes/notes/from-id-params-659a2adb727b9d18.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - | + The ``backend`` parameter in :meth:`~qiskit_ibm_runtime.Session.from_id` is being deprecated because + sessions do not support multiple backends. Additionally, the `service` parameter is no longer optional. + diff --git a/test/integration/test_session.py b/test/integration/test_session.py index b8c086a59..963033de3 100644 --- a/test/integration/test_session.py +++ b/test/integration/test_session.py @@ -21,6 +21,7 @@ from qiskit.result import Result from qiskit_ibm_runtime import Estimator, Session, Sampler, Options +from qiskit_ibm_runtime.exceptions import IBMInputValueError from ..decorators import run_integration_test, quantum_only from ..ibm_test_case import IBMIntegrationTestCase @@ -90,16 +91,16 @@ def test_using_correct_instance(self, service): @run_integration_test def test_session_from_id(self, service): - """Test creating a session from a given id""" + """Test creating a session with from_id with simulator.""" backend = service.backend("ibmq_qasm_simulator") - with Session(service, backend=backend) as session: - sampler = Sampler(session=session) - job = sampler.run(ReferenceCircuits.bell(), shots=400) - session_id = job.session_id - new_session = Session.from_id(backend=backend, session_id=session_id) - sampler = Sampler(session=new_session) + session = Session(service=service, backend=backend) + + sampler = Sampler(session=session) job = sampler.run(ReferenceCircuits.bell(), shots=400) - self.assertEqual(session_id, job.session_id) + session_id = job.session_id + + with self.assertRaises(IBMInputValueError): + _ = Session.from_id(session_id=session_id, service=service) class TestBackendRunInSession(IBMIntegrationTestCase):