How to use trainable measurement operator in Pennylane-Tensorflow model

Hi. I am trying to run a hybrid model with Pennylane and Tensorflow. The model runs fine when I return:

return qml.expval(sum([qml.pauli.string_to_pauli_word(j) for i, j in enumerate(pauli_strings)]))

However, as soon as I introduce parameters in the measurement operator like this:

return [qml.expval(params_measurement[i] * qml.pauli.string_to_pauli_word(j)) for i, j in enumerate(pauli_strings)]

running the code gives out the following error:

in user code:

    File "/qcfs/bravo/.virtualenvs/qml/lib/python3.10/site-packages/pennylane/workflow/qnode.py", line 1002, in __call__  *
        self.construct(args, kwargs)
    File "/qcfs/bravo/.virtualenvs/qml/lib/python3.10/site-packages/pennylane/workflow/qnode.py", line 888, in construct  *
        self._qfunc_output = self.func(*args, **kwargs)
    File "/tmp/ipykernel_647449/1711710577.py", line 44, in circuit  *
        return [qml.expval(params_measurement[i] * qml.pauli.string_to_pauli_word(j)) for i, j in enumerate(pauli_strings)]
    File "/qcfs/bravo/.virtualenvs/qml/lib/python3.10/site-packages/pennylane/measurements/expval.py", line 71, in expval  *
        if not op.is_hermitian:
    File "/qcfs/bravo/.virtualenvs/qml/lib/python3.10/site-packages/pennylane/ops/op_math/sprod.py", line 203, in is_hermitian
        return self.base.is_hermitian and not qml.math.iscomplex(self.scalar)

    OperatorNotAllowedInGraphError: Using a symbolic `tf.Tensor` as a Python `bool` is not allowed: AutoGraph did convert this function. This might indicate you are trying to use an unsupported feature.


Call arguments received by layer "keras_layer_1" "                 f"(type KerasLayer):
  • inputs=tf.Tensor(shape=(16, 4), dtype=float32)

Is this a limitation of qml.qnn.KerasLayer?

The runnable code is attached, but i cannot share the dataset. You can use MNIST for the same purpose, it deals with grayscale images.

qccnn_trainable_measurement_dummy.py (6.3 KB)

qml.about():

Name: PennyLane
Version: 0.35.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: /qcfs/bravo/.virtualenvs/qml/lib/python3.10/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-v
[qccnn_trainable_measurement_dummy.py|attachment](upload://rxT1wWN85y2Nw9iLa1CH4y3JE9B.py) (6.4 KB)
ersion, toml, typing-extensions
Required-by: PennyLane-Cirq, PennyLane-qiskit, PennyLane_Lightning, PennyLane_Lightning_GPU

Platform info:           Linux-5.15.0-105-generic-x86_64-with-glibc2.35
Python version:          3.10.12
Numpy version:           1.23.5
Scipy version:           1.11.3
Installed devices:
- cirq.mixedsimulator (PennyLane-Cirq-0.34.0)
- cirq.pasqal (PennyLane-Cirq-0.34.0)
- cirq.qsim (PennyLane-Cirq-0.34.0)
- cirq.qsimh (PennyLane-Cirq-0.34.0)
- cirq.simulator (PennyLane-Cirq-0.34.0)
- lightning.qubit (PennyLane_Lightning-0.35.0)
- default.clifford (PennyLane-0.35.0)
- default.gaussian (PennyLane-0.35.0)
- default.mixed (PennyLane-0.35.0)
- default.qubit (PennyLane-0.35.0)
- default.qubit.autograd (PennyLane-0.35.0)
- default.qubit.jax (PennyLane-0.35.0)
- default.qubit.legacy (PennyLane-0.35.0)
- default.qubit.tf (PennyLane-0.35.0)
- default.qubit.torch (PennyLane-0.35.0)
- default.qutrit (PennyLane-0.35.0)
- null.qubit (PennyLane-0.35.0)
- lightning.gpu (PennyLane_Lightning_GPU-0.35.0)
- qiskit.aer (PennyLane-qiskit-0.35.1)
- qiskit.basicaer (PennyLane-qiskit-0.35.1)
- qiskit.ibmq (PennyLane-qiskit-0.35.1)
- qiskit.ibmq.circuit_runner (PennyLane-qiskit-0.35.1)
- qiskit.ibmq.sampler (PennyLane-qiskit-0.35.1)
- qiskit.remote (PennyLane-qiskit-0.35.1)

Hey @joaofbravo,

Can you share a minimal example with a QNode that’s wrapped with KerasLayer that reproduces this behaviour? Tough to say what’s going on!

Hi Isaac :slight_smile:
To my understanding, the python script I shared fulfills that request.

I use

  QuantumConv = qml.qnn.KerasLayer(circuit, params_shape, output_dim=1, trainable=True)

Hi @joaofbravo ,

I noticed that you’re using version v0.35 of PennyLane. Are you able to use the latest PennyLane version? Do you still have this error with the latest version?

In case you still have the error then we’d need a minimal reproducible example.

A minimal reproducible example (or minimal working example) is the simplest version of the code that reproduces the problem. It should be self-contained, including all necessary imports, data, functions, etc., so that we can copy-paste the code and reproduce the problem. However it shouldn’t contain any unnecessary data, functions, …, for example gates and functions that can be removed to simplify the code.

By having a minimal reproducible example (MRE) we can test the code ourselves and identify any bugs, mistaken usage, or workarounds.

As you mention, using MNIST can be a good way for us to test the code, however what we would need is for you to share the code that shows the error with MNIST (instead of with your other dataset).

Ideally if you can use something like the downscaled-mnist PennyLane dataset then we’re more likely to be able to quickly reproduce your issue and help you find a solution.

Note that the packages aiohttp, fsspec, and h5py are required to use the qml.data module. These can be installed with:

pip install aiohttp fsspec h5py

Let me know if you have any questions.