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

release v0.5.0 #265

Merged
merged 57 commits into from
Jul 27, 2023
Merged

release v0.5.0 #265

merged 57 commits into from
Jul 27, 2023

Conversation

ziofil
Copy link
Collaborator

@ziofil ziofil commented Jul 26, 2023

Release 0.5.0

New features

  • Optimization callback functionalities has been improved. A dedicated Callback class is added which
    is able to access the optimizer, the cost function, the parameters as well as gradients, during the
    optimization. In addition, multiple callbacks can be specified. This opens up the endless possiblities
    of customizing the the optimization progress with schedulers, trackers, heuristics, tricks, etc.
    (#219)

  • Tensorboard-based optimization tracking is added as a builtin Callback class: TensorboardCallback.
    It can automatically track costs as well as all trainable parameters during optimization in realtime.
    Tensorboard can be most conveniently viewed from VScode.
    (#219)

    import numpy as np
    from mrmustard.training import Optimizer, TensorboardCallback
    
    def cost_fn():
        ...
    
    def as_dB(cost):
        delta = np.sqrt(np.log(1 / (abs(cost) ** 2)) / (2 * np.pi))
        cost_dB = -10 * np.log10(delta**2)
        return cost_dB
    
    tb_cb = TensorboardCallback(cost_converter=as_dB, track_grads=True)
    
    opt = Optimizer(euclidean_lr = 0.001);
    opt.minimize(cost_fn, max_steps=200, by_optimizing=[...], callbacks=tb_cb)
    
    # Logs will be stored in `tb_cb.logdir` which defaults to `./tb_logdir/...` but can be customized.
    # VScode can be used to open the Tensorboard frontend for live monitoring.
    # Or, in command line: `tensorboard --logdir={tb_cb.logdir}` and open link in browser.
  • Gaussian states support a bargmann method for returning the bargmann representation.
    (#235)

  • The ket method of State now supports new keyword arguments max_prob and max_photons.
    Use them to speed-up the filling of a ket array up to a certain probability or total photon number.
    (#235)

    from mrmustard.lab import Gaussian
    
    # Fills the ket array up to 99% probability or up to the |0,3>, |1,2>, |2,1>, |3,0> subspace, whichever is reached first.
    # The array has the autocutoff shape, unless the cutoffs are specified explicitly.
    ket = Gaussian(2).ket(max_prob=0.99, max_photons=3)
  • Gaussian transformations support a bargmann method for returning the bargmann representation.
    (#239)

  • BSGate.U now supports method='vanilla' (default) and 'schwinger' (slower, but stable to any cutoff)
    (#248)

Breaking Changes

  • The previous callback argument to Optimizer.minimize is now callbacks since we can now pass
    multiple callbacks to it.
    (#219)

  • The opt_history attribute of Optimizer does not have the placeholder at the beginning anymore.
    (#235)

Improvements

  • The math module now has a submodule lattice for constructing recurrence relation strategies in the Fock lattice.
    There are a few predefined strategies in mrmustard.math.lattice.strategies.
    (#235)

  • Gradients in the Fock lattice are now computed using the vector-jacobian product.
    This saves a lot of memory and speeds up the optimization process by roughly 4x.
    (#235)

  • Tests of the compact_fock module now use hypothesis.
    (#235)

  • Faster implementation of the fock representation of BSgate, Sgate and SqueezedVacuum, ranging from 5x to 50x.
    (#239)

  • More robust implementation of cutoffs for States.
    (#239)

  • Dependencies and versioning are now managed using Poetry.
    (#257)

Bug fixes

  • Fixed a bug that would make two progress bars appear during an optimization
    (#235)

  • The displacement of the dual of an operation had the wrong sign
    (#239)

  • When projecting a Gaussian state onto a Fock state, the upper limit of the autocutoff now respect the Fock projection.
    (#246)

  • Fixed a bug for the algorithms that allow faster PNR sampling from Gaussian circuits using density matrices. When the
    cutoff of the first detector is equal to 1, the resulting density matrix is now correct.

Documentation

Contributors

Filippo Miatto, Zeyue Niu,
Robbe De Prins, Gabriele Gullì,
Richard A. Wolf

sduquemesa and others added 30 commits October 14, 2022 15:19
**Context:**
Now the default branch for Mr Mustard repository is develop.

**Description of the Change:**
This PR updates the CI checks so that
- Every PR pointing to develop is checked using python 3.8
- Every PR pointing to develop is built and checked using python 3.8, 3.9, 3.10 so that code is surely releasable
- adds codeowners file
- adds `.coveragerc` file to avoid some non-sense codecov issues

**Benefits:**
CI checks now will be in sync with MrMustard development policies, this also reduces usage of CI checks (meaning less bills 💸 and less CO2 ☘️). Also, codecov should be less annoying from now.

**Possible Drawbacks:**
We'll have to wait for a full release cycle to check that all workflows are working as expected
**Context:**
Currently homodyne and heterodyne measurements on Mr Mustard require
user-specified outcome values.

**Description of the Change:**
This PR implements sampling for homodyne and heterodyne measurements:
when no measurement outcome value is specified (`result=None` for
homodyne and `x, y = None, None` for heterodyne), the outcome is sampled
from the measurement probability distribution and the conditional state
(conditional on the outcome) on the remaining modes is generated.

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

    # conditional state from measurement
    conditional_state1 = TMSV(r=0.5, phi=np.pi)[0, 1] >> Homodyne(quadrature_angle=np.pi/2, result=None)[1]
    conditional_state2 = TMSV(r=0.5, phi=np.pi)[0, 1] >> Heterodyne(x=None, y=None)[1]

    # outcome probability
    outcome_prob1 = SqueezedVacuum(r=0.5) >> Homodyne(result=None)
    outcome_prob2 = SqueezedVacuum(r=0.5) >> Heterodyne(x=None, y=None)
```

To do so:
- `tensorflow-probability==0.17.0` is added to Mr Mustard's
dependencies. [TensorFlow
Probability](https://www.tensorflow.org/probability/overview) provides
integration of probabilistic methods with automatic differentiation and
hardware acceleration (GPUs). Pytorch also provides similar
functionality through the
[`torch.distributions`](https://pytorch.org/docs/stable/distributions.html)
module.
- For gaussian states 𝛠, samples are drawn from the gaussian PDF
generated by a highly squeezed state ξ and the state of interest, i.e.,
`PDF=Tr[𝛠 ξ]`. Sampling from the distribution is implemented using
TensorFlow's multivariate normal distribution.
- For Homodyne only: To calculate the pdf for Fock states the
q-quadrature eigenfunctions `|x><x|` are used: `PDF=Tr[𝛠 |x><x|]`.
Sampling from the distribution uses TensorFlow's categorical
distribution. This case is inspired by the [implementation on
strawberryfields](https://github.com/XanaduAI/strawberryfields/blob/9a9a352b5b8cf7b2915e45d1538b51d7d306cfc8/strawberryfields/backends/tfbackend/circuit.py#L818-L926).

**Benefits:**
Sampling from homodyne measurements! 🎲

**Possible Drawbacks:**
Sampling for Fock states can be improved to reduce overheads on
execution: currently the pdf is recalculated every time a measurement is
performed leading to unnecessary slow-downs. To improve this, one can
use sampling algorithms that do not require the complete calculation of
the pdf, for example: using the cumulative distribution (however this is
highly dependent on normalization), rejection sampling,
Metropolis-Hasting, etc.

Co-authored-by: JacobHast <[email protected]>
Co-authored-by: elib20 <[email protected]>
**Context:**
The rotation gate unitary is currently being calculated using the Choi
isomorphisms which generates `nan` for angles of value zero where the
identity matrix should be returned.

**Description of the Change:**
The rotation gate being diagonal in the Fock basis is easy to implement
with better speed performance. This PR implements the `U` method of the
rotation gate overriding the default way of calculating the unitaries of
Gaussian transforms.

**Benefits:**
Fast computation of the rotation gate Fock representation which avoids
invalid numerical outcomes.

**Possible Drawbacks:**
None
**Context:**
[The Walrus PR #351](XanaduAI/thewalrus#351)
implemented a more stable calculation of the displacement unitary.

**Description of the Change:**
The `Dgate` now uses The Walrus to calculate the unitary and gradients
of the displacement gate.

Before:

![before](https://user-images.githubusercontent.com/675763/193073762-42a82fe8-7bcf-405b-ade0-e59e4fcdf270.png)

After:

![after](https://user-images.githubusercontent.com/675763/193073791-e573381f-3e97-4dc1-97e5-161897b34f0d.png)




**Benefits:**
This provides better numerical stability for larger cutoff and
displacement values.

**Possible Drawbacks:**
None

**Related GitHub Issues:**
None
------------------------------------------------------------------------------------------------------------

**Context:**
Math interface

**Description of the Change:**
Adds `eye_like` function, which returns the identity matrix to match the
shape and dtype of the argument.

**Benefits:**
Simpler to get the identity of the right size and dtype

**Possible Drawbacks:**
Not standard

**Related GitHub Issues:**
None
**Context:**
Calculation of the Wigner function is already included in Mr Mustard but
hidden in the graphics module.

**Description of the Change:**
This PR moves the Wigner function calculation to its own module and
numbifies it.

**Benefits:**
Now you can calculate the Wigner function using
```python
from mrmustard.utils.wigner import wigner_discretized

wigner_discretized(dm, q, p) # dm is a density matrix
```

It should be faster as well because it uses numba jit.

**Possible Drawbacks:**
None
**Context:**
There was a bug in the gradient computation of the Gate

**Description of the Change:**
Bug is fixed (there was a numpy array at some point that was breaking
the chain rule)

**Benefits:**
It works

**Possible Drawbacks:**
None

**Related GitHub Issues:**
None
**Context:**
settings are not easy do discover

**Description of the Change:**
the settings object has now a nice repr

**Benefits:**
users can see al the settings at once

**Possible Drawbacks:**
None I can think of

**Related GitHub Issues:**
None

<img width="513" alt="Screen Shot 2022-11-08 at 9 55 08 AM"
src="https://user-images.githubusercontent.com/8944955/200597522-66ebfa0f-dd87-4109-982f-c8d3231823f8.png">

Co-authored-by: Sebastián Duque Mesa <[email protected]>
**Context:**
PR #143 introduced sampling for homodyne measurements and with it a
bunch of functionality in the `utils/homodyne.py` module. Most of this
functions are related to the sampling in fock representation and are not
only useful for homodyne sampling — take for example the calculation of
the quadrature distribution.

**Description of the Change:**
This PR refactors the homodyne module into:

- `physics.fock` — functions related to the fock representation were
moved into this module. Some of them are even split and written as
separated functions such that they are available for use in other
contexts (for example `quadrature_distribution` and
`oscillator_eigenstate`).
- `math.caching` — the cache decorator used for the Hermite polys is
refactored into this new module, this decorator is not only applicable
to the Hermite polys but also to any function taking a 1D tensor + int
parameters. Now it can be used generically by any function with this
same signature. The idea of this module is to contain caching functions
of this kind.

Also

- Hermite polynomials have the modified flag removed (as per
[this](#143 (review))
comment) and now only the regular polynomials are used.

_Note:_ This PR only refactors code, meaning where the code is placed
and _not_ what it does and _nor_ how it is done; there is no change to
the logic whatsoever.

**Benefits:**
Pieces of the code that were there already are now reusable. The code
for the sampling logic is more readable now.

**Possible Drawbacks:**
None

**Related GitHub Issues:**
None
**Context:**
Currently marginals are calculated form the Wigner function. In cases in
which not all features of the state are captured on the Wigner function,
the marginals contain negativities hence not representing a true
probability density.

**Description of the Change:**
This PR makes `mikkel_plot` 
- 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,
- expose the `ticks`, `tick_labels` and `grid` arguments to customize
the visualization — useful for example when checking that your state has
peaks in those dreaded multiples of √(π)
- return the figure and axes for further processing if needed

**Benefits:**

- Marginals in the visualization will always represent physical states

_before_: note how this lion is not fully displayed in the Wigner
function leading to negativities in the marginal distributions

<img width="399" alt="image"
src="https://user-images.githubusercontent.com/675763/202817119-1824ecc0-139b-42ef-8461-8eba3ebf5405.png">

_after_: although the big kittie is not displayed in its full glory its
marginals show the true probability distribution

<img width="390" alt="image"
src="https://user-images.githubusercontent.com/675763/202817226-b06560e6-d481-4530-af0f-010cfafb6885.png">

- More configurability of the visualization

```python
from matplotlib import cm
ticks = [-2*np.sqrt(2),2*np.sqrt(2)]
labels = [r"$-2\sqrt{\hbar}$", r"$2\sqrt{\hbar}$"]
graphics.mikkel_plot(dm, xticks=ticks, yticks=ticks, xtick_labels=labels, ytick_labels=[r"$-2\sqrt{\hbar}$", r"$2\sqrt{\hbar}$"], grid=True, cmap=cm.PuOr)
```


![image](https://user-images.githubusercontent.com/675763/203838559-06b81520-5abf-4f90-87f5-5f9e9947237c.png)

- Users can take the figure and axes for post-processing or storing

**Possible Drawbacks:**
This will _marginally_ increase the computation time for the
visualization but shouldn't be too impactful.

**Related GitHub Issues:**
None

Co-authored-by: ziofil <[email protected]>
Co-authored-by: Luke Helt <[email protected]>
**Context:**
Optimizer is opaque

**Description of the Change:**
in the minimize function now we can pass a callback that will be
executed at the end of each step (with `trainable_parameters` as
argument) and the return is stored in `self.callback_history`.

**Benefits:**
Can do lots of things, e.g.

![test](https://user-images.githubusercontent.com/8944955/200894110-a34b8f5d-caa7-40fc-9716-1d67a8667ca6.gif)


**Possible Drawbacks:**
None

**Related GitHub Issues:**
None
**Context:**
The `Rgate` and the `Dgate` don't work correctly in Fock representation
in some cases.

**Description of the Change:**
1. Fixed the `Rgate` and `Dgate` by removing the parser method and
simplifying the code.
2. Added two functions in the fock module for explicitly applying an
operator to a ket or to a dm (sandwich) which avoid constructing
unitaries as large as the whole circuit. Now they are used in the
`transform_fock` method of the `Transformation` class.
3. fixed a bug in the trace function
4. minor improvements

**Benefits:**

**Possible Drawbacks:**

**Related GitHub Issues:**

Co-authored-by: Sebastián Duque Mesa <[email protected]>
**Context:**
sometimes it's useful to compute the fock representation using different
cutoffs for input-output indices of the same mode

**Description of the Change:**
transformations (gates, circuit) now accept also a list of double (or
quadruple for choi) length which specifies the cutoffs per index (rather
than per mode)

**Benefits:**
saves runtime if one needs e.g. to input fock states into a gaussian
circuit

**Possible Drawbacks:**
none

**Related GitHub Issues:**
**Context:**
norm is confusing field in the repr of `State` (it's the sqrt of the
probability if the state is pure and the probability if it's mixed).

**Description of the Change:**
Replace norm with probability 

**Benefits:**
Consistent meaning and also more practically useful

**Possible Drawbacks:**
some users may miss the good old norm?

**Related GitHub Issues:**
**Context:**
The application of a choi operator to a density matrix was resulting in
a transposed dm

**Description of the Change:**
Fixes the order of the indices in the application of a choi operator to
dm and ket

**Benefits:**
Correct result

**Possible Drawbacks:**
None

**Related GitHub Issues:**
None

Co-authored-by: Sebastián Duque Mesa <[email protected]>
**Context:**
Always forgetting to add entries to the CHANGELOG file?

**Description of the Change:**
Now github will remind you to do so — this PR implements a new CI
workflow checking if an entry has been added to the CHANGELOG file. This
check is not mandatory meaning it won't block the ability to merge the
PR, however checks will appear as failed. In case no changelog entry is
needed one can use the `no changelog` label to disable the check.

This PR also adds the changelog entry for PR #188.

**Benefits:**
No more PRs without CHANGELOG entries

**Possible Drawbacks:**
None
**Context:**
Setting a seed in MM is not trivial and often we need reproducible
reults

**Description of the Change:**
The `settings` object now supports the `SEED` attribute, which is random
unless it is set by the user. To unset it, just set it equal to `None`.

**Benefits:**
Easy to get reproducible results without messing with numpy.random.

**Possible Drawbacks:**
None?

**Related GitHub Issues:**

Co-authored-by: Sebastián Duque Mesa <[email protected]>
**Context:**
The fock representation of a state is stored internally for future
access.
However, if in the meantime the cutoff is updated this is not taken into
account, this PR solves this issue.

**Description of the Change:**
The cutoffs are applied to the internal fock representation upon access.

**Benefits:**
More correct implementation.

**Possible Drawbacks:**
None?

**Related GitHub Issues:**
None

Co-authored-by: Sebastián Duque Mesa <[email protected]>
**Context:**
Going forward we need a big refactoring of the methods to transform
between representations.
This PR is the first in this direction.

**Description of the Change:**
Introduced two new modules and refactored various methods

**Benefits:**
Allows for easier extension of representation methods

**Possible Drawbacks:**
Some methods and functions have a new name and/or arguments

**Related GitHub Issues:**
None

Co-authored-by: Sebastián Duque Mesa <[email protected]>
**Context:**
Fixes a few bugs related to transformation of states in Fock
representation

**Description of the Change:**
- adds missing default argument 
- adds a function for convenience
- fixes a bug due to old code not removed in
`Transformation.transform_fock`
- adds 11 tests to avoid same type of bug
- adds 2 parametrized tests (10 in total) to check correct application
of unitaries and channels to kets and dm in Fock representation

**Benefits:**
Correct code

**Possible Drawbacks:**
None

**Related GitHub Issues:**
No issue but FTB simulation not running on develop branch
**Context:**
Fixing a bug related to tensorflow not liking products of complex128 and
float64

**Description of the Change:**
cast where necessary

**Benefits:**
Code that runs

**Possible Drawbacks:**
None

**Related GitHub Issues:**
None
**Context:**
new black version, wants to make new changes

**Description of the Change:**
black all the files that need to be blacked

**Benefits:**
to bring the develop branch up to date so that all the PRs that are open
don't have to

**Possible Drawbacks:**
None

**Related GitHub Issues:**
None
**Context:**

Adds `map_trainer` as the interface for distributing optimization
workflows using ray so that things can happen in parallel -- replacing
the need for `for` loops.

_Code here has been personally looked at by Trudeau._

**Description of the Change:**
Demo notebook in recipes.

Documentation page for the `trainer` module is added with some examples,
which is also in the docstring of `map_trainer` so that it can be
conveniently read in Jupyter directly with shift+tab.

I've also had some weird problem with the unit tests (all passing
locally) running on github action where some times it would just hang.
I've tried removing my new tests one by one and adding them back again
one by one, and it somehow worked at the end... I changed the testing
workflow file with direct `pip install .[ray]` instead of building wheel
first, which I don't think is the reason. Thanks Trudeau I guess?


**Benefits:**
fast and simple experimentation -> more research done.

**Possible Drawbacks:**
more interface to introduce

**Related GitHub Issues:**
**Context:**
Circuit building is a bit obscure because there's no visual circuit
representation

**Description of the Change:**
Adds a basic circuit drawer (adapted from pennylane).

**Benefits:**
Circuit visualizations

**Possible Drawbacks:**
Does not yet include initializations and measurements, but for this we
need to refactor the `Circuit` class first.

**Related GitHub Issues:**
None

---------

Co-authored-by: Sebastián Duque Mesa <[email protected]>
**Context:**
Attempt to fix the (random) never ending tests on github actions.
I have so far rerun the test 5 times without the issue reoccurring. 🤞 

**Description of the Change:**
Forces ray to init with 1 cpu for testing.

**Benefits:**

**Possible Drawbacks:**

**Related GitHub Issues:**

---------

Co-authored-by: Sebastián Duque Mesa <[email protected]>
**Context:**
Tests could improve in MM

**Description of the Change:**
Add several new tests and improves hypothesis strategies

**Benefits:**
Better test suite

**Possible Drawbacks:**
More tests to maintain? Nah

**Related GitHub Issues:**
None
**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.

---------

Co-authored-by: Robbe De Prins (UGent-imec) <[email protected]>
Co-authored-by: ziofil <[email protected]>
Co-authored-by: ziofil <[email protected]>
**Context:**
Fixing small bugs/typos before 0.4 release

**Description of the Change:**
- Threshold detector can now be correctly initialized
- changes in settings.HBAR are correctly reflected everywhere
- Interferometer can be placed on any set of modes
- number of decimals are respected in circuit drawer

**Benefits:**
Fewer bugs/typos

**Possible Drawbacks:**
None

**Related GitHub Issues:**
None
ziofil and others added 26 commits February 28, 2023 14:42
**Context:**
negative parameters were showing a 0 instead of the minus sign.

**Description of the Change:**
Fixes this issue

**Benefits:**
Correct parameters!

**Possible Drawbacks:**
None

**Related GitHub Issues:**
None
**Context:**
We never addressed type annotations in MrMustard

**Description of the Change:**
This PR begins making improvements all across the board on type
annotations

**Benefits:**
Easier to spot errors and bugs, finally we can rely on a type checker as
well

**Possible Drawbacks:**
One more thing to maintain, but it should be easy once set up

**Related GitHub Issues:**
None

---------

Co-authored-by: Sebastián Duque Mesa <[email protected]>
Fix the bugs about the Riemannian optimization.

Add two optimization methods of the Interferometer (unitary update) and
RealInterferometer (orthogonal update).

---------

Co-authored-by: ziofil <[email protected]>
Co-authored-by: ziofil <[email protected]>
**Context:**
Computing a fidelity across representations (gaussian and fock) was
causing an issue with taking a slice

**Description of the Change:**
Fixed

**Benefits:**
Works

**Possible Drawbacks:**
None

**Related GitHub Issues:**
None
**Context:**
an error occurs if two mixed fock states with different cutoff are the
args of `fidelity`

**Description of the Change:**
enforce same cutoff

**Benefits:**
works

**Possible Drawbacks:**
none

**Related GitHub Issues:**
none

---------

Co-authored-by: Sebastián Duque Mesa <[email protected]>
**Context:**
Mr Mustard uses The Walrus' implementation of the displacement
operation. This creates an unnecessary coupling between the two
packages.

**Description of the Change:**
Implements the displacement gate on MrMustard directly. To do so and to
avoid circular imports
- the implementation of the displacement gate has been moved from the
`TFMath` class/backend to the `fock` module and
- the `custom_gradient` method wrapping tensorflow's custom gradient
decorator has been implemented on `TFMath`

**Benefits:**
No need to release both packages (TheWalrus and MrMustard) at the same
time for the `Dgate` to be available.

**Possible Drawbacks:**
None

**Related GitHub Issues:**
None

---------

Co-authored-by: ziofil <[email protected]>
**Context:**
v0.4 release

**Description of the Change:**
shortened long lines and fixed some typos

**Benefits:**
good changelog

**Possible Drawbacks:**
None

**Related GitHub Issues:**
None

---------

Co-authored-by: Sebastián Duque Mesa <[email protected]>
**Context:**
Due to the flaky tensorflow support for arm architectures it is hard to
install MrMustard in Mac machines as well as creating reproducible
environments.

**Description of the Change:**
This PR implements a fully furnished [vscode
devcontainer](https://code.visualstudio.com/docs/devcontainers/containers)
and provides a Dockerfile in case one wants to run a completely isolated
environment with an installation of MrMustard.

**Benefits:**
No more dependency and architecture conflicts: MrMustard is now
installed and ready to be develop with a single command. On top of that,
MrMustard repo now can use github's web devcontainers feature.

_How-to?_
- To try it out the devcontainer locally on vscode follow the
instructions
[here](https://code.visualstudio.com/docs/devcontainers/containers#_quick-start-open-a-git-repository-or-github-pr-in-an-isolated-container-volume).
- Use github codespaces
[
    ![Open in Remote - Containers](
        https://img.shields.io/static/v1?label=Remote%20-%20Containers&message=Open&color=blue&logo=visualstudiocode
    )
](
    https://vscode.dev/redirect?url=vscode://ms-vscode-remote.remote-containers/cloneInVolume?url=https://github.com/XanaduAI/mrmustard
)

- Use `docker build` to build and image with MrMustard from the
`Dockerfile`


**Possible Drawbacks:**
None

**Related GitHub Issues:**
None

---------

Co-authored-by: ziofil <[email protected]>
**Context:**
the choi method sometimes uses wrong cutoffs

**Description of the Change:**
fixes it by picking the cutoffs from the input state

**Benefits:**
correct implementation

**Possible Drawbacks:**
none

**Related GitHub Issues:**
none

---------

Co-authored-by: Sebastián Duque Mesa <[email protected]>
**Context:**
Dependencies have not been revisited in a while.

**Description of the Change:**
Updates version of dependencies. In specific, tensorflow is pinned to
version `2.10.1` since version `2.11.0` has changes in the functions
`stack` and `concat` that break the integration with Mr Mustard.

To unpin it we will have to revisit the new tf api for those functions
so that users installing MrMustard with `pip install mrmustard` won't
run into unexpected errors.

Also, since `ray` is an optional dependency, tests related to ray are
now skipped if the package is not installed.

**Benefits:**
Fixes a lot of security issues in TF as alerted by dependabot and also
provides updated versions of the packages.

**Possible Drawbacks:**
TF is pinned — we should address this for the next release. Also, we
should consider moving to a [PEP 621](https://peps.python.org/pep-0621/)
compliant packaging system by either using a `pyproject.toml` file for
dependency specification or a dependency manager like poetry.

**Related GitHub Issues:**
None

---------

Co-authored-by: ziofil <[email protected]>
Co-authored-by: ziofil <[email protected]>
Incrementing version to 0.5.0-dev
**Context:**
To facilitate the investigation of the optimization process on the
numerics side, as well as giving users more control over the
optimization progress, the callback functionality is expanded to allow
for realtime monitoring and updates. A builtin callback that tracks
optimization progresses in Tensorboard is also provided.

**Description of the Change:**

- The `callback` argument to `Optimizer.minimize` is now `callbacks`
with the new possibility of passing multiple callbacks.
- Adds tagged parameter traversal `Parametrized.traverse_trainables()`
which accumulates the path of tags while traversing. This is now used by
the optimizer to get unique human interpretable identifiers to
parameters (for better tracking).
- Callback functions now accept as argument the optimizer, the cost
value, the cost function, and the trainable parameter values as well as
their gradient. This gives users a lot of run time control, but it's not
necessary to use them all.
- The execution of callbacks moved to after the backprop but before the
parameter update (originally at the end of the step).
- A `Callback` base class for users to extend and inherit from.
- A `TensorboardCallbacks` that does automatic tracking of costs and
parameters (values and gradients) in real time.

Misc changes:

- I added a `path` item to the testing workflow so that non-code changes
won't trigger it.

**Benefits:**
- Better observability.
- Finer control.
- Opens up a lot of possibilities for optimization.

**Possible Drawbacks:**
Users could introduce too much overhead if the callbacks are too heavy,
thus potentially slowing down the optimization.

**Related GitHub Issues:**
None

---------

Co-authored-by: ziofil <[email protected]>
**Context:**
Modularized Fock strategies allow for a simple way to construct
strategies out of building blocks.

**Description of the Change:**
- All new lattice module.
- some fixes in tests
- bargmann method for State
**Context:**
Some fundamental gates gain specialized Fock methods that account for their symmetries (for example the BS gate can be built with 3 loops rather than 4 by using photon number conservation).

We also now have Vector-Jacobian products directly implemented rather than implemented as the product of the actual jacobian with the upstream gradient: the jacobian can be very big (num_params x size_of_forward_pass) so instead we "accumulate" the downstream gradient without ever needing to allocate memory for a jacobian.

**Description of the Change:**
1. Introduces special fock methods for `BSgate` (50x faster), `Sgate` (5x faster) and `SqueezedVacuum` (10x faster)
2. Replaces Jacobians with VJPs --> low-memory backprop

**Benefits:**
wooosh!
**Context:**
When projecting onto Fock the autocutoff doesn't check the photon number
of the Fock projection. Thanks to Jacob for spotting it.

**Description of the Change:**
Now it does. Added a test too.

**Benefits:**
No unnecessary cutoff errors when projecting onto a Fock state

**Possible Drawbacks:**
None

**Related GitHub Issues:**
None
**Context:**
BSgate is unstable for high Fock cutoff.

**Description of the Change:**
Implements an alternative method to calculate the BS and exposes it like
so:
```python
bs = BSgate(1.0,2.0)
bs.U(cutoffs=[10,10], method='schwinger') # or 'vanilla' (default is settings.DEFAULT_BS_METHOD='vanilla')
```

**Benefits:**
Slower but stable method to compute the BS Fock amplitudes to any cutoff

---------

Co-authored-by: Ryk <[email protected]>
**Context:** There was a bug in the algorithms for faster PNR sampling
from Gaussian circuits using density matrices. In the case where the
cutoff of the first detector was equal to 1, the resulting density
matrix was incorrect.

**Description of the Change:** The problem was solved by allowing for
diagonal pivots to be used when the cutoffs[0]=1, effectively changing
line 4 of Algorithm 1 in https://arxiv.org/pdf/2303.08879.pdf from `if
diag_1 < C_1 − 1 then` to `if (diag_1 < C_1 − 1) or (C_1==1) then`.

---------

Co-authored-by: Robbe De Prins (UGent-imec) <[email protected]>
**Context:**
The `more_strategies` PR introduced several custom gradients, but we
never noticed they were broken under some conditions.

**Description of the Change:**
- Gradients are now cast to the backend array type
- This PR also adds the numpy option to the bargmann methods

**Benefits:**
Gradients work

**Possible Drawbacks:**
None

**Related GitHub Issues:**
None
**Context:**
This PR is responsible for migrating the dependencies management from
`pip` to [`poetry`](https://python-poetry.org/). The goal is also to use
Poetry for versioning, adopting as a single source of truth the version
defined in the `pyproject.toml` file.

**Description of the Change:**

- `pyproject.toml` and `poetry.lock`: these two files have been
introduced to properly configure and lock the dependencies of the
project. The version used by poetry for the lock file is `1.4.0`.

- `requirements` files: the requirements previously defined in the
different requirements.txt files have been imported in `pyproject.toml`.
The files have been removed from the project. The additional
dependencies have been imported respectively under the groups `dev`
(requirements-dev.txt) and `doc` (doc/requirements.txt). The versions
imported are the ones previously defined in the requirements files. The
only change introduced is related to a submodule of tensorflow to make
it work between ARM/x86_64 architectures.

- `setup.py`: the package configurations have been also imported in
`pyproject.toml`. No change has been introduced in the different
attributes such as authors, classifiers and so on. Probably some of the
are out of date.

- `_version.py`: some changes introduced to retrieve the package version
from `pyproject.toml`.

- Documentation: text changes introduced describing where poetry is now
required to perform some actions. `.readthedocs.yml` has been also
updated to build the documentation using poetry.

- `Makefile`: commands to build or install the package updated to use
poetry.

- devcontainers: `Dockerfile` and script updated to use poetry.

- CI: all GitHub Actions workflows have been updated to use Poetry. An
additional change has been introduced in those workflows using a third
party action to cancel previous runs, they now use a concurrency setting
to do so.

**Benefits:**
- Dependencies are now managed in a centralized location and locked.
- Package build can be executed by Poetry.
- Package versioning can be managed through Poetry [version
commands](https://python-poetry.org/docs/cli/#version).
# Release 0.5.0

### New features

* Optimization callback functionalities has been improved. A dedicated
`Callback` class is added which
is able to access the optimizer, the cost function, the parameters as
well as gradients, during the
optimization. In addition, multiple callbacks can be specified. This
opens up the endless possiblities
of customizing the the optimization progress with schedulers, trackers,
heuristics, tricks, etc.
  [(#219)](#219)

* Tensorboard-based optimization tracking is added as a builtin
`Callback` class: `TensorboardCallback`.
It can automatically track costs as well as all trainable parameters
during optimization in realtime.
  Tensorboard can be most conveniently viewed from VScode.
  [(#219)](#219)

  ```python
  import numpy as np
  from mrmustard.training import Optimizer, TensorboardCallback

  def cost_fn():
      ...
  
  def as_dB(cost):
      delta = np.sqrt(np.log(1 / (abs(cost) ** 2)) / (2 * np.pi))
      cost_dB = -10 * np.log10(delta**2)
      return cost_dB

  tb_cb = TensorboardCallback(cost_converter=as_dB, track_grads=True)

  opt = Optimizer(euclidean_lr = 0.001);
opt.minimize(cost_fn, max_steps=200, by_optimizing=[...],
callbacks=tb_cb)

# Logs will be stored in `tb_cb.logdir` which defaults to
`./tb_logdir/...` but can be customized.
# VScode can be used to open the Tensorboard frontend for live
monitoring.
# Or, in command line: `tensorboard --logdir={tb_cb.logdir}` and open
link in browser.
  ```

* Gaussian states support a `bargmann` method for returning the bargmann
representation.
  [(#235)](#235)

* The `ket` method of `State` now supports new keyword arguments
`max_prob` and `max_photons`.
Use them to speed-up the filling of a ket array up to a certain
probability or *total* photon number.
  [(#235)](#235)

  ```python
  from mrmustard.lab import Gaussian

# Fills the ket array up to 99% probability or up to the |0,3>, |1,2>,
|2,1>, |3,0> subspace, whichever is reached first.
# The array has the autocutoff shape, unless the cutoffs are specified
explicitly.
  ket = Gaussian(2).ket(max_prob=0.99, max_photons=3)
  ```

* Gaussian transformations support a `bargmann` method for returning the
bargmann representation.
  [(#239)](#239)

* BSGate.U now supports method='vanilla' (default) and 'schwinger'
(slower, but stable to any cutoff)
  [(#248)](#248)

### Breaking Changes

* The previous `callback` argument to `Optimizer.minimize` is now
`callbacks` since we can now pass
  multiple callbacks to it.
  [(#219)](#219)

* The `opt_history` attribute of `Optimizer` does not have the
placeholder at the beginning anymore.
  [(#235)](#235)

### Improvements

* The math module now has a submodule `lattice` for constructing
recurrence relation strategies in the Fock lattice.
There are a few predefined strategies in
`mrmustard.math.lattice.strategies`.
  [(#235)](#235)

* Gradients in the Fock lattice are now computed using the
vector-jacobian product.
This saves a lot of memory and speeds up the optimization process by
roughly 4x.
  [(#235)](#235)

* Tests of the compact_fock module now use hypothesis.
  [(#235)](#235)

* Faster implementation of the fock representation of `BSgate`, `Sgate`
and `SqueezedVacuum`, ranging from 5x to 50x.
  [(#239)](#239)

* More robust implementation of cutoffs for States.
[(#239)](#239)

* Dependencies and versioning are now managed using Poetry.
[(#257)](#257)

### Bug fixes

* Fixed a bug that would make two progress bars appear during an
optimization
  [(#235)](#235)

* The displacement of the dual of an operation had the wrong sign
  [(#239)](#239)

* When projecting a Gaussian state onto a Fock state, the upper limit of
the autocutoff now respect the Fock projection.
  [(#246)](#246)

* Fixed a bug for the algorithms that allow faster PNR sampling from
Gaussian circuits using density matrices. When the
cutoff of the first detector is equal to 1, the resulting density matrix
is now correct.

### Documentation

### Contributors
[Filippo Miatto](https://github.com/ziofil), [Zeyue
Niu](https://github.com/zeyueN),
[Robbe De Prins](https://github.com/rdprins), [Gabriele
Gullì](https://github.com/ggulli),
[Richard A. Wolf](https://github.com/ryk-wolf)
@ziofil ziofil requested a review from sduquemesa July 26, 2023 21:47
@codecov
Copy link

codecov bot commented Jul 26, 2023

Codecov Report

Merging #265 (fcb9512) into main (b62d01d) will increase coverage by 6.10%.
Report is 7 commits behind head on main.
The diff coverage is 73.55%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #265      +/-   ##
==========================================
+ Coverage   71.62%   77.73%   +6.10%     
==========================================
  Files          29       53      +24     
  Lines        2848     4383    +1535     
==========================================
+ Hits         2040     3407    +1367     
- Misses        808      976     +168     
Files Changed Coverage Δ
mrmustard/math/torch.py 0.00% <0.00%> (ø)
mrmustard/physics/__init__.py 55.88% <ø> (+14.21%) ⬆️
...stard/math/numba/compactFock_1leftoverMode_grad.py 14.97% <14.97%> (ø)
mrmustard/math/numba/compactFock_diagonal_grad.py 17.47% <17.47%> (ø)
mrmustard/utils/wigner.py 25.00% <25.00%> (ø)
...stard/math/numba/compactFock_1leftoverMode_amps.py 27.83% <27.83%> (ø)
mrmustard/math/numba/compactFock_diagonal_amps.py 33.78% <33.78%> (ø)
mrmustard/math/lattice/steps.py 33.89% <33.89%> (ø)
mrmustard/math/lattice/pivots.py 37.50% <37.50%> (ø)
...rmustard/math/numba/compactFock_helperFunctions.py 45.00% <45.00%> (ø)
... and 40 more

Continue to review full report in Codecov by Sentry.

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

@sduquemesa sduquemesa merged commit df94d49 into main Jul 27, 2023
7 checks passed
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.

7 participants