Skip to content

Commit

Permalink
Fix error raised when taking dot of a single Tensor (#5651)
Browse files Browse the repository at this point in the history
When `ops` is a `Tensor`, this line:
```
if len(coeffs) != len(ops)
```
raises an error because `Tensor` does not have `len`. Moving the
`isinstance` checks for `ops` to the beginning of the function.
  • Loading branch information
astralcai committed May 6, 2024
1 parent 2522e58 commit 4879a61
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions pennylane/ops/functions/dot.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,17 @@ def dot(
:ref:`Pauli Graph Colouring<graph_colouring>` and :func:`~pennylane.pauli.group_observables`.
"""

if len(coeffs) != len(ops):
raise ValueError("Number of coefficients and operators does not match.")
if len(coeffs) == 0 and len(ops) == 0:
raise ValueError("Cannot compute the dot product of an empty sequence.")

for t in (Operator, PauliWord, PauliSentence):
if isinstance(ops, t):
raise ValueError(
f"ops must be an Iterable of {t.__name__}'s, not a {t.__name__} itself."
)

if len(coeffs) != len(ops):
raise ValueError("Number of coefficients and operators does not match.")
if len(coeffs) == 0 and len(ops) == 0:
raise ValueError("Cannot compute the dot product of an empty sequence.")

if any(callable(c) for c in coeffs):
return ParametrizedHamiltonian(coeffs, ops)

Expand Down

0 comments on commit 4879a61

Please sign in to comment.