Sampling for strawberryfields.fock pennylane backend

I’m using the strawberryfields plugin for pennylane, specifically the strawberryfields.fock backend. Is there a way to return all measurement samples (over all wires) given a certain number of shots?

Thank you!

Hi @schererant,

Welcome to the Forum!

You can specify the number of shots when you define the device. At the return of the qnode you perform your measurements so you can return a list with the measurements over the range of wires. Below is a code example. Let me know if you run into any issues while trying this. And remember that the PennyLane-SF plugin is only supported up to version 0.29 of PennyLane.

import pennylane as qml

n_wires = 3
dev = qml.device("strawberryfields.fock", wires=n_wires, shots=100, cutoff_dim=10)

@qml.qnode(dev)
def quantum_neural_net(x, y, z):
    # Encode input x into quantum state
    qml.Displacement(x, 0.0, wires=0)
    qml.Displacement(y, 0.0, wires=1)
    qml.Displacement(z, 0.0, wires=2)

    return [qml.expval(qml.NumberOperator(i)) for i in range(n_wires)]

quantum_neural_net(0.0, 1.0, 2.0)

I hope this helps!

Hi Catalina,

Thanks :slight_smile:

But this only returns the expected number of photons for each mode or? I would need a measurement that returns (to take your example) a measurement of the 6 modes for every single shot. For example measurements with [2, 0] and [0, 2] would just result in [1,1] with expval.

Hi @schererant,

If you need the samples then the best option I can think of is setting shots=1.
You will need to use a for loop or something similar if you want to get several sets of samples.

I hope this helps!