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

Fix tests for cupy and cuquantum backends #1238

Merged
merged 24 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
94c3f64
fix clifford
renatomello Feb 29, 2024
7ac7321
fix encodings
renatomello Feb 29, 2024
234ab65
rename file
renatomello Mar 1, 2024
2a79796
remove comment
renatomello Mar 1, 2024
002e603
Merge branch 'master' into cuquantum_tests
renatomello Mar 6, 2024
973b0f2
Merge branch 'master' into cuquantum_tests
renatomello Mar 6, 2024
7f331ad
Merge branch 'master' into cuquantum_tests
renatomello Mar 9, 2024
d1e22df
Merge branch 'master' into cuquantum_tests
renatomello Mar 14, 2024
73b677e
Merge branch 'master' into cuquantum_tests
renatomello Mar 14, 2024
42a0e74
Merge branch 'master' into cuquantum_tests
renatomello Mar 14, 2024
46b7b89
fix more tests
renatomello Mar 14, 2024
b70dcdb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 14, 2024
18d59d7
move function from `quantum_info.random_ensembles` to `backends.__ini…
renatomello Mar 15, 2024
b4e8384
introduce `seed` to `models.error_mitigation` and fix GPU tests
renatomello Mar 15, 2024
1a577e1
remove function
renatomello Mar 15, 2024
4632a6c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 15, 2024
033e968
fix more tests
renatomello Mar 16, 2024
fbc06c8
fix transpiler tests
renatomello Mar 16, 2024
9a744a7
update lock
renatomello Mar 16, 2024
7d85dd0
Merge branch 'master' into cuquantum_tests
renatomello Mar 18, 2024
56e506a
Merge branch 'master' into cuquantum_tests
renatomello Mar 20, 2024
d58825d
Merge branch 'master' into cuquantum_tests
renatomello Mar 23, 2024
8be0a0b
Merge branch 'master' into cuquantum_tests
renatomello Apr 27, 2024
7e04b68
Merge branch 'master' into cuquantum_tests
MatteoRobbiati May 8, 2024
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
29 changes: 28 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,29 @@ 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
17 changes: 11 additions & 6 deletions src/qibo/backends/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,19 +641,23 @@ def aggregate_shots(self, shots):
return self.cast(shots, dtype=shots[0].dtype)

def samples_to_binary(self, samples, nqubits):
qrange = self.np.arange(nqubits - 1, -1, -1, dtype=self.np.int32)
return self.np.mod(
self.np.right_shift(self.cast(samples[:, None], dtype="int32"), qrange), 2
)
### This is faster just staying @ NumPy.
qrange = np.arange(nqubits - 1, -1, -1, dtype=np.int32)
samples = self.to_numpy(samples)
return np.mod(np.right_shift(samples[:, None], qrange), 2)

def samples_to_decimal(self, samples, nqubits):
qrange = self.np.arange(nqubits - 1, -1, -1, dtype=self.np.int32)
### This is faster just staying @ NumPy.
qrange = np.arange(nqubits - 1, -1, -1, dtype=np.int32)
qrange = (2**qrange)[:, None]
return self.np.matmul(samples, qrange)[:, 0]
samples = np.asarray(samples.tolist())
return np.matmul(samples, qrange)[:, 0]

def calculate_frequencies(self, samples):
# Samples are a list of strings so there is no advantage in using other backends
res, counts = np.unique(samples, return_counts=True)
res = self.to_numpy(res).tolist()
counts = self.to_numpy(counts).tolist()
return collections.Counter(dict(zip(res, counts)))

def update_frequencies(self, frequencies, probabilities, nsamples):
Expand All @@ -677,6 +681,7 @@ def sample_frequencies(self, probabilities, nshots):
)

def apply_bitflips(self, noiseless_samples, bitflip_probabilities):
noiseless_samples = self.cast(noiseless_samples, dtype=noiseless_samples.dtype)
fprobs = self.cast(bitflip_probabilities, dtype="float64")
sprobs = self.cast(np.random.random(noiseless_samples.shape), dtype="float64")
flip_0 = self.cast(sprobs < fprobs[0], dtype=noiseless_samples.dtype)
Expand Down
Loading
Loading