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

Adding backend interface and numpy backend to Mr Mustard #301

Merged
merged 167 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from 142 commits
Commits
Show all changes
167 commits
Select commit Hold shift + click to select a range
b3a34b6
some progress
SamFerracin Sep 26, 2023
0626793
fixed hbar (probably badly)
SamFerracin Sep 26, 2023
e4696a3
method is now in settings
SamFerracin Sep 26, 2023
ca431b2
factor of 2 missing
SamFerracin Sep 27, 2023
26195de
stated working on tests
SamFerracin Sep 27, 2023
232a09a
added tests with coherent and cat states
SamFerracin Sep 28, 2023
522a0a4
added fock state tests
SamFerracin Sep 28, 2023
1ff0e41
tests pass
SamFerracin Sep 28, 2023
0796799
format
SamFerracin Sep 28, 2023
33cf0af
errors
SamFerracin Sep 28, 2023
ed033b0
Code factor
SamFerracin Sep 28, 2023
8822d93
code requirements
SamFerracin Sep 28, 2023
2a8bcea
changelog
SamFerracin Sep 28, 2023
3c49599
coverage?
SamFerracin Sep 28, 2023
22be157
code requirements pt 2
SamFerracin Sep 28, 2023
07dd920
tests cov
SamFerracin Sep 28, 2023
367acd8
conflicts
SamFerracin Sep 29, 2023
c08efd0
pragma
SamFerracin Sep 29, 2023
3316480
Merge branch 'develop' into wigner-vis
Sep 29, 2023
40be09d
conflicts
SamFerracin Oct 2, 2023
be0a735
conflicts
SamFerracin Oct 2, 2023
8a4795b
conflicts
SamFerracin Oct 10, 2023
5693fd8
added basic classes
SamFerracin Oct 10, 2023
3760bde
backend works (better handling of exceptions needed)
SamFerracin Oct 11, 2023
9b19df5
moved settings to utils
SamFerracin Oct 12, 2023
8122886
moved settings to utils (tests)
SamFerracin Oct 12, 2023
449335b
moved typing to utils
SamFerracin Oct 12, 2023
2d90d68
mikkel to states and progress bar to training (nuked graphics.py)
SamFerracin Oct 12, 2023
dd1fe46
circuit_drawer to lab
SamFerracin Oct 12, 2023
2b82a81
xptensor to math
SamFerracin Oct 12, 2023
b23f8ab
logger
SamFerracin Oct 12, 2023
4a1fceb
docs
SamFerracin Oct 12, 2023
564154b
Merge branch 'refactor1' of https://github.com/XanaduAI/MrMustard int…
SamFerracin Oct 12, 2023
b86fb88
started working on tf backend
SamFerracin Oct 13, 2023
01a1eaa
done with tensorflow backend
SamFerracin Oct 13, 2023
b2f5e1a
settings are in
SamFerracin Oct 13, 2023
7f7487e
all
SamFerracin Oct 13, 2023
e1bb4dd
custom grad
SamFerracin Oct 13, 2023
5bbe15d
some tests passed
SamFerracin Oct 13, 2023
5d17f35
physics passes
SamFerracin Oct 14, 2023
68a7f30
all physics passes for good
SamFerracin Oct 15, 2023
93f75b7
more progress
SamFerracin Oct 16, 2023
427189a
some changes
SamFerracin Oct 16, 2023
29e0f0b
some progress with seba
SamFerracin Oct 16, 2023
bb3fd72
still stuck
SamFerracin Oct 17, 2023
5b5c27b
some more progress
SamFerracin Oct 17, 2023
f86ac96
some progress
SamFerracin Oct 17, 2023
a6d81e0
revert
SamFerracin Oct 17, 2023
f2ec7c3
something works
SamFerracin Oct 17, 2023
202332c
type hint
SamFerracin Oct 17, 2023
dcda911
Merge branch 'develop' of https://github.com/XanaduAI/MrMustard into …
SamFerracin Oct 17, 2023
6410364
two tests failing
SamFerracin Oct 17, 2023
4dfb1ff
one test missing
SamFerracin Oct 17, 2023
33dc940
all tests passing!
SamFerracin Oct 17, 2023
a8d0755
some better things
SamFerracin Oct 17, 2023
e2c23b0
nuked math
SamFerracin Oct 17, 2023
cd24008
temp
SamFerracin Oct 17, 2023
cddf34b
done
SamFerracin Oct 17, 2023
bfbf006
Merge branch 'develop' of https://github.com/XanaduAI/MrMustard into …
SamFerracin Oct 17, 2023
aa8799e
conflicts
SamFerracin Oct 17, 2023
e5446e7
changelog
SamFerracin Oct 17, 2023
c46d77a
conflicts
SamFerracin Oct 18, 2023
c98de29
changed singleton
SamFerracin Oct 18, 2023
b610207
fixed merging conflicts
SamFerracin Oct 18, 2023
b6f4a21
merging conflicts, pt2
SamFerracin Oct 18, 2023
b939b86
oops
SamFerracin Oct 18, 2023
0757d37
conlficts
SamFerracin Oct 18, 2023
a90ab3e
Upping cov
SamFerracin Oct 18, 2023
394c2a3
upping cov
SamFerracin Oct 19, 2023
125634b
Merge branch 'refactor1' of https://github.com/XanaduAI/MrMustard int…
SamFerracin Oct 19, 2023
7306da6
init
SamFerracin Oct 19, 2023
2836082
oops
SamFerracin Oct 19, 2023
c25a7b6
Merge branch 'refactor1' of https://github.com/XanaduAI/MrMustard int…
SamFerracin Oct 19, 2023
557de76
cov
SamFerracin Oct 19, 2023
c55d072
Merge branch 'refactor1' of https://github.com/XanaduAI/MrMustard int…
SamFerracin Oct 19, 2023
9b70918
started working out np backend
SamFerracin Oct 19, 2023
ce55f2e
removed path
SamFerracin Oct 20, 2023
e377915
some progress
SamFerracin Oct 23, 2023
f269b68
tn passes
SamFerracin Oct 23, 2023
ff0aa00
much progress
SamFerracin Oct 23, 2023
410b531
added params and param set
SamFerracin Oct 24, 2023
d041221
added utils and init
SamFerracin Oct 24, 2023
395c7bb
added dgate
SamFerracin Oct 24, 2023
8abab27
gates
SamFerracin Oct 24, 2023
d2a2b0b
states
SamFerracin Oct 24, 2023
5ee17fe
lab working
SamFerracin Oct 24, 2023
72ba06c
more prog
SamFerracin Oct 24, 2023
2bc2224
good progress
SamFerracin Oct 24, 2023
0432c92
got rid of inline comments
SamFerracin Oct 24, 2023
ea239b8
all workingh
SamFerracin Oct 24, 2023
83ba96b
all working
SamFerracin Oct 24, 2023
68f5d82
some progress in trainer
SamFerracin Oct 24, 2023
887865d
oops
SamFerracin Oct 24, 2023
a3d3f2e
done
SamFerracin Oct 25, 2023
c442e7c
code format
SamFerracin Oct 25, 2023
635663f
code format
SamFerracin Oct 25, 2023
f30d4c2
code format
SamFerracin Oct 25, 2023
038fdbf
code format
SamFerracin Oct 25, 2023
2512e4e
code format
SamFerracin Oct 25, 2023
cc4a1be
conflicts and docs working
SamFerracin Oct 26, 2023
2bd7262
some progress
SamFerracin Oct 26, 2023
4d382c1
some progress
SamFerracin Oct 26, 2023
928f924
more progress
SamFerracin Oct 26, 2023
8e25f2a
more progress
SamFerracin Oct 26, 2023
f4d58b1
some progress
SamFerracin Oct 27, 2023
519cb73
Merge branch 'develop' of https://github.com/XanaduAI/MrMustard into …
SamFerracin Oct 27, 2023
949be99
CR
SamFerracin Oct 27, 2023
90533a9
conflicts
SamFerracin Oct 27, 2023
fae412d
CR
SamFerracin Oct 27, 2023
41bd034
Merge branch 'params2' of https://github.com/XanaduAI/MrMustard into …
SamFerracin Oct 27, 2023
d071846
oops
SamFerracin Oct 27, 2023
fdb702e
oops
SamFerracin Oct 27, 2023
6cc57ed
Merge branch 'params2' of https://github.com/XanaduAI/MrMustard into …
SamFerracin Oct 27, 2023
37c79b7
more progress
SamFerracin Oct 27, 2023
0e0a7f0
some progress
SamFerracin Oct 28, 2023
cbcf96b
more progress
SamFerracin Oct 28, 2023
bb2807c
more progress
SamFerracin Oct 28, 2023
68282c2
all tests passing
SamFerracin Oct 30, 2023
b70186d
all tests passed or skipped
SamFerracin Oct 30, 2023
599818e
tests about
SamFerracin Oct 30, 2023
c1eff05
more progress
SamFerracin Oct 30, 2023
384a26b
Merge branch 'develop' of https://github.com/XanaduAI/MrMustard into …
SamFerracin Oct 30, 2023
dfcfbe2
diagpart
SamFerracin Oct 30, 2023
cf36661
set_diag
SamFerracin Oct 30, 2023
3b8d29d
concat
SamFerracin Oct 30, 2023
b4f4629
all passes
SamFerracin Oct 30, 2023
b2fd427
params constant
SamFerracin Oct 31, 2023
2b7aea7
done
SamFerracin Oct 31, 2023
e40755c
progress with tests
SamFerracin Oct 31, 2023
9c976d8
some cleanup
SamFerracin Oct 31, 2023
82e9be9
singleton better handled
SamFerracin Oct 31, 2023
2d038c1
halleluja
SamFerracin Oct 31, 2023
7bd7625
done
SamFerracin Oct 31, 2023
2320dbe
tests
SamFerracin Oct 31, 2023
209263c
done
SamFerracin Oct 31, 2023
2551888
some prog on tests
SamFerracin Nov 1, 2023
9e53d85
more tests'
SamFerracin Nov 1, 2023
9da7187
added so many tests
SamFerracin Nov 1, 2023
e5f3651
immutable
SamFerracin Nov 1, 2023
af47067
conflicts
SamFerracin Nov 1, 2023
aa187b9
tf tests
SamFerracin Nov 1, 2023
da2f169
oops
SamFerracin Nov 1, 2023
8fd0f8c
removed every mention of torch'
SamFerracin Nov 1, 2023
438e779
changelog
SamFerracin Nov 1, 2023
e31d21e
found way to make backend immutable
SamFerracin Nov 2, 2023
b384f93
all tests passing
SamFerracin Nov 2, 2023
072e934
Update mrmustard/physics/bargmann.py
Nov 2, 2023
97e3f8b
format
SamFerracin Nov 2, 2023
6d03160
some progress
SamFerracin Nov 2, 2023
588e802
some format
SamFerracin Nov 2, 2023
45b5346
fixed pylint and test
SamFerracin Nov 2, 2023
63a5bd1
more code format
SamFerracin Nov 2, 2023
6ddc4db
docs
SamFerracin Nov 2, 2023
853efeb
more code format
SamFerracin Nov 2, 2023
62257b8
more code format
SamFerracin Nov 2, 2023
29047f7
more code format
SamFerracin Nov 2, 2023
96153bd
more code format
SamFerracin Nov 2, 2023
f75b588
more code format
SamFerracin Nov 2, 2023
a90881b
more code format
SamFerracin Nov 2, 2023
e64a04f
more code format
SamFerracin Nov 2, 2023
b6759f0
more tests
SamFerracin Nov 2, 2023
efef5c6
all working
SamFerracin Nov 2, 2023
d2391df
done
SamFerracin Nov 2, 2023
8fea2fa
done pt 2
SamFerracin Nov 2, 2023
f90318b
done pt 3
SamFerracin Nov 2, 2023
39dff4c
done pt 4
SamFerracin Nov 2, 2023
cf70153
readme and docs
SamFerracin Nov 3, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions .github/workflows/tests_numpy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Numpy tests
on:
push:
branches:
- develop
pull_request:
paths:
- '.github/workflows/tests.yml'
- 'mrmustard/**'
- 'tests/**'
- 'pyproject.toml'
- 'poetry.lock'
- 'pytest.ini'

