Skip to content

Commit

Permalink
Fix the failed observable serialization unit tests (#683)
Browse files Browse the repository at this point in the history
* Fix obs_serialize tests

* Auto update version

* Update changelog

* Update format

* trigger ci

* Add shots=None tests in test_measurements.

* trigger ci

* Update tests

---------

Co-authored-by: Dev version update bot <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Vincent Michaud-Rioux <[email protected]>
  • Loading branch information
3 people authored Apr 17, 2024
1 parent d0e1c51 commit bc9dbcb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 28 deletions.
3 changes: 3 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@
* `lightning.qubit` correctly decomposed state preparation operations with adjoint differentiation.
[(#661)](https://github.com/PennyLaneAI/pennylane-lightning/pull/661)

* Fix the failed observable serialization unit tests.
[(#683)](https://github.com/PennyLaneAI/pennylane-lightning/pull/683)

### Contributors

This release contains contributions from (in alphabetical order):
Expand Down
2 changes: 1 addition & 1 deletion pennylane_lightning/core/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
Version number (major.minor.patch[-label])
"""

__version__ = "0.36.0-dev28"
__version__ = "0.36.0-dev29"
22 changes: 18 additions & 4 deletions tests/test_measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,23 +695,36 @@ def circuit2():


@flaky(max_runs=5)
@pytest.mark.parametrize("shots", [10000, [10000, 11111]])
@pytest.mark.parametrize("shots", [None, 10000, [10000, 11111]])
@pytest.mark.parametrize("measure_f", [qml.counts, qml.expval, qml.probs, qml.sample, qml.var])
@pytest.mark.parametrize(
"obs", [[0], [0, 1], qml.PauliZ(0), qml.PauliY(1), qml.PauliZ(0) @ qml.PauliY(1)]
"obs",
[
[0],
[0, 1],
qml.PauliZ(0),
qml.PauliY(1),
qml.PauliZ(0) @ qml.PauliY(1),
qml.PauliZ(1) @ qml.PauliY(2),
],
)
@pytest.mark.parametrize("mcmc", [False, True])
@pytest.mark.parametrize("kernel_name", ["Local", "NonZeroRandom"])
def test_shots_single_measure_obs(shots, measure_f, obs, mcmc, kernel_name):
"""Tests that Lightning handles shots in a circuit where a single measurement of a common observable is performed at the end."""
n_qubits = 2
n_qubits = 3

if device_name in ("lightning.gpu", "lightning.kokkos") and (mcmc or kernel_name != "Local"):
if (shots is None or device_name in ("lightning.gpu", "lightning.kokkos")) and (
mcmc or kernel_name != "Local"
):
pytest.skip(f"Device {device_name} does not have an mcmc option.")

if measure_f in (qml.expval, qml.var) and isinstance(obs, Sequence):
pytest.skip("qml.expval, qml.var do not take wire arguments.")

if measure_f in (qml.counts, qml.sample) and shots is None:
pytest.skip("qml.counts, qml.sample do not work with shots = None.")

if device_name in ("lightning.gpu", "lightning.kokkos"):
dev = qml.device(device_name, wires=n_qubits, shots=shots)
else:
Expand All @@ -725,6 +738,7 @@ def func(x, y):
qml.RX(x, 0)
qml.RX(y, 0)
qml.RX(y, 1)
qml.RX(x, 2)
return measure_f(wires=obs) if isinstance(obs, Sequence) else measure_f(op=obs)

func1 = qml.QNode(func, dev)
Expand Down
32 changes: 9 additions & 23 deletions tests/test_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,28 +317,13 @@ def test_hamiltonian_tensor_return(self, use_csingle, wires_map):
tape, wires_map
)

# Expression (ham @ obs) is converted internally by Pennylane
# where obs is appended to each term of the ham
if qml.operation.active_new_opmath():
first_term = tensor_prod_obs(
[
tensor_prod_obs(
[
hermitian_obs(np.eye(4, dtype=c_dtype).ravel(), [0, 1]),
named_obs("PauliY", [2]),
]
),
named_obs("PauliZ", [3]),
]
)
else:
first_term = tensor_prod_obs(
[
hermitian_obs(np.eye(4, dtype=c_dtype).ravel(), [0, 1]),
named_obs("PauliY", [2]),
named_obs("PauliZ", [3]),
]
)
first_term = tensor_prod_obs(
[
hermitian_obs(np.eye(4, dtype=c_dtype).ravel(), [0, 1]),
named_obs("PauliY", [2]),
named_obs("PauliZ", [3]),
]
)

s_expected = hamiltonian_obs(
np.array([0.3, 0.5, 0.4], dtype=r_dtype),
Expand Down Expand Up @@ -498,7 +483,8 @@ def test_prod(self, use_csingle, wires_map):

s_expected = tensor_obs(
[
tensor_obs([named_obs("PauliX", [1]), named_obs("PauliZ", [0])]),
named_obs("PauliZ", [0]),
named_obs("PauliX", [1]),
named_obs("Hadamard", [2]),
]
)
Expand Down

0 comments on commit bc9dbcb

Please sign in to comment.