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 1 commit
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
Prev Previous commit
Next Next commit
progress
  • Loading branch information
SamFerracin committed May 9, 2024
commit 42f52733931b8d0636833817d4c3b76e4efe9ae2
34 changes: 33 additions & 1 deletion mrmustard/lab_dev/circuits.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,39 @@
# pylint: disable=too-many-branches

"""
A class to quantum circuits.
A class to represent quantum circuits.

Quantum circuits store a list of ``CircuitComponent``s, without performing any contraction.

.. code-block::

>>> from mrmustard.lab_dev import BSgate, Sgate, Vacuum, Circuit

>>> vac = Vacuum([0, 1, 2])
>>> s01 = Sgate([0, 1], r=[0.1, 0.2])
>>> bs01 = BSgate([0, 1])
>>> bs12 = BSgate([1, 2])

>>> components = [vac, s01, bs01, bs12]
>>> circ = Circuit(components)
>>> assert circ.components == components

The ``>>`` operator allows appending new components, or even entire circuits.

.. code-block::

>>> from mrmustard.lab_dev import BSgate, Sgate, Vacuum, Circuit

>>> vac = Vacuum([0, 1, 2])
>>> s01 = Sgate([0, 1], r=[0.1, 0.2])
>>> bs01 = BSgate([0, 1])
>>> bs12 = BSgate([1, 2])

>>> circ1 = Circuit() >> vac >> s01
>>> circ2 = Circuit([bs01]) >> bs12
>>> assert circ1 >> circ2 == Circuit([vac, s01, bs01, bs12])

The operations in a circuit can be contracted using the :class:`~mrmustard.lab_dev.simulator.Simulator`.
"""

from __future__ import annotations
Expand Down
60 changes: 59 additions & 1 deletion mrmustard/lab_dev/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,66 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""
r"""
Simulators for quantum circuits.

The simulator allows contracting the components stored in quantum circuits.

.. code-block::

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

>>> # initialize a circuit
>>> state = Number(modes=[0, 1], n=[2, 0], cutoffs=2)
>>> gate = BSgate([0, 1], theta=np.pi/4)
>>> proj1 = Number(modes=[1], n=[0]).dual
>>> circuit = Circuit([state, gate, proj1])

>>> # run the simulation
>>> result = Simulator().run(circuit)

>>> # the simulator returns a component that can be potentially be plugged
>>> # into another circuit
>>> assert isinstance(result, CircuitComponent)

The simulation is carried out by contracting the components of the given circuit in pairs,
until only one component is left and returned. In the examples above, the contractions happen
in a "left-to-right" fashion, meaning that the left-most component in the circuit (``state``)
is contracted with the one in its right (``gate``), and finally the resulting component is
contracted with the projector. This provides a simple and convenient way to run simulations,
but for large circuits, different contraction paths may be more efficient.

The ``path`` attribute of ``Circuit``\s allows customising the contraction order and potentially
speeding up the simulation. When a ``path`` of the type ``[(i, j), (l, m), ...]`` is given, the
simulator creates a dictionary of the type ``{0: c0, ..., N: cN}``, where ``[c0, .., cN]``
is the ``circuit.component`` list. Then:

* The two components ``ci`` and ``cj`` in positions ``i`` and ``j`` are contracted. ``ci`` is
replaced by the resulting component ``cj >> cj``, while ``cj`` is popped.
* The two components ``cl`` and ``cm`` in positions ``l`` and ``m`` are contracted. ``cl`` is
replaced by the resulting component ``cl >> cm``, while ``cm`` is popped.
* Et cetera.

Below is an example where a circuit is simulated in a "right-to-left" fashion:

.. code-block::

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

>>> state = Number(modes=[0, 1], n=[2, 0], cutoffs=2)
>>> gate = BSgate([0, 1], theta=np.pi/4)
>>> proj01 = Number(modes=[0, 1], n=[2, 0]).dual

>>> # initialize the circuit and specify a custom path
>>> circuit = Circuit([state, gate, proj01])
>>> circuit.path = [(1, 2), (0, 1)]

>>> result = Simulator().run(circuit)

The setter for ``path`` also validates the path using the ``validate_path`` function of
``Circuit``.
"""

from __future__ import annotations
Expand Down
9 changes: 4 additions & 5 deletions mrmustard/lab_dev/states/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
>>> assert dm.representation.c.shape == (1,)

In addition to providing these built-in states, Mr Mustard allows initializing custom
:class:`~mrmustard.lab_dev.states.Ket`\s and :class:`~mrmustard.lab_dev.states.DM`\s via the
appropriate constructors. The snippet belowe shows how to initialize a ``Ket`` from
:class:`~mrmustard.lab_dev.states.Ket`\s and :class:`~mrmustard.lab_dev.states.DM`\s with the
desired representation. The snippet belowe shows how to initialize a ``Ket`` from
the Bargmann triple of the squeezed vacuum state. Analogous methods exist to initialize
``Ket``\s and ``DM``\s from Fock arrays and from quadrature.

Expand All @@ -71,13 +71,12 @@
>>> from mrmustard.lab_dev.transformations import Sgate
>>> from mrmustard.physics import triples

>>> # write the A, b, c
>>> # the A, b, c triple of the squeezed vacuum state
>>> A, b, c = triples.squeezed_vacuum_state_Abc(r=0.8)

>>> # use it to generate a ``Ket``
>>> # use the tripls to generate a ``Ket``
>>> my_ket = Ket.from_bargmann(modes=[0], triple=(A, b, c), name="my_ket")

>>> # let's check
>>> assert my_ket == Vacuum([0]) >> Sgate([0], r=0.8)
>>> assert my_ket == SqueezedVacuum([0], r=0.8)

Expand Down
22 changes: 20 additions & 2 deletions mrmustard/lab_dev/transformations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,26 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""
The classes representing transformations in quantum circuits.
r"""
The avaliable transformation.

As for states, Mr Mustard provides a range of built-in transformations, such as:

.. code-block::

>>> from mrmustard.lab_dev.states import Dgate, Sgate, Attenuator

>>> # the displacement gate
>>> dgate = Dgate(modes=[0], x=1)

>>> # the squeezing gate
>>> sgate = Sgate(modes=[1, 2], r=0.8)

>>> # the attenuator channel
>>> att = Attenuator(modes=[3], eta=0.9)

All these transformations are of one of two types, namely :class:`~mrmustard.lab_dev.transformations.Unitary` or
:class:`~mrmustard.lab_dev.transformations.Channel`.
"""

from .base import *
Expand Down