Skip to content

Commit

Permalink
remove function
Browse files Browse the repository at this point in the history
  • Loading branch information
renatomello committed Mar 15, 2024
1 parent b4e8384 commit 1a577e1
Showing 1 changed file with 12 additions and 38 deletions.
50 changes: 12 additions & 38 deletions src/qibo/quantum_info/random_ensembles.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from scipy.stats import rv_continuous

from qibo import Circuit, gates
from qibo.backends import NumpyBackend, _check_backend
from qibo.backends import NumpyBackend, _check_backend_and_local_state
from qibo.config import MAX_ITERATIONS, PRECISION_TOL, raise_error
from qibo.quantum_info.basis import comp_basis_to_pauli
from qibo.quantum_info.superoperator_transformations import (
Expand Down Expand Up @@ -66,7 +66,7 @@ def uniform_sampling_U3(ngates: int, seed=None, backend=None):
elif ngates <= 0:
raise_error(ValueError, f"ngates must be non-negative, but it is {ngates}.")

backend, local_state = _set_backend_and_local_state(seed, backend)
backend, local_state = _check_backend_and_local_state(seed, backend)

sampler = _probability_distribution_sin(a=0, b=np.pi, seed=local_state)
phases = local_state.random((ngates, 3))
Expand Down Expand Up @@ -132,7 +132,7 @@ def random_gaussian_matrix(
if stddev is not None and stddev <= 0.0:
raise_error(ValueError, "stddev must be a positive float.")

backend, local_state = _set_backend_and_local_state(seed, backend)
backend, local_state = _check_backend_and_local_state(seed, backend)

dims = (dims, rank)

Expand Down Expand Up @@ -178,7 +178,7 @@ def random_hermitian(
if not isinstance(semidefinite, bool) or not isinstance(normalize, bool):
raise_error(TypeError, "semidefinite and normalize must be type bool.")

backend, local_state = _set_backend_and_local_state(seed, backend)
backend, local_state = _check_backend_and_local_state(seed, backend)

matrix = random_gaussian_matrix(dims, dims, seed=local_state, backend=backend)

Expand Down Expand Up @@ -225,7 +225,7 @@ def random_unitary(dims: int, measure: Optional[str] = None, seed=None, backend=
if measure != "haar":
raise_error(ValueError, f"measure {measure} not implemented.")

backend, local_state = _set_backend_and_local_state(seed, backend)
backend, local_state = _check_backend_and_local_state(seed, backend)

if measure == "haar":
unitary = random_gaussian_matrix(dims, dims, seed=local_state, backend=backend)
Expand Down Expand Up @@ -341,7 +341,7 @@ def random_quantum_channel(
NotImplementedError, f"order {order} not implemented for measure {measure}."
)

backend, local_state = _set_backend_and_local_state(seed, backend)
backend, local_state = _check_backend_and_local_state(seed, backend)

if measure == "bcsz":
super_op = _super_op_from_bcsz_measure(
Expand Down Expand Up @@ -433,7 +433,7 @@ def random_statevector(dims: int, seed=None, backend=None):
TypeError, "seed must be either type int or numpy.random.Generator."
)

backend, local_state = _set_backend_and_local_state(seed, backend)
backend, local_state = _check_backend_and_local_state(seed, backend)

state = local_state.standard_normal(dims).astype(complex)
state += 1.0j * local_state.standard_normal(dims)
Expand Down Expand Up @@ -538,7 +538,7 @@ def random_density_matrix(
elif normalize is True and basis is None:
raise_error(ValueError, "normalize cannot be True when basis=None.")

backend, local_state = _set_backend_and_local_state(seed, backend)
backend, local_state = _check_backend_and_local_state(seed, backend)

if metric == "hilbert-schmidt":
rank = None
Expand Down Expand Up @@ -627,7 +627,7 @@ def random_clifford(
f"return_circuit must be type bool, but it is type {type(return_circuit)}.",
)

backend, local_state = _set_backend_and_local_state(seed, backend)
backend, local_state = _check_backend_and_local_state(seed, backend)

hadamards, permutations = _sample_from_quantum_mallows_distribution(
nqubits, local_state=local_state
Expand Down Expand Up @@ -808,7 +808,7 @@ def random_pauli(
"subset argument must be a subset of strings in the set ['I', 'X', 'Y', 'Z'].",
)

backend, local_state = _set_backend_and_local_state(seed, backend)
backend, local_state = _check_backend_and_local_state(seed, backend)

complete_set = {"I": gates.I, "X": gates.X, "Y": gates.Y, "Z": gates.Z}

Expand Down Expand Up @@ -911,7 +911,7 @@ def random_pauli_hamiltonian(
"when normalize=True, gap is = 1, thus max_eigenvalue must be > 1.",
)

backend, local_state = _set_backend_and_local_state(seed, backend)
backend, local_state = _check_backend_and_local_state(seed, backend)

d = 2**nqubits

Expand Down Expand Up @@ -1015,7 +1015,7 @@ def random_stochastic_matrix(
if max_iterations <= 0.0:
raise_error(ValueError, "max_iterations must be a positive int.")

backend, local_state = _set_backend_and_local_state(seed, backend)
backend, local_state = _check_backend_and_local_state(seed, backend)

if precision_tol is None:
precision_tol = PRECISION_TOL
Expand Down Expand Up @@ -1203,29 +1203,3 @@ def _super_op_from_bcsz_measure(dims: int, rank: int, order: str, seed, backend)
super_op = operator @ super_op @ operator

return super_op


def _set_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 1a577e1

Please sign in to comment.