Repository: PennyLaneAI/pennylane

Issue:
 [BUG] Hamiltonians defined using operator arithmetic queuing extra gates
### Expected behavior

When defining

```
dev = qml.device('default.qubit', wires = [0,1])

def H(alpha, beta):
  coeffs = [alpha, beta]
  ops = [qml.PauliX(0) @ qml.PauliX(1),qml.PauliZ(0) @ qml.PauliZ(1)]
  return qml.dot(coeffs,ops)

def H2(alpha, beta):
  return alpha * qml.PauliX(0) @ qml.PauliX(1) + beta * qml.PauliZ(0) @ qml.PauliZ(1)

@qml.qnode(dev)
def Ham_evolve(alpha, beta, time):

    qml.evolve(H(alpha,beta), coeff = time)

    return qml.probs(wires = [0,1])
  
@qml.qnode(dev)
def Ham_evolve2(alpha, beta, time):

    qml.evolve(H2(alpha,beta), coeff = time)

    return qml.probs(wires = [0,1])
```
    
 I would expect `Ham_evolve` and `Ham_evolve2` to build the same circuit. 

### Actual behavior

Drawing `print(qml.draw(Ham_evolve, expansion_strategy="device")(0.9,1.0,0.4))` gives the correct circuit, since the Hamiltonian was defined with `qml.dot`.

![image](https://github.com/PennyLaneAI/pennylane/assets/91897656/8fd42422-513e-4831-9204-0dde9c7dc334)

Drawing `print(qml.draw(Ham_evolve2, expansion_strategy="device")(0.9,1.0,0.4))` queues extra gates.

![image](https://github.com/PennyLaneAI/pennylane/assets/91897656/c692c008-d38f-476f-b1a6-a190ea628fbe)

This can be fixed by running `qml.operation.enable_new_opmath()` but it does seem like an extra step users wouldn't be aware of.

### Additional information

Happens all the time with version `0.35.1`, as long as new_opmath is disabled. 

### Source code

```shell
Working examples are above.
```


### Tracebacks

```shell
No error tracebacks since it's a queuing issue.
```


### System information

```shell
Name: PennyLane
Version: 0.35.1
Summary: PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
Home-page: https://github.com/PennyLaneAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /usr/local/lib/python3.10/dist-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: PennyLane_Lightning

Platform info:           Linux-6.1.58+-x86_64-with-glibc2.35
Python version:          3.10.12
Numpy version:           1.25.2
Scipy version:           1.11.4
Installed devices:
- lightning.qubit (PennyLane_Lightning-0.35.1)
- default.clifford (PennyLane-0.35.1)
- default.gaussian (PennyLane-0.35.1)
- default.mixed (PennyLane-0.35.1)
- default.qubit (PennyLane-0.35.1)
- default.qubit.autograd (PennyLane-0.35.1)
- default.qubit.jax (PennyLane-0.35.1)
- default.qubit.legacy (PennyLane-0.35.1)
- default.qubit.tf (PennyLane-0.35.1)
- default.qubit.torch (PennyLane-0.35.1)
- default.qutrit (PennyLane-0.35.1)
- null.qubit (PennyLane-0.35.1)
```


### Existing GitHub issues

- [X] I have searched existing GitHub issues to make sure the issue does not already exist.


Discussion:

Resolving patch:
diff --git a/pennylane/ops/qubit/hamiltonian.py b/pennylane/ops/qubit/hamiltonian.py
--- a/pennylane/ops/qubit/hamiltonian.py
+++ b/pennylane/ops/qubit/hamiltonian.py
@@ -662,6 +662,9 @@ def __matmul__(self, H):
         coeffs1 = copy(self.coeffs)
         ops1 = self.ops.copy()
 
+        qml.QueuingManager.remove(H)
+        qml.QueuingManager.remove(self)
+
         if isinstance(H, Hamiltonian):
             shared_wires = Wires.shared_wires([self.wires, H.wires])
             if len(shared_wires) > 0:


Test patch:
diff --git a/tests/ops/qubit/test_hamiltonian.py b/tests/ops/qubit/test_hamiltonian.py
--- a/tests/ops/qubit/test_hamiltonian.py
+++ b/tests/ops/qubit/test_hamiltonian.py
@@ -678,6 +678,17 @@ def circuit2(param):
 dev = qml.device("default.qubit", wires=2)
 
 
+@pytest.mark.usefixtures("use_legacy_and_new_opmath")
+def test_matmul_queuing():
+    """Test that the other and self are removed during Hamiltonian.__matmul__ ."""
+
+    with qml.queuing.AnnotatedQueue() as q:
+        H = 0.5 * qml.X(0) @ qml.Y(1)
+
+    assert len(q) == 1
+    assert q.queue[0] is H
+
+
 @pytest.mark.usefixtures("use_legacy_and_new_opmath")
 def test_deprecation_with_new_opmath(recwarn):
     """Test that a warning is raised if attempting to create a Hamiltonian with new operator