Skip to content

Commit

Permalink
Merge pull request #36 from qiboteam/fixrocmtests
Browse files Browse the repository at this point in the history
Fix tests and methods with AMD ROCM
  • Loading branch information
scarrazza committed Nov 3, 2021
2 parents 1d38f92 + 33c7293 commit afb2797
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
13 changes: 13 additions & 0 deletions src/qibojit/custom_operators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,19 @@ def expm(self, x):
return self.backend.asarray(super().expm(x))
return super().expm(x)

def eigh(self, x):
if self.engine.name == "cupy" and self.engine.is_hip: # pragma: no cover
# FIXME: Fallback to numpy because eigh is not implemented in rocblas
result = self.np.linalg.eigh(self.to_numpy(x))
return self.cast(result[0]), self.cast(result[1])
return super().eigh(x)

def eigvalsh(self, x):
if self.engine.name == "cupy" and self.engine.is_hip: # pragma: no cover
# FIXME: Fallback to numpy because eigvalsh is not implemented in rocblas
return self.cast(self.np.linalg.eigvalsh(self.to_numpy(x)))
return super().eigvalsh(x)

def unique(self, x, return_counts=False):
if self.engine.name == "cupy": # pragma: no cover
if isinstance(x, self.native_types):
Expand Down
21 changes: 17 additions & 4 deletions src/qibojit/custom_operators/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,25 @@ def __init__(self):
self.np = np
self.cp = cp
base_dir = os.path.dirname(os.path.realpath(__file__))
is_hip = cupy_backends.cuda.api.runtime.is_hip
self.is_hip = cupy_backends.cuda.api.runtime.is_hip

if is_hip: # pragma: no cover
if self.is_hip: # pragma: no cover
self.kernel_double_suffix = f"_complex_double"
self.kernel_float_suffix = f"_complex_float"
self.test_regressions = {} # TODO: Fix this
self.test_regressions = {
"test_measurementresult_apply_bitflips": [
[2, 2, 6, 1, 0, 0, 0, 0, 1, 0],
[2, 2, 6, 1, 0, 0, 0, 0, 1, 0],
[0, 0, 4, 1, 0, 0, 0, 0, 1, 0],
[0, 2, 4, 0, 0, 0, 0, 0, 0, 0]
],
"test_probabilistic_measurement": {2: 267, 3: 247, 0: 243, 1: 243},
"test_unbalanced_probabilistic_measurement": {3: 500, 2: 174, 0: 163, 1: 163},
"test_post_measurement_bitflips_on_circuit": [
{5: 30}, {5: 17, 7: 7, 1: 2, 4: 2, 2: 1, 3: 1},
{7: 7, 1: 5, 3: 4, 6: 4, 2: 3, 5: 3, 0: 2, 4: 2}
]
}
else: # pragma: no cover
self.kernel_double_suffix = f"<complex<double>>"
self.kernel_float_suffix = f"<complex<float>>"
Expand Down Expand Up @@ -188,7 +201,7 @@ def __init__(self):
kernels.append(f"initial_state_kernel{self.kernel_double_suffix}")
kernels.append(f"initial_state_kernel{self.kernel_float_suffix}")
kernels = tuple(kernels)
gates_dir = os.path.join(base_dir, "gates.hip.cc" if is_hip else "gates.cu.cc")
gates_dir = os.path.join(base_dir, "gates.hip.cc" if self.is_hip else "gates.cu.cc")
with open(gates_dir, "r") as file:
code = r"{}".format(file.read())
self.gates = cp.RawModule(code=code, options=("--std=c++11",),
Expand Down
15 changes: 15 additions & 0 deletions src/qibojit/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ def test_basic_matrices(backend):
K.assert_allclose(K.expm(m), expm(m))


def test_backend_eigh(backend):
m = np.random.random((16, 16))
eigvals2, eigvecs2 = np.linalg.eigh(m)
eigvals1, eigvecs1 = K.eigh(K.cast(m))
K.assert_allclose(eigvals1, eigvals2)
K.assert_allclose(K.abs(eigvecs1), np.abs(eigvecs2))


def test_backend_eigvalsh(backend):
m = np.random.random((16, 16))
target = np.linalg.eigvalsh(m)
result = K.eigvalsh(K.cast(m))
K.assert_allclose(target, result)


def test_unique_and_gather(backend):
samples = np.random.randint(0, 2, size=(100,))
K.assert_allclose(K.unique(samples), np.unique(samples))
Expand Down

0 comments on commit afb2797

Please sign in to comment.