# Related to H.6. How to find the expectation value of LCU?

**URL:** <https://discuss.pennylane.ai/t/related-to-h-6-how-to-find-the-expectation-value-of-lcu/3895>\
**Category:** Codebook\
**Created:** [January 15, 2024, 12:12pm UTC](https://discuss.pennylane.ai/t/related-to-h-6-how-to-find-the-expectation-value-of-lcu/3895 "2024-01-15T12:12:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![verstrikt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/verstrikt/32/2205_2.png) [@verstrikt](https://discuss.pennylane.ai/u/verstrikt)\
**Post date:** [January 15, 2024, 12:12pm UTC](https://discuss.pennylane.ai/t/related-to-h-6-how-to-find-the-expectation-value-of-lcu/3895/1 "2024-01-15T12:12:58Z")

</div>

So after finishing H.6, we got to know that we can use LCU for simulating time evolution. But how to find the expectation value of the LCU, for example, U+V? I am planning to use it on quantum signal processing, so it can approximate arbitrary function (not just odd or even function), but I don’t know how to code it. Can anyone help me?

---

<div class="post-metadata">

**Author:** ![Alvaro\_Ballon](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/alvaro_ballon/32/874_2.png) [@Alvaro\_Ballon](https://discuss.pennylane.ai/u/Alvaro_Ballon)\
**Post date:** [January 15, 2024, 10:01pm UTC](https://discuss.pennylane.ai/t/related-to-h-6-how-to-find-the-expectation-value-of-lcu/3895/2 "2024-01-15T22:01:19Z")

</div>

Hello @verstrikt

Welcome to the forum, this is a great question! If I understand your question correctly, the `postselect` option for mid-circuit measurements might be just what you need.

Let’s say your circuit with two wires looks like this:

```auto
dev = qml.device("default.qubit", wires = [0,1])

@qml.qnode(dev)
def lcu_circuit():
    Your LCU circuit calculating U + V
    return qml.expval(qml.PauliZ(1))

```

But that’s not what you want, since you only want the state when we have already measured `0` in `wire = 0`. This means you have to add a mid circuit measurement that selects only the results that end up with a \vert 0 \rangle state in the first wire. You can do this as follows

```auto
dev = qml.device("default.qubit", wires = [0,1])

@qml.qnode(dev)
def lcu_circuit():
    Your LCU circuit calculating U+V
    m0 = qml.measure(0, postselect = 0)
    return qml.expval(qml.PauliZ(1))

```

You won’t be able to return `qml.state()` in this case, but you can also return `qml.density_matrix(wires = 1)` if you want to debug! That’ll return the state in the last wires when the first wire is measured to be \vert 0 \rangle.

I hope this helps, let me know if you have any follow-up questions 🙂 .

Cheers,

Alvaro

---

<div class="post-metadata">

**Author:** ![verstrikt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/verstrikt/32/2205_2.png) [@verstrikt](https://discuss.pennylane.ai/u/verstrikt)\
**Post date:** [January 16, 2024, 1:45am UTC](https://discuss.pennylane.ai/t/related-to-h-6-how-to-find-the-expectation-value-of-lcu/3895/3 "2024-01-16T01:45:24Z")

</div>

Alright, thank you @Alvaro_Ballon. I will try it!
