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

Vanilla strategy with complex512 (using Julia) #274

Merged
merged 96 commits into from Oct 18, 2023

Conversation

rdprins
Copy link
Collaborator

@rdprins rdprins commented Sep 28, 2023

Calculating Fock representations using the "vanilla strategy" is now more numerically stable (i.e. numerical blowups
that result from repeatedly applying the recurrence relation are now postponed to higher cutoff values).
This is done by representing Fock amplitudes with precision complex512 rather than complex128
(which counters the accumulation of floating-point errors).
We run Julia code via PyJulia (where Numba was used before) to keep the code fast.

Before submitting

Please complete the following checklist when submitting a PR:

  • All new features must include a unit test.
    If you've fixed a bug or added code that should be tested, add a test to the
    test directory!

  • All new functions and code must be clearly commented and documented.
    If you do make documentation changes, make sure that the docs build and
    render correctly by running make docs.

  • Ensure that the test suite passes, by running make test.

  • Ensure that code and tests are properly formatted, by running make format or black -l 100 <filename> on any relevant files. You will need to have the Black code format installed:
    pip install black.

  • Add a new entry to the .github/CHANGELOG.md file, summarizing the
    change, and including a link back to the PR.

  • The Mr Mustard source code conforms to
    PEP8 standards except line length.
    We check all of our code against Pylint.
    To lint modified files, simply pip install pylint, and then
    run pylint mrmustard/path/to/file.py.

When all the above are checked, delete everything above the dashed
line and fill in the pull request template.


Context: Vanilla strategy for calculating Fock representations

Description of the Change: code is now run in Julia via PyJulia + we represent Fock amplitudes with 512 bits of precision

Benefits: numerical stability

Possible Drawbacks: slows down code

Related GitHub Issues:

@rdprins rdprins added the WIP work in progress label Sep 28, 2023
@rdprins rdprins changed the title Vanilla strategy with higher precision than complex128 (using Julia) Vanilla strategy with complex512 (using Julia) Sep 28, 2023
@ziofil ziofil marked this pull request as ready for review September 28, 2023 17:16
@ziofil
Copy link
Collaborator

ziofil commented Sep 28, 2023

oh sorry, I accidentally hit the "ready for review" button

@elib20 elib20 marked this pull request as draft September 28, 2023 17:19
mrmustard/math/__init__.py Outdated Show resolved Hide resolved
mrmustard/math/__init__.py Outdated Show resolved Hide resolved
mrmustard/settings.py Show resolved Hide resolved
@rdprins
Copy link
Collaborator Author

rdprins commented Oct 12, 2023

The line from julia import Main as Main_julia pops up in CodeFactor as it's run outside of toplevel (both in settings.py as in tensorflow_wrapper.py), but I think it can't be moved, because it has to be run after running jl = Julia(compiled_modules=False), which is done in settings.py.

