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

Fix wires object handling in Sum.terms method #6750

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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 @@ -616,6 +616,9 @@ same information.

<h3>Bug fixes 🐛</h3>

* `Identity` operator wires are properly tracked when using `qml.Sum.terms`.
[(#6750)](https://github.com/PennyLaneAI/pennylane/pull/6750)

* `qml.ControlledQubitUnitary` has consistent behaviour with program capture enabled.
[(#6719)](https://github.com/PennyLaneAI/pennylane/pull/6719)

Expand Down
2 changes: 1 addition & 1 deletion pennylane/ops/op_math/sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ def terms(self):
# try using pauli_rep:
if pr := self.pauli_rep:
with qml.QueuingManager.stop_recording():
ops = [pauli.operation() for pauli in pr.keys()]
ops = [pauli.operation(self.wires) for pauli in pr.keys()]
return list(pr.values()), ops

with qml.QueuingManager.stop_recording():
Expand Down
11 changes: 11 additions & 0 deletions tests/ops/op_math/test_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,17 @@ def test_terms_does_not_change_queue(self, op, coeffs_true, ops_true):

assert q.queue == [op]

def test_identity_wires_are_preserved(self):
"""Test that Identity wires are preserved after calling Sum.terms method."""
hamiltonian = qml.dot([1, 0.5], [qml.Identity(wires=[0, 1]), qml.PauliZ(0)])
coeffs, operators = hamiltonian.terms()
assert coeffs == [1, 0.5]
op1, op2 = operators
assert op1.name == "Identity"
assert op2.name == "PauliZ"
assert op1.wires == Wires([0, 1])
assert op2.wires == Wires([0])

def test_eigen_caching(self):
"""Test that the eigendecomposition is stored in cache."""
diag_sum_op = Sum(qml.PauliZ(wires=0), qml.Identity(wires=1))
Expand Down
10 changes: 5 additions & 5 deletions tests/qchem/test_factorization.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,14 +314,14 @@ def test_empty_error(two_tensor):
[ # computed manually
[
qml.PauliZ(wires=[0]),
qml.Identity(wires=[0]),
qml.Identity(wires=[0, 1, 2, 3]),
qml.PauliZ(wires=[1]),
qml.PauliZ(wires=[2]),
qml.PauliZ(wires=[3]),
],
[
qml.PauliZ(wires=[0]),
qml.Identity(wires=[0]),
qml.Identity(wires=[0, 1, 2, 3]),
qml.PauliZ(wires=[1]),
qml.PauliZ(wires=[0]) @ qml.PauliZ(wires=[1]),
qml.PauliZ(wires=[2]),
Expand All @@ -338,12 +338,12 @@ def test_empty_error(two_tensor):
qml.PauliZ(wires=[0]) @ qml.PauliZ(wires=[3]),
qml.PauliZ(wires=[1]) @ qml.PauliZ(wires=[2]),
qml.PauliZ(wires=[1]) @ qml.PauliZ(wires=[3]),
qml.Identity(wires=[2]),
qml.Identity(wires=[0, 1, 2, 3]),
qml.PauliZ(wires=[2]) @ qml.PauliZ(wires=[3]),
],
[
qml.PauliZ(wires=[0]),
qml.Identity(wires=[0]),
qml.Identity(wires=[0, 1, 2, 3]),
qml.PauliZ(wires=[1]),
qml.PauliZ(wires=[0]) @ qml.PauliZ(wires=[1]),
qml.PauliZ(wires=[2]),
Expand Down Expand Up @@ -392,12 +392,12 @@ def test_empty_error(two_tensor):
),
],
)
# pylint: disable = too-many-positional-arguments
def test_basis_rotation_output(
one_matrix, two_tensor, tol_factor, coeffs_ref, ops_ref, eigvecs_ref
):
r"""Test that basis_rotation function returns the correct values."""
coeffs, ops, eigvecs = qml.qchem.basis_rotation(one_matrix, two_tensor, tol_factor)

for i, coeff in enumerate(coeffs):
assert np.allclose(np.sort(coeff), np.sort(coeffs_ref[i]))

Expand Down
Loading