-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make sure backend.run and Sampler.run can run in a single session (#1203
) * Added test for backend.run and Sampler.run in a single session * Issue warning when Primitive is run within a backend session * Fixed comment * black * Added warning for when a backend is run in a primitive session and vice versa. * Fixed imports from default_session * minor wording change --------- Co-authored-by: Kevin Tian <kevin.tian@ibm.com>
- Loading branch information
1 parent
030c54e
commit 6cc8197
Showing
8 changed files
with
78 additions
and
24 deletions.
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,34 @@ | ||
# This code is part of Qiskit. | ||
# | ||
# (C) Copyright IBM 2023. | ||
# | ||
# 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. | ||
|
||
"""Methods for checking if we are inside a Session context manager""" | ||
|
||
from contextvars import ContextVar | ||
from typing import Optional, TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from .session import Session | ||
|
||
# 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
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