Repository: PennyLaneAI/pennylane
Pull request #5797 (2024-06-04 19:15:43)

fix CNOT and Toffoli arithmetic depth
**Context:**

As reported by @obliviateandsurrender , `CNOT` and `Toffoli` had an arithmetic depth of `0`, but it should be `1` because they are controlled operations.

**Description of the Change:**

**Benefits:**

**Possible Drawbacks:**

**Related GitHub Issues:**


codecov[bot]: ## [Codecov](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/5797?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.67%. Comparing base [(`c14ce7b`)](https://app.codecov.io/gh/PennyLaneAI/pennylane/commit/c14ce7bc18b7c38e5246f2a5451ffb3cd0d10077?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) to head [(`b9d1c98`)](https://app.codecov.io/gh/PennyLaneAI/pennylane/commit/b9d1c98f4f96c2889416dd737951194f26788d33?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI).
> Report is 4 commits behind head on master.


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


```diff
@@            Coverage Diff             @@
##           master    #5797      +/-   ##
==========================================
- Coverage   99.67%   99.67%   -0.01%     
==========================================
  Files         414      414              
  Lines       39316    39165     -151     
==========================================
- Hits        39188    39036     -152     
- Misses        128      129       +1     
```



</details>

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


Patch:
diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md
index 485afe496..16d0483df 100644
--- a/doc/releases/changelog-dev.md
+++ b/doc/releases/changelog-dev.md
@@ -288,6 +288,9 @@
 * `qml.matrix` is now compatible with qnodes compiled by catalyst.qjit.
   [(#5753)](https://github.com/PennyLaneAI/pennylane/pull/5753)
 
+* `CNOT` and `Toffoli` now have an `arithmetic_depth` of `1`, as they are controlled operations.
+  [(#5797)](https://github.com/PennyLaneAI/pennylane/pull/5797)
+
 <h3>Contributors ✍️</h3>
 
 This release contains contributions from (in alphabetical order):
diff --git a/pennylane/ops/op_math/adjoint.py b/pennylane/ops/op_math/adjoint.py
index e40cac4e5..dd614b98b 100644
--- a/pennylane/ops/op_math/adjoint.py
+++ b/pennylane/ops/op_math/adjoint.py
@@ -296,7 +296,11 @@ class Adjoint(SymbolicOp):
 
     def label(self, decimals=None, base_label=None, cache=None):
         base_label = self.base.label(decimals, base_label, cache=cache)
-        return f"({base_label})†" if self.base.arithmetic_depth > 0 else f"{base_label}†"
+        return (
+            f"({base_label})†"
+            if self.base.arithmetic_depth > 0 and len(base_label) > 1
+            else f"{base_label}†"
+        )
 
     def matrix(self, wire_order=None):
         if isinstance(self.base, qml.ops.Hamiltonian):
diff --git a/pennylane/ops/op_math/controlled_ops.py b/pennylane/ops/op_math/controlled_ops.py
index 3d3af4373..34b13535d 100644
--- a/pennylane/ops/op_math/controlled_ops.py
+++ b/pennylane/ops/op_math/controlled_ops.py
@@ -777,8 +777,6 @@ class CNOT(ControlledOp):
     ndim_params = ()
     """tuple[int]: Number of dimensions per trainable parameter that the operator depends on."""
 
-    arithmetic_depth = 0
-
     name = "CNOT"
 
     def _flatten(self):
@@ -863,8 +861,6 @@ class Toffoli(ControlledOp):
     ndim_params = ()
     """tuple[int]: Number of dimensions per trainable parameter that the operator depends on."""
 
-    arithmetic_depth = 0
-
     name = "Toffoli"
 
     def _flatten(self):


Test patch:
diff --git a/tests/ops/functions/test_matrix.py b/tests/ops/functions/test_matrix.py
index e199032c2..d1b61487a 100644
--- a/tests/ops/functions/test_matrix.py
+++ b/tests/ops/functions/test_matrix.py
@@ -664,7 +664,7 @@ class TestInterfaces:
     def test_catalyst(self):
         """Test with Catalyst interface"""
 
-        import catalyst
+        catalyst = pytest.importorskip("catalyst")
 
         dev = qml.device("lightning.qubit", wires=1)
 
diff --git a/tests/ops/op_math/test_composite.py b/tests/ops/op_math/test_composite.py
index fe592107f..6512bc6e5 100644
--- a/tests/ops/op_math/test_composite.py
+++ b/tests/ops/op_math/test_composite.py
@@ -37,8 +37,8 @@ ops = (
 
 ops_rep = (
     "X(0) # Z(0) # Hadamard(wires=[0])",
-    "CNOT(wires=[0, 1]) # RX(1.23, wires=[1]) # I(0)",
-    "IsingXX(4.56, wires=[2, 3]) # Toffoli(wires=[1, 2, 3]) # Rot(0.34, 1.0, 0, wires=[0])",
+    "(CNOT(wires=[0, 1])) # RX(1.23, wires=[1]) # I(0)",
+    "IsingXX(4.56, wires=[2, 3]) # (Toffoli(wires=[1, 2, 3])) # Rot(0.34, 1.0, 0, wires=[0])",
 )