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

Tutorials for lab_dev and physics #392

Open
wants to merge 92 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
92 commits
Select commit Hold shift + click to select a range
5937525
added func
SamFerracin Apr 15, 2024
be61862
done
SamFerracin Apr 16, 2024
046af14
done
SamFerracin Apr 16, 2024
ee13a6c
test trace out
SamFerracin Apr 16, 2024
1ff349c
tests
SamFerracin Apr 16, 2024
7934dae
more progress
SamFerracin Apr 17, 2024
4316359
tests
SamFerracin Apr 17, 2024
7fdfd39
done
SamFerracin Apr 17, 2024
56e4467
done
SamFerracin Apr 17, 2024
59019a0
ugh
SamFerracin Apr 17, 2024
375d260
Merge branch 'develop' of https://github.com/XanaduAI/MrMustard into …
SamFerracin Apr 17, 2024
91a9678
done
SamFerracin Apr 17, 2024
55df8d1
Merge branch 'develop' of https://github.com/XanaduAI/MrMustard into …
SamFerracin Apr 19, 2024
decc3d0
need to fix things
SamFerracin Apr 19, 2024
7b642a1
Merge branch 'develop' into exp-val
ziofil Apr 19, 2024
d133ecc
Update mrmustard/lab_dev/states/base.py
Apr 22, 2024
f479998
Update mrmustard/lab_dev/states/base.py
Apr 22, 2024
90161ef
some progress
SamFerracin Apr 22, 2024
6e7de41
etsts pass
SamFerracin Apr 22, 2024
a3ab903
done?
SamFerracin Apr 22, 2024
8e80056
Update mrmustard/lab_dev/states/base.py
Apr 22, 2024
6198a17
Update mrmustard/lab_dev/states/base.py
Apr 22, 2024
397d251
bl
SamFerracin Apr 22, 2024
665bc82
odc
SamFerracin Apr 22, 2024
076f2f0
done
SamFerracin Apr 25, 2024
2a4b1c7
ll
SamFerracin Apr 25, 2024
0eeb99f
bug
SamFerracin Apr 25, 2024
cc38e05
Update mrmustard/lab_dev/states/base.py
Apr 25, 2024
36b66eb
Update mrmustard/lab_dev/states/base.py
Apr 25, 2024
d25e6bb
Update mrmustard/lab_dev/states/base.py
Apr 25, 2024
66a90de
Update mrmustard/lab_dev/states/base.py
Apr 25, 2024
9513299
Merge branch 'exp-val' of https://github.com/XanaduAI/MrMustard into …
SamFerracin Apr 25, 2024
b073f0a
Merge branch 'develop' of https://github.com/XanaduAI/MrMustard into …
SamFerracin Apr 25, 2024
d31eaa8
ds
SamFerracin Apr 25, 2024
562295e
revert
SamFerracin Apr 25, 2024
a78ea14
cr
SamFerracin Apr 26, 2024
010b732
gaussian integrals
SamFerracin Apr 26, 2024
7dc4088
more
SamFerracin Apr 26, 2024
f6b1cf5
Merge branch 'exp-val' of https://github.com/XanaduAI/MrMustard into …
SamFerracin Apr 26, 2024
c673986
..
SamFerracin Apr 29, 2024
a5fe7a3
..
SamFerracin Apr 29, 2024
09aa175
..
SamFerracin Apr 29, 2024
013abdb
..
SamFerracin Apr 29, 2024
e45ccd8
..
SamFerracin Apr 29, 2024
bc01a51
progress
SamFerracin Apr 29, 2024
e52d85f
yuan
SamFerracin May 3, 2024
9122fc0
progress
SamFerracin May 3, 2024
f8a6357
add docs for init physics
sylviemonet May 3, 2024
9815b93
add more docs
sylviemonet May 3, 2024
02a9249
add docs
sylviemonet May 3, 2024
3b2c366
add anstaz docs
sylviemonet May 3, 2024
fc95c68
Merge branch 'develop' into tutorials
sylviemonet May 3, 2024
f254561
bargmann theory docs
sylviemonet May 3, 2024
e5bd587
test acs|| diagram
sylviemonet May 6, 2024
110f959
converters doc
sylviemonet May 6, 2024
b6e6c08
moved phys funcs
sylviemonet May 7, 2024
a220c98
oops
sylviemonet May 7, 2024
e3b8822
add diagram and fix codefactor
sylviemonet May 7, 2024
0ab8fd3
fix code block converters
sylviemonet May 7, 2024
a25f9db
s
SamFerracin May 7, 2024
5d2e3d3
docs in converters
sylviemonet May 7, 2024
c020208
fix the structure
sylviemonet May 7, 2024
a7cb003
fix the structure again
sylviemonet May 7, 2024
6660ea4
big changes in structure in physics
sylviemonet May 7, 2024
fd4cdda
try again
sylviemonet May 7, 2024
387c0c3
try to fix errors with math
sylviemonet May 7, 2024
190a526
states
SamFerracin May 7, 2024
9d79fb7
Merge branch 'tutorials' of https://github.com/XanaduAI/MrMustard int…
SamFerracin May 7, 2024
5626f27
bargmann docs
sylviemonet May 7, 2024
e8fbe74
bargmamnn docs
sylviemonet May 7, 2024
a0be9ab
fix bugs in cc
sylviemonet May 7, 2024
d6b3dd7
julia
SamFerracin May 7, 2024
51238fa
Merge branch 'tutorials' of https://github.com/XanaduAI/MrMustard int…
SamFerracin May 7, 2024
e46d9a9
the docs in bargmann
sylviemonet May 7, 2024
0acd882
fix diagrams
sylviemonet May 7, 2024
39089af
bargmann docs goooo
sylviemonet May 7, 2024
17f9b31
fix diagrams
sylviemonet May 7, 2024
9c339a2
add docs in fock
sylviemonet May 7, 2024
1fbc022
add the docs for ansatze
sylviemonet May 8, 2024
5d54544
gaussian integral docs
sylviemonet May 8, 2024
42f5273
progress
SamFerracin May 9, 2024
d7a5a67
fix bugs in docs
sylviemonet May 9, 2024
d887eaa
add comments
sylviemonet May 9, 2024
7122176
Merge branch 'tutorials' of https://github.com/XanaduAI/MrMustard int…
SamFerracin May 9, 2024
c9e9189
oops
SamFerracin May 10, 2024
2586020
Merge branch 'develop' into tutorials
sylviemonet May 13, 2024
0c02cee
Merge branch 'develop' into tutorials
sylviemonet May 15, 2024
ea7333e
Update mrmustard/lab_dev/circuit_components.py
sylviemonet May 15, 2024
f4889f8
fix bugs from kasper's comments
sylviemonet May 15, 2024
fea110a
fix codefactor
sylviemonet May 15, 2024
da55f30
blacked
sylviemonet May 15, 2024
248bc1b
Merge branch 'develop' into tutorials
sylviemonet May 15, 2024
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
2 changes: 1 addition & 1 deletion .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ cutoff of the first detector is equal to 1, the resulting density matrix is now

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

