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

Refactor thewalrus.quantum #197

Merged
merged 19 commits into from
Sep 14, 2020
Merged

Refactor thewalrus.quantum #197

merged 19 commits into from
Sep 14, 2020

Conversation

thisac
Copy link
Contributor

@thisac thisac commented Sep 4, 2020

Look in the quantum/__init__.py file to get an overview for how the functions have been moved around.

Context:
The Walrus quantum module (thewalrus.quantum) is currently a bit unwieldy due to the large number of functions in the same file. It needs to be refactored to a package with better organization.

Description of the Change:
thewalrus.quantum.py is split up into several files collected in a folder/submodule named quantum. This folder contains the following files:

  • fock_tensors.py
    Set of functions for calculating various state representations, probabilities and classical subsystems of Gaussian states.

  • adjacency_matrices.py
    Functions for rescaling adjacency matrices as well as calculating the Qmat covariance matrix from an adjacency matrix.

  • gaussian_state_tests.py
    Tests for various properties of covariance matrices as well as fidelity calculations for Gaussian states.

  • means_and_variances.py
    Functions for constructing/calculating the means, variances and covariances of Gaussian states.

  • photon_number_distributions.py
    Functions for calculating the photon number distributions of various states.

  • conversions.py
    Functions for transforming one type of covariance-matrix-like object into another as well as reducing a Gaussian state.

Furthermore, the following functions have been renamed, although the old names are still functional:

Means -> real_to_complex_displacements
Beta -> complex_to_real_displacements
prefactor -> _prefactor
gen_multi_mode_dist -> _convolve_squeezed_state_distribution
gen_single_mode_dist -> _squeezed_state_distribution
total_photon_num_dist_pure_state -> pure_state_distribution

Benefits:
The quantum submodule is much cleaner and clearer. Everything works the same way as it did before, only the names are updated.

Possible Drawbacks:
The function renaming shouldn't cause any issues since the old names still function, although removing the old names shouldn't either since neither TW nor SF seem to use any of the renamed functions.

There are currently no circular imports, although this could be an issue if adding more functions to these files. One has to be careful when doing so! :octocat:

Related GitHub Issues:
N/A

@thisac thisac requested a review from nquesada September 4, 2020 21:26
@thisac thisac self-assigned this Sep 4, 2020
@thisac thisac requested a review from josh146 September 9, 2020 14:18
@codecov
Copy link

codecov bot commented Sep 9, 2020

Codecov Report

Merging #197 into master will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##            master      #197   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           14        20    +6     
  Lines         1087      1131   +44     
=========================================
+ Hits          1087      1131   +44     
Impacted Files Coverage Δ
thewalrus/__init__.py 100.00% <100.00%> (ø)
thewalrus/quantum/__init__.py 100.00% <100.00%> (ø)
thewalrus/quantum/adjacency_matrices.py 100.00% <100.00%> (ø)
thewalrus/quantum/conversions.py 100.00% <100.00%> (ø)
thewalrus/quantum/fock_tensors.py 100.00% <100.00%> (ø)
thewalrus/quantum/gaussian_checks.py 100.00% <100.00%> (ø)
thewalrus/quantum/means_and_variances.py 100.00% <100.00%> (ø)
thewalrus/quantum/photon_number_distributions.py 100.00% <100.00%> (ø)
... and 5 more

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 49f675b...82cd63f. Read the comment docs.

Copy link
Collaborator

@nquesada nquesada left a comment

Choose a reason for hiding this comment

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

It's looking really good.

thisac and others added 3 commits September 9, 2020 15:50
@thisac thisac marked this pull request as ready for review September 9, 2020 21:40
@thisac thisac requested a review from nquesada September 9, 2020 21:40
Copy link
Member

@josh146 josh146 left a comment

Choose a reason for hiding this comment

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

Looks great @thisac! I'm happy to approve this in its current form, as it is already an organization improvement. The only things I have really earmarked are some module names which could be modified slightly.

Apart from that, everything else marked 'minor' are just possible minor improvements I've noticed that are worth considering, but could be more breaking for SF.

