def test_set_backend(backend): """Check ``set_backend`` for switching gate backends.""" original_backend = backends.get_backend() backends.set_backend(backend) assert backends.get_backend() == backend target_name = "numpy" if "numpy" in backend else "tensorflow" assert K.name == target_name assert str(K) == target_name assert K.__repr__() == "{}Backend".format(target_name.capitalize()) if backend == "custom": assert K.custom_gates assert K.custom_einsum is None from qibo.core import cgates as custom_gates assert isinstance(gates.H(0), custom_gates.BackendGate) else: assert not K.custom_gates if "defaulteinsum" in backend: assert K.custom_einsum == "DefaultEinsum" else: assert K.custom_einsum == "MatmulEinsum" from qibo.core import gates as native_gates h = gates.H(0) assert isinstance(h, native_gates.BackendGate) backends.set_backend(original_backend)
def test_set_backend_with_platform(backend_name): """Check ``set_backend`` with ``platform`` argument.""" original_backend = backends.get_backend() original_platform = K.get_platform() backends.set_backend(backend_name, platform="test") current_platform = K.get_platform() backends.set_backend(original_backend, platform=original_platform)
def test_set_threads(backend, caplog): original_threads = backends.get_threads() bkname = backends.get_backend() backends.set_threads(1) if bkname == "numpy" or bkname == "tensorflow": assert "WARNING" in caplog.text assert backends.get_threads() == 1 backends.set_threads(original_threads)
def test_set_backend_errors(caplog): original_backend = backends.get_backend() with pytest.raises(ValueError): backends.set_backend("test") if original_backend != "numpy": h = gates.H(0) backends.set_backend("numpy") assert "WARNING" in caplog.text backends.set_backend(original_backend)
def test_set_precision_errors(backend): original_backend = backends.get_backend() original_precision = backends.get_precision() backends.set_backend(backend) gate = gates.H(0) with pytest.warns(RuntimeWarning): backends.set_precision("single") with pytest.raises(ValueError): backends.set_precision("test") backends.set_precision(original_precision) backends.set_backend(original_backend)
def test_set_backend_errors(): original_backend = backends.get_backend() with pytest.raises(ValueError): backends.set_backend("test") with pytest.raises(ValueError): backends.set_backend("numpy_custom") with pytest.raises(ValueError): backends.set_backend("numpy_badgates") h = gates.H(0) with pytest.warns(RuntimeWarning): backends.set_backend("numpy_defaulteinsum") backends.set_backend(original_backend)
def test_set_backend(backend_name): """Check ``set_backend`` for switching gate backends.""" original_backend = backends.get_backend() backends.set_backend(backend_name) assert K.name == backend_name assert str(K) == backend_name assert repr(K) == backend_name assert K.executing_eagerly() h = gates.H(0) if backend_name == "qibotf" or backend_name == "qibojit": assert h.gate_op else: assert h.gate_op is None backends.set_backend(original_backend)
def test_set_backend(backend_name): """Check ``set_backend`` for switching gate backends.""" original_backend = backends.get_backend() backends.set_backend(backend_name) assert K.name == backend_name if K.platform is None: assert str(K) == backend_name assert repr(K) == backend_name else: platform = K.get_platform() assert str(K) == f"{backend_name} ({platform})" assert repr(K) == f"{backend_name} ({platform})" assert K.executing_eagerly() h = gates.H(0) backends.set_backend(original_backend)
def test_set_precision(backend, precision): original_backend = backends.get_backend() original_precision = backends.get_precision() backends.set_backend(backend) backends.set_precision(precision) if precision == "single": expected_dtype = K.backend.complex64 else: expected_dtype = K.backend.complex128 assert backends.get_precision() == precision assert K.dtypes('DTYPECPX') == expected_dtype # Test that circuits use proper precision circuit = models.Circuit(2) circuit.add([gates.H(0), gates.H(1)]) final_state = circuit() assert final_state.dtype == expected_dtype backends.set_precision(original_precision) backends.set_backend(original_backend)
def test_set_precision_matrices(backend, precision): import numpy as np from qibo import matrices original_backend = backends.get_backend() original_precision = backends.get_precision() backends.set_backend(backend) backends.set_precision(precision) if precision == "single": assert matrices.dtype == np.complex64 assert matrices.H.dtype == np.complex64 assert K.matrices.dtype == K.backend.complex64 assert K.matrices.X.dtype == K.backend.complex64 else: assert matrices.dtype == np.complex128 assert matrices.H.dtype == np.complex128 assert K.matrices.dtype == K.backend.complex128 assert K.matrices.X.dtype == K.backend.complex128 backends.set_precision(original_precision) backends.set_backend(original_backend)
def test_set_device(backend): original_backend = backends.get_backend() backends.set_backend(backend) original_device = backends.get_device() if "numpy" in backend: with pytest.warns(RuntimeWarning): backends.set_device("/CPU:0") else: backends.set_device("/CPU:0") with pytest.raises(ValueError): backends.set_device("test") with pytest.raises(ValueError): backends.set_device("/TPU:0") with pytest.raises(ValueError): backends.set_device("/gpu:10") with pytest.raises(ValueError): backends.set_device("/GPU:10") backends.set_device(original_device) backends.set_backend(original_backend)
def test_set_device(backend, caplog): original_devices = { bk: bk.default_device for bk in K.constructed_backends.values() } if backends.get_backend() == "numpy": backends.set_device("/CPU:0") assert "WARNING" in caplog.text else: backends.set_device("/CPU:0") assert backends.get_device() == "/CPU:0" with pytest.raises(ValueError): backends.set_device("test") with pytest.raises(ValueError): backends.set_device("/TPU:0") with pytest.raises(ValueError): backends.set_device("/gpu:10") with pytest.raises(ValueError): backends.set_device("/GPU:10") for bk, device in original_devices.items(): bk.set_device(device)
def test_set_backend(backend): """Check ``set_backend`` for switching gate backends.""" original_backend = backends.get_backend() backends.set_backend(backend) if backend == "defaulteinsum": target_name = "tensorflow_defaulteinsum" elif backend == "matmuleinsum": target_name = "tensorflow_matmuleinsum" else: target_name = backend assert K.name == target_name assert str(K) == target_name assert repr(K) == target_name assert K.executing_eagerly() h = gates.H(0) if backend == "custom": assert K.custom_einsum is None assert h.gate_op else: assert h.gate_op is None backends.set_backend(original_backend)