jobs:
pytest:
runs-on: ubuntu-latest
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
env:
HYPOTHESIS_PROFILE: ci

steps:
- name: Checkout repo
uses: actions/checkout@v3

- name: Setup python
uses: actions/setup-python@v4
with:
python-version: '3.9'

- name: Setup Julia
uses: julia-actions/setup-julia@v1
with:
version: 1.9.3

- name: Install dependencies
run: |
python -m pip install --no-cache-dir --upgrade pip
pip install --no-cache-dir poetry==1.4.0
poetry config virtualenvs.create false
poetry install --extras "ray" --with dev
julia --project="julia_pkg" -e "using Pkg; Pkg.instantiate()"

- name: Run tests
run: python -m pytest tests --backend=numpy -p no:warnings --tb=native
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Tests
name: Tensorflow Tests
on:
push:
branches:
Expand Down Expand Up @@ -44,7 +44,7 @@ jobs:
julia --project="julia_pkg" -e "using Pkg; Pkg.instantiate()"

- name: Run tests
run: python -m pytest tests --cov=mrmustard --cov-report=term-missing --cov-report=xml -p no:warnings --tb=native
run: python -m pytest tests --backend=tensorflow --cov=mrmustard --cov-report=term-missing --cov-report=xml -p no:warnings --tb=native

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,7 @@ The physics module contains a growing number of functions that we can apply to s
The math module is the backbone of Mr Mustard, which consists in the [`Math`](https://github.com/XanaduAI/MrMustard/blob/main/mrmustard/math/math_interface.py) interface. Mr Mustard comes with a plug-and-play backends through a math interface. You can use it as a drop-in replacement for tensorflow or pytorch and your code will be plug-and-play too!
```python
from mrmustard import settings
from mrmustard.math import Math
math = Math()
import mrmustard.math as math

math.cos(0.1) # tensorflow

Expand Down
6 changes: 3 additions & 3 deletions doc/code/math.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ mrmustard.math

.. currentmodule:: mrmustard.math

Backends
---------
The `math` module
-----------------

.. toctree::
:maxdepth: 1

math/parameter_set
math/parameter
math/tensorflow
math/backend_manager

.. automodapi:: mrmustard.math
:no-heading:
Expand Down
8 changes: 8 additions & 0 deletions doc/code/math/backend_manager.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Backend Manager
===============

.. currentmodule:: mrmustard.math.backend_manager

.. automodapi:: mrmustard.math.backend_manager
:no-heading:
:include-all-objects:
8 changes: 8 additions & 0 deletions doc/code/math/backend_numpy.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Backend Manager
===============

.. currentmodule:: mrmustard.math.backend_numpy

.. automodapi:: mrmustard.math.backend_numpy
:no-heading:
:include-all-objects:
8 changes: 0 additions & 8 deletions doc/code/math/tensorflow.rst

This file was deleted.

3 changes: 1 addition & 2 deletions doc/introduction/basic_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ The math module is the backbone of Mr Mustard, which consists in the [Math](http
Mr Mustard comes with a plug-and-play backends through a math interface. You can use it as a drop-in replacement for tensorflow or pytorch and your code will be plug-and-play too!
```python
from mrmustard import settings
from mrmustard.math import Math
math = Math()
import mrmustard.math as math
ziofil marked this conversation as resolved.
Show resolved Hide resolved

math.cos(0.1) # tensorflow

Expand Down
5 changes: 1 addition & 4 deletions mrmustard/lab/abstract/measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@
from typing import Iterable, Sequence, Union

from mrmustard import settings
from mrmustard.math import Math
from mrmustard.math.parameter_set import ParameterSet
from mrmustard.math.parameters import Constant, Variable
from mrmustard.utils.typing import Tensor

from .state import State

math = Math()
import mrmustard.math as math


class Measurement(ABC):
Expand Down
4 changes: 1 addition & 3 deletions mrmustard/lab/abstract/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
from matplotlib import cm

from mrmustard import settings
from mrmustard.math import Math
from mrmustard.math.parameters import Constant, Variable
from mrmustard.physics import bargmann, fock, gaussian
from mrmustard.utils.typing import (
Expand All @@ -44,13 +43,12 @@
RealVector,
)
from mrmustard.physics.wigner import wigner_discretized
import mrmustard.math as math


if TYPE_CHECKING:
from .transformation import Transformation

math = Math()


# pylint: disable=too-many-instance-attributes
class State: # pylint: disable=too-many-public-methods
Expand Down
6 changes: 1 addition & 5 deletions mrmustard/lab/abstract/transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import numpy as np

from mrmustard import settings
from mrmustard.math import Math
from mrmustard.math.parameter_set import ParameterSet
from mrmustard.math.parameters import Constant, Variable
from mrmustard.math.tensor_networks import Tensor
Expand All @@ -33,7 +32,7 @@

from .state import State

math = Math()
import mrmustard.math as math


class Transformation(Tensor):
Expand Down Expand Up @@ -122,9 +121,6 @@ def _transform_gaussian(self, state: State, dual: bool) -> State:
) # NOTE: assumes modes don't change
return new_state

