 Sample NumberOperator gives non integer values

Hello all,

I nee some help to clarify the number operator. I thought that this would give me the number of photons, i.e. an integer. However, if i set shots =1 (to have samples) and compute the expectation value, i get non integers values.
For instance

dev = qml.device("strawberryfields.fock", wires=1, cutoff_dim=30,shots=1)

@qml.qnode(dev)
def quantum_neural_net(var):
qml.CoherentState(var,var,wires = 0)
return qml.expval(qml.NumberOperator(0))

i would expect samples according to the Poisson(3) distribution and indeed the expectation value is 3 but the individual samples are non integer, like below:

>>> print(quantum_neural_net([np.sqrt(3),0]))
1.596027833083245

How can i do to get integer when sampling (shots =1 )?
Thanks
Oriel

Hi @Oriel!

Unfortunately this is a bug relating to how expval with finite-shots is implemented internally in the strawberryfields.fock plugin. This is something the team is aware off, and is documented here: https://github.com/PennyLaneAI/pennylane-sf/issues/69

Essentially, the reason for this is that the PennyLane-SF plugin is currently always computing the analytic expectation value, and then using the Central limit theorem to sample from a normal distribution

N(\mathbb{E}[\hat{N}], \mathbb{E}[\hat{N}]/S)

(where S is the number of shots). This is a good approximation for a large number of finite-shots, but as you note, becomes less than ideal for S=1.

We should definitely modify this logic to match other plugins. For finite shots, the plugin should instead compute samples directly, and then average these samples to determine the mean.

(Further, this behaviour also leads to a discrepancy between hardware devices — which do currently average samples — and simulators. The above QNode will in fact return discrete samples if executed on the X8 chip.)