From 2730c10770b2acb0f087243fd39e81214ea23c3b Mon Sep 17 00:00:00 2001 From: andrijapau Date: Thu, 2 Jan 2025 13:50:26 -0500 Subject: [PATCH 1/4] fix: Update sum.py --- pennylane/ops/op_math/sum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pennylane/ops/op_math/sum.py b/pennylane/ops/op_math/sum.py index 641a08fe4cd..9a0d2bb1c78 100644 --- a/pennylane/ops/op_math/sum.py +++ b/pennylane/ops/op_math/sum.py @@ -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(): From 548e87a07a219613d3d277f61c5427d0b1572a59 Mon Sep 17 00:00:00 2001 From: andrijapau Date: Thu, 2 Jan 2025 13:50:47 -0500 Subject: [PATCH 2/4] fix: Update test_sum.py with previously failing test --- tests/ops/op_math/test_sum.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/ops/op_math/test_sum.py b/tests/ops/op_math/test_sum.py index f6c84ba2bff..477654cfbc9 100644 --- a/tests/ops/op_math/test_sum.py +++ b/tests/ops/op_math/test_sum.py @@ -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)) From 9bdbc2d95ee9eb5d16f9f489c0c7d634fddbb131 Mon Sep 17 00:00:00 2001 From: andrijapau Date: Thu, 2 Jan 2025 13:56:27 -0500 Subject: [PATCH 3/4] doc: Update changelog-dev.md --- doc/releases/changelog-dev.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index 3680c3dd6ea..f1d62e7489c 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -616,6 +616,9 @@ same information.

Bug fixes 🐛

+* `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) From c8d683883a711e7f35e29a3de688cf808936c864 Mon Sep 17 00:00:00 2001 From: andrijapau Date: Thu, 2 Jan 2025 14:12:53 -0500 Subject: [PATCH 4/4] fix: update test_factorization test cases to be more accurate --- tests/qchem/test_factorization.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/qchem/test_factorization.py b/tests/qchem/test_factorization.py index 42214f830e3..3ff94be277b 100644 --- a/tests/qchem/test_factorization.py +++ b/tests/qchem/test_factorization.py @@ -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]), @@ -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]), @@ -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]))