Computing Jacobian for multiple measurement outcomes

Hello!

I want to compute the Jacobian of a QNode which has a tuple of measurement results as output. I am referring to the example shown in qml.jacobian — PennyLane 0.38.0 documentation. However, I am getting an error for the example given in that page. Not sure if it has to do with the python package versions that I am using. I would truly appreciate if someone can help me run this example. I have attached my code and error message here. Thank you very much in advance!

import pennylane as qml
from pennylane import numpy as np


dev = qml.device("default.qubit", wires=2)

print(qml.about())
@qml.qnode(dev)
def circuit(x, y, z):
    qml.RX(x, wires=0)
    qml.RY(y, wires=1)
    qml.RZ(z, wires=0)
    return tuple(qml.expval(qml.Z(w)) for w in dev.wires)

x = np.array(0.2, requires_grad=True)
y = np.array(0.9, requires_grad=True)
z = np.array(-1.4, requires_grad=True)

jac = qml.jacobian(circuit)(x, y, z)

please find the full error message below:

UserWarning: Output seems independent of input.
  warnings.warn("Output seems independent of input.")
Traceback (most recent call last):
  File "C:\Users\hedge\PycharmProjects\cfd-quantum\venv\Lib\site-packages\numpy\core\fromnumeric.py", line 59, in _wrapfunc
    return bound(*args, **kwds)
           ^^^^^^^^^^^^^^^^^^^^
TypeError: 'ArrayVSpace' object cannot be interpreted as an integer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\hedge\AppData\Roaming\JetBrains\PyCharmEdu2022.1\scratches\scratch.py", line 19, in <module>
    jac = qml.jacobian(circuit)(x, y, z)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hedge\PycharmProjects\cfd-quantum\venv\Lib\site-packages\pennylane\_grad.py", line 456, in _jacobian_function
    jac = tuple(_jacobian(func, arg)(*args, **kwargs) for arg in _argnum)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hedge\PycharmProjects\cfd-quantum\venv\Lib\site-packages\pennylane\_grad.py", line 456, in <genexpr>
    jac = tuple(_jacobian(func, arg)(*args, **kwargs) for arg in _argnum)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hedge\PycharmProjects\cfd-quantum\venv\Lib\site-packages\autograd\wrap_util.py", line 20, in nary_f
    return unary_operator(unary_f, x, *nary_op_args, **nary_op_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hedge\PycharmProjects\cfd-quantum\venv\Lib\site-packages\autograd\differential_operators.py", line 64, in jacobian
    return np.reshape(np.stack(grads), jacobian_shape)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hedge\PycharmProjects\cfd-quantum\venv\Lib\site-packages\autograd\tracer.py", line 48, in f_wrapped
    return f_raw(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hedge\PycharmProjects\cfd-quantum\venv\Lib\site-packages\numpy\core\fromnumeric.py", line 285, in reshape
    return _wrapfunc(a, 'reshape', newshape, order=order)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hedge\PycharmProjects\cfd-quantum\venv\Lib\site-packages\numpy\core\fromnumeric.py", line 68, in _wrapfunc
    return _wrapit(obj, method, *args, **kwds)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hedge\PycharmProjects\cfd-quantum\venv\Lib\site-packages\numpy\core\fromnumeric.py", line 45, in _wrapit
    result = getattr(asarray(obj), method)(*args, **kwds)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'ArrayVSpace' object cannot be interpreted as an integer

the output of qml.about():

Name: PennyLane
Version: 0.37.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: GitHub - PennyLaneAI/pennylane: 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.
Author:
Author-email:
License: Apache License 2.0
Location: C:\Users\hedge\PycharmProjects\cfd-quantum\venv\Lib\site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, packaging, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: PennyLane_Lightning

Platform info: Windows-10-10.0.22631-SP0
Python version: 3.11.9
Numpy version: 1.26.4
Scipy version: 1.14.1
Installed devices:

  • default.clifford (PennyLane-0.37.0)
  • default.gaussian (PennyLane-0.37.0)
  • default.mixed (PennyLane-0.37.0)
  • default.qubit (PennyLane-0.37.0)
  • default.qubit.autograd (PennyLane-0.37.0)
  • default.qubit.jax (PennyLane-0.37.0)
  • default.qubit.legacy (PennyLane-0.37.0)
  • default.qubit.tf (PennyLane-0.37.0)
  • default.qubit.torch (PennyLane-0.37.0)
  • default.qutrit (PennyLane-0.37.0)
  • default.qutrit.mixed (PennyLane-0.37.0)
  • default.tensor (PennyLane-0.37.0)
  • null.qubit (PennyLane-0.37.0)
  • lightning.qubit (PennyLane_Lightning-0.37.0)
    None

Hi @Pratibha_Hegde :slight_smile:
It seems to be a bug when you try to return the measurement tuple. I’ll notify the team but in the meantime, this code should give you the desired output:

import pennylane as qml
from pennylane import numpy as np


dev = qml.device("default.qubit", wires=2)

@qml.qnode(dev)
def circuit(x, y, z, measure_wire):
    qml.RX(x, wires=0)
    qml.RY(y, wires=1)
    qml.RZ(z, wires=0)
    return qml.expval(qml.PauliZ(measure_wire))

x = np.array(0.2, requires_grad=True)
y = np.array(0.9, requires_grad=True)
z = np.array(-1.4, requires_grad=True)

jac = [qml.jacobian(circuit)(x, y, z, measure_wire) for measure_wire in dev.wires]

print(jac)

thanks for sharing!

1 Like

Thank you @Guillermo_Alonso for your reply. This alternative code works for me. Thank you once again!

Hi @Pratibha_Hegde ,

Thank you for reporting this problem.
I’m able to replicate it with version v0.38 of PennyLane.

Would you be able to open a bug report in our GitHub repo? This report will allow the core PennyLane developers to understand this issue and look into fixing it.

It’s important to share a minimal reproducible example so that we can focus on what’s actually causing the problem. The one you shared above would be perfect.

When asked about error tracebacks in the GitHub bug template, remember to include the full error traceback (same as you did above).

Once you make the bug report feel free to add the link here.

Let me know if you have any questions, and thanks again for uncovering this issue! :raised_hands:

Hi @CatalinaAlbornoz thank you for your reply. I have made bug report here. You can find it at
[BUG]Computing Jacobian for multiple measurement outcomes · Issue #6259 · PennyLaneAI/pennylane · GitHub Thank you once again!

1 Like

Thanks @Pratibha_Hegde ! :raised_hands: