# Measurement conditioned on measurement?

**URL:** <https://discuss.pennylane.ai/t/measurement-conditioned-on-measurement/7857>\
**Category:** PennyLane Help\
**Created:** [January 13, 2025, 4:17pm UTC](https://discuss.pennylane.ai/t/measurement-conditioned-on-measurement/7857 "2025-01-13T16:17:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![i\_am\_confusion](https://avatars.discourse-cdn.com/v4/letter/i/67e7ee/32.png) [@i\_am\_confusion](https://discuss.pennylane.ai/u/i_am_confusion)\
**Post date:** [January 13, 2025, 4:17pm UTC](https://discuss.pennylane.ai/t/measurement-conditioned-on-measurement/7857/1 "2025-01-13T16:17:35Z")

</div>

Hello!

I want to create a quantum circuit where a measurement is applied conditioned on the result of another measurement. I tried something like this (not the full example, but it reproduces the error I get).

```auto
@qml.qnode(qml.device("default.qubit"))
def circuit():
    qml.cond(qml.measure(0), qml.measure(1))
    return qml.expval(qml.X(0) @ qml.X(1))
circuit()

```

The error that I get is the following

```auto
ConditionalTransformError: Only operations and quantum functions with no measurements can be applied conditionally.

```

My guess is that PennyLane doesn’t support this yet, or maybe something is wrong with my syntax? 🤔 I tried changing stuff around, but it didn’t work either.

If it’s the case that PennyLane cannot do this natively, is there a way to get around it?

Here’s the output of `qml.about()`:

```auto
Name: PennyLane
Version: 0.39.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: /usr/local/lib/python3.10/dist-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, packaging, pennylane-lightning, requests, rustworkx, scipy, toml, typing-extensions
Required-by: PennyLane_Lightning

Platform info: Linux-6.1.85+-x86_64-with-glibc2.35
Python version: 3.10.12
Numpy version: 1.26.4
Scipy version: 1.13.1
Installed devices:
- lightning.qubit (PennyLane_Lightning-0.39.0)
- default.clifford (PennyLane-0.39.0)
- default.gaussian (PennyLane-0.39.0)
- default.mixed (PennyLane-0.39.0)
- default.qubit (PennyLane-0.39.0)
- default.qutrit (PennyLane-0.39.0)
- default.qutrit.mixed (PennyLane-0.39.0)
- default.tensor (PennyLane-0.39.0)
- null.qubit (PennyLane-0.39.0)
- reference.qubit (PennyLane-0.39.0)

```

Thank you!

---

<div class="post-metadata">

**Author:** ![dangulom](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/dangulom/32/3508_2.png) [@dangulom](https://discuss.pennylane.ai/u/dangulom)\
**Post date:** [January 18, 2025, 7:43pm UTC](https://discuss.pennylane.ai/t/measurement-conditioned-on-measurement/7857/2 "2025-01-18T19:43:25Z")

</div>

Hello @i_am_confusion  
Welcome to the forum.

I was able to reproduce your error. For qml.cond, the conditioned operation can’t be a measurement or one that involves measurements. I don’t think there is anything wrong with your syntax, it is just that the application your looking for is not supported by [qml.cond](https://docs.pennylane.ai/en/stable/_modules/pennylane/ops/op_math/condition.html#cond) (see source for more details).

However, an idea 💡 will be using the postselection (only available on “default qubit”) feature of [qml.measure](https://docs.pennylane.ai/en/stable/code/api/pennylane.measure.html) (see the bottom of the page for more). This discards the cases where the outcome of the measurement doesn’t correspond to the desired postselection.

I provide an example for you to play with.Two qubits are initialized in zero, then qubit 0 is flipped to 1 and a mid circuit measurement is implemented with postselection. When m0 is 1, all cases in this example, a CNOT is applied and the second qubit is flipped to 1 to subsequently be measured in the Z basis.

```auto
import pennylane as qml

dev = qml.device("default.qubit")

@qml.qnode(dev)
def func():
    qml.X(0)
    m0 = qml.measure(0, postselect=1)
    qml.CNOT(wires=[0, 1])
    return qml.expval(qml.Z(1))

func(shots=100)

```

From what I see in the code you provided, in the first part, you wanted to measure qubit 1 in the computational basis only when the outcome of the measurement on qubit 0 was equal to 1. Something like the following:

```auto
@qml.qnode(dev)
def func():
    m0 = qml.measure(0, postselect=1)
    return qml.expval(qml.Z(1))

func(shots=100)

```

There is also a nice PennyLane demo on [mid-circuit measurements](https://pennylane.ai/qml/demos/tutorial_mcm_introduction).  
Let me know if this helped and/or there any other questions.

---

<div class="post-metadata">

**Author:** ![i\_am\_confusion](https://avatars.discourse-cdn.com/v4/letter/i/67e7ee/32.png) [@i\_am\_confusion](https://discuss.pennylane.ai/u/i_am_confusion)\
**Post date:** [January 20, 2025, 7:23pm UTC](https://discuss.pennylane.ai/t/measurement-conditioned-on-measurement/7857/3 "2025-01-20T19:23:45Z")

</div>

Hi @dangulom !

I think this is close to what I want, but I also want to sample the measurement results from the first wire. I think `postselect = 1` destroys the qubit and I wouldn’t be able to get the statistics (probability) of measuring `0` on wire 0 🤔 . Do let me know if you have any ideas to go around this. Thanks for your help 🙂

---

<div class="post-metadata">

**Author:** ![dangulom](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/dangulom/32/3508_2.png) [@dangulom](https://discuss.pennylane.ai/u/dangulom)\
**Post date:** [January 21, 2025, 3:46am UTC](https://discuss.pennylane.ai/t/measurement-conditioned-on-measurement/7857/4 "2025-01-21T03:46:14Z")

</div>

Hi!  
💡 😃 I figured that could be a follow-up question, it occurred to me as well while I was typing my answer.  
I could suggest a “brute-force” approach: implementing an appropriate version of your circuit (or the function) for the case with `postselection = 0` so you can collect the statistics separately (yep, like running the code twice). Let me know how that sounds, I believe this could be a safe way around it.

Just to add. I tried a way around it using `qml.cond()` again 😅, now with a subcircuit in the argument. See code:

```auto
def sub_circ():
    qml.X(1)
    #mcm = qml.measure(1)

dev = qml.device("default.qubit")

@qml.qnode(dev)
def func():
    qml.X(0)
    m0 = qml.measure(0)
    qml.cond(m0, sub_circ)()
    return qml.sample(qml.Z(1))

print(func(shots=100))

```

and it only works when I don’t perform the measurement in `sub_circ`. Otherwise, it outputs the following error `AttributeError: 'MidMeasureMP' object has no attribute 'num_params'`  
And I actually found an open [issue](https://github.com/PennyLaneAI/pennylane/issues/5944) related to this in the PennyLane github repo.

---

<div class="post-metadata">

**Author:** ![i\_am\_confusion](https://avatars.discourse-cdn.com/v4/letter/i/67e7ee/32.png) [@i\_am\_confusion](https://discuss.pennylane.ai/u/i_am_confusion)\
**Post date:** [January 21, 2025, 2:45pm UTC](https://discuss.pennylane.ai/t/measurement-conditioned-on-measurement/7857/5 "2025-01-21T14:45:38Z")

</div>

I see! Yes I think this approach will work. I’m glad that the PennyLane team is working on this.

Thank you for your help 🙂
