# How to use trainable measurement operator in Pennylane-Tensorflow model

**URL:** <https://discuss.pennylane.ai/t/how-to-use-trainable-measurement-operator-in-pennylane-tensorflow-model/5104>\
**Category:** PennyLane Help\
**Created:** [August 29, 2024, 9:12pm UTC](https://discuss.pennylane.ai/t/how-to-use-trainable-measurement-operator-in-pennylane-tensorflow-model/5104 "2024-08-29T21:12:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![joaofbravo](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/joaofbravo/32/1770_2.png) [@joaofbravo](https://discuss.pennylane.ai/u/joaofbravo)\
**Post date:** [August 29, 2024, 9:12pm UTC](https://discuss.pennylane.ai/t/how-to-use-trainable-measurement-operator-in-pennylane-tensorflow-model/5104/1 "2024-08-29T21:12:54Z")

</div>

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

```auto
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:

```auto
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:

```auto
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](https://discuss.pennylane.ai/uploads/short-url/unaZ54UXI6fWJ7geehwjt70uCK7.py) (6.3 KB)

`qml.about()`:

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)\
**Post date:** [August 30, 2024, 2:10pm UTC](https://discuss.pennylane.ai/t/how-to-use-trainable-measurement-operator-in-pennylane-tensorflow-model/5104/2 "2024-08-30T14:10:52Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![joaofbravo](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/joaofbravo/32/1770_2.png) [@joaofbravo](https://discuss.pennylane.ai/u/joaofbravo)\
**Post date:** [August 30, 2024, 2:44pm UTC](https://discuss.pennylane.ai/t/how-to-use-trainable-measurement-operator-in-pennylane-tensorflow-model/5104/3 "2024-08-30T14:44:12Z")

</div>

Hi Isaac 🙂  
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)

```

---

<div class="post-metadata">

**Author:** ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)\
**Post date:** [September 4, 2024, 4:22am UTC](https://discuss.pennylane.ai/t/how-to-use-trainable-measurement-operator-in-pennylane-tensorflow-model/5104/4 "2024-09-04T04:22:22Z")

</div>

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](https://pennylane.ai/datasets/other/downscaled-mnist) 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`](https://docs.pennylane.ai/en/stable/code/qml_data.html#module-pennylane.data) module. These can be installed with:

`pip install aiohttp fsspec h5py`

Let me know if you have any questions.