setup.py Show resolved Hide resolved
thewalrus/quantum/__init__.py Outdated Show resolved Hide resolved
thewalrus/quantum/__init__.py Outdated Show resolved Hide resolved
thewalrus/quantum/__init__.py Outdated Show resolved Hide resolved
thewalrus/quantum/adjacency_matrices.py Outdated Show resolved Hide resolved
return (hbar / 2) * cov


def Amat(cov, hbar=2, cov_is_qmat=False):
Copy link
Member

Choose a reason for hiding this comment

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

@nquesada how is the Amat related to the adjacency matrix again?

Copy link
Collaborator

Choose a reason for hiding this comment

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

the Amat is the adjacency matrix. Well, one of them, in the case of a pure state A = B \directsum B^* and sometimes we also call B the adjacency matrix. One way to get around this is to call A the Kernel matrix and B the adjacency matrix.

Copy link
Member

Choose a reason for hiding this comment

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

One way to get around this is to call A the Kernel matrix and B the adjacency matrix.

This sounds quite nice!

cov (array): :math:`2N\times 2N` covariance matrix
hbar (float): the value of :math:`\hbar` in the commutation
relation :math:`[\x,\p]=i\hbar`.
cov_is_qmat (bool): if ``True``, it is assumed that ``cov`` is in fact the Q matrix.
Copy link
Member

Choose a reason for hiding this comment

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

This is another inconsistency; the Amat function accepts either a covariance matrix or Q matrix depending on keyword arguments.

However, we have two separate Qmat functions; one that accepts an adjacency matrix, and another accepting a covariance matrix.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Are you suggesting to merge the later or separate the former?

Copy link
Member

Choose a reason for hiding this comment

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

I don't know 😆 just something I noticed. Would you have a preference?

thewalrus/quantum/gaussian_state_tests.py Outdated Show resolved Hide resolved
Comment on lines +15 to +16
Functions for constructing/calculating the means, variances and covariances of
Gaussian states.
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps a better name for this module is gaussian_statistics.py? with statistics implying means, variances, covariances, etc.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The problem with that name is that Gaussian statistics in the lay world simple means normally distributed things. The GBS distribution is anything but normal.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm... gaussian_state_statistics.py?

raise ValueError("The Gaussian state is not pure")


def _squeezed_state_distribution(s, cutoff=50, N=1):
Copy link
Member

Choose a reason for hiding this comment

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

Curious why this is hidden (has it always been hidden?) Sounds like it could be quite useful in some contexts

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It wasn't actually hidden before. I think that @nquesada mentioned it could be hidden, but I might have misunderstood.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It used to be not hidden. The main idea behind this is that most of the functions should take as arguments a cov matrix, this one does not.

@thisac thisac requested a review from josh146 September 10, 2020 20:05
Copy link
Member

@josh146 josh146 left a comment

Choose a reason for hiding this comment

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

This is a really nice improvement that should make Walrus maintenance and code review much easier going forward 😺

thewalrus/quantum/__init__.py Show resolved Hide resolved

.. autosummary::

pure_state_distribution
Copy link
Member

Choose a reason for hiding this comment

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

I almost want to suggest just making the two private functions public and putting them here. The Walrus is quite low-level, so it could always be the case that someone wants that functionality, but ends up coding it themselves just because they don't know it exists

Copy link
Collaborator

Choose a reason for hiding this comment

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

My argument against that is that almost all the Walrus functions take as arguments a covariance matrix (and possibly a vector of means). These other two functions take one or a few squeezing parameters.

thewalrus/quantum/__init__.py Outdated Show resolved Hide resolved
thewalrus/quantum/__init__.py Show resolved Hide resolved
thewalrus/quantum/__init__.py Outdated Show resolved Hide resolved
thisac and others added 3 commits September 14, 2020 12:35
Co-authored-by: Josh Izaac <josh146@gmail.com>
Co-authored-by: Josh Izaac <josh146@gmail.com>
Copy link
Collaborator

@nquesada nquesada left a comment

Choose a reason for hiding this comment

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

Great work @thisac . This module needed a cleanup and it is much more manageable after your refactor!

@thisac thisac merged commit 658e62a into master Sep 14, 2020
@thisac thisac deleted the ch328-the-walrus-quantum-refactor branch September 14, 2020 17:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants