From 9a97f101e885c0836ee1040847d0610d21f674b2 Mon Sep 17 00:00:00 2001 From: merav-aharoni Date: Thu, 21 Sep 2023 15:17:28 +0000 Subject: [PATCH] Added the Session.from_id method --- qiskit_ibm_runtime/session.py | 13 +++++++++++++ test/unit/test_session.py | 8 ++++++++ 2 files changed, 21 insertions(+) diff --git a/qiskit_ibm_runtime/session.py b/qiskit_ibm_runtime/session.py index ac54809ed..19edf2b73 100644 --- a/qiskit_ibm_runtime/session.py +++ b/qiskit_ibm_runtime/session.py @@ -230,6 +230,19 @@ def service(self) -> QiskitRuntimeService: """ return self._service + @classmethod + def from_id( + cls, + session_id: str, + service: Optional[QiskitRuntimeService] = None, + backend: Optional[Union[str, IBMBackend]] = None, + max_time: Optional[Union[int, str]] = None, + ) -> "Session": + """Construct a Session object with a given session_id""" + session = cls(service, backend, max_time) + session._session_id = session_id + return session + def __enter__(self) -> "Session": set_cm_session(self) return self diff --git a/test/unit/test_session.py b/test/unit/test_session.py index 446caacc4..17af86aa6 100644 --- a/test/unit/test_session.py +++ b/test/unit/test_session.py @@ -163,3 +163,11 @@ def test_global_service(self): service=FakeRuntimeService(channel="ibm_quantum", token="uvw"), backend="ibm_gotham" ) as session: self.assertEqual(session._service._account.token, "uvw") + + def test_session_from_id(self): + """Create session with given session_id""" + service = MagicMock() + session_id = "123" + session = Session.from_id(session_id=session_id, service=service) + session.run(program_id="foo", inputs={}) + self.assertEqual(session.session_id, session_id)