Skip to content

Commit a36fe22

Browse files
LuisAlfredoNuringo-but-quantummaliasadi
authored
Solve test measurements issue with CI (#895)
### Before submitting Please complete the following checklist when submitting a PR: - [ ] All new features must include a unit test. If you've fixed a bug or added code that should be tested, add a test to the [`tests`](../tests) directory! - [ ] All new functions and code must be clearly commented and documented. If you do make documentation changes, make sure that the docs build and render correctly by running `make docs`. - [ ] Ensure that the test suite passes, by running `make test`. - [ ] Add a new entry to the `.github/CHANGELOG.md` file, summarizing the change, and including a link back to the PR. - [ ] Ensure that code is properly formatted by running `make format`. When all the above are checked, delete everything above the dashed line and fill in the pull request template. ------------------------------------------------------------------------------------------------------------ **Context:** Revert changes about testing the measurements with shots. The probability of failing the test is non zero then something the test can fail randomly and reduces the CI quality in all PR. **Description of the Change:** **Benefits:** Stop failing the CI regarding the measurement test with shots **Possible Drawbacks:** **Related GitHub Issues:** --------- Co-authored-by: ringo-but-quantum <[email protected]> Co-authored-by: Ali Asadi <[email protected]>
1 parent 7943928 commit a36fe22

File tree

2 files changed

+15
-24
lines changed

2 files changed

+15
-24
lines changed

pennylane_lightning/core/_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616
Version number (major.minor.patch[-label])
1717
"""
1818

19-
__version__ = "0.39.0-dev9"
19+
__version__ = "0.39.0-dev10"

tests/lightning_qubit/test_measurements_class.py

+14-23
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,8 @@ def calculate_reference(tape, lightning_sv):
408408
m = LightningMeasurements(statevector)
409409
return m.measure_final_state(tape)
410410

411-
@flaky(max_runs=15)
412-
@pytest.mark.parametrize("shots", [None, 200_000, [190_000, 190_000]])
411+
@flaky(max_runs=5)
412+
@pytest.mark.parametrize("shots", [None, 500_000, [500_000, 500_000]])
413413
@pytest.mark.parametrize("measurement", [qml.expval, qml.probs, qml.var])
414414
@pytest.mark.parametrize(
415415
"observable",
@@ -449,16 +449,12 @@ def test_single_return_value(self, shots, measurement, observable, lightning_sv,
449449
pytest.skip(
450450
f"Measurement of type {type(measurement).__name__} does not have a keyword argument 'wires'."
451451
)
452-
rtol = 1.0e-2 # 1% of expected value as tolerance
453452
if shots != None and measurement is qml.expval:
454453
# Increase the number of shots
455454
if isinstance(shots, int):
456-
shots *= 10
455+
shots = 1_000_000
457456
else:
458-
shots = [i * 10 for i in shots]
459-
460-
# Extra tolerance
461-
rtol = 5.0e-2 # 5% of expected value as tolerance
457+
shots = [1_000_000, 1_000_000]
462458

463459
n_qubits = 4
464460
n_layers = 1
@@ -499,14 +495,13 @@ def test_single_return_value(self, shots, measurement, observable, lightning_sv,
499495
if shots is None:
500496
assert np.allclose(result, expected, max(tol, 1.0e-4))
501497
else:
502-
atol = max(tol, 1.0e-2) if statevector.dtype == np.complex64 else max(tol, 1.0e-3)
503-
rtol = max(tol, rtol) # % of expected value as tolerance
504-
498+
# TODO Set better atol and rtol
499+
dtol = max(tol, 1.0e-2)
505500
# allclose -> absolute(a - b) <= (atol + rtol * absolute(b))
506-
assert np.allclose(result, expected, rtol=rtol, atol=atol)
501+
assert np.allclose(result, expected, rtol=dtol, atol=dtol)
507502

508-
@flaky(max_runs=10)
509-
@pytest.mark.parametrize("shots", [None, 100_000, (90_000, 90_000)])
503+
@flaky(max_runs=5)
504+
@pytest.mark.parametrize("shots", [None, 400_000, (400_000, 400_000)])
510505
@pytest.mark.parametrize("measurement", [qml.expval, qml.probs, qml.var])
511506
@pytest.mark.parametrize(
512507
"obs0_",
@@ -557,16 +552,12 @@ def test_double_return_value(self, shots, measurement, obs0_, obs1_, lightning_s
557552
f"Observable of type {type(obs0_).__name__} is not supported for rotating probabilities."
558553
)
559554

560-
rtol = 1.0e-2 # 1% of expected value as tolerance
561555
if shots != None and measurement is qml.expval:
562556
# Increase the number of shots
563557
if isinstance(shots, int):
564-
shots *= 10
558+
shots = 1_000_000
565559
else:
566-
shots = [i * 10 for i in shots]
567-
568-
# Extra tolerance
569-
rtol = 5.0e-2 # 5% of expected value as tolerance
560+
shots = [1_000_000, 1_000_000]
570561

571562
n_qubits = 4
572563
n_layers = 1
@@ -610,14 +601,14 @@ def test_double_return_value(self, shots, measurement, obs0_, obs1_, lightning_s
610601
assert isinstance(result, Sequence)
611602
assert len(result) == len(expected)
612603
# a few tests may fail in single precision, and hence we increase the tolerance
613-
atol = tol if shots is None else max(tol, 1.0e-2)
614-
rtol = max(tol, rtol) # % of expected value as tolerance
604+
dtol = tol if shots is None else max(tol, 1.0e-2)
605+
# TODO Set better atol and rtol
615606
for r, e in zip(result, expected):
616607
if isinstance(shots, tuple) and isinstance(r[0], np.ndarray):
617608
r = np.concatenate(r)
618609
e = np.concatenate(e)
619610
# allclose -> absolute(r - e) <= (atol + rtol * absolute(e))
620-
assert np.allclose(r, e, atol=atol, rtol=rtol)
611+
assert np.allclose(r, e, atol=dtol, rtol=dtol)
621612

622613
@pytest.mark.parametrize(
623614
"cases",

0 commit comments

Comments
 (0)