Skip to content

Commit

Permalink
Mark tests slower than 1.5 seconds (#1633)
Browse files Browse the repository at this point in the history
* mark tests slower than 1.5 seconds

* changelog

* black

* Update .github/CHANGELOG.md

Co-authored-by: Romain <rmoyard@gmail.com>

Co-authored-by: Romain <rmoyard@gmail.com>
  • Loading branch information
albi3ro and rmoyard committed Sep 9, 2021
1 parent 02c264d commit e702624
Show file tree
Hide file tree
Showing 17 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@

<h3>Improvements</h3>

* The slowest tests, more than 1.5 seconds, now have the pytest mark `slow`, and can be
selected or deselected during local execution of tests.
[(#1633)](https://github.com/PennyLaneAI/pennylane/pull/1633)

* Hamiltonians are now natively supported on the `default.qubit` device if `shots=None`.
This makes VQE workflows a lot faster in some cases.
[(#1551)](https://github.com/PennyLaneAI/pennylane/pull/1551)
Expand Down
1 change: 1 addition & 0 deletions tests/circuit_drawer/test_circuit_drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,7 @@ def test_wide_cv_circuit(self, wide_cv_qnode, drawn_wide_cv_qnode):

assert output == drawn_wide_cv_qnode

@pytest.mark.slow
def test_cv_circuit_with_values(
self, parameterized_cv_qnode, drawn_parameterized_cv_qnode_with_values
):
Expand Down
1 change: 1 addition & 0 deletions tests/circuit_graph/test_qasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,7 @@ def qnode():
assert res == expected


@pytest.mark.slow
class TestQASMConformanceTests:
"""Conformance tests to ensure that the CircuitGraph
serialized QASM conforms to the QASM standard as implemented
Expand Down
1 change: 1 addition & 0 deletions tests/gradients/test_finite_difference.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ def cost_fn(x):
expected = np.array([-np.cos(x) * np.cos(y) / 2, np.sin(x) * np.sin(y) / 2])
assert np.allclose(res, expected, atol=tol, rtol=0)

@pytest.mark.slow
def test_tf(self, approx_order, strategy, tol):
"""Tests that the output of the finite-difference transform
can be differentiated using TF, yielding second derivatives."""
Expand Down
3 changes: 3 additions & 0 deletions tests/gradients/test_parameter_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,7 @@ def cost_fn(x):
)
assert np.allclose(res, expected, atol=tol, rtol=0)

@pytest.mark.slow
def test_tf(self, tol):
"""Tests that the output of the finite-difference transform
can be differentiated using TF, yielding second derivatives."""
Expand Down Expand Up @@ -1130,6 +1131,7 @@ def test_autograd(self, tol):
assert np.allclose(res[1][:, 2:5], np.zeros([2, 3, 3]), atol=tol, rtol=0)
assert np.allclose(res[2][:, -1], np.zeros([2, 1, 1]), atol=tol, rtol=0)

@pytest.mark.slow
def test_tf(self, tol):
"""Test gradient of multiple trainable Hamiltonian coefficients
using tf"""
Expand Down Expand Up @@ -1176,6 +1178,7 @@ def test_torch(self, tol):
assert np.allclose(hess[1][:, 2:5], np.zeros([2, 3, 3]), atol=tol, rtol=0)
assert np.allclose(hess[2][:, -1], np.zeros([2, 1, 1]), atol=tol, rtol=0)

@pytest.mark.slow
def test_jax(self, tol):
"""Test gradient of multiple trainable Hamiltonian coefficients
using JAX"""
Expand Down
2 changes: 2 additions & 0 deletions tests/gradients/test_vjp.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ def test_torch(self, tol):
exp = qml.jacobian(lambda x: expected(x)[0])(params.detach().numpy())
assert np.allclose(params.grad, exp, atol=tol, rtol=0)

@pytest.mark.slow
def test_tf(self, tol):
"""Tests that the output of the VJP transform
can be differentiated using TF."""
Expand All @@ -282,6 +283,7 @@ def test_tf(self, tol):
res = t.jacobian(vjp, params)
assert np.allclose(res, qml.jacobian(expected)(params.numpy()), atol=tol, rtol=0)

@pytest.mark.slow
def test_jax(self, tol):
"""Tests that the output of the VJP transform
can be differentiated using JAX."""
Expand Down
1 change: 1 addition & 0 deletions tests/interfaces/test_batch_tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,7 @@ def test_sampling(self, execute_kwargs):
class TestHigherOrderDerivatives:
"""Test that the TensorFlow execute function can be differentiated"""

@pytest.mark.slow
@pytest.mark.parametrize(
"params",
[
Expand Down
2 changes: 2 additions & 0 deletions tests/interfaces/test_tape_tf.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ def test_jacobian_options(self, mocker, tol):
assert args[1]["order"] == 2
assert args[1]["h"] == 1e-8

@pytest.mark.slow
def test_reusing_quantum_tape(self, tol):
"""Test re-using a quantum tape by passing new parameters"""
a = tf.Variable(0.1, dtype=tf.float64)
Expand Down Expand Up @@ -518,6 +519,7 @@ def test_jacobian(self, mocker, tol):

spy.assert_not_called()

@pytest.mark.slow
def test_reusing_quantum_tape(self, mocker, tol):
"""Test re-using a quantum tape by passing new parameters"""
spy = mocker.spy(JacobianTape, "jacobian")
Expand Down
2 changes: 2 additions & 0 deletions tests/math/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,7 @@ def test_numpy(self, t):
"""Vanilla NumPy arrays, sequences, and lists will always return False"""
assert not fn.requires_grad(t)

@pytest.mark.slow
def test_jax(self):
"""JAX DeviceArrays differentiability depends on the argnums argument"""
res = None
Expand Down Expand Up @@ -1368,6 +1369,7 @@ def circuit(weights):
expected = self.expected_grad(weights)
assert np.allclose(grad, expected, atol=tol, rtol=0)

@pytest.mark.slow
def test_jax(self, tol):
"""Test that the covariance matrix computes the correct
result, and is differentiable, using the JAX interface"""
Expand Down
1 change: 1 addition & 0 deletions tests/optimize/test_shot_adaptive.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ def circuit(x):
assert np.allclose(circuit(params), -1, atol=0.1, rtol=0.2)
assert opt.shots_used > min_shots

@pytest.mark.slow
def test_vqe_optimization(self):
"""Test that a simple VQE circuit can be optimized"""
dev = qml.device("default.qubit", wires=2, shots=100)
Expand Down
3 changes: 3 additions & 0 deletions tests/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
markers =
slow: marks tests as slow (deselect with '-m "not slow"')
1 change: 1 addition & 0 deletions tests/qnn/test_keras.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ def c_default(w1, w2, w3, w4, w5, w6, w7, inputs=None):
assert layer_out.shape == (batch_size, output_dim)
assert np.allclose(layer_out[0], c(x[0], *weights))

@pytest.mark.slow
@pytest.mark.parametrize("n_qubits, output_dim", indices_up_to(2))
@pytest.mark.parametrize("batch_size", [2, 4, 6])
@pytest.mark.parametrize("middle_dim", [2, 5, 8])
Expand Down
1 change: 1 addition & 0 deletions tests/templates/test_subroutines/test_double_excitation.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ def test_autograd(self):
# without error
grad_fn(weight)

@pytest.mark.slow
def test_jax(self):
"""Tests the jax interface."""

Expand Down
1 change: 1 addition & 0 deletions tests/templates/test_subroutines/test_ucssd.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ def test_autograd(self, tol):

assert np.allclose(grads, grads2, atol=tol, rtol=0)

@pytest.mark.slow
def test_jax(self, tol):
"""Tests the jax interface."""

Expand Down
1 change: 1 addition & 0 deletions tests/test_about.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import pennylane as qml


@pytest.mark.slow
def test_about():
"""
about: Tests if the about string prints correct.
Expand Down
5 changes: 5 additions & 0 deletions tests/test_vqe.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ def test_passing_kwargs(self, coeffs, observables, expected):
assert qnode.diff_options["h"] == 123
assert qnode.diff_options["order"] == 2

@pytest.mark.slow
@pytest.mark.parametrize("interface", ["tf", "torch", "autograd"])
def test_optimize(self, interface, tf_support, torch_support):
"""Test that an ExpvalCost with observable optimization gives the same result as another
Expand Down Expand Up @@ -519,6 +520,7 @@ def test_grad_zero_hamiltonian(self, opt):
dc = qml.grad(cost)(w)
assert np.allclose(dc, 0)

@pytest.mark.slow
def test_optimize_grad_torch(self, torch_support):
"""Test that the gradient of ExpvalCost is accessible and correct when using observable
optimization and the Torch interface."""
Expand All @@ -544,6 +546,7 @@ def test_optimize_grad_torch(self, torch_support):

assert np.allclose(dc, big_hamiltonian_grad)

@pytest.mark.slow
def test_optimize_grad_tf(self, tf_support):
"""Test that the gradient of ExpvalCost is accessible and correct when using observable
optimization and the TensorFlow interface."""
Expand Down Expand Up @@ -827,6 +830,7 @@ def circuit(w):
dc = qml.grad(circuit)(w)
assert np.allclose(dc, 0, atol=tol)

@pytest.mark.slow
def test_grad_torch(self, torch_support, tol):
"""Tests VQE gradients in the torch interface."""
if not torch_support:
Expand Down Expand Up @@ -870,6 +874,7 @@ def circuit(w):

assert np.allclose(dc, big_hamiltonian_grad, atol=tol)

@pytest.mark.slow
def test_grad_jax(self, tol):
"""Tests VQE gradients in the jax interface."""
jax = pytest.importorskip("jax")
Expand Down
3 changes: 3 additions & 0 deletions tests/transforms/test_qmc_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def test_apply_controlled_v(n_wires):
class TestApplyControlledQ:
"""Tests for the apply_controlled_Q function"""

@pytest.mark.slow
@pytest.mark.parametrize("n_wires", range(2, 5))
def test_apply(self, n_wires):
"""Test if the apply_controlled_Q performs the correct transformation by reconstructing the
Expand Down Expand Up @@ -137,6 +138,7 @@ def test_raises(self):
class TestQuantumMonteCarlo:
"""Tests for the quantum_monte_carlo function"""

@pytest.mark.slow
@pytest.mark.parametrize("n_wires", range(2, 4))
def test_apply(self, n_wires):
"""Test if the quantum_monte_carlo performs the correct transformation by reconstructing the
Expand Down Expand Up @@ -181,6 +183,7 @@ def test_shared_wires(self):
lambda: None, wires=wires, target_wire=0, estimation_wires=estimation_wires
)

@pytest.mark.slow
def test_integration(self):
"""Test if quantum_monte_carlo generates the correct circuit by comparing it to the
QuantumMonteCarlo template on the practical example specified in the usage details. Custom
Expand Down

0 comments on commit e702624

Please sign in to comment.