From 339857920f84ac2a17900b708a1173471e353698 Mon Sep 17 00:00:00 2001 From: Marco Lazzarin <48728634+mlazzarin@users.noreply.github.com> Date: Mon, 1 Nov 2021 08:46:56 +0100 Subject: [PATCH 1/4] Fix regression tests for AMD GPUs --- src/qibojit/custom_operators/backends.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/qibojit/custom_operators/backends.py b/src/qibojit/custom_operators/backends.py index 9b7d088e..53669ff9 100644 --- a/src/qibojit/custom_operators/backends.py +++ b/src/qibojit/custom_operators/backends.py @@ -165,7 +165,20 @@ def __init__(self): if 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">" self.kernel_float_suffix = f">" From 87f4341a662d711cecb2c77068d3f482a8614dc9 Mon Sep 17 00:00:00 2001 From: Marco Lazzarin <48728634+mlazzarin@users.noreply.github.com> Date: Mon, 1 Nov 2021 09:07:06 +0100 Subject: [PATCH 2/4] Rename is_hip to self.is_hip in CupyBackend __init__ method --- src/qibojit/custom_operators/backends.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/qibojit/custom_operators/backends.py b/src/qibojit/custom_operators/backends.py index 53669ff9..5db60a88 100644 --- a/src/qibojit/custom_operators/backends.py +++ b/src/qibojit/custom_operators/backends.py @@ -160,9 +160,9 @@ 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 = { @@ -209,7 +209,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",), From 0e776dd1976aaaaf7228ef317fab9ed69bb387f4 Mon Sep 17 00:00:00 2001 From: Marco Lazzarin <48728634+mlazzarin@users.noreply.github.com> Date: Mon, 1 Nov 2021 10:27:44 +0100 Subject: [PATCH 3/4] Fix rocblas_status_not_implemented issues by falling back to numpy --- src/qibojit/custom_operators/__init__.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/qibojit/custom_operators/__init__.py b/src/qibojit/custom_operators/__init__.py index 45e45962..b5463cd0 100644 --- a/src/qibojit/custom_operators/__init__.py +++ b/src/qibojit/custom_operators/__init__.py @@ -144,6 +144,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): From 33c729352c6ffad7355a05a5d2e5fada9de82ca8 Mon Sep 17 00:00:00 2001 From: Marco Lazzarin <48728634+mlazzarin@users.noreply.github.com> Date: Wed, 3 Nov 2021 17:15:07 +0400 Subject: [PATCH 4/4] Fix coverage --- src/qibojit/tests/test_backends.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/qibojit/tests/test_backends.py b/src/qibojit/tests/test_backends.py index e27f06d2..db6e0649 100644 --- a/src/qibojit/tests/test_backends.py +++ b/src/qibojit/tests/test_backends.py @@ -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))