Skip to content

Commit

Permalink
change: adding check for main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
krneta committed Jul 8, 2024
1 parent fe23e87 commit 84d5fd2
Showing 1 changed file with 12 additions and 0 deletions.
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():
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

0 comments on commit 84d5fd2

Please sign in to comment.