Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

relevant tests skip if cvxpy or dask is not installed #1617

Merged
merged 4 commits into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,9 @@ and requirements-ci.txt (unpinned). This latter would be used by the CI.

<h3>Bug fixes</h3>

* Dask and CVXPY dependent tests are skipped if those packages are not installed.
[(#1617)](https://github.com/PennyLaneAI/pennylane/pull/1617)

albi3ro marked this conversation as resolved.
Show resolved Hide resolved
* The `qml.layer` template now works with tensorflow variables.
[(#1615)](https://github.com/PennyLaneAI/pennylane/pull/1615)

Expand Down
1 change: 1 addition & 0 deletions tests/circuit_graph/test_circuit_graph_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion tests/collections/test_qnode_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
25 changes: 25 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,28 @@ def gaussian_device_4modes():
return DummyDevice(wires=4)


############### Package Support ##########################


@pytest.fixture(scope="session")
def dask_support():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great 💯

"""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"""
Expand Down Expand Up @@ -155,6 +177,9 @@ def skip_if_no_jax_support():
pytest.importorskip("jax")


#######################################################################


@pytest.fixture(scope="module", params=[1, 2, 3])
def seed(request):
"""Different seeds."""
Expand Down
26 changes: 20 additions & 6 deletions tests/kernels/test_kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@
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."""
Expand Down Expand Up @@ -361,18 +379,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."
Expand Down Expand Up @@ -402,6 +415,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:
Expand Down
1 change: 1 addition & 0 deletions tests/tape/test_qnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""

Expand Down