Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 15 commits into from
Jul 21, 2021

Conversation

nquesada
Copy link
Collaborator

@nquesada nquesada commented Jun 25, 2021

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,i_1,\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.

@nquesada nquesada requested a review from ziofil June 25, 2021 15:43
@codecov
Copy link

codecov bot commented Jun 25, 2021

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.

@nquesada nquesada requested a review from albi3ro July 8, 2021 16:36
Copy link
Contributor

@albi3ro albi3ro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly I'm just a little confused by what this function is trying to do.

So my first request is to improve the docstring.

thewalrus/quantum/fock_tensors.py Show resolved Hide resolved
thewalrus/quantum/fock_tensors.py Outdated Show resolved Hide resolved
thewalrus/quantum/fock_tensors.py Show resolved Hide resolved
thewalrus/quantum/fock_tensors.py Show resolved Hide resolved
thewalrus/quantum/fock_tensors.py Show resolved Hide resolved
thewalrus/quantum/fock_tensors.py Show resolved Hide resolved
Co-authored-by: Christina Lee <chrissie.c.l@gmail.com>
@nquesada nquesada requested a review from albi3ro July 9, 2021 22:44
Co-authored-by: Christina Lee <chrissie.c.l@gmail.com>
Co-authored-by: Christina Lee <chrissie.c.l@gmail.com>
Copy link
Contributor

@albi3ro albi3ro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since I know wheel builds can be flaky, I'm going to go ahead and approve.

Copy link
Contributor

@albi3ro albi3ro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixing some rendering issues.

But thanks for adding this documentation 💯

thewalrus/quantum/fock_tensors.py Outdated Show resolved Hide resolved
thewalrus/quantum/fock_tensors.py Outdated Show resolved Hide resolved
thewalrus/quantum/fock_tensors.py Outdated Show resolved Hide resolved
Co-authored-by: Christina Lee <chrissie.c.l@gmail.com>
Copy link
Contributor

@albi3ro albi3ro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed the itertools.product code snippet, but once that's merged, all good to go 👍

thewalrus/quantum/fock_tensors.py Outdated Show resolved Hide resolved
Co-authored-by: Christina Lee <chrissie.c.l@gmail.com>
@nquesada nquesada merged commit d89c570 into master Jul 21, 2021
@nquesada nquesada deleted the n_body_marginals branch July 21, 2021 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants