From f83c9f03becb0f0db965e3a6fce263121ec82852 Mon Sep 17 00:00:00 2001 From: albi3ro Date: Wed, 1 Sep 2021 12:46:52 +0100 Subject: [PATCH 1/3] fix failing tests in clean env --- .github/CHANGELOG.md | 2 ++ .../circuit_graph/test_circuit_graph_hash.py | 1 + tests/collections/test_qnode_collection.py | 6 ++++- tests/conftest.py | 21 ++++++++++++++++ tests/kernels/test_kernels.py | 24 ++++++++++++++----- tests/tape/test_qnode.py | 1 + 6 files changed, 48 insertions(+), 7 deletions(-) diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index 1d62a188fc0..fdf08a79488 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -398,6 +398,8 @@ and requirements-ci.txt (unpinned). This latter would be used by the CI.

Bug fixes

+* Dask and CVXPY dependent tests are skipped if those packages are not installed. + * The `qml.layer` template now works with tensorflow variables. [(#1615)](https://github.com/PennyLaneAI/pennylane/pull/1615) diff --git a/tests/circuit_graph/test_circuit_graph_hash.py b/tests/circuit_graph/test_circuit_graph_hash.py index b5c34e4f1da..7883155b661 100644 --- a/tests/circuit_graph/test_circuit_graph_hash.py +++ b/tests/circuit_graph/test_circuit_graph_hash.py @@ -614,6 +614,7 @@ def circuit2(x, y): assert circuit_hash_1 != circuit_hash_2 + @pytest.mark.usefixtures("skip_if_no_dask_support") def test_compiled_program_was_stored(self): """Test that QVM device stores the compiled program correctly""" dev = qml.device("default.qubit", wires=3) diff --git a/tests/collections/test_qnode_collection.py b/tests/collections/test_qnode_collection.py index 03a353c70e7..1c27e574681 100644 --- a/tests/collections/test_qnode_collection.py +++ b/tests/collections/test_qnode_collection.py @@ -174,9 +174,13 @@ class TestEvalation: """Tests for the QNodeCollection evaluation""" @pytest.mark.parametrize("interface", ["autograd"]) - def test_eval_autograd(self, qnodes, parallel, interface): + def test_eval_autograd(self, qnodes, parallel, interface, dask_support): """Test correct evaluation of the QNodeCollection using the Autograd interface""" + + if (not dask_support) and (parallel): + pytest.skip("Skipped, no dask support") + qnode1, qnode2 = qnodes qc = qml.QNodeCollection([qnode1, qnode2]) params = [0.5643, -0.45] diff --git a/tests/conftest.py b/tests/conftest.py index 0771840a389..9129d195d9b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -110,6 +110,25 @@ def gaussian_device_4modes(): return DummyDevice(wires=4) +############### Package Support ########################## + +@pytest.fixture(scope="session") +def dask_support(): + """Boolean fixture for dask support""" + try: + import dask + + dask_support = True + except ImportError as e: + dask_support = False + + return dask_support + +@pytest.fixture() +def skip_if_no_dask_support(dask_support): + if not dask_support: + pytest.skip("Skipped, no dask support") + @pytest.fixture(scope="session") def torch_support(): """Boolean fixture for PyTorch support""" @@ -154,6 +173,8 @@ def skip_if_no_tf_support(tf_support): def skip_if_no_jax_support(): pytest.importorskip("jax") +####################################################################### + @pytest.fixture(scope="module", params=[1, 2, 3]) def seed(request): diff --git a/tests/kernels/test_kernels.py b/tests/kernels/test_kernels.py index 7724d83f29e..d1a1183c772 100644 --- a/tests/kernels/test_kernels.py +++ b/tests/kernels/test_kernels.py @@ -23,6 +23,22 @@ import sys +@pytest.fixture() +def cvxpy_support(): + try: + import cvxpy + + cvxpy_support = True + except: + cvxpy_support = False + + return cvxpy_support + +@pytest.fixture() +def skip_if_no_cvxpy_support(cvxpy_support): + if not cvxpy_support: + pytest.skip("Skipped, no cvxpy support") + @qml.template def _simple_ansatz(x, params): """A simple quantum circuit ansatz.""" @@ -361,18 +377,13 @@ def test_flip_matrix(self, input, expected_output): (np.array([[0, 1.000001], [1, 0]]), True, np.array([[1, 1], [1, 1]])), ], ) + @pytest.mark.usefixtures("skip_if_no_cvxpy_support") def test_closest_psd_matrix(self, input, fix_diagonal, expected_output): """Test obtaining the closest positive semi-definite matrix using a semi-definite program.""" try: import cvxpy as cp output = kern.closest_psd_matrix(input, fix_diagonal=fix_diagonal, feastol=1e-10) - except ModuleNotFoundError: - pytest.skip( - "cvxpy seems to not be installed on the system." - "It is required for qml.kernels.closest_psd_matrix" - " and can be installed via `pip install cvxpy`." - ) except cp.error.SolverError: pytest.skip( "The cvxopt solver seems to not be installed on the system." @@ -402,6 +413,7 @@ def test_closest_psd_matrix_import_error(self, input, mocker): (np.diag([1, -1]), "I am not a solver"), ], ) + @pytest.mark.usefixtures("skip_if_no_cvxpy_support") def test_closest_psd_matrix_solve_error(self, input, solver): """Test verbose error raising if problem.solve crashes.""" with pytest.raises(Exception) as solve_error: diff --git a/tests/tape/test_qnode.py b/tests/tape/test_qnode.py index d9fcea9c568..f6880be6c23 100644 --- a/tests/tape/test_qnode.py +++ b/tests/tape/test_qnode.py @@ -770,6 +770,7 @@ def func(x, y): assert func.qtape is not old_tape +@pytest.mark.usefixtures("skip_if_no_dask_support") class TestQNodeCollection: """Unittests for the QNodeCollection""" From 9c87d9ef16e3e8cc523343587dd987dfff603d4f Mon Sep 17 00:00:00 2001 From: Christina Lee Date: Wed, 1 Sep 2021 13:07:06 +0100 Subject: [PATCH 2/3] Update .github/CHANGELOG.md --- .github/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index fdf08a79488..6cdfea1c197 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -399,6 +399,7 @@ and requirements-ci.txt (unpinned). This latter would be used by the CI.

Bug fixes

* Dask and CVXPY dependent tests are skipped if those packages are not installed. +[(#1617)](https://github.com/PennyLaneAI/pennylane/pull/1617) * The `qml.layer` template now works with tensorflow variables. [(#1615)](https://github.com/PennyLaneAI/pennylane/pull/1615) From e165e7710c2dfc4afe6f7019be2ee44a4283414b Mon Sep 17 00:00:00 2001 From: albi3ro Date: Wed, 1 Sep 2021 13:10:05 +0100 Subject: [PATCH 3/3] black --- tests/collections/test_qnode_collection.py | 2 +- tests/conftest.py | 6 +++++- tests/kernels/test_kernels.py | 2 ++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/collections/test_qnode_collection.py b/tests/collections/test_qnode_collection.py index 1c27e574681..2f0f50d2836 100644 --- a/tests/collections/test_qnode_collection.py +++ b/tests/collections/test_qnode_collection.py @@ -177,7 +177,7 @@ class TestEvalation: def test_eval_autograd(self, qnodes, parallel, interface, dask_support): """Test correct evaluation of the QNodeCollection using the Autograd interface""" - + if (not dask_support) and (parallel): pytest.skip("Skipped, no dask support") diff --git a/tests/conftest.py b/tests/conftest.py index 9129d195d9b..b05124b800c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -112,23 +112,26 @@ def gaussian_device_4modes(): ############### Package Support ########################## + @pytest.fixture(scope="session") def dask_support(): """Boolean fixture for dask support""" try: import dask - + dask_support = True except ImportError as e: dask_support = False return dask_support + @pytest.fixture() def skip_if_no_dask_support(dask_support): if not dask_support: pytest.skip("Skipped, no dask support") + @pytest.fixture(scope="session") def torch_support(): """Boolean fixture for PyTorch support""" @@ -173,6 +176,7 @@ def skip_if_no_tf_support(tf_support): def skip_if_no_jax_support(): pytest.importorskip("jax") + ####################################################################### diff --git a/tests/kernels/test_kernels.py b/tests/kernels/test_kernels.py index d1a1183c772..34a5687fd92 100644 --- a/tests/kernels/test_kernels.py +++ b/tests/kernels/test_kernels.py @@ -34,11 +34,13 @@ def cvxpy_support(): return cvxpy_support + @pytest.fixture() def skip_if_no_cvxpy_support(cvxpy_support): if not cvxpy_support: pytest.skip("Skipped, no cvxpy support") + @qml.template def _simple_ansatz(x, params): """A simple quantum circuit ansatz."""