Repository: PennyLaneAI/pennylane (python)

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.

Pull request description:
dequeue ops in `Hamiltonian.__matmul__`
**Context:**

A user was encountering issues where operations were remaining in the circuit after being used to construct a hamiltonian.

**Description of the Change:**

De-queue the arguments to `Hamiltonian.__matmul__`

**Benefits:**

No more things in the circuit that shouldn't be in the circuit.

**Possible Drawbacks:**

**Related GitHub Issues:**

Fixes #5454 [sc-60158]

Patch:
diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md
index 621b094b7..5356fa8a7 100644
--- a/doc/releases/changelog-dev.md
+++ b/doc/releases/changelog-dev.md
@@ -291,6 +291,9 @@
 
 <h3>Bug fixes 🐛</h3>
 
+* Using `@` with legacy Hamiltonian instances now properly de-queues the previously existing operations.
+  [(#5454)](https://github.com/PennyLaneAI/pennylane/pull/5455)
+
 * The `QNSPSAOptimizer` now properly handles differentiable parameters, resulting in being able to use it for more than one optimization step.
   [(#5439)](https://github.com/PennyLaneAI/pennylane/pull/5439)
 
diff --git a/pennylane/ops/qubit/hamiltonian.py b/pennylane/ops/qubit/hamiltonian.py
index 2f6410de2..17c6de252 100644
--- a/pennylane/ops/qubit/hamiltonian.py
+++ b/pennylane/ops/qubit/hamiltonian.py
@@ -662,6 +662,9 @@ class Hamiltonian(Observable):
         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
index 0a3ab5104..c88b1978a 100644
--- 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