Skip to content

QubitUnitary is directly passed to cpp layer with adjoint_diff #453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
chaeyeunpark opened this issue May 25, 2023 · 0 comments
Open

QubitUnitary is directly passed to cpp layer with adjoint_diff #453

chaeyeunpark opened this issue May 25, 2023 · 0 comments

Comments

@chaeyeunpark
Copy link
Contributor

The following code now raises an error from C++ layer:

import sys
import pennylane as qml
from pennylane import numpy as np
import json

dev = qml.device('lightning.qubit', wires=3)

@qml.qnode(dev, diff_method="adjoint")
def circuit(theta):
    arr = np.eye(8)

    qml.QubitUnitary(arr, wires = [0, 1,2])
    qml.RX(theta, wires = [1])
    qml.adjoint(qml.QubitUnitary)(arr, wires = [0, 1,2])
    return qml.expval(qml.PauliX(0))

if __name__ == "__main__":
    params = np.array([0.1])
    grad = qml.grad(circuit)
    print(grad(params))

Here's the full output:

Traceback (most recent call last):
  File "/home/chae-yeun/work/Xanadu/pennylane-tests/test_qubit_unitary.py", line 20, in <module>
    print(grad(params))
  File "/home/chae-yeun/work/Xanadu/pennylane/pennylane/_grad.py", line 115, in __call__
    grad_value, ans = grad_fn(*args, **kwargs)
  File "/home/chae-yeun/work/Xanadu/devel/lib/python3.10/site-packages/autograd/wrap_util.py", line 20, in nary_f
    return unary_operator(unary_f, x, *nary_op_args, **nary_op_kwargs)
  File "/home/chae-yeun/work/Xanadu/pennylane/pennylane/_grad.py", line 133, in _grad_with_forward
    vjp, ans = _make_vjp(fun, x)
  File "/home/chae-yeun/work/Xanadu/devel/lib/python3.10/site-packages/autograd/core.py", line 10, in make_vjp
    end_value, end_node =  trace(start_node, fun, x)
  File "/home/chae-yeun/work/Xanadu/devel/lib/python3.10/site-packages/autograd/tracer.py", line 10, in trace
    end_box = fun(start_box)
  File "/home/chae-yeun/work/Xanadu/devel/lib/python3.10/site-packages/autograd/wrap_util.py", line 15, in unary_f
    return fun(*subargs, **kwargs)
  File "/home/chae-yeun/work/Xanadu/pennylane/pennylane/qnode.py", line 867, in __call__
    res = qml.execute(
  File "/home/chae-yeun/work/Xanadu/pennylane/pennylane/interfaces/execution.py", line 483, in execute
    res = _execute(
  File "/home/chae-yeun/work/Xanadu/pennylane/pennylane/interfaces/autograd.py", line 326, in execute
    return _execute(
  File "/home/chae-yeun/work/Xanadu/devel/lib/python3.10/site-packages/autograd/tracer.py", line 44, in f_wrapped
    ans = f_wrapped(*argvals, **kwargs)
  File "/home/chae-yeun/work/Xanadu/devel/lib/python3.10/site-packages/autograd/tracer.py", line 48, in f_wrapped
    return f_raw(*args, **kwargs)
  File "/home/chae-yeun/work/Xanadu/pennylane/pennylane/interfaces/autograd.py", line 370, in _execute
    res, jacs = execute_fn(unwrapped_tapes, **gradient_kwargs)
  File "/usr/lib/python3.10/contextlib.py", line 79, in inner
    return func(*args, **kwds)
  File "/home/chae-yeun/work/Xanadu/pennylane/pennylane/_device.py", line 571, in execute_and_gradients
    jacs.append(gradient_method(circuit, **kwargs))
  File "/home/chae-yeun/work/Xanadu/pennylane-lightning/pennylane_lightning/lightning_qubit.py", line 603, in adjoint_jacobian
    jac = adjoint_diff.adjoint_jacobian(
IndexError: _Map_base::at

Partially, the problem is that 3-qubit QubitUnitary does not have proper decomposition. Still, it is also possible to support (non-trainable) QubitUnitary in Lightning as we anyway support it in the forward path (by obtaining the matrix and apply it using e.g. applyMultiQubitOp defined in the LM kernel).

Thus I think we need to decide whether we want to support those operations in adjoint-diff path in Lightning or raise a proper error in the Python layer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant