Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dequeue ops in Hamiltonian.__matmul__ #5455

Merged
merged 2 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@

<h3>Bug fixes 🐛</h3>

* Using `@` with legacy Hamiltonian instances now properly de-queues the previously existing operations.
albi3ro marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens in the new-op-arithmetic setting?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Things are properly de-queued.

[(#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)

Expand Down
3 changes: 3 additions & 0 deletions pennylane/ops/qubit/hamiltonian.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions tests/ops/qubit/test_hamiltonian.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading