# Unable to compute expectation value

**URL:** <https://discuss.pennylane.ai/t/unable-to-compute-expectation-value/1595>\
**Category:** PennyLane Help\
**Created:** [January 4, 2022, 10:48pm UTC](https://discuss.pennylane.ai/t/unable-to-compute-expectation-value/1595 "2022-01-04T22:48:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![grove0100](https://avatars.discourse-cdn.com/v4/letter/g/d9b06d/32.png) [@grove0100](https://discuss.pennylane.ai/u/grove0100)\
**Post date:** [January 4, 2022, 10:48pm UTC](https://discuss.pennylane.ai/t/unable-to-compute-expectation-value/1595/1 "2022-01-04T22:48:37Z")

</div>

Hello!

I am trying to compute the expval of a certain observable given a predefined circuit. However, I get the following error:

 ![expval](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/1X/9077bac4a4a650223536182c8aa091425306caa3.png)  
Could someone help me figure out the problem with my code?  
Thanks in advance!

---

<div class="post-metadata">

**Author:** ![josh](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/josh/32/100_2.png) [@josh](https://discuss.pennylane.ai/u/josh)\
**Post date:** [January 5, 2022, 6:07am UTC](https://discuss.pennylane.ai/t/unable-to-compute-expectation-value/1595/2 "2022-01-05T06:07:21Z")

</div>

Hi @grove0100!

Unfortunately this is a known bug that we are currently working to fix (see the relevant GitHub Issue here: [WireError being thrown whenever running circuits where wire names are given explicitly · Issue #1459 · PennyLaneAI/pennylane · GitHub](https://github.com/PennyLaneAI/pennylane/issues/1459)).

This bug occurs whenever the `autograd` interface is used with QNodes that take positional, non-trainable arguments (here, `H` falls into this category).

A workaround at the moment is to simply call your QNode with non-trainable arguments using keyword syntax, e.g.,

```python
toy('exp', theta, H=qml.PauliX(1))

```

For example:

```python
n = 4
dev = qml.device("default.qubit", wires=n)

@qml.qnode(dev)
def circuit(op, params, H):
    qml.RX(params[0], wires=0)
    qml.broadcast(qml.CRX, wires=range(n), pattern="chain", parameters=params[1:n])

    if op == "state":
        return qml.state
    else:
        return qml.expval(H)

theta = np.ones((5,), requires_grad=True)
print(circuit("exp", theta, H=qml.PauliX(1)))

```

should work as expected.

A separate issue is that PennyLane does not currently support returning `qml.state` alongside expectation values; for example, the following will raise an error:

```python
@qml.qnode(dev)
def circuit(x, H):
    qml.RX(x, wires=0)
    return qml.state(), qml.expval(H)

>>> circuit(0.5, H=qml.PauliX(0))
pennylane.QuantumFunctionError: The state or density matrix cannot be returned in combination with other return types

```

However this is something we can definitely look into supporting — if you have time, it would be super helpful if you could open a feature request over on our GitHub page detailing your requirements 🙂

---

<div class="post-metadata">

**Author:** ![grove0100](https://avatars.discourse-cdn.com/v4/letter/g/d9b06d/32.png) [@grove0100](https://discuss.pennylane.ai/u/grove0100)\
**Post date:** [January 5, 2022, 11:02am UTC](https://discuss.pennylane.ai/t/unable-to-compute-expectation-value/1595/3 "2022-01-05T11:02:51Z")

</div>

Thank you very much. Moving over to the TensorFlow environment has solved the problem.
