Skip to content

Commit

Permalink
Added the Session.from_id method
Browse files Browse the repository at this point in the history
  • Loading branch information
merav-aharoni committed Sep 21, 2023
1 parent b7fef82 commit 9a97f10
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
13 changes: 13 additions & 0 deletions qiskit_ibm_runtime/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions test/unit/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 9a97f10

Please sign in to comment.