@rdprins rdprins removed the WIP work in progress label Oct 12, 2023
tests/test_math/test_lattice.py Outdated Show resolved Hide resolved
tests/test_math/test_lattice.py Outdated Show resolved Hide resolved
@rdprins rdprins merged commit 41b500f into develop Oct 18, 2023
6 checks passed
@rdprins rdprins deleted the vanilla_higher_precision branch October 18, 2023 08:18
rdprins added a commit that referenced this pull request Oct 30, 2023
**Context:** Julia dependencies (added in order to stabilize the
recurrence relation: #274)

**Description of the Change:** Added Manifest.toml and Project.toml
which specify the julia version and dependencies. Updated the README and
development guide to instruct the user to install julia and to
instantiate the toml files. This means PyJulia now runs in the
environment defined by the toml files, rather than in Julia's base
environment.

- When installing via devcontainer, julia (v1.9.3) is installed and the
environment is instantiaded.
- When installing via MakeFile, the environment is only instantiaded
when Julia was previously installed by the user.
- When installing via `poetry install` or `pip install`, the julia
installation and environment instantiation need to be performed by the
user.

**Benefits:**

**Possible Drawbacks:**

**Related GitHub Issues:**

---------

Co-authored-by: Filippo Miatto <ziofil@users.noreply.github.com>
Co-authored-by: Tanner Rogalsky <tanner@tannerrogalsky.com>
Co-authored-by: ziofil <miatto@gmail.com>
@SamFerracin SamFerracin mentioned this pull request Feb 1, 2024
SamFerracin added a commit that referenced this pull request Feb 6, 2024
### New features
* Added a new interface for backends, as well as a `numpy` backend
(which is now default). Users can run
all the functions in the `utils`, `math`, `physics`, and `lab` with both
backends, while `training`
requires using `tensorflow`. The `numpy` backend provides significant
improvements both in import
time and runtime.
[(#301)](#301)

* Added the classes and methods to create, contract, and draw tensor
networks with `mrmustard.math`.
  [(#284)](#284)

* Added functions in physics.bargmann to join and contract (A,b,c)
triples.
  [(#295)](#295)

* Added an Ansatz abstract class and PolyExpAnsatz concrete
implementation. This is used in the Bargmann representation.
  [(#295)](#295)

* Added `complex_gaussian_integral` and `real_gaussian_integral`
methods.
  [(#295)](#295)

* Added `Bargmann` representation (parametrized by Abc). Supports all
algebraic operations and CV (exact) inner product.
  [(#296)](#296)

### Breaking changes
* Removed circular dependencies by:
* Removing `graphics.py`--moved `ProgressBar` to `training` and
`mikkel_plot` to `lab`.
  * Moving `circuit_drawer` and `wigner` to `physics`.
  * Moving `xptensor` to `math`.
  [(#289)](#289)

* Created `settings.py` file to host `Settings`.
  [(#289)](#289)

* Moved `settings.py`, `logger.py`, and `typing.py` to `utils`.
  [(#289)](#289)

* Removed the `Math` class. To use the mathematical backend, replace
`from mrmustard.math import Math ; math = Math()` with `import
mrmustard.math as math`
  in your scripts.
  [(#301)](#301)

* The `numpy` backend is now default. To switch to the `tensorflow`
backend, add the line `math.change_backend("tensorflow")` to your
scripts.
  [(#301)](#301)

### Improvements

* Calculating Fock representations and their gradients is now more
numerically stable (i.e. numerical blowups that
result from repeatedly applying the recurrence relation are postponed to
higher cutoff values).
This holds for both the "vanilla strategy"
[(#274)](#274) and for the
"diagonal strategy" and "single leftover mode strategy"
[(#288)](#288).
This is done by representing Fock amplitudes with a higher precision
than complex128 (countering floating-point errors).
We run Julia code via PyJulia (where Numba was used before) to keep the
code fast.
The precision is controlled by `setting
settings.PRECISION_BITS_HERMITE_POLY`. The default value is ``128``,
which uses the old Numba code. When setting to a higher value, the new
Julia code is run.

* Replaced parameters in `training` with `Constant` and `Variable`
classes.
  [(#298)](#298)

* Improved how states, transformations, and detectors deal with
parameters by replacing the `Parametrized` class with `ParameterSet`.
  [(#298)](#298)

* Includes julia dependencies into the python packaging for downstream
installation reproducibility.
Removes dependency on tomli to load pyproject.toml for version info,
uses importlib.metadata instead.
  [(#303)](#303)
  [(#304)](#304)

* Improves the algorithms implemented in `vanilla` and `vanilla_vjp` to
achieve a speedup.
Specifically, the improved algorithms work on flattened arrays (which
are reshaped before being returned) as opposed to multi-dimensional
array.
  [(#312)](#312)
  [(#318)](#318)

* Adds functions `hermite_renormalized_batch` and
`hermite_renormalized_diagonal_batch` to speed up calculating
  Hermite polynomials over a batch of B vectors.
  [(#308)](#308)

* Added suite to filter undesired warnings, and used it to filter
tensorflow's ``ComplexWarning``s.
  [(#332)](#332)


### Bug fixes

* Added the missing `shape` input parameters to all methods `U` in the
`gates.py` file.
[(#291)](#291)
* Fixed inconsistent use of `atol` in purity evaluation for Gaussian
states.
[(#294)](#294)
* Fixed the documentations for loss_XYd and amp_XYd functions for
Gaussian channels.
[(#305)](#305)
* Replaced all instances of `np.empty` with `np.zeros` to fix
instabilities.
[(#309)](#309)

---------

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: ziofil <ziofil@users.noreply.github.com>
Co-authored-by: ziofil <miatto@gmail.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>
Co-authored-by: Ryk <47638463+ryk-wolf@users.noreply.github.com>
Co-authored-by: Gabriele Gullì <120967042+ggulli@users.noreply.github.com>
Co-authored-by: Yuan Yao <yuan.yao@xanadu-Yuan-YAO.local>
Co-authored-by: Yuan Yao <yuan.yao@xanadu-infras-MacBook-Air.local>
Co-authored-by: heltluke <luke.helt@gmail.com>
Co-authored-by: Tanner Rogalsky <tanner@tannerrogalsky.com>
Co-authored-by: Jan Provazník <101715194+jan-provaznik@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.

None yet

4 participants