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

Improve test suite speed: pytest-xdist; qcut and qchem markers; update reconstruct tests #2504

Merged
merged 17 commits into from
Apr 29, 2022
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
107 changes: 97 additions & 10 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install -r requirements-ci.txt
pip install wheel pytest pytest-cov pytest-mock flaky --upgrade

- name: Conditionally install openfermionpyscf
if: contains(' 3.9', matrix.config.python-version)
run: pip3 install openfermionpyscf
pip install wheel pytest pytest-cov pytest-mock pytest-xdist flaky --upgrade

- name: Conditionally install PyTorch
if: contains(matrix.config.interfaces, 'torch')
Expand All @@ -65,9 +61,6 @@ jobs:
if: contains(matrix.config.interfaces, 'jax')
run: pip3 install jax jaxlib

- name: Install KaHyPar
run: pip3 install kahypar==1.1.7

- name: Install PennyLane
run: |
pip install -r requirements-ci.txt
Expand All @@ -78,7 +71,7 @@ jobs:
run: python -m pip install -i https://test.pypi.org/simple/ PennyLane-Lightning --pre --upgrade

- name: Run tests
run: python -m pytest tests --cov=pennylane $COVERAGE_FLAGS
run: python -m pytest tests --cov=pennylane $COVERAGE_FLAGS -m "not qcut and not qchem" -n auto
antalszava marked this conversation as resolved.
Show resolved Hide resolved

- name: Adjust coverage file for Codecov
run: bash <(sed -i 's/filename=\"/filename=\"pennylane\//g' coverage.xml)
Expand All @@ -88,6 +81,100 @@ jobs:
with:
file: ./coverage.xml

qcut:
runs-on: ubuntu-latest

steps:
- name: Cancel Previous Runs
uses: styfle/cancel-workflow-action@0.4.1
with:
access_token: ${{ github.token }}

- uses: actions/checkout@v2
with:
fetch-depth: 2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.8

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements-ci.txt
pip install wheel pytest pytest-cov pytest-mock pytest-xdist flaky --upgrade

- name: Conditionally install TensorFlow
run: pip3 install tensorflow==$TF_VERSION keras==$TF_VERSION
antalszava marked this conversation as resolved.
Show resolved Hide resolved

- name: Install KaHyPar
run: pip3 install kahypar==1.1.7

- name: Install PennyLane
run: |
pip install -r requirements-ci.txt
python setup.py bdist_wheel
pip install dist/PennyLane*.whl

- name: Run tests
run: |
python -m pytest tests --cov=pennylane $COVERAGE_FLAGS -m qcut -n auto

- name: Adjust coverage file for Codecov
run: bash <(sed -i 's/filename=\"/filename=\"pennylane\//g' coverage.xml)

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
file: ./coverage.xml

qchem:
runs-on: ubuntu-latest

steps:
- name: Cancel Previous Runs
uses: styfle/cancel-workflow-action@0.4.1
with:
access_token: ${{ github.token }}

- uses: actions/checkout@v2
with:
fetch-depth: 2

- name: Set up Python
uses: actions/setup-python@v2
with:
# Based on previous experience, PySCF installation might fail or take
# longer time with some python versions
python-version: 3.9
antalszava marked this conversation as resolved.
Show resolved Hide resolved

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements-ci.txt
pip install wheel pytest pytest-cov pytest-mock pytest-xdist flaky --upgrade

- name: Install openfermionpyscf
run: pip3 install openfermionpyscf

- name: Install PennyLane
run: |
pip install -r requirements-ci.txt
python setup.py bdist_wheel
pip install dist/PennyLane*.whl

- name: Run tests
run: |
python -m pytest tests --cov=pennylane $COVERAGE_FLAGS -m qchem -n auto

- name: Adjust coverage file for Codecov
run: bash <(sed -i 's/filename=\"/filename=\"pennylane\//g' coverage.xml)

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
file: ./coverage.xml
antalszava marked this conversation as resolved.
Show resolved Hide resolved

