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

Bump rng_salt version to v0.40.0 #6854

Merged
merged 43 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
6b55ec7
fix: Update pytest.ini
andrijapau Jan 17, 2025
bdffdd4
doc: Update changelog
andrijapau Jan 17, 2025
19ddcc0
fix: Update tests that no longer need local_salt
andrijapau Jan 17, 2025
3505100
add local salts where appropriate
andrijapau Jan 23, 2025
4f3bd10
Merge branch 'master' into fix-rng-salt-version
andrijapau Jan 23, 2025
d428928
Merge branch 'master' into fix-rng-salt-version
andrijapau Jan 27, 2025
efbd16d
fix: Update test_jax_jit_qnode.py to use atol
andrijapau Jan 27, 2025
ef45fad
fix: locally salt test_parameter_shift_shot_vec.py
andrijapau Jan 27, 2025
97da913
fix: remove local salt, bump atol a bit for test_default_qubit.py
andrijapau Jan 27, 2025
a642bc3
Merge branch 'master' into fix-rng-salt-version
andrijapau Jan 28, 2025
b31a087
adjust more tests
andrijapau Jan 28, 2025
db4fcda
Merge branch 'master' into fix-rng-salt-version
andrijapau Jan 28, 2025
b9ef3a6
adjust more test
andrijapau Jan 28, 2025
6eb31d8
adjust more tests
andrijapau Jan 28, 2025
23f912f
Update tests/devices/default_qubit/test_default_qubit.py
andrijapau Feb 6, 2025
cee9850
Merge branch 'master' into fix-rng-salt-version
JerryChen97 Feb 6, 2025
581eaa2
Fix test_autograph.py
andrijapau Feb 6, 2025
a6e120d
Update tests/devices/qutrit_mixed/test_qutrit_mixed_sampling.py
andrijapau Feb 6, 2025
45bd405
Update tests/gradients/core/test_pulse_gradient.py
andrijapau Feb 6, 2025
2671ed6
Update tests/measurements/test_expval.py
andrijapau Feb 6, 2025
4c7cc3f
Merge branch 'master' into fix-rng-salt-version
andrijapau Feb 6, 2025
b3527c1
erase allclose check bc its broadcasting tests
andrijapau Feb 6, 2025
5ef96cd
Update tests/gradients/parameter_shift/test_parameter_shift_shot_vec.py
andrijapau Feb 6, 2025
78d0d47
Update tests/gradients/parameter_shift/test_parameter_shift_shot_vec.py
andrijapau Feb 6, 2025
c3d9a7d
adjust spsa tolerance
andrijapau Feb 6, 2025
c6b3bea
remove salt from test_default_qubit.py
andrijapau Feb 12, 2025
9ed0fe7
fix test_parameter_shift_shot_vec.py
andrijapau Feb 13, 2025
c73abf6
fix test_parameter_shift_shot_vec.py
andrijapau Feb 13, 2025
d3e2580
Merge branch 'master' into fix-rng-salt-version
andrijapau Feb 13, 2025
14c983a
doc: changelog
andrijapau Feb 13, 2025
b89614e
fix jax jit qnode file
andrijapau Feb 20, 2025
2f27625
Update tests/gradients/parameter_shift/test_parameter_shift_shot_vec.py
andrijapau Feb 20, 2025
f12da26
Merge branch 'master' into fix-rng-salt-version
andrijapau Feb 20, 2025
df541bd
Merge branch 'master' into fix-rng-salt-version
andrijapau Feb 27, 2025
d5ab380
restore allclose check per christinas request
andrijapau Feb 27, 2025
559f746
fix xfail message
andrijapau Feb 27, 2025
034856f
Merge branch 'master' into fix-rng-salt-version
andrijapau Feb 28, 2025
1c71228
bump tol
andrijapau Feb 28, 2025
189db1b
Merge branch 'master' into fix-rng-salt-version
andrijapau Feb 28, 2025
56b32e7
clean-up test
andrijapau Feb 28, 2025
9ff3b52
fix: Update test_pulse_gradient.py
andrijapau Feb 28, 2025
64be74d
fix: Update test_pulse_gradient.py
andrijapau Feb 28, 2025
672fba1
get rid of print
andrijapau Feb 28, 2025
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
3 changes: 3 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@

