From d2c6d32b820341dc8e07bcb34a80986c18158143 Mon Sep 17 00:00:00 2001 From: Antal Szava Date: Wed, 27 Apr 2022 15:21:44 -0400 Subject: [PATCH 01/13] Fewer samples and fewer layers --- tests/fourier/test_reconstruct.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/fourier/test_reconstruct.py b/tests/fourier/test_reconstruct.py index 024c35ca73d..568825f7886 100644 --- a/tests/fourier/test_reconstruct.py +++ b/tests/fourier/test_reconstruct.py @@ -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): X = np.linspace(-np.pi, np.pi, samples) if zero is not None: X = qml.math.convert_like(X, zero) @@ -730,7 +730,7 @@ def qnode_2(X, y): def qnode_3(X, Y): - for i in range(5): + for i in range(3): qml.RX(X[i], wires=0) qml.RY(Y[i], wires=0) qml.RX(X[i], wires=0) @@ -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) @pytest.mark.parametrize( "qnode, params, ids, nums_frequency, spectra, shifts, exp_calls", From 3b5a8fa37f0dc1f4a3f7293a36ce7cc76d8a0fca Mon Sep 17 00:00:00 2001 From: Antal Szava Date: Wed, 27 Apr 2022 17:17:34 -0400 Subject: [PATCH 02/13] kahypar markers; dedicated suite --- .github/workflows/tests.yml | 49 ++++++++++++++++++++++++++++++++--- tests/transforms/test_qcut.py | 2 ++ 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index c4055800462..24abe78d857 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -65,9 +65,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 @@ -88,6 +85,50 @@ jobs: with: file: ./coverage.xml + qcut-kahypar: + 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 flaky --upgrade + + - 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/ -m kahypar + + - 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 device-tests: runs-on: ubuntu-latest @@ -153,4 +194,4 @@ jobs: - name: Upload coverage to Codecov uses: codecov/codecov-action@v3 with: - file: ./coverage.xml \ No newline at end of file + file: ./coverage.xml diff --git a/tests/transforms/test_qcut.py b/tests/transforms/test_qcut.py index 82773aa82be..786fe34c8f8 100644 --- a/tests/transforms/test_qcut.py +++ b/tests/transforms/test_qcut.py @@ -4188,6 +4188,7 @@ def make_weakly_connected_tape( return tape +@pytest.mark.kahypar class TestKaHyPar: """Tests for the KaHyPar cutting function and utilities.""" @@ -4495,6 +4496,7 @@ def test_find_and_place_cuts(self, local_measurement, with_manual_cut, cut_strat ) +@pytest.mark.kahypar class TestAutoCutCircuit: """Integration tests for automatic-cutting-enabled `cut_circuit` transform. Mostly borrowing tests cases from ``TestCutCircuitTransform``. From daeb4e26c22ba0e6deb6711b908bbf35826c6144 Mon Sep 17 00:00:00 2001 From: Antal Szava Date: Wed, 27 Apr 2022 17:31:49 -0400 Subject: [PATCH 03/13] kahypar marker reg; try xdist and distributed test runs --- .github/workflows/tests.yml | 6 +++--- tests/pytest.ini | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 24abe78d857..0f76c10705b 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -45,7 +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 + pip install wheel pytest pytest-cov pytest-mock pytest-xdist flaky --upgrade - name: Conditionally install openfermionpyscf if: contains(' 3.9', matrix.config.python-version) @@ -75,7 +75,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 -n auto - name: Adjust coverage file for Codecov run: bash <(sed -i 's/filename=\"/filename=\"pennylane\//g' coverage.xml) @@ -120,7 +120,7 @@ jobs: - name: Run tests run: | - python -m pytest tests/ -m kahypar + python -m pytest tests/ -m kahypar -n auto - name: Adjust coverage file for Codecov run: bash <(sed -i 's/filename=\"/filename=\"pennylane\//g' coverage.xml) diff --git a/tests/pytest.ini b/tests/pytest.ini index 6bbdabf6df8..c230dda0b9f 100644 --- a/tests/pytest.ini +++ b/tests/pytest.ini @@ -1,6 +1,7 @@ [pytest] markers = slow: marks tests as slow (deselect with '-m "not slow"') + kahypar: marks tests from the QCut that use the KaHyPar package (deselect with '-m "not kahypar"') gpu: marks tests run on a GPU (deselect with '-m "not gpu"') filterwarnings = ignore::DeprecationWarning:autograd.numpy.numpy_wrapper From 97db67a2cdef1551be83a20ea9e5f3a45b4d2821 Mon Sep 17 00:00:00 2001 From: Antal Szava Date: Wed, 27 Apr 2022 17:34:05 -0400 Subject: [PATCH 04/13] xdist kahypar too --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 0f76c10705b..a3ff00d16c0 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -107,7 +107,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 + pip install wheel pytest pytest-cov pytest-mock pytest-xdist flaky --upgrade - name: Install KaHyPar run: pip3 install kahypar==1.1.7 From 8588b8200679f778cdcac55827a6dbc65460c0ab Mon Sep 17 00:00:00 2001 From: Antal Szava Date: Wed, 27 Apr 2022 17:38:13 -0400 Subject: [PATCH 05/13] sorted params --- tests/devices/test_default_gaussian.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/devices/test_default_gaussian.py b/tests/devices/test_default_gaussian.py index 0ca647afc25..c3c442e12ce 100644 --- a/tests/devices/test_default_gaussian.py +++ b/tests/devices/test_default_gaussian.py @@ -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"])) + ) 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""" From c1d6a22d5d6afbeb0b3d4f29c5fc72bd482b5438 Mon Sep 17 00:00:00 2001 From: Antal Szava Date: Wed, 27 Apr 2022 17:41:09 -0400 Subject: [PATCH 06/13] don't distribute kahypar (setup time cost is not worth it) --- .github/workflows/tests.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a3ff00d16c0..d7ed236fefb 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -107,7 +107,7 @@ jobs: 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 + pip install wheel pytest pytest-cov pytest-mock flaky --upgrade - name: Install KaHyPar run: pip3 install kahypar==1.1.7 @@ -120,7 +120,7 @@ jobs: - name: Run tests run: | - python -m pytest tests/ -m kahypar -n auto + python -m pytest tests/ -m kahypar - name: Adjust coverage file for Codecov run: bash <(sed -i 's/filename=\"/filename=\"pennylane\//g' coverage.xml) From 9318deb2f2a93cf9bd1ff00509dadfcbd879b77a Mon Sep 17 00:00:00 2001 From: Antal Szava Date: Thu, 28 Apr 2022 10:35:45 -0400 Subject: [PATCH 07/13] Revert "don't distribute kahypar (setup time cost is not worth it)" This reverts commit c1d6a22d5d6afbeb0b3d4f29c5fc72bd482b5438. --- .github/workflows/tests.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index d7ed236fefb..a3ff00d16c0 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -107,7 +107,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 + pip install wheel pytest pytest-cov pytest-mock pytest-xdist flaky --upgrade - name: Install KaHyPar run: pip3 install kahypar==1.1.7 @@ -120,7 +120,7 @@ jobs: - name: Run tests run: | - python -m pytest tests/ -m kahypar + python -m pytest tests/ -m kahypar -n auto - name: Adjust coverage file for Codecov run: bash <(sed -i 's/filename=\"/filename=\"pennylane\//g' coverage.xml) From 30877a1301421b04bec2198752289669588099a1 Mon Sep 17 00:00:00 2001 From: Antal Szava Date: Thu, 28 Apr 2022 10:36:46 -0400 Subject: [PATCH 08/13] coverage flags for kahypar --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a3ff00d16c0..78249af0c15 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -120,7 +120,7 @@ jobs: - name: Run tests run: | - python -m pytest tests/ -m kahypar -n auto + python -m pytest tests --cov=pennylane $COVERAGE_FLAGS -m kahypar -n auto - name: Adjust coverage file for Codecov run: bash <(sed -i 's/filename=\"/filename=\"pennylane\//g' coverage.xml) From d407340821573d979ed2481cb5a2eeb8b43cc734 Mon Sep 17 00:00:00 2001 From: Antal Szava Date: Thu, 28 Apr 2022 10:44:50 -0400 Subject: [PATCH 09/13] mark all qcut tests together (instead of kahypar separately) --- .github/workflows/tests.yml | 4 ++-- tests/pytest.ini | 2 +- tests/transforms/test_qcut.py | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 78249af0c15..0b33a72b437 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -85,7 +85,7 @@ jobs: with: file: ./coverage.xml - qcut-kahypar: + qcut: runs-on: ubuntu-latest steps: @@ -120,7 +120,7 @@ jobs: - name: Run tests run: | - python -m pytest tests --cov=pennylane $COVERAGE_FLAGS -m kahypar -n auto + 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) diff --git a/tests/pytest.ini b/tests/pytest.ini index c230dda0b9f..7c508819610 100644 --- a/tests/pytest.ini +++ b/tests/pytest.ini @@ -1,7 +1,7 @@ [pytest] markers = slow: marks tests as slow (deselect with '-m "not slow"') - kahypar: marks tests from the QCut that use the KaHyPar package (deselect with '-m "not kahypar"') + qcut: marks tests for the QCut transform (deselect with '-m "not qcut"') gpu: marks tests run on a GPU (deselect with '-m "not gpu"') filterwarnings = ignore::DeprecationWarning:autograd.numpy.numpy_wrapper diff --git a/tests/transforms/test_qcut.py b/tests/transforms/test_qcut.py index 786fe34c8f8..4f0b517bd75 100644 --- a/tests/transforms/test_qcut.py +++ b/tests/transforms/test_qcut.py @@ -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(), @@ -4188,7 +4190,6 @@ def make_weakly_connected_tape( return tape -@pytest.mark.kahypar class TestKaHyPar: """Tests for the KaHyPar cutting function and utilities.""" @@ -4496,7 +4497,6 @@ def test_find_and_place_cuts(self, local_measurement, with_manual_cut, cut_strat ) -@pytest.mark.kahypar class TestAutoCutCircuit: """Integration tests for automatic-cutting-enabled `cut_circuit` transform. Mostly borrowing tests cases from ``TestCutCircuitTransform``. From 846ff93da99304727a923e21196ee3f8a4f6dd76 Mon Sep 17 00:00:00 2001 From: Antal Szava Date: Thu, 28 Apr 2022 10:48:24 -0400 Subject: [PATCH 10/13] deselect qcut tests --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 0b33a72b437..0cb6ad72fc0 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -75,7 +75,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 -n auto + run: python -m pytest tests --cov=pennylane $COVERAGE_FLAGS -m "not qcut" -n auto - name: Adjust coverage file for Codecov run: bash <(sed -i 's/filename=\"/filename=\"pennylane\//g' coverage.xml) From 81bf9165e12e2513bb067b45f4b93112350763ca Mon Sep 17 00:00:00 2001 From: Antal Szava Date: Thu, 28 Apr 2022 14:50:32 -0400 Subject: [PATCH 11/13] Create a QChem test marker --- .github/workflows/tests.yml | 51 +++++++++++++++++++++++++++++++++---- tests/conftest.py | 14 ++++++++-- tests/pytest.ini | 3 ++- 3 files changed, 60 insertions(+), 8 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 0cb6ad72fc0..301e863dc5a 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -47,10 +47,6 @@ jobs: pip install -r requirements-ci.txt pip install wheel pytest pytest-cov pytest-mock pytest-xdist flaky --upgrade - - name: Conditionally install openfermionpyscf - if: contains(' 3.9', matrix.config.python-version) - run: pip3 install openfermionpyscf - - name: Conditionally install PyTorch if: contains(matrix.config.interfaces, 'torch') run: pip3 install torch==$TORCH_VERSION -f https://download.pytorch.org/whl/torch_stable.html @@ -75,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 -m "not qcut" -n auto + run: python -m pytest tests --cov=pennylane $COVERAGE_FLAGS -m "not qcut and not qchem" -n auto - name: Adjust coverage file for Codecov run: bash <(sed -i 's/filename=\"/filename=\"pennylane\//g' coverage.xml) @@ -130,6 +126,51 @@ jobs: 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: + python-version: 3.9 + + - 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 + device-tests: runs-on: ubuntu-latest diff --git a/tests/conftest.py b/tests/conftest.py index b05124b800c..5a6e850d8d5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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 @@ -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: + mark = getattr(pytest.mark, "qchem") + item.add_marker(mark) diff --git a/tests/pytest.ini b/tests/pytest.ini index 7c508819610..5fde771dfbb 100644 --- a/tests/pytest.ini +++ b/tests/pytest.ini @@ -1,7 +1,8 @@ [pytest] markers = slow: marks tests as slow (deselect with '-m "not slow"') - qcut: marks tests for the QCut transform (deselect with '-m "not qcut"') 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 From 8636f7f61ffbca3c7cda4479b14d1f565c511d72 Mon Sep 17 00:00:00 2001 From: Antal Szava Date: Thu, 28 Apr 2022 15:21:42 -0400 Subject: [PATCH 12/13] Install TensorFlow for Qcut --- .github/workflows/tests.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 301e863dc5a..b4cb4a92350 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -105,6 +105,9 @@ jobs: 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 + - name: Install KaHyPar run: pip3 install kahypar==1.1.7 From eecadf3446b088c45e345fb5a4553e42e0450b77 Mon Sep 17 00:00:00 2001 From: Antal Szava Date: Fri, 29 Apr 2022 11:07:41 -0400 Subject: [PATCH 13/13] comment on PySCF in tests.yml --- .github/workflows/tests.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index b4cb4a92350..f07fb7bd41c 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -145,6 +145,8 @@ jobs: - 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 - name: Install dependencies