Skip to content

Commit 6fe21bb

Browse files
authored
Merge pull request #1238 from qiboteam/cuquantum_tests
Fix tests for `cupy` and `cuquantum` backends
2 parents f4ddfdd + 7e04b68 commit 6fe21bb

12 files changed

+232
-118
lines changed

src/qibo/backends/__init__.py

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22

3-
from qibo.backends.abstract import Backend
3+
import numpy as np
4+
45
from qibo.backends.clifford import CliffordBackend
56
from qibo.backends.npmatrices import NumpyMatrices
67
from qibo.backends.numpy import NumpyBackend
@@ -200,3 +201,29 @@ def _check_backend(backend):
200201
return GlobalBackend()
201202

202203
return backend
204+
205+
206+
def _check_backend_and_local_state(seed, backend):
207+
if (
208+
seed is not None
209+
and not isinstance(seed, int)
210+
and not isinstance(seed, np.random.Generator)
211+
):
212+
raise_error(
213+
TypeError, "seed must be either type int or numpy.random.Generator."
214+
)
215+
216+
backend = _check_backend(backend)
217+
218+
if seed is None or isinstance(seed, int):
219+
if backend.__class__.__name__ in [
220+
"CupyBackend",
221+
"CuQuantumBackend",
222+
]: # pragma: no cover
223+
local_state = backend.np.random.default_rng(seed)
224+
else:
225+
local_state = np.random.default_rng(seed)
226+
else:
227+
local_state = seed
228+
229+
return backend, local_state

src/qibo/backends/numpy.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -641,19 +641,23 @@ def aggregate_shots(self, shots):
641641
return self.cast(shots, dtype=shots[0].dtype)
642642

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

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

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

659663
def update_frequencies(self, frequencies, probabilities, nsamples):
@@ -677,6 +681,7 @@ def sample_frequencies(self, probabilities, nshots):
677681
)
678682

679683
def apply_bitflips(self, noiseless_samples, bitflip_probabilities):
684+
noiseless_samples = self.cast(noiseless_samples, dtype=noiseless_samples.dtype)
680685
fprobs = self.cast(bitflip_probabilities, dtype="float64")
681686
sprobs = self.cast(np.random.random(noiseless_samples.shape), dtype="float64")
682687
flip_0 = self.cast(sprobs < fprobs[0], dtype=noiseless_samples.dtype)

0 commit comments

Comments
 (0)