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

Compact fock #154

Merged
merged 49 commits into from
Feb 28, 2023
Merged

Compact fock #154

merged 49 commits into from
Feb 28, 2023

Conversation

rdprins
Copy link
Collaborator

@rdprins rdprins commented Sep 9, 2022

Context: Make PNR sampling faster for Gaussian circuits when using density matrices. This is done by applying the recurrence relation in a selective manner such that useless (off-diagonal) amplitudes from the Fock representation are not calculated. When all modes are detected, math.hermite_renormalized can be replaced by math.hermite_renormalized_diagonal. In case all but the first mode are detected, math.hermite_renormalized_1leftoverMode can be used. The complexity of these new methods is equal to performing a pure state simulation. The methods are differentiable, such that they can be used for defining costfunctions.

Description of the Change: Adds the function math.hermite_renormalized_diagonal and math.hermite_renormalized_1leftoverMode.

Benefits: Faster simulation and optimization of Gaussian circuits with PNR detectors when using density matrices.

@rdprins rdprins added the WIP work in progress label Sep 9, 2022
Code now works without displacement and without numba (in the diagonal case)
…ment. leftover mode case works with numba, without strides, without displacement
@codecov
Copy link

codecov bot commented Sep 15, 2022

Codecov Report

Merging #154 (1e5881d) into develop (8f17c23) will decrease coverage by 6.67%.
The diff coverage is 43.40%.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #154      +/-   ##
===========================================
- Coverage    82.24%   75.57%   -6.67%     
===========================================
  Files           35       42       +7     
  Lines         3171     3816     +645     
===========================================
+ Hits          2608     2884     +276     
- Misses         563      932     +369     
Impacted Files Coverage Δ
...stard/math/numba/compactFock_1leftoverMode_grad.py 14.97% <14.97%> (ø)
mrmustard/math/numba/compactFock_diagonal_grad.py 17.47% <17.47%> (ø)
...stard/math/numba/compactFock_1leftoverMode_amps.py 23.57% <23.57%> (ø)
mrmustard/math/numba/compactFock_diagonal_amps.py 33.78% <33.78%> (ø)
...rmustard/math/numba/compactFock_helperFunctions.py 45.00% <45.00%> (ø)
mrmustard/utils/circdrawer.py 92.00% <92.00%> (ø)
mrmustard/physics/fock.py 86.38% <93.33%> (+1.80%) ⬆️
mrmustard/__init__.py 97.01% <100.00%> (+0.04%) ⬆️
mrmustard/lab/abstract/state.py 86.25% <100.00%> (-0.72%) ⬇️
mrmustard/lab/abstract/transformation.py 74.48% <100.00%> (ø)
... and 15 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 8f6d70d...1e5881d. Read the comment docs.

@sduquemesa sduquemesa changed the base branch from main to develop October 13, 2022 18:39
@sduquemesa
Copy link
Contributor

This is a valuable PR! It be nice to reactivate its development. Apart from syncing it with the latest version of MM, what else is missing?

@rdprins rdprins removed the WIP work in progress label Feb 23, 2023
@rdprins rdprins requested a review from ziofil February 23, 2023 09:12
@ziofil ziofil marked this pull request as ready for review February 27, 2023 23:46
.github/CHANGELOG.md Outdated Show resolved Hide resolved
.github/CHANGELOG.md Outdated Show resolved Hide resolved
.gitignore Outdated Show resolved Hide resolved
ziofil and others added 8 commits February 28, 2023 00:22
**Context:**
autocutoffs are a bit flimsy, it would be nice to have a norm-based
autocutoff

**Description of the Change:**
Uses Robbe's new function to improve autocutoff: each mode now gets a
cutoff such that if it were the marginal it would capture
`settings.AUTOCUTOFF_PROBABILITY` probability (default 0.999).

**Benefits:**
Much more sensible autocutoffs

**Possible Drawbacks:**
Some users may need to update their code