def _transform_fock(self, state: State, dual: bool) -> State:
raise NotImplementedError

@property
def num_modes(self) -> int:
r"""The number of modes on which the transformation acts."""
Expand Down
2 changes: 1 addition & 1 deletion mrmustard/lab/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from mrmustard.lab.abstract import State, Transformation
from mrmustard.utils.typing import RealMatrix, RealVector
from mrmustard.lab.circuit_drawer import circuit_text
from mrmustard.math.xptensor import XPMatrix, XPVector
from mrmustard.math.tensor_wrappers import XPMatrix, XPVector

import numpy as np

Expand Down
3 changes: 1 addition & 2 deletions mrmustard/lab/detectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from typing import Iterable, List, Optional, Tuple, Union

from mrmustard import settings
from mrmustard.math import Math
from mrmustard.physics import fock, gaussian
from mrmustard.utils.typing import RealMatrix, RealVector

Expand All @@ -28,7 +27,7 @@
from .states import Coherent, DisplacedSqueezed
from .utils import make_parameter

math = Math()
import mrmustard.math as math

__all__ = ["PNRDetector", "ThresholdDetector", "Generaldyne", "Homodyne", "Heterodyne"]

Expand Down
28 changes: 16 additions & 12 deletions mrmustard/lab/gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@
import numpy as np

