From 82fa99d7a1351ef37382c5d02810f6a37a653cb0 Mon Sep 17 00:00:00 2001 From: Astral Cai Date: Wed, 30 Oct 2024 14:39:09 -0400 Subject: [PATCH] Fix seeds for more tests (#6460) Follow up PR to https://github.com/PennyLaneAI/pennylane/pull/6435, fixing seeds for more tests. See https://github.com/PennyLaneAI/pennylane/actions/runs/11558049719/job/32170809496 --- tests/devices/test_default_qutrit.py | 5 ++--- .../test_finite_difference_shot_vec.py | 20 +++++++++---------- .../test_spsa_gradient_shot_vec.py | 18 ++++++++--------- .../test_parameter_shift_shot_vec.py | 16 ++++++--------- tests/interfaces/test_jacobian_products.py | 5 ++++- tests/measurements/test_classical_shadow.py | 4 ++-- .../test_amplitude_amplification.py | 4 ++-- .../test_subroutines/test_reflection.py | 4 ++-- .../test_diagonalize_measurements.py | 8 ++++---- tests/transforms/test_qcut.py | 2 +- tests/transforms/test_split_non_commuting.py | 6 +++--- 11 files changed, 45 insertions(+), 47 deletions(-) diff --git a/tests/devices/test_default_qutrit.py b/tests/devices/test_default_qutrit.py index 5b7c62bea74..820a4f07cd3 100644 --- a/tests/devices/test_default_qutrit.py +++ b/tests/devices/test_default_qutrit.py @@ -18,7 +18,6 @@ import math import pytest -from flaky import flaky from gate_data import GELL_MANN, OMEGA, TADD, TCLOCK, TSHIFT, TSWAP from scipy.stats import unitary_group @@ -1102,12 +1101,12 @@ def test_gell_mann_obs(self, index_1, index_2, tol_stochastic): ) assert np.allclose(var, expected, atol=tol_stochastic, rtol=0) - @flaky(max_runs=3) @pytest.mark.parametrize("index", list(range(1, 9))) - def test_hermitian(self, index, tol_stochastic): + def test_hermitian(self, index, tol_stochastic, seed): """Tests that sampling on a tensor product of Hermitian observables with another observable works correctly""" + np.random.seed(seed) dev = qml.device("default.qutrit", wires=3, shots=int(1e6)) A = np.array([[2, -0.5j, -1j], [0.5j, 1, -6], [1j, -6, 0]]) diff --git a/tests/gradients/finite_diff/test_finite_difference_shot_vec.py b/tests/gradients/finite_diff/test_finite_difference_shot_vec.py index ac6d19f575f..c4b7a879e0c 100644 --- a/tests/gradients/finite_diff/test_finite_difference_shot_vec.py +++ b/tests/gradients/finite_diff/test_finite_difference_shot_vec.py @@ -509,10 +509,10 @@ def test_ragged_output(self, approx_order, strategy, validate): assert res[1][1].shape == (4,) assert res[1][2].shape == (4,) - def test_single_expectation_value(self, approx_order, strategy, validate): + def test_single_expectation_value(self, approx_order, strategy, validate, seed): """Tests correct output shape and evaluation for a tape with a single expval output""" - dev = qml.device("default.qubit", wires=2, shots=many_shots_shot_vector) + dev = qml.device("default.qubit", wires=2, shots=many_shots_shot_vector, seed=seed) x = 0.543 y = -0.654 @@ -637,7 +637,7 @@ def test_single_expectation_value_with_argnum_one(self, approx_order, strategy, assert np.allclose(res, expected, atol=0.12, rtol=0) - def test_probs_expval_with_argnum_one(self, approx_order, strategy, validate): + def test_probs_expval_with_argnum_one(self, approx_order, strategy, validate, seed): """Tests correct output shape and evaluation for a tape with a multiple measurement, where only one parameter is chosen to be trainable. @@ -646,7 +646,7 @@ def test_probs_expval_with_argnum_one(self, approx_order, strategy, validate): jacobian will match the expected analytical value. """ - dev = qml.device("default.qubit", wires=2, shots=many_shots_shot_vector) + dev = qml.device("default.qubit", wires=2, shots=many_shots_shot_vector, seed=seed) x = 0.543 y = -0.654 @@ -685,10 +685,10 @@ def test_probs_expval_with_argnum_one(self, approx_order, strategy, validate): assert np.allclose(res[0], exp_probs, atol=0.07) assert np.allclose(res[1], exp_expval, atol=0.2) - def test_multiple_expectation_values(self, approx_order, strategy, validate): + def test_multiple_expectation_values(self, approx_order, strategy, validate, seed): """Tests correct output shape and evaluation for a tape with multiple expval outputs""" - dev = qml.device("default.qubit", wires=2, shots=many_shots_shot_vector) + dev = qml.device("default.qubit", wires=2, shots=many_shots_shot_vector, seed=seed) x = 0.543 y = -0.654 @@ -728,10 +728,10 @@ def test_multiple_expectation_values(self, approx_order, strategy, validate): assert isinstance(res[1][0], numpy.ndarray) assert isinstance(res[1][1], numpy.ndarray) - def test_var_expectation_values(self, approx_order, strategy, validate): + def test_var_expectation_values(self, approx_order, strategy, validate, seed): """Tests correct output shape and evaluation for a tape with expval and var outputs""" - dev = qml.device("default.qubit", wires=2, shots=many_shots_shot_vector) + dev = qml.device("default.qubit", wires=2, shots=many_shots_shot_vector, seed=seed) x = 0.543 y = -0.654 @@ -771,10 +771,10 @@ def test_var_expectation_values(self, approx_order, strategy, validate): assert isinstance(res[1][0], numpy.ndarray) assert isinstance(res[1][1], numpy.ndarray) - def test_prob_expectation_values(self, approx_order, strategy, validate): + def test_prob_expectation_values(self, approx_order, strategy, validate, seed): """Tests correct output shape and evaluation for a tape with prob and expval outputs""" - dev = qml.device("default.qubit", wires=2, shots=many_shots_shot_vector) + dev = qml.device("default.qubit", wires=2, shots=many_shots_shot_vector, seed=seed) x = 0.543 y = -0.654 diff --git a/tests/gradients/finite_diff/test_spsa_gradient_shot_vec.py b/tests/gradients/finite_diff/test_spsa_gradient_shot_vec.py index 37035171a5d..db00250ea78 100644 --- a/tests/gradients/finite_diff/test_spsa_gradient_shot_vec.py +++ b/tests/gradients/finite_diff/test_spsa_gradient_shot_vec.py @@ -15,7 +15,7 @@ Tests for the gradients.spsa_gradient module using shot vectors. """ -# pylint: disable=abstract-method +# pylint: disable=abstract-method,too-many-arguments import numpy as np import pytest @@ -516,7 +516,7 @@ class TestSpsaGradientIntegration: def test_ragged_output(self, approx_order, strategy, validate, seed): """Test that the Jacobian is correctly returned for a tape with ragged output""" - dev = qml.device("default.qubit", wires=3, shots=many_shots_shot_vector) + dev = qml.device("default.qubit", wires=3, shots=many_shots_shot_vector, seed=seed) params = [1.0, 1.0, 1.0] rng = np.random.default_rng(seed) @@ -560,7 +560,7 @@ def test_single_expectation_value(self, approx_order, strategy, validate, seed): """Tests correct output shape and evaluation for a tape with a single expval output""" rng = np.random.default_rng(seed) - dev = qml.device("default.qubit", wires=2, shots=many_shots_shot_vector) + dev = qml.device("default.qubit", wires=2, shots=many_shots_shot_vector, seed=seed) x = 0.543 y = -0.654 @@ -607,7 +607,7 @@ def test_single_expectation_value_with_argnum_all(self, approx_order, strategy, with a single expval output where all parameters are chosen to compute the jacobian""" rng = np.random.default_rng(seed) - dev = qml.device("default.qubit", wires=2, shots=many_shots_shot_vector) + dev = qml.device("default.qubit", wires=2, shots=many_shots_shot_vector, seed=seed) x = 0.543 y = -0.654 @@ -660,7 +660,7 @@ def test_single_expectation_value_with_argnum_one(self, approx_order, strategy, jacobian will match the expected analytical value. """ rng = np.random.default_rng(seed) - dev = qml.device("default.qubit", wires=2, shots=many_shots_shot_vector) + dev = qml.device("default.qubit", wires=2, shots=many_shots_shot_vector, seed=seed) x = 0.543 y = -0.654 @@ -715,7 +715,7 @@ def test_multiple_expectation_value_with_argnum_one( jacobian will match the expected analytical value. """ rng = np.random.default_rng(seed) - dev = qml.device("default.qubit", wires=2, shots=many_shots_shot_vector) + dev = qml.device("default.qubit", wires=2, shots=many_shots_shot_vector, seed=seed) x = 0.543 y = -0.654 @@ -757,7 +757,7 @@ def test_multiple_expectation_values(self, approx_order, strategy, validate, see """Tests correct output shape and evaluation for a tape with multiple expval outputs""" rng = np.random.default_rng(seed) - dev = qml.device("default.qubit", wires=2, shots=many_shots_shot_vector) + dev = qml.device("default.qubit", wires=2, shots=many_shots_shot_vector, seed=seed) x = 0.543 y = -0.654 @@ -811,7 +811,7 @@ def test_var_expectation_values(self, approx_order, strategy, validate, seed): """Tests correct output shape and evaluation for a tape with expval and var outputs""" rng = np.random.default_rng(seed) - dev = qml.device("default.qubit", wires=2, shots=many_shots_shot_vector, seed=rng) + dev = qml.device("default.qubit", wires=2, shots=many_shots_shot_vector, seed=seed) x = 0.543 y = -0.654 @@ -866,7 +866,7 @@ def test_prob_expectation_values(self, approx_order, strategy, validate, seed): """Tests correct output shape and evaluation for a tape with prob and expval outputs""" rng = np.random.default_rng(seed) - dev = qml.device("default.qubit", wires=2, shots=many_shots_shot_vector) + dev = qml.device("default.qubit", wires=2, shots=many_shots_shot_vector, seed=seed) x = 0.543 y = -0.654 diff --git a/tests/gradients/parameter_shift/test_parameter_shift_shot_vec.py b/tests/gradients/parameter_shift/test_parameter_shift_shot_vec.py index ca55758062c..3062841ba14 100644 --- a/tests/gradients/parameter_shift/test_parameter_shift_shot_vec.py +++ b/tests/gradients/parameter_shift/test_parameter_shift_shot_vec.py @@ -17,7 +17,6 @@ import pytest from default_qubit_legacy import DefaultQubitLegacy -from flaky import flaky import pennylane as qml from pennylane import numpy as np @@ -1271,11 +1270,10 @@ def test_involutory_variance_multi_param(self, broadcast): assert gradF[1] == pytest.approx(expected, abs=finite_diff_tol) assert gradA[1] == pytest.approx(expected, abs=finite_diff_tol) - @flaky(max_runs=5) - def test_non_involutory_variance_single_param(self, broadcast): + def test_non_involutory_variance_single_param(self, broadcast, seed): """Tests a qubit Hermitian observable that is not involutory with a single trainable parameter""" shot_vec = many_shots_shot_vector - dev = qml.device("default.qubit", wires=1, shots=shot_vec) + dev = qml.device("default.qubit", wires=1, shots=shot_vec, seed=seed) a = 0.54 _herm_shot_vec_tol = shot_vec_tol * 100 @@ -1369,12 +1367,11 @@ def test_non_involutory_variance_multi_param(self, broadcast, seed): assert gradF[0] == pytest.approx(expected, abs=2) assert qml.math.allclose(gradF[1], expected, atol=1.5) - @flaky(max_runs=8) - def test_involutory_and_noninvolutory_variance_single_param(self, broadcast): + def test_involutory_and_noninvolutory_variance_single_param(self, broadcast, seed): """Tests a qubit Hermitian observable that is not involutory alongside an involutory observable when there's a single trainable parameter.""" shot_vec = tuple([1000000] * 3) - dev = qml.device("default.qubit", wires=2, shots=shot_vec) + dev = qml.device("default.qubit", wires=2, shots=shot_vec, seed=seed) a = 0.54 _herm_shot_vec_tol = shot_vec_tol * 100 @@ -1423,12 +1420,11 @@ def test_involutory_and_noninvolutory_variance_single_param(self, broadcast): assert shot_vec_result[0] == pytest.approx(expected[0], abs=finite_diff_tol) assert shot_vec_result[1] == pytest.approx(expected[1], abs=_herm_shot_vec_tol) - @flaky(max_runs=8) - def test_involutory_and_noninvolutory_variance_multi_param(self, broadcast): + def test_involutory_and_noninvolutory_variance_multi_param(self, broadcast, seed): """Tests a qubit Hermitian observable that is not involutory alongside an involutory observable.""" shot_vec = many_shots_shot_vector - dev = qml.device("default.qubit", wires=2, shots=shot_vec) + dev = qml.device("default.qubit", wires=2, shots=shot_vec, seed=seed) a = 0.54 with qml.queuing.AnnotatedQueue() as q: diff --git a/tests/interfaces/test_jacobian_products.py b/tests/interfaces/test_jacobian_products.py index ca90d6f48a3..24641465302 100644 --- a/tests/interfaces/test_jacobian_products.py +++ b/tests/interfaces/test_jacobian_products.py @@ -192,8 +192,11 @@ def test_execute_jvp_basic(self, jpc, shots): assert qml.math.allclose(res[0][1], np.cos(x), atol=_tol_for_shots(shots)) assert qml.math.allclose(jvp[0][1], -0.5 * np.sin(x), atol=_tol_for_shots(shots)) - def test_vjp_basic(self, jpc, shots): + def test_vjp_basic(self, jpc, shots, seed): """Test compute_vjp for a simple single input single output.""" + + np.random.seed(seed) + if shots and not _accepts_finite_shots(jpc): pytest.skip("jpc does not work with finite shots.") diff --git a/tests/measurements/test_classical_shadow.py b/tests/measurements/test_classical_shadow.py index 44609213ec8..7d019093628 100644 --- a/tests/measurements/test_classical_shadow.py +++ b/tests/measurements/test_classical_shadow.py @@ -479,10 +479,10 @@ def test_shots_none_error(self): with pytest.raises(qml.DeviceError, match=msg): _ = circuit(H, k=10) - def test_multi_measurement_allowed(self): + def test_multi_measurement_allowed(self, seed): """Test that no error is raised when classical shadows is returned with other measurement processes""" - dev = qml.device("default.qubit", wires=2, shots=10000) + dev = qml.device("default.qubit", wires=2, shots=10000, seed=seed) @qml.qnode(dev) def circuit(): diff --git a/tests/templates/test_subroutines/test_amplitude_amplification.py b/tests/templates/test_subroutines/test_amplitude_amplification.py index 1c7ca4c2c97..dbcdf312e2d 100644 --- a/tests/templates/test_subroutines/test_amplitude_amplification.py +++ b/tests/templates/test_subroutines/test_amplitude_amplification.py @@ -157,10 +157,10 @@ def circuit(params): @pytest.mark.autograd @pytest.mark.parametrize("shots", [None, 50000]) - def test_qnode_autograd(self, shots): + def test_qnode_autograd(self, shots, seed): """Test that the QNode executes with Autograd.""" - dev = qml.device("default.qubit", wires=3, shots=shots) + dev = qml.device("default.qubit", wires=3, shots=shots, seed=seed) diff_method = "backprop" if shots is None else "parameter-shift" qnode = qml.QNode(self.circuit, dev, interface="autograd", diff_method=diff_method) diff --git a/tests/templates/test_subroutines/test_reflection.py b/tests/templates/test_subroutines/test_reflection.py index 6123daed312..310474a8747 100644 --- a/tests/templates/test_subroutines/test_reflection.py +++ b/tests/templates/test_subroutines/test_reflection.py @@ -164,10 +164,10 @@ def test_lightning_qubit(self): @pytest.mark.autograd @pytest.mark.parametrize("shots", [None, 50000]) - def test_qnode_autograd(self, shots): + def test_qnode_autograd(self, shots, seed): """Test that the QNode executes with Autograd.""" - dev = qml.device("default.qubit", shots=shots, wires=3) + dev = qml.device("default.qubit", shots=shots, wires=3, seed=seed) diff_method = "backprop" if shots is None else "parameter-shift" qnode = qml.QNode(self.circuit, dev, interface="autograd", diff_method=diff_method) diff --git a/tests/transforms/test_diagonalize_measurements.py b/tests/transforms/test_diagonalize_measurements.py index 35b9f763923..70122f5bb69 100644 --- a/tests/transforms/test_diagonalize_measurements.py +++ b/tests/transforms/test_diagonalize_measurements.py @@ -625,12 +625,12 @@ def test_bad_to_eigvals_input_raises_error(self, supported_base_obs): @pytest.mark.parametrize("to_eigvals", [True, False]) @pytest.mark.parametrize("supported_base_obs", ([qml.Z], [qml.Z, qml.X], [qml.Z, qml.X, qml.Y])) @pytest.mark.parametrize("shots", [None, 2000, (4000, 5000, 6000)]) - def test_qnode_integration(self, to_eigvals, supported_base_obs, shots): + def test_qnode_integration(self, to_eigvals, supported_base_obs, shots, seed): if to_eigvals and supported_base_obs != [qml.Z]: pytest.skip("to_eigvals is not supported when not diagonalizing all gates") - dev = qml.device("default.qubit", shots=shots) + dev = qml.device("default.qubit", shots=shots, seed=seed) @qml.qnode(dev) def circuit(): @@ -650,6 +650,6 @@ def circuit_diagonalized(): if len(dev.shots.shot_vector) > 1: for r_diagonalized, r in zip(res, expected_res): - assert np.allclose(r_diagonalized, r, atol=0.1) + assert np.allclose(r_diagonalized, r, rtol=0.1) else: - assert np.allclose(expected_res, res, atol=0.1) + assert np.allclose(expected_res, res, rtol=0.1) diff --git a/tests/transforms/test_qcut.py b/tests/transforms/test_qcut.py index d0bc675ca81..2ed9eeeffb3 100644 --- a/tests/transforms/test_qcut.py +++ b/tests/transforms/test_qcut.py @@ -2535,7 +2535,7 @@ def target_circuit(v): qml.RX(2.3, wires=2) return qml.expval(qml.PauliZ(wires=0) @ qml.PauliZ(wires=2)) - dev = dev_fn(wires=2, shots=20000) + dev = dev_fn(wires=2, shots=20000, seed=seed) @partial(qml.cut_circuit_mc, classical_processing_fn=fn) @qml.qnode(dev) diff --git a/tests/transforms/test_split_non_commuting.py b/tests/transforms/test_split_non_commuting.py index bc7853272b6..b3374fc52ac 100644 --- a/tests/transforms/test_split_non_commuting.py +++ b/tests/transforms/test_split_non_commuting.py @@ -14,7 +14,7 @@ """Tests for the transform ``qml.transform.split_non_commuting`` """ -# pylint: disable=import-outside-toplevel,unnecessary-lambda +# pylint: disable=import-outside-toplevel,unnecessary-lambda,too-many-arguments import itertools from functools import partial @@ -740,10 +740,10 @@ def circuit(angles): ), ], ) - def test_multiple_expval(self, grouping_strategy, shots, params, expected_results): + def test_multiple_expval(self, grouping_strategy, shots, params, expected_results, seed): """Tests that a QNode with multiple expval measurements is executed correctly""" - dev = qml.device("default.qubit", wires=2, shots=shots) + dev = qml.device("default.qubit", wires=2, shots=shots, seed=seed) obs_list = complex_obs_list if not qml.operation.active_new_opmath():