Repository: PennyLaneAI/pennylane
Pull request #3828 (2023-02-24 20:30:28)

Fix hadamard grad
**Context:**

Hadamard grad returns wrong results when we have multiple operations generating multiple tapes like (CR).

**Description of the Change:**

Update the idx correctly.


codecov[bot]: # [Codecov](https://codecov.io/gh/PennyLaneAI/pennylane/pull/3828?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) Report
> :exclamation: No coverage uploaded for pull request base (`v0.29.0-rc0@7bc7d7e`). [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).
> The diff coverage is `n/a`.

```diff
@@              Coverage Diff               @@
##             v0.29.0-rc0    #3828   +/-   ##
==============================================
  Coverage               ?   99.72%           
==============================================
  Files                  ?      336           
  Lines                  ?    29464           
  Branches               ?        0           
==============================================
  Hits                   ?    29384           
  Misses                 ?       80           
  Partials               ?        0           
```



Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI)


Patch:
diff --git a/pennylane/gradients/hadamard_gradient.py b/pennylane/gradients/hadamard_gradient.py
index fd31f219f..8c04e94c9 100644
--- a/pennylane/gradients/hadamard_gradient.py
+++ b/pennylane/gradients/hadamard_gradient.py
@@ -354,7 +354,7 @@ def _expval_hadamard_grad(tape, argnum, aux_wire):
             else:
                 axis = None if not multi_measurements else 0
                 grads.append(qml.math.sum(final_res[idx : idx + num_tape], axis=axis))
-                idx += 1
+                idx += num_tape
 
         if not multi_measurements and not multi_params:
             return grads[0]


Test patch:
diff --git a/tests/returntypes/gradients/test_hadamard_grad.py b/tests/returntypes/gradients/test_hadamard_grad.py
index 70f8e3cbf..cc17ab257 100644
--- a/tests/returntypes/gradients/test_hadamard_grad.py
+++ b/tests/returntypes/gradients/test_hadamard_grad.py
@@ -143,6 +143,22 @@ class TestHadamardGrad:
 
         assert np.allclose(res_hadamard, expected, atol=tol, rtol=0)
 
+    def test_control_rotations(self, tol):
+        """Test RX, CRX and CRY."""
+        dev = qml.device("default.qubit", wires=4)
+
+        with qml.queuing.AnnotatedQueue() as q:
+            qml.RX(0.1, wires=0)
+            qml.CRY(0.2, wires=[2, 0])
+            qml.CRX(0.3, wires=[0, 1])
+            qml.expval(qml.PauliX(0))
+
+        tape = qml.tape.QuantumScript.from_queue(q)
+        tape.trainable_params = {0, 1, 2}
+
+        res_hadamard, _ = grad_fn(tape, dev)
+        assert np.allclose(res_hadamard, np.zeros(3), atol=tol, rtol=0)
+
     @pytest.mark.parametrize("theta", np.linspace(-2 * np.pi, 2 * np.pi, 7))
     @pytest.mark.parametrize("G", [qml.CRX, qml.CRY, qml.CRZ])
     def test_controlled_rotation_gradient_multi(self, G, theta, tol):