How to use a sf measurement operator with pennylane?

I want to use the sf.ops.MeasureThreshold in pennylane, how do I go forward with it?

This is what I am doing presently, but it won’t work as MeasureThreshold does not take a wires argument.

@qml.qnode(device)
def single_layer_receiver(beta=0):
    qml.Beamsplitter(0, 0, wires=["wire-1", "wire-2"])
    qml.Displacement(beta, 0, wires="wire-1")

    return qml.expval(sf.ops.MeasureThreshold(wires="wire-1"))

Hi @say4n! Thank you for your question!

The way you’re using it will have several problems because effectively MeasureThreshold doesn’t take wires as an argument, but also expval can only take an input that inherits from the PennyLane class Obervable, meaning you can’t use MeasureThreshold.

If you try this code you will see that MeasureThreshold is the wrong type.

import pennylane as qml
import strawberryfields as sf`

dev = qml.device("strawberryfields.fock", wires=2, cutoff_dim=10)

@qml.qnode(dev)
def single_layer_receiver(beta=0):
    qml.Beamsplitter(0, 0, wires=[0, 1])
    qml.Displacement(beta, 0, wires=0)
    print("MT type: ",type(sf.ops.MeasureThreshold(select=[1])))

    return qml.expval(qml.X(wires=0))

res=single_layer_receiver()
print(res)

MeasureThreshold is very similar to MeasureFock so maybe this tutorial can help you.
https://strawberryfields.ai/photonics/demos/run_post_selection.html

Please let me know if this helps!

1 Like

Thanks @say4n,

In case it is useful, here is a minimum working example of using sf.ops.MeasureThreshold() in StrawberryFields.

import numpy as np
import strawberryfields as sf

prog = sf.Program(1)
​
with prog.context as q:
    sf.ops.Sgate(0.7) | q[0]
    sf.ops.MeasureThreshold() | q[0]
​
eng = sf.Engine('gaussian')
​
result = eng.run(prog)
print(result.samples)
1 Like

Hi @CatalinaAlbornoz and @Diego thank you so much from the prompt responses, I could not revert back earlier as I was travelling.

I look forward to trying out your proposed solutions and asking more questions in the forums!

Cheers,
Sayan

Hi @say4n, I hope your trip went well! Let us know how it goes trying out Diego’s solution :smiley: