Hi, I had a follow up question for when you have multiple inputs with parameter-shift in separate arrays as opposed to 1 array for all inputs:
import pennylane as qml
from pennylane import numpy as np
dev_lightning = qml.device('lightning.qubit', wires=2)
dev = qml.device('default.qubit', wires=2)
@qml.qnode(dev_lightning, "parameter-shift",)
def circuit_adjoint(a,b,c):
qml.RX(a, wires=0)
qml.CNOT(wires=(0,1))
qml.RY(b, wires=1)
qml.RZ(c, wires=1)
return qml.expval(qml.PauliX(wires=1))
x_a = np.array([0.1], requires_grad=True)
x_b = np.array([0.2], requires_grad=True)
x_c = np.array([0.3], requires_grad=True)
print(qml.jacobian(qml.grad(circuit_adjoint))(x_a, x_b, x_c))
The above code is not accepted as shown below - how do I deal with multiple inputs?
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
File /quantum_risk_engine/.venv/lib/python3.11/site-packages/numpy/core/fromnumeric.py:57, in _wrapfunc(obj, method, *args, **kwds)
56 try:
---> 57 return bound(*args, **kwds)
58 except TypeError:
59 # A TypeError occurs if the object does have such a method in its
60 # class, but its signature is not identical to that of NumPy's. This
(...)
64 # Call _wrapit from within the except clause to ensure a potential
65 # exception has a traceback chain.
TypeError: 'ArrayVSpace' object cannot be interpreted as an integer
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
File /quantum_risk_engine/.venv/lib/python3.11/site-packages/pennylane/_grad.py:330, in jacobian.<locals>._jacobian_function(*args, **kwargs)
329 try:
--> 330 jac = tuple(_jacobian(func, arg)(*args, **kwargs) for arg in _argnum)
331 except TypeError as e:
File /quantum_risk_engine/.venv/lib/python3.11/site-packages/pennylane/_grad.py:330, in <genexpr>(.0)
329 try:
--> 330 jac = tuple(_jacobian(func, arg)(*args, **kwargs) for arg in _argnum)
331 except TypeError as e:
File /quantum_risk_engine/.venv/lib/python3.11/site-packages/autograd/wrap_util.py:20, in unary_to_nary.<locals>.nary_operator.<locals>.nary_f(*args, **kwargs)
19 x = tuple(args[i] for i in argnum)
---> 20 return unary_operator(unary_f, x, *nary_op_args, **nary_op_kwargs)
File /quantum_risk_engine/.venv/lib/python3.11/site-packages/autograd/differential_operators.py:64, in jacobian(fun, x)
63 grads = map(vjp, ans_vspace.standard_basis())
---> 64 return np.reshape(np.stack(grads), jacobian_shape)
File /quantum_risk_engine/.venv/lib/python3.11/site-packages/autograd/tracer.py:48, in primitive.<locals>.f_wrapped(*args, **kwargs)
47 else:
---> 48 return f_raw(*args, **kwargs)
File <__array_function__ internals>:180, in reshape(*args, **kwargs)
File /quantum_risk_engine/.venv/lib/python3.11/site-packages/numpy/core/fromnumeric.py:298, in reshape(a, newshape, order)
200 """
201 Gives a new shape to an array without changing its data.
202
(...)
296 [5, 6]])
297 """
--> 298 return _wrapfunc(a, 'reshape', newshape, order=order)
File /quantum_risk_engine/.venv/lib/python3.11/site-packages/numpy/core/fromnumeric.py:66, in _wrapfunc(obj, method, *args, **kwds)
58 except TypeError:
59 # A TypeError occurs if the object does have such a method in its
60 # class, but its signature is not identical to that of NumPy's. This
(...)
64 # Call _wrapit from within the except clause to ensure a potential
65 # exception has a traceback chain.
---> 66 return _wrapit(obj, method, *args, **kwds)
File /quantum_risk_engine/.venv/lib/python3.11/site-packages/numpy/core/fromnumeric.py:43, in _wrapit(obj, method, *args, **kwds)
42 wrap = None
---> 43 result = getattr(asarray(obj), method)(*args, **kwds)
44 if wrap:
TypeError: 'ArrayVSpace' object cannot be interpreted as an integer
The above exception was the direct cause of the following exception:
ValueError Traceback (most recent call last)
Cell In[12], line 18
16 x_b = np.array([0.2], requires_grad=True)
17 x_c = np.array([0.3], requires_grad=True)
---> 18 print(qml.jacobian(qml.grad(circuit_adjoint))(x_a, x_b, x_c))
File /quantum_risk_engine/.venv/lib/python3.11/site-packages/pennylane/_grad.py:333, in jacobian.<locals>._jacobian_function(*args, **kwargs)
331 except TypeError as e:
332 if active_return():
--> 333 raise ValueError(
334 "PennyLane has a new return shape specification that"
335 " may not work well with autograd and more than one measurement. That may"
336 " be the source of the error. \n\n"
337 "See the documentation here for more information:\n"
338 "https://docs.pennylane.ai/en/stable/introduction/returns.html"
339 ) from e
340 raise e
342 return jac[0] if unpack else jac
ValueError: PennyLane has a new return shape specification that may not work well with autograd and more than one measurement. That may be the source of the error.
See the documentation here for more information:
https://docs.pennylane.ai/en/stable/introduction/returns.html