Skip to content

Commit

Permalink
Fix failing test_specs test cases for legacy opmath (#5871)
Browse files Browse the repository at this point in the history
**Context:**
Latest failures with legacy opmath:
https://github.com/PennyLaneAI/pennylane/actions/runs/9557831967/job/26346373805

**Description of the Change:**
- Replaces `qml.X(0) + qml.Y(1)` with `qml.sum(qml.X(0), qml.Y(1))`
because the `Sum` here does not have `data` but `Hamiltonian` does (it
treats the two coefficients of `1` and `1` as data), which caused
`test_int_specs_level` to fail at verifying the number of parameters.
- Adds a `__len__` to the legacy `Hamiltonian` such that `assert
len(specs_list) == len(H)` in `test_splitting_transforms` passes
correctly.

[sc-66284]
  • Loading branch information
astralcai authored Jun 18, 2024
1 parent 518bcc3 commit 37bc2e6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
4 changes: 4 additions & 0 deletions pennylane/ops/qubit/hamiltonian.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@ def __init__(
super().__init__(*coeffs_flat, wires=self._wires, id=id)
self._pauli_rep = "unset"

def __len__(self):
"""The number of terms in the Hamiltonian."""
return len(self.ops)

@property
def pauli_rep(self):
if self._pauli_rep != "unset":
Expand Down
2 changes: 1 addition & 1 deletion tests/resource/test_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def circuit(x):
qml.SWAP((0, 1))
qml.X(0)
qml.X(0)
return qml.expval(qml.X(0) + qml.Y(1))
return qml.expval(qml.sum(qml.X(0), qml.Y(1)))

return circuit

Expand Down

0 comments on commit 37bc2e6

Please sign in to comment.