Skip to content

Commit

Permalink
Fix seeds for more tests (#6460)
Browse files Browse the repository at this point in the history
  • Loading branch information
astralcai authored Oct 30, 2024
1 parent 355d143 commit 82fa99d
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 47 deletions.
5 changes: 2 additions & 3 deletions tests/devices/test_default_qutrit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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]])
Expand Down
20 changes: 10 additions & 10 deletions tests/gradients/finite_diff/test_finite_difference_shot_vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
18 changes: 9 additions & 9 deletions tests/gradients/finite_diff/test_spsa_gradient_shot_vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
16 changes: 6 additions & 10 deletions tests/gradients/parameter_shift/test_parameter_shift_shot_vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion tests/interfaces/test_jacobian_products.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Expand Down
4 changes: 2 additions & 2 deletions tests/measurements/test_classical_shadow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions tests/templates/test_subroutines/test_reflection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions tests/transforms/test_diagonalize_measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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)
2 changes: 1 addition & 1 deletion tests/transforms/test_qcut.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions tests/transforms/test_split_non_commuting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit 82fa99d

Please sign in to comment.