Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
astralcai committed Sep 20, 2024
1 parent b1b7ae2 commit d8f389b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
18 changes: 13 additions & 5 deletions pennylane/ops/functions/assert_valid.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,20 @@ def circuit(*args):
circuit, device=qml.device("reference.qubit"), diff_method="parameter-shift"
)

ps = qml.jacobian(qnode_ps)(*(qml.numpy.array(x) for x in data))
expected_ps = qml.jacobian(qnode_ref)(*(qml.numpy.array(x) for x in data))
params = [x if isinstance(x, int) else qml.numpy.array(x) for x in data]

assert qml.math.allclose(
ps, expected_ps
), "Parameter shift does not produce the expected Jacobian with this operator."
ps = qml.jacobian(qnode_ps)(*params)
expected_ps = qml.jacobian(qnode_ref)(*params)

if isinstance(ps, tuple):
for actual, expected in zip(ps, expected_ps):
assert qml.math.allclose(
actual, expected
), "Backpropagation does not produce the expected Jacobian with this operator."
else:
assert qml.math.allclose(
ps, expected_ps
), "Parameter shift does not produce the expected Jacobian with this operator."


def _check_wires(op, skip_wire_mapping):
Expand Down
3 changes: 2 additions & 1 deletion pennylane/ops/op_math/prod.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ def circuit(weights):

_op_symbol = "@"
_math_op = math.prod
grad_method = None

@property
def is_hermitian(self):
Expand Down Expand Up @@ -359,7 +360,7 @@ def arithmetic_depth(self) -> int:
def _build_pauli_rep(self):
"""PauliSentence representation of the Product of operations."""
if all(operand_pauli_reps := [op.pauli_rep for op in self.operands]):
return reduce(lambda a, b: a @ b, operand_pauli_reps)
return reduce(lambda a, b: a @ b, operand_pauli_reps) if operand_pauli_reps else None
return None

def _simplify_factors(self, factors: tuple[Operator]) -> tuple[complex, Operator]:
Expand Down

0 comments on commit d8f389b

Please sign in to comment.