Skip to content

Commit

Permalink
add test and fix typo
Browse files Browse the repository at this point in the history
  • Loading branch information
astralcai committed Jun 19, 2024
1 parent 3238db6 commit fd6c5ed
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pennylane/ops/functions/equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ def _equal_tensor(op1: Tensor, op2: Observable, **kwargs):
return f"{op2} is not of type Observable"

if isinstance(op2, (Hamiltonian, LinearCombination, Hermitian)):
return op2.compare(op1) or f"{op1} and {op2} are not same"
return op2.compare(op1) or f"{op1} and {op2} are not the same"

if isinstance(op2, Tensor):
return (
Expand All @@ -636,7 +636,7 @@ def _equal_hamiltonian(op1: Hamiltonian, op2: Observable, **kwargs):
return f"{op2} is not of type Observable"

if not op1.compare(op2):
return f"'{op1}' and '{op2}' are not same"
return f"'{op1}' and '{op2}' are not the same"

return True

Expand Down
9 changes: 9 additions & 0 deletions tests/ops/functions/test_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,15 @@ def test_tensor_and_operation_not_equal(self):
with pytest.raises(AssertionError, match="is not of type Observable"):
assert_equal(op1, op2)

def test_tensor_and_observable_not_equal(self):
"""Tests that comparing a Tensor with an Observable that is not a Tensor returns False"""
op1 = qml.PauliX(0) @ qml.PauliY(1)
op2 = qml.Z(0)
assert qml.equal(op1, op2) is False
assert qml.equal(op2, op1) is False
with pytest.raises(AssertionError, match="is of type <class 'pennylane.operation.Tensor'>"):
assert_equal(op1, op2)

def test_tensor_and_unsupported_observable_returns_false(self):
"""Tests that trying to compare a Tensor to something other than another Tensor or a Hamiltonian returns False"""
op1 = qml.PauliX(0) @ qml.PauliY(1)
Expand Down

0 comments on commit fd6c5ed

Please sign in to comment.