device-tests:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -153,4 +240,4 @@ jobs:
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
file: ./coverage.xml
file: ./coverage.xml
14 changes: 12 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
Pytest configuration file for PennyLane test suite.
"""
import os
import pathlib

import pytest
import numpy as np
import pytest

import pennylane as qml
from pennylane.devices import DefaultGaussian


# defaults
TOL = 1e-3
TF_TOL = 2e-2
Expand Down Expand Up @@ -203,3 +203,13 @@ def mock_device(monkeypatch):
def tear_down_hermitian():
yield None
qml.Hermitian._eigs = {}


def pytest_collection_modifyitems(config, items):
# python 3.4/3.5 compat: rootdir = pathlib.Path(str(config.rootdir))
rootdir = pathlib.Path(config.rootdir)
for item in items:
rel_path = pathlib.Path(item.fspath).relative_to(rootdir)
if "qchem" in rel_path.parts:
antalszava marked this conversation as resolved.
Show resolved Hide resolved
mark = getattr(pytest.mark, "qchem")
item.add_marker(mark)
4 changes: 3 additions & 1 deletion tests/devices/test_default_gaussian.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,9 @@ def test_sample_error_multi_wire(self, gaussian_device_2_wires):
with pytest.raises(ValueError, match="Only one mode can be measured in homodyne"):
sample = gaussian_device_2_wires.sample("P", [0, 1], [])

@pytest.mark.parametrize("observable", set(qml.ops.cv.obs) - set(["P", "X", "QuadOperator"]))
@pytest.mark.parametrize(
"observable", sorted(set(qml.ops.cv.obs) - set(["P", "X", "QuadOperator"]))
antalszava marked this conversation as resolved.
Show resolved Hide resolved
)
def test_sample_error_unsupported_observable(self, gaussian_device_2_wires, observable):
"""Test that the sample function raises an error if the given observable is not supported"""

Expand Down
6 changes: 3 additions & 3 deletions tests/fourier/test_reconstruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def circuit(x):
return circuit


def fun_close(fun1, fun2, zero=None, tol=1e-5, samples=100):
def fun_close(fun1, fun2, zero=None, tol=1e-5, samples=10):
antalszava marked this conversation as resolved.
Show resolved Hide resolved
X = np.linspace(-np.pi, np.pi, samples)
if zero is not None:
X = qml.math.convert_like(X, zero)
Expand Down Expand Up @@ -730,7 +730,7 @@ def qnode_2(X, y):


def qnode_3(X, Y):
for i in range(5):
for i in range(3):
antalszava marked this conversation as resolved.
Show resolved Hide resolved
qml.RX(X[i], wires=0)
qml.RY(Y[i], wires=0)
qml.RX(X[i], wires=0)
Expand Down Expand Up @@ -941,7 +941,7 @@ def test_differentiability_jax(
grad = jax.grad(rec)
assert np.isclose(grad(x0), exp_qnode_grad(*params)[inner_key])
assert np.isclose(grad(x0 + 0.1), exp_grad(x0 + 0.1))
assert fun_close(grad, exp_grad, 10)
assert fun_close(grad, exp_grad, samples=3)
antalszava marked this conversation as resolved.
Show resolved Hide resolved

@pytest.mark.parametrize(
"qnode, params, ids, nums_frequency, spectra, shifts, exp_calls",
Expand Down
2 changes: 2 additions & 0 deletions tests/pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
markers =
slow: marks tests as slow (deselect with '-m "not slow"')
gpu: marks tests run on a GPU (deselect with '-m "not gpu"')
qchem: marks tests for the QChem module (deselect with '-m "not qchem"')
qcut: marks tests for the QCut transform (deselect with '-m "not qcut"')
filterwarnings =
ignore::DeprecationWarning:autograd.numpy.numpy_wrapper
2 changes: 2 additions & 0 deletions tests/transforms/test_qcut.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
from pennylane.transforms import qcut
from pennylane.wires import Wires

pytestmark = pytest.mark.qcut

I, X, Y, Z = (
np.eye(2),
qml.PauliX.compute_matrix(),
Expand Down