Skip to content

Commit

Permalink
move function from quantum_info.random_ensembles to `backends.__ini…
Browse files Browse the repository at this point in the history
…t__`
  • Loading branch information
renatomello committed Mar 15, 2024
1 parent b70dcdb commit 18d59d7
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/qibo/backends/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

from qibo.backends.abstract import Backend
import numpy as np

from qibo.backends.clifford import CliffordBackend
from qibo.backends.npmatrices import NumpyMatrices
from qibo.backends.numpy import NumpyBackend
Expand Down Expand Up @@ -200,3 +201,28 @@ def _check_backend(backend):
return GlobalBackend()

return backend

def _check_backend_and_local_state(seed, backend):
if (
seed is not None
and not isinstance(seed, int)
and not isinstance(seed, np.random.Generator)
):
raise_error(
TypeError, "seed must be either type int or numpy.random.Generator."
)

backend = _check_backend(backend)

if seed is None or isinstance(seed, int):
if backend.__class__.__name__ in [
"CupyBackend",
"CuQuantumBackend",
]: # pragma: no cover
local_state = backend.np.random.default_rng(seed)
else:
local_state = np.random.default_rng(seed)
else:
local_state = seed

return backend, local_state

0 comments on commit 18d59d7

Please sign in to comment.