Some questions about mid-measurement

Hello, attached code is an example code from qml.measure().

If I plot the function by the attached code, I will get a figure without mid-measurement of wires=1.
However, if I comment (add #) qml.qnode(dev), I will get a figure with mid-measurement correctly.

My questions are listed below:

(1) I wonder whether qml.measure support lightning.qubit.
Since I delete qml.cond(), the returned probability changes.
Then, it validates that the qml.cond() indeed works.

import pennylane as qml
import os

dev = qml.device("lightning.qubit", wires=2)

qml.qnode(dev)
def func(x, y):
    qml.Hadamard(0)
    qml.CNOT(wires=[0, 1])

    m_0 = qml.measure(1)
    qml.cond(m_0, qml.RY)(y, wires=0)
    
    return qml.probs(wires=[0])

x = 1
y = 0.3
fig, ax = qml.draw_mpl(func)(x, y)
fig.savefig(os.path.join(os.getcwd(), "QNN_ex.jpg"))

(2)The note from qml.cond states that:

If the first argument of cond is a measurement value (e.g., m_0 in qml.cond(m_0, qml.RY)), then m_0 == 1 is considered internally.

Can I say that qml.cond implements qml.RY no matter what the mid-measurement value is?
Then, what does the mid-measurement operation affect wires=0?

***The environment I used:

Name: PennyLane
Version: 0.33.1
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/PennyLaneAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /home/ihcl10f/anaconda3/envs/GJ_0122/lib/python3.8/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: PennyLane-Lightning

Platform info:           Linux-6.5.0-14-generic-x86_64-with-glibc2.17
Python version:          3.8.18
Numpy version:           1.24.4
Scipy version:           1.10.1
Installed devices:
- default.gaussian (PennyLane-0.33.1)
- default.mixed (PennyLane-0.33.1)
- default.qubit (PennyLane-0.33.1)
- default.qubit.autograd (PennyLane-0.33.1)
- default.qubit.jax (PennyLane-0.33.1)
- default.qubit.legacy (PennyLane-0.33.1)
- default.qubit.tf (PennyLane-0.33.1)
- default.qubit.torch (PennyLane-0.33.1)
- default.qutrit (PennyLane-0.33.1)
- null.qubit (PennyLane-0.33.1)
- lightning.qubit (PennyLane-Lightning-0.33.1)
None

Thanks for replying in advance!

Hey @q36111095! Welcome to the forum :slight_smile:

Indeed, mid-circuit measurements aren’t supported on lightning at the moment. Here’s a list of supported operations: Lightning Qubit device — Lightning 0.34.0 documentation

Can I say that qml.cond implements qml.RY no matter what the mid-measurement value is?

Nope! It implements RY when the measurement is 1.

Okay, I got it!
I have tried to use default.qubit:

import pennylane as qml
import os

dev = qml.device("default.qubit", wires=2)

@qml.qnode(dev)
def func(x, y):
    qml.Hadamard(0)
    qml.CNOT(wires=[0, 1])

    m_0 = qml.measure(1)
    qml.cond(m_0, qml.RY)(y, wires=0)
    
    return qml.probs(wires=[0])

x = 1
y = 0.3
fig, ax = qml.draw_mpl(func)(x, y)
fig.savefig(os.path.join(os.getcwd(), "QNN_ex.jpg"))

print(func(x, y))

The func(x, y) returns [0.51116588 0.48883412].
It verifies that the qml.cond() works.

However, the figure still lacks of the midmeasure operation.
QNN_ex.jpg

Perhaps there are something wrong that I don’t notice.
I will search for the figure problem first.

Hi @q36111095, what version of PennyLane are you using? With the latest v0.34, I get the following drawing:
image

If you would like to upgrade your PennyLane version, you can do:

pip install -u pennylane

The version of pennylane I use is 0.33.1, and the matplotlib version is 3.6.1.

I have tried to upgrade pennylane version to 0.34.0; however, it returns some error about incompatibility with other libraries.
It seems that it is just the plot problem.
Thanks for your replying!

Hi @q36111095,

What is the incompatibility you’re seeing?

You could potentially try using qml.draw() instead.

Hi, @CatalinaAlbornoz , sorry for my late replying.
The error message of incompatibility is listed below:
(pennylane=0.34.0, pennylane-lightning=0.34.0)

import pennylane as qml File “/home/user/anaconda3/envs/ENV/lib/python3.8/site-packages/pennylane/init.py”, line 33, in import pennylane.templates File “/home/user/anaconda3/envs/ENV/lib/python3.8/site-packages/pennylane/templates/init.py”, line 21, in from .embeddings import * File “/home/user/anaconda3/envs/ENV/lib/python3.8/site-packages/pennylane/templates/embeddings/init.py”, line 20, in from .angle import AngleEmbedding File “/home/user/anaconda3/envs/ENV/lib/python3.8/site-packages/pennylane/templates/embeddings/angle.py”, line 19, in from pennylane.ops import RX, RY, RZ File “/home/user/anaconda3/envs/ENV/lib/python3.8/site-packages/pennylane/ops/init.py”, line 25, in from .op_math import * File “/home/user/anaconda3/envs/ENV/lib/python3.8/site-packages/pennylane/ops/op_math/init.py”, line 93, in from .adjoint import Adjoint, adjoint File “/home/user/anaconda3/envs/ENV/lib/python3.8/site-packages/pennylane/ops/op_math/adjoint.py”, line 24, in from pennylane.compiler import compiler File “/home/user/anaconda3/envs/ENV/lib/python3.8/site-packages/pennylane/compiler/init.py”, line 241, in from .compiler import available_compilers, available, active_compiler, active File “/home/user/anaconda3/envs/ENV/lib/python3.8/site-packages/pennylane/compiler/compiler.py”, line 109, in _refresh_compilers() File “/home/user/anaconda3/envs/ENV/lib/python3.8/site-packages/pennylane/compiler/compiler.py”, line 91, in _refresh_compilers else metadata.entry_points(group=“pennylane.compilers”) TypeError: entry_points() got an unexpected keyword argument ‘group’

In addition, I try to use qml.draw() with pennylane-0.33.1 pennylane-lightning-0.34.0,
and the function returns:
image

Hello, @CatalinaAlbornoz.
I recreate a new virtual envirionment, and reinstall pennylane=0.34.0.
I successfully plot a figure below.

I think the reason is that the version of some dependency libraries should be renewed when changing pennylane version.
Thanks for your help!!!

1 Like