# Sampling from GBS¶

Technical details are available in the API documentation: sf.apps.sample

A GBS device can be programmed to sample from any symmetric matrix $$A$$. To sample, we must specify the mean number of photons being generated in the device and optionally the form of detection used at the output: threshold detection or photon-number resolving (PNR) detection. Threshold detectors are restricted to measuring whether photons have arrived at the detector, whereas PNR detectors are able to count the number of photons. Photon loss can also be specified with the loss argument.

Sampling functionality is provided in the sample module.

Let’s take a look at both types of sampling methods. We can generate samples from a random 5-dimensional symmetric matrix:

from strawberryfields.apps import sample
import numpy as np

modes = 5
n_mean = 6
samples = 5

A = np.random.normal(0, 1, (modes, modes))
A = A + A.T

s_thresh = sample.sample(A, n_mean, samples, threshold=True)
s_pnr = sample.sample(A, n_mean, samples, threshold=False)

print(s_thresh)
print(s_pnr)


Out:

[[0, 1, 1, 1, 1], [0, 1, 1, 0, 1], [1, 0, 0, 1, 0], [0, 1, 1, 0, 0], [1, 0, 0, 0, 1]]
[[0, 3, 3, 0, 0], [1, 0, 0, 1, 0], [1, 1, 2, 2, 0], [0, 1, 0, 0, 1], [0, 0, 0, 0, 0]]


In each case, a sample is a sequence of integers of length five, i.e., len(modes) = 5. Threshold samples are 0’s and 1’s, corresponding to whether or not photons were detected in a mode. A 1 here is conventionally called a “click”. PNR samples are non-negative integers counting the number of photons detected in each mode. For example, suppose a PNR sample is [2, 1, 1, 0, 0], meaning that 2 photons were detected in mode 0, 1 photons were detected in modes 1 and 2, and 0 photons were detected in modes 3 and 4. If threshold detectors were used instead, the sample would be: [1, 1, 1, 0, 0].

A more general gaussian() function allows for sampling from arbitrary pure Gaussian states.

## Sampling subgraphs¶

So when would threshold detection or PNR detection be preferred in GBS? Since threshold samples can be post-processed from PNR samples, we might expect that PNR detection is always the preferred choice. However, in practice simulating PNR-based GBS is significantly slower, and it turns out that threshold samples can provide enough useful information for a range of applications.

Strawberry Fields provides tools for solving graph-based problems. In this setting, we typically want to use GBS to sample subgraphs, which are likely to be dense due to the probability distribution of GBS [44]. In this case, threshold sampling is enough, since it lets us select nodes of the subgraph. Let’s take a look at this by using a small fixed graph as an example:

from strawberryfields.apps import plot
import networkx as nx
import plotly

[
[0, 1, 0, 0, 1, 1],
[1, 0, 1, 0, 1, 1],
[0, 1, 0, 1, 1, 0],
[0, 0, 1, 0, 1, 0],
[1, 1, 1, 1, 0, 1],
[1, 1, 0, 0, 1, 0],
]
)