Skip to content

Commit

Permalink
New method to create a new Session object with a given id (#1101)
Browse files Browse the repository at this point in the history
* Added the Session.from_id method

* release notes

* Added integration test

---------

Co-authored-by: Kevin Tian <[email protected]>
  • Loading branch information
merav-aharoni and kt474 authored Sep 26, 2023
1 parent 03334e9 commit f9d37cd
Show file tree
Hide file tree
Showing 4 changed files with 42 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 @@ -211,6 +211,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
6 changes: 6 additions & 0 deletions releasenotes/notes/from_id-23fc85f3fbf01e0b.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
features:
- |
Added new method ``Session.from_id`` which creates a new session with a given id.
15 changes: 15 additions & 0 deletions test/integration/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,18 @@ def test_using_correct_instance(self, service):
job = sampler.run(ReferenceCircuits.bell(), shots=400)
self.assertEqual(instance, backend._instance)
self.assertEqual(instance, job.backend()._instance)

@run_integration_test
def test_session_from_id(self, service):
"""Test creating a session from a given id"""
instance = self.dependencies.instance
backend = service.backend("ibmq_qasm_simulator", instance=instance)
with Session(service, backend=backend) as session:
sampler = Sampler(session=session)
job = sampler.run(ReferenceCircuits.bell(), shots=400)
session_id = job.session_id
session.close()
new_session = Session.from_id(backend=backend, session_id=session_id)
sampler = Sampler(session=new_session)
job = sampler.run(ReferenceCircuits.bell(), shots=400)
self.assertEqual(session_id, job.session_id)
8 changes: 8 additions & 0 deletions test/unit/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,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 f9d37cd

Please sign in to comment.