# Query regarding saving measurement probabilities

**URL:** <https://discuss.pennylane.ai/t/query-regarding-saving-measurement-probabilities/4939>\
**Category:** PennyLane Help\
**Created:** [July 28, 2024, 3:16am UTC](https://discuss.pennylane.ai/t/query-regarding-saving-measurement-probabilities/4939 "2024-07-28T03:16:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kaushiki\_Mukherjee](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/kaushiki_mukherjee/32/2741_2.png) [@Kaushiki\_Mukherjee](https://discuss.pennylane.ai/u/Kaushiki_Mukherjee)\
**Post date:** [July 28, 2024, 3:16am UTC](https://discuss.pennylane.ai/t/query-regarding-saving-measurement-probabilities/4939/1 "2024-07-28T03:16:59Z")

</div>

I am trying to save probability(to use it to calculate expectation value) after measuring one out of three qubits in a circuit and then performing some other operations on remaining two qubits. But can’t do that. Every time getting error message.  
Example of the code that I tried is as follows:

```auto
import pennylane as qml
from pennylane import numpy as np
dev = qml.device("default.qubit", wires=3)
@qml.qnode(dev)
def func(theta):
    qml.Y(0)
    qml.RX(theta,wires=1)
    qml.RZ(theta,wires=2)
    m_0 = qml.measure([2])
    s2=qml.probs(wires=[2])
    m_1 = qml.measure([1], reset=True)
    return s2
print(func(0.8))

```

Error message:  
ValueError: ops operation measure(wires=[1]) must occur prior to measurements. Please place earlier in the queue.  
Is it not possible to measure some of the qubits in a circuit(say n out of m with n\<m), use the expectation value of those measurements(on n qubits) and using rest of the qubits(m-n) for some other operations?  
I came across some qml.cond command in the documentation where one can perform some operations on one qubit(say 2nd qubit) conditioned on measurement outcome of the measured qubit(say 1st qubit). But as per my requirement the operations that I need to perform on 2nd qubit do not depend on the measurement outcome from 1st qubit.  
Version of Pennylane used:0.37.0(as seen after running qml.about()).

It will be really helpful if I can get some way out to resolve my issue.  
Thanks in advance.

---

<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:** [July 30, 2024, 10:06pm UTC](https://discuss.pennylane.ai/t/query-regarding-saving-measurement-probabilities/4939/2 "2024-07-30T22:06:52Z")

</div>

Hi @Kaushiki_Mukherjee , welcome to the Forum!

Indeed qml.probs cannot be used in the middle of the circuit. For mid-circuit measurements you should use [qml.measure](https://docs.pennylane.ai/en/stable/code/api/pennylane.measure.html).

Is there a reason why you cannot use qml.probs in the return statement as shown below?

```auto
import pennylane as qml
from pennylane import numpy as np
dev = qml.device("default.qubit", wires=3)

@qml.qnode(dev)
def func(theta):
    qml.Y(0)
    qml.RX(theta,wires=1)
    qml.RZ(theta,wires=2)
    m_0 = qml.measure([2])
    m_1 = qml.measure([1], reset=True)
    return qml.probs(wires=[2])
print(func(0.8))

```

Alternatively you can calculate expectation values (and other measurements) directly from m\_0 if you want as seen below:

```auto
import pennylane as qml
from pennylane import numpy as np
dev = qml.device("default.qubit", wires=3)

@qml.qnode(dev)
def func(theta):
    qml.Y(0)
    qml.RX(theta,wires=1)
    qml.RZ(theta,wires=2)
    m_0 = qml.measure([2])
    m_1 = qml.measure([1], reset=True)
    return (
        qml.sample(m_0), qml.expval(m_0), qml.expval(qml.Z(wires=2)), qml.probs(op=m_0), qml.counts(op=m_0),
    )
sample,expval_m0,expval_Z,probs,counts = func(0.8, shots=1000)

print('sample \n',sample,'\n expval_m0 \n',expval_m0,'\n expval_Z \n',expval_Z,'\n probs \n',probs,'\n counts \n',counts, sep='\n')

```

> **Important note**
> 
> Computational basis measurements are performed using the 0, 1 convention rather than the ±1 convention. So, for example, `expval(qml.measure(0))` and `expval(qml.Z(0))` will give different answers.

I hope this helps!

---

<div class="post-metadata">

**Author:** ![Kaushiki\_Mukherjee](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/kaushiki_mukherjee/32/2741_2.png) [@Kaushiki\_Mukherjee](https://discuss.pennylane.ai/u/Kaushiki_Mukherjee)\
**Post date:** [August 3, 2024, 1:50pm UTC](https://discuss.pennylane.ai/t/query-regarding-saving-measurement-probabilities/4939/3 "2024-08-03T13:50:07Z")

</div>

![RNN](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/2X/4/4bd7816120174cc44904a486c63b03e0202fe5b4.png)

@CatalinaAlbornoz  
Thanks for your response. Actually I am trying to build circuit for RNN model(as shown in the attachment) from paper:arXiv:2012.11242. There the whole set of qubits is divided into 2 parts(n1 and n2,say). At any time step(t1,say), different operations are performed on both parts and then one of those parts(n1,say) is measured and expectation value of measurements are collected and then n1 qubits are reset. While the other set of qubits(n2), after being operated, are not measured. Instead those transformed qubits along with the reset n1 qubits are again measured at next time step t2. This is why I need the mid-circuit measurements. Can I somehow use pennylane to design any such type of circuit?  
Thanks in advance.

---

<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:** [August 6, 2024, 8:29pm UTC](https://discuss.pennylane.ai/t/query-regarding-saving-measurement-probabilities/4939/4 "2024-08-06T20:29:21Z")

</div>

Hi @Kaushiki_Mukherjee ,

Thank you for the additional details.

Just to clarify, if you’re looking to get expectation values it means averaging over many shots.

I made a modification to my previous code to show how it can do what you’re looking for.

If you look at the code and the circuit diagram below you’ll notice that the first qubit is measured and then reset, so you can keep applying gates to it. At the end we return the expectation value of these measurements using the 0,1 convention.

I hope this helps!

```auto
import pennylane as qml
from pennylane import numpy as np
dev = qml.device("default.qubit", wires=2)

@qml.qnode(dev)
def circuit(theta):
    # Apply gates
    qml.RX(theta,wires=0)
    qml.RZ(theta,wires=1)

    # Measure and reset first qubit
    m_0 = qml.measure([0], reset=True)

    # Apply gates
    qml.RX(theta,wires=0)
    qml.RZ(theta,wires=1)
    
    # Measure and reset first qubit
    m_1 = qml.measure([0], reset=True)

    # Apply gates
    qml.RX(theta,wires=0)
    qml.RZ(theta,wires=1)

    # Measure second qubit using 0,1 convention
    m_2 = qml.measure([1])

    # Return the expectation value for the measurements
    return qml.expval(m_0), qml.expval(m_1), qml.expval(m_2)

print(circuit(0.8))

qml.draw_mpl(circuit, style='pennylane')(0.8)

```

 ![image](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/2X/f/f8f9353a506731300f4c3873dc10b677f88b99ac.png)

---

<div class="post-metadata">

**Author:** ![Kaushiki\_Mukherjee](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/kaushiki_mukherjee/32/2741_2.png) [@Kaushiki\_Mukherjee](https://discuss.pennylane.ai/u/Kaushiki_Mukherjee)\
**Post date:** [August 9, 2024, 4:23pm UTC](https://discuss.pennylane.ai/t/query-regarding-saving-measurement-probabilities/4939/5 "2024-08-09T16:23:09Z")

</div>

Thanks for your help. Using your code, I can now construct a part of the repetitive block of the circuit.
