Repository: PennyLaneAI/pennylane
Pull request #5518 (2024-04-15 20:41:21)

Update device tests to work with Lightning devices
The new tests added to the device test suite weren't totally compatible with lightning devices. I've updated the failing tests to work now. The tensorflow tests failed because our execution pipeline with tensorflow doesn't correctly respect the dtypes, which is a larger issue for us to resolve, so it will be handled in a different PR. For now I've skipped the failing tests.

github-actions[bot]: Hello. You may have forgotten to update the changelog!
Please edit [`doc/releases/changelog-dev.md`](/PennyLaneAI/pennylane/blob/master/doc/releases/changelog-dev.md) with:
* A one-to-two sentence description of the change. You may include a small working example for new features.
* A link back to this PR.
* Your name (or GitHub username) in the contributors section.
mudit2812: [sc-61419]
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/5518?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 99.66%. Comparing base [(`f540e69`)](https://app.codecov.io/gh/PennyLaneAI/pennylane/commit/f540e69d05893ae2fbab5ad9d5a426228aefe613?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) to head [(`94372e5`)](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/5518?dropdown=coverage&src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI).


<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master    #5518      +/-   ##
==========================================
- Coverage   99.67%   99.66%   -0.01%     
==========================================
  Files         404      404              
  Lines       37852    37561     -291     
==========================================
- Hits        37728    37436     -292     
- Misses        124      125       +1     
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/5518?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI).

mudit2812: @albi3ro I've made the suggested changes.

Patch:
diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md
index 5072429d7..faebb6e6b 100644
--- a/doc/releases/changelog-dev.md
+++ b/doc/releases/changelog-dev.md
@@ -250,6 +250,7 @@
 
 * Extend the device test suite to cover gradient methods, templates and arithmetic observables.
   [(#5273)](https://github.com/PennyLaneAI/pennylane/pull/5273)
+  [(#5518)](https://github.com/PennyLaneAI/pennylane/pull/5518)
 
 * Add type hints for unimplemented methods of the abstract class `Operator`.
   [(#5490)](https://github.com/PennyLaneAI/pennylane/pull/5490)


Test patch:
diff --git a/pennylane/devices/tests/conftest.py b/pennylane/devices/tests/conftest.py
index 62d6592bc..d9cef1bf4 100755
--- a/pennylane/devices/tests/conftest.py
+++ b/pennylane/devices/tests/conftest.py
@@ -95,6 +95,11 @@ def validate_diff_method(device, diff_method, device_kwargs):
         passthru_devices = dev.capabilities().get("passthru_devices")
         if diff_method == "backprop" and passthru_devices is None:
             pytest.skip(reason="device does not support backprop")
+        return
+
+    config = qml.devices.ExecutionConfig(gradient_method=diff_method)
+    if not dev.supports_derivatives(execution_config=config):
+        pytest.skip(reason="device does not support diff_method")
 
 
 @pytest.fixture(scope="function", name="device")
diff --git a/pennylane/devices/tests/test_gradients_tf.py b/pennylane/devices/tests/test_gradients_tf.py
index 262106079..7d475500c 100644
--- a/pennylane/devices/tests/test_gradients_tf.py
+++ b/pennylane/devices/tests/test_gradients_tf.py
@@ -152,6 +152,11 @@ class TestGradients:
         wires = 3 if diff_method == "hadamard" else 2
         dev = device(wires=wires)
         tol = tol(dev.shots)
+
+        # TODO: remove the following lines after tensorflow dtype preservation is fixed
+        if "lightning" in getattr(dev, "name", "").lower():
+            pytest.skip("tf interfaces not working correctly with lightning")
+
         x = tf.Variable(0.543)
         y = tf.Variable(-0.654)
 
@@ -197,6 +202,10 @@ class TestGradients:
         dev = device(wires=wires)
         tol = tol(dev.shots)
 
+        # TODO: remove the following lines after tensorflow dtype preservation is fixed
+        if "lightning" in getattr(dev, "name", "").lower():
+            pytest.skip("tf interfaces not working correctly with lightning")
+
         @qml.qnode(dev, diff_method=diff_method, max_diff=2)
         def circuit(x):
             qml.RY(x[0], wires=0)
diff --git a/pennylane/devices/tests/test_measurements.py b/pennylane/devices/tests/test_measurements.py
index 72bedbbc1..5aeb23220 100644
--- a/pennylane/devices/tests/test_measurements.py
+++ b/pennylane/devices/tests/test_measurements.py
@@ -436,7 +436,7 @@ class TestExpval:
 
         res_dq = qml.QNode(circuit, qml.device("default.qubit"))()
         res = qml.QNode(circuit, dev)()
-        assert res.shape == ()
+        assert qml.math.shape(res) == ()
         assert np.isclose(res, res_dq, atol=tol(dev.shots))