From 01bcd4f0a9d70638fc7a5415288f63c048fcc441 Mon Sep 17 00:00:00 2001 From: Alessandro Candido Date: Wed, 21 Aug 2024 18:24:45 +0200 Subject: [PATCH] test: Design a test to load a backend without Qibojit Since qibojit is always installed in the CI, we have to temporarily remove it manually, and restore after the test. --- tests/test_backends.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/tests/test_backends.py b/tests/test_backends.py index 1be00e6d22..251d874ef8 100644 --- a/tests/test_backends.py +++ b/tests/test_backends.py @@ -1,11 +1,16 @@ +import importlib import platform +import shutil import sys +from pathlib import Path +from tempfile import TemporaryDirectory import numpy as np import pytest +import qibojit from qibo import construct_backend, gates, list_available_backends, set_backend -from qibo.backends import MetaBackend +from qibo.backends import GlobalBackend, MetaBackend, get_backend ####################### Test `matrix` ####################### GATES = [ @@ -112,6 +117,28 @@ def test_construct_backend(backend): ) +@pytest.fixture +def uninstall_qibojit(): + p = Path(qibojit.__file__).parent + modules = list(sys.modules.keys()) + for mod in modules: + if mod.startswith("qibojit"): + del sys.modules[mod] + with TemporaryDirectory() as tdir: + shutil.move(p, tdir) + yield + shutil.move(Path(tdir) / p.name, p) + importlib.import_module("qibojit") + + +def test_default_backend(uninstall_qibojit): + """Reproducing https://github.com/qiboteam/qibo/issues/1424.""" + # reset global backend + GlobalBackend._instance = None + # attempt loading + default_backend = get_backend() + + def test_list_available_backends(): tensorflow = False if platform.system() == "Windows" else True qulacs = (