**Related GitHub Issues:**
perhaps [#118](#118)
@ziofil ziofil merged commit 2b9b182 into develop Feb 28, 2023
@ziofil ziofil deleted the compactFock branch February 28, 2023 17:14
@ziofil ziofil mentioned this pull request Mar 7, 2023
ziofil added a commit that referenced this pull request Mar 8, 2023
# Release 0.4.0

### New features

* Ray-based distributed trainer is now added to `training.trainer`. It
acts as a replacement
for `for` loops and enables the parallelization of running many circuits
as well as their
optimizations. To install the extra dependencies: `pip install .[ray]`.
  [(#194)](#194)

  ```python
  from mrmustard.lab import Vacuum, Dgate, Ggate
  from mrmustard.physics import fidelity
  from mrmustard.training.trainer import map_trainer

  def make_circ(x=0.):
return Ggate(num_modes=1, symplectic_trainable=True) >> Dgate(x=x,
x_trainable=True, y_trainable=True)

  def cost_fn(circ=make_circ(0.1), y_targ=0.):
      target = Gaussian(1) >> Dgate(-1.5, y_targ)
      s = Vacuum(1) >> circ
      return -fidelity(s, target)

# Use case 0: Calculate the cost of a randomly initialized circuit 5
times without optimizing it.
  results_0 = map_trainer(
      cost_fn=cost_fn,
      tasks=5,
  )

# Use case 1: Run circuit optimization 5 times on randomly initialized
circuits.
  results_1 = map_trainer(
      cost_fn=cost_fn,
      device_factory=make_circ,
      tasks=5,
      max_steps=50,
      symplectic_lr=0.05,
  )

# Use case 2: Run circuit optimization 2 times on randomly initialized
circuits with custom parameters.
  results_2 = map_trainer(
      cost_fn=cost_fn,
      device_factory=make_circ,
      tasks=[
{'x': 0.1, 'euclidean_lr': 0.005, 'max_steps': 50, 'HBAR': 1.},
          {'x': -0.7, 'euclidean_lr': 0.1, 'max_steps': 2, 'HBAR': 2.},
      ],
      y_targ=0.35,
      symplectic_lr=0.05,
      AUTOCUTOFF_MAX_CUTOFF=7,
  )
  ```

* Sampling for homodyne measurements is now integrated in Mr Mustard:
when no measurement outcome
value is specified by the user, a value is sampled from the reduced
state probability distribution
  and the conditional state on the remaining modes is generated.
  [(#143)](#143)

  ```python
  import numpy as np
  from mrmustard.lab import Homodyne, TMSV, SqueezedVacuum

  # conditional state from measurement
conditional_state = TMSV(r=0.5, phi=np.pi)[0, 1] >>
Homodyne(quadrature_angle=np.pi/2)[1]

  # measurement outcome
  measurement_outcome = SqueezedVacuum(r=0.5) >> Homodyne()
  ```

* The optimizer `minimize` method now accepts an optional callback
function, which will be called
at each step of the optimization and it will be passed the step number,
the cost value,
and the value of the trainable parameters. The result is added to the
`callback_history`
  attribute of the optimizer.
  [(#175)](#175)

* the Math interface now supports linear system solving via
`math.solve`.
  [(#185)](#185)

* We introduce the tensor wrapper `MMTensor` (available in
`math.mmtensor`) that allows for
a very easy handling of tensor contractions. Internally MrMustard
performs lots of tensor
contractions and this wrapper allows one to label each index of a tensor
and perform
contractions using the `@` symbol as if it were a simple matrix
multiplication (the indices
  with the same name get contracted).
  [(#185)](https://github.com/XanaduAI/MrMustard/pull/185)<br>
  [(#195)](#195)

  ```python
  from mrmustard.math.mmtensor import MMTensor

  # define two tensors
A = MMTensor(np.random.rand(2, 3, 4), axis_labels=["foo", "bar",
"contract"])
B = MMTensor(np.random.rand(4, 5, 6), axis_labels=["contract", "baz",
"qux"])

  # perform a tensor contraction
  C = A @ B
  C.axis_labels  # ["foo", "bar", "baz", "qux"]
  C.shape # (2, 3, 5, 6)
  C.tensor # extract actual result
  ```

* MrMustard's settings object (accessible via `from mrmustard import
settings`) now supports
`SEED` (an int). This will give reproducible results whenever randomness
is involved.
The seed is assigned randomly by default, and it can be reassigned again
by setting it to None:
`settings.SEED = None`. If one desires, the seeded random number
generator is accessible directly
  via `settings.rng` (e.g. `settings.rng.normal()`).
  [(#183)](#183)

* The `Circuit` class now has an ascii representation, which can be
accessed via the repr method.
It looks great in Jupyter notebooks! There is a new option at
`settings.CIRCUIT_DECIMALS`
which controls the number of decimals shown in the ascii representation
of the gate parameters.
  If `None`, only the name of the gate is shown.
  [(#196)](#196)

* PNR sampling from Gaussian circuits using density matrices can now be
performed faster.
When all modes are detected, this is done by replacing
`math.hermite_renormalized` by `math.hermite_renormalized_diagonal`. If
all but the first mode are detected,
  `math.hermite_renormalized_1leftoverMode` can be used.
The complexity of these new methods is equal to performing a pure state
simulation.
The methods are differentiable, so that they can be used for defining a
cost function.
  [(#154)](#154)

* MrMustard repo now provides a fully furnished vscode development
container and a Dockerfile. To
find out how to use dev containers for development check the
documentation
  [here](https://code.visualstudio.com/docs/devcontainers/containers).
  [(#214)](#214)

### Breaking changes

### Improvements

* The `Dgate` is now implemented directly in MrMustard (instead of on
The Walrus) to calculate the
unitary and gradients of the displacement gate in Fock representation,
providing better numerical
  stability for larger cutoff and displacement values.
  [(#147)](#147)
  [(#211)](#211)

* Now the Wigner function is implemented in its own module and uses
numba for speed.
  [(#171)](#171)

  ```python
    from mrmustard.utils.wigner import wigner_discretized
    W, Q, P = wigner_discretized(dm, q, p) # dm is a density matrix
  ```

* Calculate marginals independently from the Wigner function thus
ensuring that the marginals are
physical even though the Wigner function might not contain all the
features of the state
within the defined window. Also, expose some plot parameters and return
the figure and axes.
  [(#179)](#179)

* Allows for full cutoff specification (index-wise rather than
mode-wise) for subclasses
of `Transformation`. This allows for a more compact Fock representation
where needed.
  [(#181)](#181)

* The `mrmustard.physics.fock` module now provides convenience functions
for applying kraus
  operators and choi operators to kets and density matrices.
  [(#180)](#180)

  ```python
from mrmustard.physics.fock import apply_kraus_to_ket,
apply_kraus_to_dm, apply_choi_to_ket, apply_choi_to_dm
  ket_out = apply_kraus_to_ket(kraus, ket_in, indices)
  dm_out = apply_choi_to_dm(choi, dm_in, indices)
  dm_out = apply_kraus_to_dm(kraus, dm_in, indices)
  dm_out = apply_choi_to_ket(choi, ket_in, indices)
  ```

* Replaced norm with probability in the repr of `State`. This improves
consistency over the
old behaviour (norm was the sqrt of prob if the state was pure and prob
if the state was mixed).
  [(#182)](#182)

* Added two new modules (`physics.bargmann` and `physics.husimi`) to
host the functions related
to those representations, which have been refactored and moved out of
`physics.fock`.
  [(#185)](#185)

* The internal type system in MrMustard has been beefed up with much
clearer types, like ComplexVector,
RealMatrix, etc... as well as a generic type `Batch`, which can be
parametrized using the other types,
like `Batch[ComplexTensor]`. This will allow for better type checking
and better error messages.
  [(#199)](#199)

* Added multiple tests and improved the use of Hypothesis.
  [(#191)](#191)

* The `fock.autocutoff` function now uses the new diagonal methods for
calculating a
probability-based cutoff. Use `settings.AUTOCUTOFF_PROBABILITY` to set
the probability threshold.
  [(#203)](#203)

* The unitary group optimization (for the interferometer) and the
orthogonal group optimization
(for the real interferometer) have been added. The symplectic matrix
that describes an
interferometer belongs to the intersection of the orthogonal group and
the symplectic group,
  which is a unitary group, so we needed both.
  [(#208)](#208)

### Bug fixes

* The `Dgate` and the `Rgate` now correctly parse the case when a single
scalar is intended
  as the same parameter of a number of gates in parallel.
  [(#180)](#180)

* The trace function in the fock module was giving incorrect results
when called with certain
  choices of modes. This is now fixed.
  [(#180)](#180)

* The purity function for fock states no longer normalizes the density
matrix before computing
  the purity.
  [(#180)](#180)

* The function `dm_to_ket` no longer normalizes the density matrix
before diagonalizing it.
  [(#180)](#180)

* The internal fock representation of states returns the correct cutoffs
in all cases
  (solves an issue when a pure dm was converted to ket).
  [(#184)](#184)

* The ray related tests were hanging in github action causing tests to
halt and fail.
Now ray is forced to init with 1 cpu when running tests preventing the
issue.
  [(#201)](#201)

* Various minor bug fixes.
  [(#202)](#202)

* Fixed the issue that the optimization of the interferometer was using
orthogonal group
  optimization rather than unitary.
  [(#208)](#208)

* Fixes a slicing issue that arises when we compute the fidelity between
gaussian and fock states.
  [(#210)](#210)

* The sign of parameters in the circuit drawer are now displayed
correctly.
  [(#209)](#209)

* Fixed a bug in the Gaussian state which caused its covariance matrix
to be multiplied
  by hbar/2 twice. Adds the argument `modes` to `Ggate`.
  [(#212)](#212)

* Fixes a bug in the cutoffs of the choi operator.
  [(#216)](#216)


### Documentation

### Contributors

This release contains contributions from (in alphabetical order):
[Robbe De Prins](https://github.com/rdprins), [Sebastian Duque
Mesa](https://github.com/sduquemesa),
[Filippo Miatto](https://github.com/ziofil), [Zeyue
Niu](https://github.com/zeyueN),
[Yuan Yao](https://github.com/sylviemonet)

---------

Co-authored-by: Sebastián Duque Mesa <675763+sduquemesa@users.noreply.github.com>
Co-authored-by: JacobHast <jacobhastrup@gmail.com>
Co-authored-by: elib20 <53090166+elib20@users.noreply.github.com>
Co-authored-by: Luke Helt <31250931+heltluke@users.noreply.github.com>
Co-authored-by: zeyueN <48225584+zeyueN@users.noreply.github.com>
Co-authored-by: Robbe De Prins <52749580+rdprins@users.noreply.github.com>
Co-authored-by: Robbe De Prins (UGent-imec) <Robbe.DePrins@UGent.be>
Co-authored-by: Yuan <16817699+sylviemonet@users.noreply.github.com>
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.

3 participants