Qml.grad throws error when complex_dtype is np.complex64

Minimal reproducer:

import pennylane as qml
from pennylane import numpy as np

complex_dtype = np.complex64
real_dtype = np.float32

n = 4
wires = range(n)
n_params = n


dev_gpu = qml.device("lightning.qubit", wires=n, c_dtype=complex_dtype)
@qml.qnode(dev_gpu, diff_method="adjoint", device_vjp=False)
def full_circuit(params):
    for i in range(n):
        qml.RX(params[0], wires=i)
        qml.SX(wires=i)
    return qml.expval(qml.Z(2))


params = np.random.rand(n_params, requires_grad=True).astype(real_dtype)
qml.grad(full_circuit)(params)

Error message:

Traceback (most recent call last):
  File "/a.py", line 24, in <module>
    qml.grad(full_circuit)(params)
  File "/venv/lib/python3.12/site-packages/pennylane/_grad.py", line 166, in __call__
    grad_value, ans = grad_fn(*args, **kwargs)  # pylint: disable=not-callable
                      ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/venv/lib/python3.12/site-packages/autograd/wrap_util.py", line 20, in nary_f
    return unary_operator(unary_f, x, *nary_op_args, **nary_op_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/venv/lib/python3.12/site-packages/pennylane/_grad.py", line 184, in _grad_with_forward
    vjp, ans = _make_vjp(fun, x)  # pylint: disable=redefined-outer-name
               ^^^^^^^^^^^^^^^^^
  File "/venv/lib/python3.12/site-packages/autograd/core.py", line 10, in make_vjp
    end_value, end_node =  trace(start_node, fun, x)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/venv/lib/python3.12/site-packages/autograd/tracer.py", line 10, in trace
    end_box = fun(start_box)
              ^^^^^^^^^^^^^^
  File "/venv/lib/python3.12/site-packages/autograd/wrap_util.py", line 15, in unary_f
    return fun(*subargs, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/venv/lib/python3.12/site-packages/pennylane/workflow/qnode.py", line 1020, in __call__
    return self._impl_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/venv/lib/python3.12/site-packages/pennylane/workflow/qnode.py", line 1008, in _impl_call
    res = self._execution_component(args, kwargs, override_shots=override_shots)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/venv/lib/python3.12/site-packages/pennylane/workflow/qnode.py", line 957, in _execution_component
    res = qml.execute(
          ^^^^^^^^^^^^
  File "/venv/lib/python3.12/site-packages/pennylane/workflow/execution.py", line 771, in execute
    results = ml_boundary_execute(tapes, execute_fn, jpc, device=device)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/venv/lib/python3.12/site-packages/pennylane/workflow/interfaces/autograd.py", line 147, in autograd_execute
    return _execute(parameters, tuple(tapes), execute_fn, jpc)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/venv/lib/python3.12/site-packages/autograd/tracer.py", line 44, in f_wrapped
    ans = f_wrapped(*argvals, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/venv/lib/python3.12/site-packages/autograd/tracer.py", line 48, in f_wrapped
    return f_raw(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/venv/lib/python3.12/site-packages/pennylane/workflow/interfaces/autograd.py", line 168, in _execute
    return execute_fn(tapes)
           ^^^^^^^^^^^^^^^^^
  File "/venv/lib/python3.12/site-packages/pennylane/workflow/jacobian_products.py", line 462, in execute_and_cache_jacobian
    results, jac = self._dev_execute_and_compute_derivatives(tapes)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/venv/lib/python3.12/site-packages/pennylane/workflow/jacobian_products.py", line 427, in _dev_execute_and_compute_derivatives
    return self._device.execute_and_compute_derivatives(numpy_tapes, self._execution_config)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/venv/lib/python3.12/site-packages/pennylane/devices/modifiers/simulator_tracking.py", line 97, in execute_and_compute_derivatives
    return untracked_execute_and_compute_derivatives(self, circuits, execution_config)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/venv/lib/python3.12/site-packages/pennylane/devices/modifiers/single_tape_support.py", line 62, in execute_and_compute_derivatives
    results, jacs = batch_execute_and_compute_derivatives(self, circuits, execution_config)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/venv/lib/python3.12/site-packages/pennylane_lightning/lightning_qubit/lightning_qubit.py", line 713, in execute_and_compute_derivatives
    results = tuple(
              ^^^^^^
  File "/venv/lib/python3.12/site-packages/pennylane_lightning/lightning_qubit/lightning_qubit.py", line 714, in <genexpr>
    simulate_and_jacobian(
  File "/venv/lib/python3.12/site-packages/pennylane_lightning/lightning_qubit/lightning_qubit.py", line 155, in simulate_and_jacobian
    jac = LightningAdjointJacobian(state, batch_obs=batch_obs).calculate_jacobian(circuit)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/venv/lib/python3.12/site-packages/pennylane_lightning/lightning_qubit/_adjoint_jacobian.py", line 217, in calculate_jacobian
    processed_data = self._process_jacobian_tape(tape)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/venv/lib/python3.12/site-packages/pennylane_lightning/lightning_qubit/_adjoint_jacobian.py", line 123, in _process_jacobian_tape
    ops_serialized = self._create_ops_list_lightning(*ops_serialized)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: create_ops_listC64(): incompatible function arguments. The following argument types are supported:
    1. (arg0: List[str], arg1: List[List[float]], arg2: List[List[int]], arg3: List[bool], arg4: List[numpy.ndarray[numpy.complex64]], arg5: List[List[int]], arg6: List[List[bool]]) -> pennylane_lightning.lightning_qubit_ops.algorithms.OpsStructC64
Invoked with: ['RX', 'SX', 'RX', 'SX', 'RX', 'SX', 'RX', 'SX'], [[0.1591522991657257], [], [0.1591522991657257], [], [0.1591522991657257], [], [0.1591522991657257], []], [[0], [0], [1], [1], [2], [2], [3], [3]], [False, False, False, False, False, False, False, False], [[], array([[0.5+0.5j, 0.5-0.5j],
       [0.5-0.5j, 0.5+0.5j]]), [], array([[0.5+0.5j, 0.5-0.5j],
       [0.5-0.5j, 0.5+0.5j]]), [], array([[0.5+0.5j, 0.5-0.5j],
       [0.5-0.5j, 0.5+0.5j]]), [], array([[0.5+0.5j, 0.5-0.5j],
       [0.5-0.5j, 0.5+0.5j]])], [[], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], []]
  1. If I remove qml.SX, it works.
  2. If I replace qml.grad with qml.jacobian, it also works. With this, it seems to be a fixable problem, and the error is not intentional?

Output of qml.about():

Name: PennyLane
Version: 0.38.0
Summary: PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
Home-page: https://github.com/PennyLaneAI/pennylane
Author:
Author-email:
License: Apache License 2.0
Location: /venv/lib/python3.12/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, packaging, pennylane-lightning, requests, rustworkx, scipy, toml, typing-extensions
Required-by: PennyLane-qiskit, PennyLane_Lightning

Platform info:           Linux-6.6.52-x86_64-with-glibc2.39
Python version:          3.12.5
Numpy version:           1.26.4
Scipy version:           1.14.1
  1. If I replace qml.grad with qml.jacobian, it also works. With this, it seems to be a fixable problem, and the error is not intentional?

This is not the case. qml.jacobian also has an identical problem. I simply accidentally run the example without a qml.SX gate.

Thanks for posting this @rht! We’ll investigate this to find the source of the issue.