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

New method to create a new Session object with a given id #1101

Merged
merged 7 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)