Repository: PennyLaneAI/pennylane
Pull request #5121 (2024-01-29 22:42:14)

Added abstract `ErrorOperation` class
**Context:**
Another PR adding error propagation functionality to PennyLane.

**Description of the Change:**
Add the abstract `ErrorOperation` class.

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.
Jaybsoni: [sc-55439]
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/5121?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:
> :exclamation: No coverage uploaded for pull request base (`algo_error@932dd21`). [Click here to learn what that means](https://docs.codecov.io/docs/error-reference?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI#section-missing-base-commit).


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


```diff
@@              Coverage Diff              @@
##             algo_error    #5121   +/-   ##
=============================================
  Coverage              ?   99.68%           
=============================================
  Files                 ?      395           
  Lines                 ?    35770           
  Branches              ?        0           
=============================================
  Hits                  ?    35657           
  Misses                ?      113           
  Partials              ?        0           
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/5121?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).

AmintorDusko: Hey @Jaybsoni, we have a test failing here. Please retag me when CIs are successful. Or let me know if you have any help.
codecov-commenter: ## [Codecov](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/5121?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) Report
Attention: `1 lines` in your changes are missing coverage. Please review.
> Comparison is base [(`86cbaa6`)](https://app.codecov.io/gh/PennyLaneAI/pennylane/commit/86cbaa6db77f1cbc153050a144923c4292c36ba1?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) 99.67% compared to head [(`ab813f0`)](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/5121?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) 99.67%.
> Report is 2 commits behind head on algo_error.

| [Files](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/5121?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) | Patch % | Lines |
|---|---|---|
| [pennylane/resource/error.py](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/5121?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI#diff-cGVubnlsYW5lL3Jlc291cmNlL2Vycm9yLnB5) | 83.33% | [1 Missing :warning: ](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/5121?src=pr&el=tree&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               @@
##           algo_error    #5121      +/-   ##
==============================================
- Coverage       99.67%   99.67%   -0.01%     
==============================================
  Files             395      395              
  Lines           35900    35906       +6     
==============================================
+ Hits            35785    35790       +5     
- Misses            115      116       +1     
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/5121?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).


Patch:
diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md
index 7aeec9a36..b60ab27f6 100644
--- a/doc/releases/changelog-dev.md
+++ b/doc/releases/changelog-dev.md
@@ -126,6 +126,8 @@
 
 * Added new error tracking and propagation functionality. 
   [(#5115)](https://github.com/PennyLaneAI/pennylane/pull/5115)
+  [(#5121)](https://github.com/PennyLaneAI/pennylane/pull/5121)
+
 
 <h3>Improvements 🛠</h3>
 
diff --git a/pennylane/resource/__init__.py b/pennylane/resource/__init__.py
index d0a061cf7..0899bf8d0 100644
--- a/pennylane/resource/__init__.py
+++ b/pennylane/resource/__init__.py
@@ -58,6 +58,7 @@ Error Tracking
     :toctree: api
 
     ~AlgorithmicError
+    ~ErrorOperation
 
 Resource Classes
 ----------------
@@ -122,4 +123,4 @@ from .first_quantization import FirstQuantization
 from .second_quantization import DoubleFactorization
 from .measurement import estimate_error, estimate_shots
 from .specs import specs
-from .error import AlgorithmicError
+from .error import AlgorithmicError, ErrorOperation
diff --git a/pennylane/resource/error.py b/pennylane/resource/error.py
index c2f9c1110..34a434e0e 100644
--- a/pennylane/resource/error.py
+++ b/pennylane/resource/error.py
@@ -16,6 +16,8 @@ Stores classes and logic to define and track algorithmic error in a quantum work
 """
 from abc import ABC, abstractmethod
 
+from pennylane.operation import Operation
+
 
 class AlgorithmicError(ABC):
     """Abstract base class representing an abstract type of error.
@@ -57,3 +59,21 @@ class AlgorithmicError(ABC):
             approximation.
         """
         raise NotImplementedError
+
+
+class ErrorOperation(Operation):
+    r"""Base class that represents quantum operations which carry some form of algorithmic error.
+
+    .. note::
+        Child classes must implement the :func:`~.ErrorOperation.error` method which computes
+        the error of the operation.
+    """
+
+    @property
+    @abstractmethod
+    def error(self) -> AlgorithmicError:
+        """Computes the error of the operation.
+
+        Returns:
+            AlgorithmicError: The error.
+        """


Test patch:
diff --git a/tests/ops/functions/conftest.py b/tests/ops/functions/conftest.py
index c4617301e..8b8819478 100644
--- a/tests/ops/functions/conftest.py
+++ b/tests/ops/functions/conftest.py
@@ -125,6 +125,7 @@ _ABSTRACT_OR_META_TYPES = {
     qml.ops.qubit.StateVectorProjector,
     qml.ops.qubit.StatePrepBase,
     qml.resource.ResourcesOperation,
+    qml.resource.ErrorOperation,
     PowOpObs,
     PowOperation,
     PowObs,
diff --git a/tests/resource/test_error.py b/tests/resource/test_error.py
index 0d19c481b..0e17bcd0e 100644
--- a/tests/resource/test_error.py
+++ b/tests/resource/test_error.py
@@ -17,7 +17,7 @@ Test base AlgorithmicError class and its associated methods.
 import pytest
 
 import pennylane as qml
-from pennylane.resource.error import AlgorithmicError
+from pennylane.resource.error import AlgorithmicError, ErrorOperation
 
 
 class SimpleError(AlgorithmicError):
@@ -45,7 +45,6 @@ class TestAlgorithmicError:
 
     def test_combine_not_implemented(self):
         """Test can't instantiate Error if the combine method is not defined."""
-
         with pytest.raises(TypeError, match="Can't instantiate abstract class"):
 
             class ErrorNoCombine(AlgorithmicError):  # pylint: disable=too-few-public-methods
@@ -81,3 +80,27 @@ class TestAlgorithmicError:
 
         res = SimpleError.get_error(approx_op, exact_op)
         assert res == 0.5
+
+
+class TestErrorOperation:  # pylint: disable=too-few-public-methods
+    """Test the base ErrorOperation class."""
+
+    def test_error_method(self):
+        """Test that error method works as expected"""
+
+        class SimpleErrorOperation(ErrorOperation):  # pylint: disable=too-few-public-methods
+            @property
+            def error(self):
+                return len(self.wires)
+
+        no_error_op = SimpleErrorOperation(wires=[1, 2, 3])
+        assert no_error_op.error == 3
+
+    def test_no_error_method(self):
+        """Test error is raised if the error method is not defined."""
+        with pytest.raises(TypeError, match="Can't instantiate abstract class"):
+
+            class NoErrorOp(ErrorOperation):  # pylint: disable=too-few-public-methods
+                num_wires = 3
+
+            _ = NoErrorOp(wires=[1, 2, 3])