From 18d59d7620b1172b1ca10c8ab0192f2ca7c5db7c Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Fri, 15 Mar 2024 10:49:35 +0400 Subject: [PATCH] move function from `quantum_info.random_ensembles` to `backends.__init__` --- src/qibo/backends/__init__.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/qibo/backends/__init__.py b/src/qibo/backends/__init__.py index a80c9932e1..0927c3ad97 100644 --- a/src/qibo/backends/__init__.py +++ b/src/qibo/backends/__init__.py @@ -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 @@ -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