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

change: adding check for main thread #27

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions src/braket/simulator_v2/base_simulator_v2.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import warnings
import threading
from collections.abc import Sequence
from typing import Any, Optional, Union

Expand Down Expand Up @@ -92,6 +93,7 @@ def run_jaqcd(
as a result type when shots=0. Or, if StateVector and Amplitude result types
are requested when shots>0.
"""
_validate_thread()
if qubit_count is not None:
warnings.warn(
f"qubit_count is deprecated for {type(self).__name__} and can be set to None"
Expand Down Expand Up @@ -155,6 +157,7 @@ def run_openqasm(
as a result type when shots=0. Or, if StateVector and Amplitude result types
are requested when shots>0.
"""
_validate_thread()
try:
r = jl.simulate(self._device, self._openqasm_to_jl(openqasm_ir), shots)
except JuliaError as e:
Expand Down Expand Up @@ -200,6 +203,7 @@ def run_multiple(
list[GateModelTaskResult]: A list of result objects, with the ith object being
the result of the ith program.
"""
_validate_thread()
try:
results = jl.simulate(
self._device,
Expand Down Expand Up @@ -245,6 +249,14 @@ def _validate_jaqcd(self, circuit_ir, qubit_count: int, shots: int):
)


def _validate_thread():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add some kind of test for this warning?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if threading.current_thread() is not threading.main_thread():
raise RuntimeError(
"Simulations must be run from the Main thread. "
"For multiple simulations, please use run_batch() instead."
)


def _result_value_to_ndarray(
task_result: GateModelTaskResult,
) -> GateModelTaskResult:
Expand Down
Loading