This repository has been archived by the owner on Jul 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
Initial support for session #675
Closed
Closed
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
5b423b0
Initial support for session
merav-aharoni cb02578
Merge branch 'main' into support_session
merav-aharoni cc1f28f
Added test for session. Cleaned up some of the unnecessary code
merav-aharoni c9ca298
Added max_execution_time parameter when calling program_run
merav-aharoni f8890bf
Added hms_to_seconds
merav-aharoni 59de266
Placeholder for session close
merav-aharoni 6135081
Merge branch 'main' into support_session
merav-aharoni a715bf1
Updated example in documentation. Removed Session._circuits_map.
merav-aharoni 766c36e
Merge branch 'support_session' of github.com:merav-aharoni/qiskit-ibm…
merav-aharoni 79607f8
New test file for session in unit test
merav-aharoni 9107e8f
More unit tests
merav-aharoni 5a4e9f0
Added more tests, unit and integration
merav-aharoni 0dc9756
Changed tests to use FakeProvider
merav-aharoni 91c6f0a
Added support to closing a session
merav-aharoni ee7f100
lint and black
merav-aharoni d9ab78a
Adding runtime_session.py
merav-aharoni f6c864a
Moved tests from unit to integration
merav-aharoni 43b2bd3
lint
merav-aharoni 366c9f0
Updated documentation
merav-aharoni bd74605
Added test
merav-aharoni 92efec9
Merge branch 'main' into support_session
merav-aharoni 9f97104
Added Session to __init__
merav-aharoni 577e3f9
Fixed tab of documentation
merav-aharoni bf19d18
Added newline
merav-aharoni 320e9ca
Merge branch 'main' into support_session
merav-aharoni 5fc999f
Added test for session as a parameter
merav-aharoni a2fe052
Merge branch 'support_session' of github.com:merav-aharoni/qiskit-ibm…
merav-aharoni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# This code is part of Qiskit. | ||
# | ||
# (C) Copyright IBM 2022. | ||
# | ||
# This code is licensed under the Apache License, Version 2.0. You may | ||
# obtain a copy of this license in the LICENSE.txt file in the root directory | ||
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
# | ||
# Any modifications or derivative works of this code must retain this | ||
# copyright notice, and modified files need to carry a notice indicating | ||
# that they have been altered from the originals. | ||
|
||
"""Runtime Session REST adapter.""" | ||
|
||
|
||
from qiskit_ibm_provider.api.rest.base import RestAdapterBase | ||
from ..session import RetrySession | ||
|
||
|
||
class RuntimeSession(RestAdapterBase): | ||
"""Rest adapter for session related endpoints.""" | ||
|
||
URL_MAP = { | ||
"close": "/close", | ||
} | ||
|
||
def __init__( | ||
self, session: RetrySession, session_id: str, url_prefix: str = "" | ||
) -> None: | ||
"""Job constructor. | ||
|
||
Args: | ||
session: RetrySession to be used in the adapter. | ||
session_id: Job ID of the first job in a runtime session. | ||
url_prefix: Prefix to use in the URL. | ||
""" | ||
super().__init__(session, "{}/sessions/{}".format(url_prefix, session_id)) | ||
|
||
def close(self) -> None: | ||
"""Close this session.""" | ||
url = self.get_url("close") | ||
self.session.delete(url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
# This code is part of Qiskit. | ||
# | ||
# (C) Copyright IBM 2022. | ||
# | ||
# This code is licensed under the Apache License, Version 2.0. You may | ||
# obtain a copy of this license in the LICENSE.txt file in the root directory | ||
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
# | ||
# Any modifications or derivative works of this code must retain this | ||
# copyright notice, and modified files need to carry a notice indicating | ||
# that they have been altered from the originals. | ||
|
||
"""Qiskit Runtime flexible session.""" | ||
|
||
from typing import Optional, Type, Union | ||
from types import TracebackType | ||
from contextvars import ContextVar | ||
|
||
from qiskit_ibm_provider.utils.converters import hms_to_seconds | ||
|
||
|
||
class Session: | ||
"""Class for creating a flexible Qiskit Runtime session. | ||
|
||
A Qiskit Runtime ``session`` allows you to group a collection of iterative calls to | ||
the quantum computer. A session is started when the first job within the session | ||
is started. Subsequent jobs within the session are prioritized by the scheduler. | ||
Data used within a session, such as transpiled circuits, is also cached to avoid | ||
unnecessary overhead. | ||
|
||
You can open a Qiskit Runtime session using this ``Session`` class | ||
and submit one or more jobs. | ||
|
||
For example:: | ||
|
||
from qiskit.test.reference_circuits import ReferenceCircuits | ||
from qiskit_ibm_provider import IBMProvider | ||
from qiskit_ibm_provider.session import Session | ||
|
||
circ = ReferenceCircuits.bell() | ||
backend = "ibmq_qasm_simulator" | ||
with Session(backend_name=backend) as session: | ||
provider = IBMProvider(session=session) | ||
job = provider.get_backend(name=backend).run(circ) | ||
print(f"Job ID: {job.job_id()}") | ||
print(f"Result: {job.result()}") | ||
# Close the session only if all jobs are finished and | ||
# you don't need to run more in the session. | ||
provider.close_session() | ||
|
||
""" | ||
|
||
def __init__( | ||
self, | ||
backend_name: Optional[str] = None, | ||
max_time: Optional[Union[int, str]] = None, | ||
): | ||
"""Session constructor. | ||
|
||
Args: | ||
backend_name: string name of backend. | ||
If not specified, a backend will be selected automatically | ||
by the IBMProvider(IBM Cloud channel only). | ||
|
||
max_time: (EXPERIMENTAL setting, can break between releases without warning) | ||
Maximum amount of time, a runtime session can be open before being | ||
forcibly closed. Can be specified as seconds (int) or a string like "2h 30m 40s". | ||
This value must be in between 300 seconds and the | ||
`system imposed maximum | ||
<https://qiskit.org/documentation/partners/qiskit_ibm_runtime/faqs/max_execution_time.html>`_. | ||
|
||
Raises: | ||
ValueError: If an input value is invalid. | ||
""" | ||
self._instance = None | ||
self._backend = backend_name | ||
self._session_id: Optional[str] = None | ||
self._active = True | ||
|
||
self._max_time = ( | ||
max_time | ||
if max_time is None or isinstance(max_time, int) | ||
else hms_to_seconds(max_time, "Invalid max_time value: ") | ||
) | ||
|
||
def backend(self) -> Optional[str]: | ||
"""Return backend for this session. | ||
|
||
Returns: | ||
Backend for this session. None if unknown. | ||
""" | ||
return self._backend | ||
|
||
@property | ||
def session_id(self) -> str: | ||
"""Return the session ID. | ||
|
||
Returns: | ||
Session ID. None until a job runs in the session. | ||
""" | ||
return self._session_id | ||
|
||
@property | ||
def active(self) -> bool: | ||
"""Return the status of the session. | ||
|
||
Returns: | ||
True if the session is active, False otherwise. | ||
""" | ||
return self._active | ||
|
||
def close(self) -> None: | ||
"""Set the session._active status to False""" | ||
self._active = False | ||
|
||
def __enter__(self) -> "Session": | ||
set_cm_session(self) | ||
return self | ||
|
||
def __exit__( | ||
self, | ||
exc_type: Optional[Type[BaseException]], | ||
exc_val: Optional[BaseException], | ||
exc_tb: Optional[TracebackType], | ||
) -> None: | ||
set_cm_session(None) | ||
|
||
|
||
# Default session | ||
_DEFAULT_SESSION: ContextVar[Optional[Session]] = ContextVar( | ||
"_DEFAULT_SESSION", default=None | ||
) | ||
_IN_SESSION_CM: ContextVar[bool] = ContextVar("_IN_SESSION_CM", default=False) | ||
|
||
|
||
def set_cm_session(session: Optional[Session]) -> None: | ||
"""Set the context manager session.""" | ||
_DEFAULT_SESSION.set(session) | ||
_IN_SESSION_CM.set(session is not None) | ||
|
||
|
||
def get_cm_session() -> Session: | ||
"""Return the context managed session.""" | ||
return _DEFAULT_SESSION.get() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be consistent with runtime, we should probably use
backend
here instead ofbackend_name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could, but I wanted to stress that here it is only a string and not an actual backend. In runtime, it can be either.