def make_circ(x=0.):
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ We can turn any simulation in Mr Mustard into an optimization by marking which p
```python
from mrmustard import math
from mrmustard.lab import Dgate, Ggate, Attenuator, Vacuum, Coherent, DisplacedSqueezed
from mrmustard.physics import fidelity
from mrmustard.physics.utils import fidelity
from mrmustard.training import Optimizer

math.change_backend("tensorflow")
Expand Down
9 changes: 7 additions & 2 deletions doc/code/lab_dev.rst
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
mrmustard.lab_dev
=================

.. currentmodule:: mrmustard.lab_dev

.. automodapi:: mrmustard.lab_dev
:no-heading:
:include-all-objects:

.. toctree::
:maxdepth: 1

lab_dev/wires
lab_dev/circuit_components
lab_dev/circuit_components_utils
lab_dev/states
lab_dev/transformations
lab_dev/circuits
lab_dev/simulator

.. currentmodule:: mrmustard.lab_dev
8 changes: 8 additions & 0 deletions doc/code/lab_dev/circuit_components_utils.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
mrmustard.lab_dev.circuit_components_utils
==========================================

.. currentmodule:: mrmustard.lab_dev.circuit_components_utils

.. automodapi:: mrmustard.lab_dev.circuit_components_utils
:no-heading:
:include-all-objects:
21 changes: 7 additions & 14 deletions doc/code/physics.rst
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
mrmustard.physics
=================

.. currentmodule:: mrmustard.physics

.. automodapi:: mrmustard.physics
:no-heading:
:include-all-objects:

.. toctree::
:maxdepth: 1

physics/ansatze
physics/representations
physics/converters

.. toctree::
:maxdepth: 1

physics/bargmann
physics/utils/fock_calculations
physics/utils/gaussian_calculations

.. currentmodule:: mrmustard.physics

.. automodapi:: mrmustard.physics
:no-heading:
:include-all-objects:
physics/converters
2 changes: 1 addition & 1 deletion doc/code/physics/ansatze.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The representations' Ansatze
The Ansatze of the representation
============================

.. currentmodule:: mrmustard.physics.ansatze
Expand Down
11 changes: 8 additions & 3 deletions doc/code/physics/bargmann.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
Calculations on Bargmann objects
================================
The Bargmann representation theory
==========================================
.. currentmodule:: mrmustard.physics.bargmann

.. automodapi:: mrmustard.physics.bargmann
:no-heading:
:include-all-objects:

.. toctree::
:maxdepth: 1

utils/triples
utils/bargmann_calculations
utils/gaussian_integral

3 changes: 1 addition & 2 deletions doc/code/physics/converters.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
The Converters between different Representations
The converters between different representations
==========================================

.. currentmodule:: mrmustard.physics.converters

.. automodapi:: mrmustard.physics.converters
Expand Down
10 changes: 10 additions & 0 deletions doc/code/physics/fock.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
The Fock representation theory
==========================================

.. currentmodule:: mrmustard.physics.fock

.. automodapi:: mrmustard.physics.fock
:no-heading:
:include-all-objects:

This section of functions is the helper function section to calculate properties of object in Fock representation, which needs to be rearranged and possibly be moved under Fock representation class.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
This section of functions is the helper function section to calculate properties of object in Fock representation, which needs to be rearranged and possibly be moved under Fock representation class.
This module contains helper functions to calculate properties of objects in Fock representation.

10 changes: 8 additions & 2 deletions doc/code/physics/representations.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
The Fock-Bargmann and Fock Representations
The representations of quantum objects
==========================================

.. currentmodule:: mrmustard.physics.representations

.. automodapi:: mrmustard.physics.representations
:no-heading:
:include-all-objects:
:include-all-objects:

.. toctree::
:maxdepth: 1

bargmann
fock
8 changes: 0 additions & 8 deletions doc/code/physics/utils/bargmann_calculations.rst

This file was deleted.

8 changes: 0 additions & 8 deletions doc/code/physics/utils/fock_calculations.rst

This file was deleted.

8 changes: 0 additions & 8 deletions doc/code/physics/utils/gaussian_calculations.rst

This file was deleted.

2 changes: 1 addition & 1 deletion doc/code/physics/utils/gaussian_integral.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Calculations on Gaussian Integrals
================================
==================================

.. currentmodule:: mrmustard.physics.gaussian_integrals

Expand Down
2 changes: 1 addition & 1 deletion doc/development/development_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ The ``-e`` flag ensures that edits to the source code will be reflected when
importing Mr Mustard in Python.

Increased numerical stability using Julia [optional]
------------------
----------------------------------------------------
Converting phase space objects to Fock space can be numerically unstable due to accumulating floating point errors.
To resolve this, the conversion can be performed with extended-precision arithmetic. To use this feature,
an installation of `Julia <https://github.com/JuliaLang/juliaup#installation>`_ is required (version 1.9.3 recommended).
Expand Down
2 changes: 1 addition & 1 deletion doc/introduction/basic_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ displaced squeezed state.
```python
from mrmustard import math
from mrmustard.lab import Dgate, Ggate, Attenuator, Vacuum, Coherent, DisplacedSqueezed
from mrmustard.physics import fidelity
from mrmustard.physics.utils import fidelity
from mrmustard.training import Optimizer

math.change_backend("tensorflow")
Expand Down
2 changes: 1 addition & 1 deletion mrmustard/lab/abstract/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ def primal(self, other: Union[State, Transformation]) -> State:
``self``: ``other << self`` is like ``self >> other^dual``.

Note that the returned state is not normalized. To normalize a state you can use
``mrmustard.physics.normalize``.
``mrmustard.physics.utils.normalize``.
"""
# import pdb

Expand Down
110 changes: 107 additions & 3 deletions mrmustard/lab_dev/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,117 @@
# limitations under the License.

r"""
The lab module in development.
"""
The :mod:`~mrmustard.lab_dev` module contains all the objects that would normally be found in a photonic lab, such as:

* Several useful states, such as :class:`~mrmustard.lab_dev.states.Vacuum`,
:class:`~mrmustard.lab_dev.states.Coherent`, and :class:`~mrmustard.lab_dev.states.SqueezedVacuum`.

* The gates routinely performed in optical experiments, including
:class:`~mrmustard.lab_dev.transformations.Dgate`, :class:`~mrmustard.lab_dev.transformations.Sgate`,
and :class:`~mrmustard.lab_dev.transformations.BSgate`.

* Typical noise channels such as the :class:`~mrmustard.lab_dev.transformations.Attenuator`.

* Detectors (currently in development).

Additionally, it enables users to initialize custom states, gates, channels, and measurements.

The ``>>`` operator allows combining these objects together with intuitive, paper-style syntax.

.. code-block::

>>> from mrmustard.lab_dev import *
>>> from mrmustard import settings
>>> import numpy as np

>>> settings.AUTOCUTOFF_MAX_CUTOFF = 40

>>> # initialize three single-mode states with one, zero, and zero photons, respectively
>>> s0 = Number(modes=[0], n=1)
>>> s1 = Number(modes=[1], n=0)
>>> s2 = Number(modes=[2], n=0)

>>> # initialize 50/50 beam splitters between modes (0, 1) and (1, 2)
>>> bs01 = BSgate(modes=[0, 1], theta=np.pi/4)
>>> bs12 = BSgate(modes=[1, 2], theta=np.pi/4)

>>> # simulate the result of an experiment where the three states are sent through
>>> # the beam splitters
>>> result = s0 >> s1 >> s2 >> bs01 >> bs12

>>> # calculate expectation values on the resulting state
>>> assert np.allclose(result.expectation(Number(modes=[0], n=0)), 0.5)
>>> assert np.allclose(result.expectation(Number(modes=[0], n=1)), 0.5)
>>> assert np.allclose(result.expectation(Number(modes=[0], n=2)), 0)
>>> assert np.allclose(result.expectation(Number(modes=[0, 1, 2], n=[0, 1, 0])), 0.25)

While :mod:`~mrmustard.lab_dev` is designed to be intuitive and easy to use, proficient users can take advantage
of several features to speed up their computations. For example, using
:class:`~mrmustard.lab_dev.circuits.Circuit`\s and the :class:`~mrmustard.lab_dev.simulator.Simulator`,
they can take advantage of built-in tensor network functionality to run circuits more efficiently.

.. code-block::

>>> from mrmustard.lab_dev import *
>>> from mrmustard import settings
>>> import numpy as np

>>> settings.AUTOCUTOFF_MAX_CUTOFF = 40

>>> # initialize three single-mode states with one, zero, and zero photons, respectively
>>> s0 = Number(modes=[0], n=1)
>>> s1 = Number(modes=[1], n=0)
>>> s2 = Number(modes=[2], n=0)

>>> # initialize 50/50 beam splitters between modes (0, 1) and (1, 2)
>>> bs01 = BSgate(modes=[0, 1], theta=np.pi/4)
>>> bs12 = BSgate(modes=[1, 2], theta=np.pi/4)

>>> # initialize projectors for modes 1 and 2
>>> p1 = Number(modes=[1], n=0).dual
>>> p2 = Number(modes=[2], n=0).dual

>>> # initialize a circuit
>>> circ = Circuit([s0, s1, s2, bs01, bs12, p1, p2])

>>> # specify a "contraction path" for the circuit above -- i.e., in what order you'd
>>> # like to combine its components via ``>>``
>>> circ.path = [(4, 5), (4, 6), (2, 4), (0, 3), (1, 0), (1, 2)]

>>> # run the circuit -- the `Simulator` will follow the circuit's path
>>> result = Simulator().run(circ)
>>> assert result == s0 >> s1 >> s2 >> bs01 >> bs12 >> p1 >> p2

>>> # every path leads to the same result, but some paths are faster than others.
>>> # for example, selecting this path leads to a simulation time of about 17ms on a
>>> # typical laptop ...
>>> circ.path = [(4, 5), (4, 6), (2, 4), (0, 3), (1, 0), (1, 2)]
>>> assert result == Simulator().run(circ)

>>> # ... while with this path, the simulation time drops to about 7ms.
>>> circ.path = [(0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6)]
>>> assert result == Simulator().run(circ)

Check out our guides to learn more about :mod:`~mrmustard.lab_dev` and its core functionalities:

* The :mod:`~mrmustard.lab_dev.circuit_components` guide introduces the
:class:`~mrmustard.lab_dev.circuit_components.CircuitComponent` class, which is the parent of
every state, gate, channel, and measurement object in :mod:`~mrmustard.lab_dev`.
* The :mod:`~mrmustard.lab_dev.states` guide illustrates how to initialize states.
* The :mod:`~mrmustard.lab_dev.transformations` guide shows how to initialize unitary gates and
channels.
* :mod:`~mrmustard.lab_dev.circuits` tells you everything you need to know about circuits.
* The :mod:`~mrmustard.lab_dev.simulator` page shows how to use the simulator object and discusses
how to select the best paths for your circuits.
* For more advanced functionality, take a look at the :mod:`~mrmustard.lab_dev.circuit_components`
module, which contains a series of maps (e.g., the trace-out operation) expressed as circuit
components.
"""

from .circuit_components import *
from .circuit_components_utils import *
from .circuits import *
from .states import *
from .simulator import *
from .transformations import *
from .wires import Wires
from .wires import *
Loading
Loading