from mrmustard import settings
from mrmustard.math import Math
from mrmustard.math.parameters import update_orthogonal, update_symplectic, update_unitary
from mrmustard.physics import fock, gaussian
from mrmustard.physics import gaussian, fock
from mrmustard.utils.typing import ComplexMatrix, RealMatrix
from mrmustard.math.parameters import update_orthogonal, update_symplectic, update_unitary
from .abstract import Channel, Unitary, State
from .utils import make_parameter

math = Math()
import mrmustard.math as math

__all__ = [
"Dgate",
Expand Down Expand Up @@ -114,6 +112,8 @@ def U(self, cutoffs: Optional[Sequence[int]] = None, shape: Optional[Sequence[in
Raises:
ValueError: if the length of the cutoffs array is different from N and 2N
"""
from mrmustard.physics.fock_custom_grads import displacement

N = self.num_modes
if cutoffs is None:
pass
Expand All @@ -137,17 +137,17 @@ def U(self, cutoffs: Optional[Sequence[int]] = None, shape: Optional[Sequence[in
Ud = None
for idx, out_in in enumerate(zip(shape[:N], shape[N:])):
if Ud is None:
Ud = fock.displacement(x[idx], y[idx], shape=out_in)
Ud = displacement(x[idx], y[idx], shape=out_in)
else:
U_next = fock.displacement(x[idx], y[idx], shape=out_in)
U_next = displacement(x[idx], y[idx], shape=out_in)
Ud = math.outer(Ud, U_next)

return math.transpose(
Ud,
list(range(0, 2 * N, 2)) + list(range(1, 2 * N, 2)),
)
else:
return fock.displacement(x[0], y[0], shape=shape)
return displacement(x[0], y[0], shape=shape)


class Sgate(Unitary):
Expand Down Expand Up @@ -206,6 +206,8 @@ def U(self, cutoffs: Optional[Sequence[int]] = None, shape: Optional[Sequence[in
Returns:
array[complex]: the unitary matrix
"""
from mrmustard.physics.fock_custom_grads import squeezer

N = self.num_modes
if cutoffs is None:
pass
Expand All @@ -230,16 +232,16 @@ def U(self, cutoffs: Optional[Sequence[int]] = None, shape: Optional[Sequence[in
Us = None
for idx, single_shape in enumerate(zip(shape[:N], shape[N:])):
if Us is None:
Us = fock.squeezer(r[idx], phi[idx], shape=single_shape)
Us = squeezer(r[idx], phi[idx], shape=single_shape)
else:
U_next = fock.squeezer(r[idx], phi[idx], shape=single_shape)
U_next = squeezer(r[idx], phi[idx], shape=single_shape)
Us = math.outer(Us, U_next)
return math.transpose(
Us,
list(range(0, 2 * N, 2)) + list(range(1, 2 * N, 2)),
)
else:
return fock.squeezer(r[0], phi[0], shape=shape)
return squeezer(r[0], phi[0], shape=shape)

@property
def X_matrix(self):
Expand Down Expand Up @@ -511,6 +513,8 @@ def U(
Returns:
array[complex]: the unitary tensor of the beamsplitter
"""
from mrmustard.physics.fock_custom_grads import beamsplitter

if cutoffs is None:
pass
elif len(cutoffs) == 4:
Expand All @@ -522,7 +526,7 @@ def U(

shape = shape or cutoffs

return fock.beamsplitter(
return beamsplitter(
self.theta.value,
self.phi.value,
shape=shape,
Expand Down
3 changes: 1 addition & 2 deletions mrmustard/lab/states.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from typing import List, Optional, Sequence, Tuple, Union

from mrmustard import settings
from mrmustard.math import Math
from mrmustard.math.parameters import update_symplectic
from mrmustard.math.parameter_set import ParameterSet
from mrmustard.physics import fock, gaussian
Expand All @@ -28,7 +27,7 @@
from .abstract import State
from .utils import make_parameter

math = Math()
import mrmustard.math as math


__all__ = [
Expand Down
4 changes: 1 addition & 3 deletions mrmustard/lab/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
from typing import Callable, Optional, Tuple
from mrmustard.math.parameters import update_euclidean

from mrmustard.math import Math
from mrmustard.math.parameters import Constant, Variable

math = Math()
import mrmustard.math as math


def make_parameter(
Expand Down
Loading
Loading