Any qml.device supporting immediate values for mid-circuit measurements?

Hi colleagues! I am interested to know are there any native-Python (see below) quantum devices, either built-in or from a third-party plugin, which would return me a “real” sampled value of a mid-circuit measurement rather than a MeasurementValue.

In essence, I’d like the following code to work without qml.cond

import pennylane as qml

@qml.qnode(qml.device(???))  # Anything ? 
def circuit():
  qml.PauliX(wires=0)
  m1 = qml.measure(0)
  if m1: # NOT qml.cond(m1==1, qml.PauliZ)(wires=0)
    qml.PauliZ(wires=0)
  return qml.state()

print(circuit())

I would like the above code to return the sampled final states, possibly varying from shot to shot.

I am aware of the Catalyst approach which surprisingly does what I need. However, in my actual problem, I would like to pass m1 to a non-traceable Python procedure (and the callbacks look like an overhead), so that is why I’m asking about a native device.

Hi @sergeimironov , welcome to the Forum!

Unfortunately you cannot return the state in the way that you want.

However, you can take samples from the measurement and the final state as shown with the code below:

import pennylane as qml

@qml.qnode(qml.device("default.qubit", wires=1, shots=10))
def circuit():
  qml.Hadamard(wires=0)
  m1 = qml.measure(0)
  qml.cond(m1==1, qml.PauliX)(wires=0)
  return qml.sample(op=m1), qml.sample()

print(circuit())

The output here is composed of two arrays. The first one shows “real” sample values of the mid-circuit measurement, while the second array shows the final measurement (all zeros in this case because of how we used the PauliX gate).

(array([1, 0, 0, 0, 1, 1, 1, 1, 0, 0]), array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]))

Let us know if this works for what you need!

Thank you for the answer. Please count my voice in favor of this feature then!

Thanks for sharing your feedback @sergeimironov !

Would you like to create a new feature request in PennyLane’s GitHub repo?

It’s generally better if you create it so that we can keep you updated and ask you follow-up questions if needed.

However I can make it if you prefer not to.