XanaduAI / thewalrus

A library for the calculation of hafnians, Hermite polynomials and Gaussian boson sampling.
https://the-walrus.readthedocs.io
Apache License 2.0
100 stars 55 forks source link

Calculation of n-body marginals of a GBS distribution #253

Closed nquesada closed 3 years ago

nquesada commented 3 years ago

Calculation of n-body marginals of a GBS distribution.

For an M-mode Gaussian state there exists a photon number distribution with probability mass function

p[i_0,i1,\ldots, i{M-1}]

The function n_body_marginals calculates the first n-body marginals of the (all-mode) probability distribution p. The n=1 marginals or single body marginals are simply the probability that mode k has i photons, i.e. p_k[i].

For n=2 one obtains the two-body probabilities. For two modes k and l this is a two dimensional probability distribution p_{k,l}[i,j]

Note that these marginals have interesting permutation properties, for example p{k,l}[i,j] = p{l,k}[j,i].

The function provided here takes advantage of these symmetries to minimize the amount of calculations.

The return of the function is a list of tensors where the first entry contains the one-body marginals of the M modes (giving a tensor of shape (M, cutoff)), the second entry contains the two-body marginals (giving a tensor of shape (M,M,cutoff, cutoff)) and so on and so forth.

To be clever about not calculating things that can be obtained by permutations it checks whether the index vector representing the modes is sorted. From the way itertools.product works we know that it will always a sorted index vector before generating any of its unordered permutations. Thus whenever the index vector is ordered we perform the numerical calculation.

If it is a unsorted index vector it realizes, in line 671 that it can be obtained by permuting the marginal distribution of something that has already been calculated.

Finally, one extra thing about notation, we take a mode index vector with repetitions, for example (1,1,2,2) to not mean a 4 body marginal but rather a two-boyd marginal for modes (1,2). That is why the ind variable is turned into a set in line 666.

We use OrderedDict to remove repeated elements from a list and we use argsort to find the correct permutations allowing to figure how to permute the k-body marginal of a sorted index vector into an unsorted one.

codecov[bot] commented 3 years ago

Codecov Report

Merging #253 (c11f996) into master (46f80c5) will not change coverage. The diff coverage is 100.00%.

@@            Coverage Diff            @@
##            master      #253   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           21        21           
  Lines         1319      1337   +18     
=========================================
+ Hits          1319      1337   +18     
Impacted Files Coverage Δ
thewalrus/quantum/__init__.py 100.00% <ø> (ø)
thewalrus/quantum/fock_tensors.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 46f80c5...c11f996. Read the comment docs.