<h3>Improvements 🛠</h3>

* Bump `rng_salt` to `v0.40.0`.
[(#6854)](https://github.com/PennyLaneAI/pennylane/pull/6854)

* `qml.gradients.hadamard_grad` can now differentiate anything with a generator, and can accept circuits with non-commuting measurements.
[(#6928)](https://github.com/PennyLaneAI/pennylane/pull/6928)

Expand Down
5 changes: 4 additions & 1 deletion tests/capture/autograph/test_autograph.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,10 @@ def circuit(x):
with pytest.raises(NotImplementedError):
ag_fn(0.5)

@pytest.mark.xfail
@pytest.mark.xfail(
strict=False,
reason="MCM one shot not currently supported with program capture. See sc-83580.",
)
def test_mcm_one_shot(self, seed):
"""Test if mcm one-shot miss transforms."""
dev = qml.device("default.qubit", wires=5, shots=20, seed=seed)
Expand Down
4 changes: 2 additions & 2 deletions tests/devices/default_qubit/test_default_qubit.py
Original file line number Diff line number Diff line change
Expand Up @@ -1541,7 +1541,7 @@ def test_hamiltonian_expval(self, max_workers, seed):
res = dev.execute(qs)

expected = 0.8 * np.cos(x) + 0.5 * np.real(np.exp(y * 1j)) * np.sin(x)
assert np.allclose(res, expected, atol=0.01)
assert np.allclose(res, expected, atol=0.02)

@pytest.mark.parametrize("max_workers", max_workers_list)
def test_sum_expval(self, max_workers, seed):
Expand All @@ -1555,7 +1555,7 @@ def test_sum_expval(self, max_workers, seed):
res = dev.execute(qs)

expected = 0.8 * np.cos(x) + 0.5 * np.real(np.exp(y * 1j)) * np.sin(x)
assert np.allclose(res, expected, atol=0.01)
assert np.allclose(res, expected, atol=0.02)

@pytest.mark.parametrize("max_workers", max_workers_list)
def test_multi_wires(self, max_workers, seed):
Expand Down
4 changes: 2 additions & 2 deletions tests/devices/qubit/test_sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -1174,8 +1174,8 @@ def test_hamiltonian_expval_shot_vector(self, seed):

assert len(res) == 2
assert isinstance(res, tuple)
assert np.allclose(res[0], expected, atol=0.01)
assert np.allclose(res[1], expected, atol=0.01)
assert np.allclose(res[0], expected, atol=0.02)
assert np.allclose(res[1], expected, atol=0.02)

def test_sum_expval(self, seed):
"""Test that sampling works well for Sum observables"""
Expand Down
2 changes: 1 addition & 1 deletion tests/devices/qutrit_mixed/test_qutrit_mixed_sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ def test_nonsample_measure_shot_vector(self, mocker, shots, measurement, expecte

for r in res:
assert r.shape == expected.shape
assert np.allclose(r, expected, atol=0.02)
assert np.allclose(r, expected, atol=0.03)


@pytest.mark.parametrize(
Expand Down
9 changes: 6 additions & 3 deletions tests/gradients/core/test_pulse_gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,9 @@ def qnode(params_0, params_1):
res = fn(qml.execute(tapes, dev, None))
exp_grad = jax.grad(qnode, argnums=(0, 1))(params_0, params_1)
exp_grad = exp_grad[0] + exp_grad[1]
assert all(qml.math.allclose(r, e, rtol=0.4) for r, e in zip(res, exp_grad))
# Values are close to zero so we need to use `atol` instead of `rtol`
# to avoid numerical issues
assert all(qml.math.allclose(r, e, atol=5e-4) for r, e in zip(res, exp_grad))
jax.clear_caches()

@pytest.mark.slow
Expand Down Expand Up @@ -1566,9 +1568,10 @@ def ansatz(params):
grad_pulse_grad = jax.grad(qnode_pulse_grad)(params)
assert tracker.totals["executions"] == 1 + 2 * 3 * num_split_times
grad_backprop = jax.grad(qnode_backprop)(params)

# Values are close to zero so we need to use `atol` instead of `rtol`
# to avoid numerical issues
assert all(
qml.math.allclose(r, e, rtol=0.4) for r, e in zip(grad_pulse_grad, grad_backprop)
qml.math.allclose(r, e, atol=5e-3) for r, e in zip(grad_pulse_grad, grad_backprop)
)
jax.clear_caches()

Expand Down
2 changes: 1 addition & 1 deletion tests/gradients/finite_diff/test_spsa_gradient_shot_vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from pennylane.operation import AnyWires, Observable

h_val = 0.1
spsa_shot_vec_tol = 0.31
spsa_shot_vec_tol = 0.33

default_shot_vector = (1000, 2000, 3000)
many_shots_shot_vector = tuple([100000] * 3)
Expand Down
14 changes: 7 additions & 7 deletions tests/gradients/parameter_shift/test_parameter_shift_shot_vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -1377,7 +1377,6 @@ def test_involutory_and_noninvolutory_variance_single_param(self, broadcast, see
dev = qml.device("default.qubit", wires=2, shots=shot_vec, seed=seed)
a = 0.54

_herm_shot_vec_tol = shot_vec_tol * 100
with qml.queuing.AnnotatedQueue() as q:
qml.RX(a, wires=0)
qml.RX(a, wires=1)
Expand All @@ -1391,7 +1390,7 @@ def test_involutory_and_noninvolutory_variance_single_param(self, broadcast, see
res = dev.execute(tape)
expected = [1 - np.cos(a) ** 2, (39 / 2) - 6 * np.sin(2 * a) + (35 / 2) * np.cos(2 * a)]
for r in res:
assert np.allclose(r, expected, atol=_herm_shot_vec_tol, rtol=0)
assert qml.math.allclose(r, expected, atol=5e-2)

# circuit jacobians
tapes, fn = qml.gradients.param_shift(tape, broadcast=broadcast)
Expand All @@ -1412,16 +1411,17 @@ def test_involutory_and_noninvolutory_variance_single_param(self, broadcast, see
assert isinstance(param_res, np.ndarray)
assert param_res.shape == ()

assert shot_vec_result[0] == pytest.approx(expected[0], abs=finite_diff_tol)
assert shot_vec_result[1] == pytest.approx(expected[1], abs=0.3)
assert qml.math.allclose(shot_vec_result[0], expected[0], atol=0.1)
assert qml.math.allclose(shot_vec_result[1], expected[1], atol=0.15)

# Finite-diff
for shot_vec_result in gradF:
for param_res in shot_vec_result:
assert isinstance(param_res, np.ndarray)
assert param_res.shape == ()

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)
# Tolerance came from multi_param test below
assert qml.math.allclose(shot_vec_result[0], expected[0], atol=0.1)
assert qml.math.allclose(shot_vec_result[1], expected[1], atol=1.5)

def test_involutory_and_noninvolutory_variance_multi_param(self, broadcast, seed):
"""Tests a qubit Hermitian observable that is not involutory alongside
Expand Down
7 changes: 3 additions & 4 deletions tests/measurements/test_expval.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,11 @@ def test_shape(self, obs):
assert res.shape(None, 1) == ()
assert res.shape(100, 1) == ()

@pytest.mark.local_salt(2)
@pytest.mark.parametrize("state", [np.array([0, 0, 0]), np.array([1, 0, 0, 0, 0, 0, 0, 0])])
@pytest.mark.parametrize("shots", [None, 1000, [1000, 1111]])
def test_projector_expval(self, state, shots, seed):
def test_projector_expval_qnode(self, state, shots, seed):
"""Tests that the expectation of a ``Projector`` object is computed correctly for both of
its subclasses."""
its subclasses when integrating with the ``QNode``."""
dev = qml.device("default.qubit", wires=3, shots=shots, seed=seed)

@qml.qnode(dev)
Expand All @@ -231,7 +230,7 @@ def circuit():

res = circuit()
expected = [0.5, 0.5] if isinstance(shots, list) else 0.5
assert np.allclose(res, expected, atol=0.02, rtol=0.02)
assert np.allclose(res, expected, atol=0.02, rtol=0.04)

def test_permuted_wires(self):
"""Test that the expectation value of an operator with permuted wires is the same."""
Expand Down
2 changes: 1 addition & 1 deletion tests/pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ filterwarnings =
error:AutoGraph will not transform the function:pennylane.capture.autograph.AutoGraphWarning
addopts = --benchmark-disable
xfail_strict=true
rng_salt = v0.39.0
rng_salt = v0.40.0
7 changes: 3 additions & 4 deletions tests/templates/test_subroutines/test_qdrift.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def test_private_sample(self, coeffs, ops, time, seed, n): # pylint: disable=to
assert term.base in ops # sample from ops
assert term.coeff == (s * normalization * time * 1j / n) # with this exponent

@pytest.mark.parametrize("coeffs", ([0.99, 0.01], [0.5 + 0.49j, -0.01j]))
@pytest.mark.parametrize("coeffs", ([0.999, 0.001], [0.5 + 0.499j, -0.001j]))
def test_private_sample_statistics(self, coeffs, seed):
"""Test the private function samples from the right distribution"""
ops = [qml.PauliX(0), qml.PauliZ(1)]
Expand Down Expand Up @@ -176,7 +176,7 @@ def test_compute_decomposition(self, seed):
class TestIntegration:
"""Test that the QDrift template integrates well with the rest of PennyLane"""

@pytest.mark.local_salt(8)
@pytest.mark.local_salt(3)
@pytest.mark.parametrize("n", (1, 2, 3))
@pytest.mark.parametrize("time", (0.5, 1, 2))
@pytest.mark.parametrize("coeffs, ops", test_hamiltonians)
Expand Down Expand Up @@ -205,9 +205,8 @@ def circ():
)
state = circ()

assert allclose(expected_state, state)
assert allclose(state, expected_state)

@pytest.mark.local_salt(8)
@pytest.mark.autograd
@pytest.mark.parametrize("coeffs, ops", test_hamiltonians)
def test_execution_autograd(self, coeffs, ops, seed):
Expand Down
2 changes: 1 addition & 1 deletion tests/transforms/test_broadcast_expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def test_shot_vector_expval(self, params, size, obs, exp_fn, tol_stochastic, see
"""Test that expansion works as expected with shot vectors"""
ops = make_ops(*params)
expvals = [qml.expval(ob) for ob in obs]
shots = [20000, 20001]
shots = [30000, 30001]
tape = qml.tape.QuantumScript(ops, expvals, shots=shots)
assert tape.batch_size == size

Expand Down
1 change: 0 additions & 1 deletion tests/workflow/interfaces/execute/test_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,6 @@ def cost(a):
assert torch.allclose(res, expected, atol=atol_for_shots(shots), rtol=0)
assert torch.allclose(res, -torch.sin(a), atol=atol_for_shots(shots))

@pytest.mark.local_salt(1)
def test_jacobian(self, execute_kwargs, shots, device_name, seed):
"""Test jacobian calculation"""
a = torch.tensor(0.1, requires_grad=True)
Expand Down
28 changes: 17 additions & 11 deletions tests/workflow/interfaces/qnode/test_jax_jit_qnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,6 @@ def cost_fn(a, b):
jax.grad(cost_fn)(a, b)
assert dev.tracker.totals["executions"] == 1

@pytest.mark.local_salt(2)
@pytest.mark.parametrize("shots", [(10000, 10000), (10000, 10005)])
def test_shot_vectors_single_measurements(self, interface, shots, seed):
"""Test jax-jit can work with shot vectors."""
Expand All @@ -890,13 +889,13 @@ def circuit(x):

res = circuit(0.5)
expected = 1 - np.cos(0.5) ** 2
assert qml.math.allclose(res[0], expected, atol=5e-2)
assert qml.math.allclose(res[1], expected, rtol=5e-2)
assert qml.math.allclose(res[0], expected, atol=1 / qml.math.sqrt(shots[0]), rtol=0.03)
assert qml.math.allclose(res[1], expected, atol=1 / qml.math.sqrt(shots[1]), rtol=0.03)

g = jax.jacobian(circuit)(0.5)
expected_g = 2 * np.cos(0.5) * np.sin(0.5)
assert qml.math.allclose(g[0], expected_g, rtol=5e-2)
assert qml.math.allclose(g[1], expected_g, rtol=5e-2)
assert qml.math.allclose(g[0], expected_g, atol=1 / qml.math.sqrt(shots[0]), rtol=0.03)
assert qml.math.allclose(g[1], expected_g, atol=1 / qml.math.sqrt(shots[1]), rtol=0.03)

@pytest.mark.parametrize("shots", [(10000, 10000), (10000, 10005)])
def test_shot_vectors_multiple_measurements(self, interface, shots, seed):
Expand All @@ -911,13 +910,21 @@ def circuit(x):
return qml.expval(qml.PauliZ(0)), qml.probs(wires=0)

res = circuit(0.5)
assert qml.math.allclose(res[0][0], np.cos(0.5), rtol=5e-2)
assert qml.math.allclose(res[1][0], np.cos(0.5), rtol=5e-2)
expected = np.cos(0.5)
assert qml.math.allclose(res[0][0], expected, atol=1 / qml.math.sqrt(shots[0]), rtol=0.03)
assert qml.math.allclose(res[1][0], expected, atol=1 / qml.math.sqrt(shots[0]), rtol=0.03)

expected_probs = np.array([np.cos(0.25) ** 2, np.sin(0.25) ** 2])
assert qml.math.allclose(res[0][1], expected_probs, atol=1e-2)
assert qml.math.allclose(res[1][1][0], expected_probs[0], rtol=5e-2)
assert qml.math.allclose(res[1][1][1], expected_probs[1], atol=5e-3)
assert qml.math.allclose(
res[0][1], expected_probs, atol=1 / qml.math.sqrt(shots[1]), rtol=0.03
)
assert qml.math.allclose(
res[1][1][0], expected_probs[0], atol=1 / qml.math.sqrt(shots[0]), rtol=0.03
)
# Smaller atol since sin(0.25)**2 is close to zero
assert qml.math.allclose(
res[1][1][1], expected_probs[1], atol=0.5 * 1 / qml.math.sqrt(shots[1])
)


@pytest.mark.parametrize("interface", ["auto", "jax-jit"])
Expand Down Expand Up @@ -1108,7 +1115,6 @@ def expected_circuit(theta):
class TestQubitIntegrationHigherOrder:
"""Tests that ensure various qubit circuits integrate correctly when computing higher-order derivatives"""

@pytest.mark.local_salt(1)
def test_second_derivative(
self, dev_name, diff_method, grad_on_execution, device_vjp, interface, tol, seed
):
Expand Down
1 change: 0 additions & 1 deletion tests/workflow/interfaces/qnode/test_jax_qnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,6 @@ def expected_circuit(theta):
class TestQubitIntegrationHigherOrder:
"""Tests that ensure various qubit circuits integrate correctly when computing higher-order derivatives"""

@pytest.mark.local_salt(1)
def test_second_derivative(
self, dev_name, diff_method, grad_on_execution, device_vjp, interface, tol, seed
):
Expand Down