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
tests
  • Loading branch information
SamFerracin committed Apr 17, 2024
commit 43163590767b60f6f10860db9dbefe156cbde259
2 changes: 1 addition & 1 deletion mrmustard/lab_dev/states/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ def expectation(self, operator: CircuitComponent):
msg = f"Expected an observable defined for modes `{self.modes}` or a subset thereof, "
msg += f"found one defined for modes `{operator.modes}.`"
raise ValueError(msg)

result = (self @ operator @ self.dual).representation
return result.array if isinstance(result, Fock) else result.c

Expand Down
56 changes: 24 additions & 32 deletions mrmustard/physics/triples.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,30 @@ def beamsplitter_gate_Abc(
return A, b, c


def identity_Abc(n_modes: int) -> Union[Matrix, Vector, Scalar]:
r"""
The ``(A, b, c)`` triple of a tensor product of identity gates.

Args:
n_modes: The number of modes.

Returns:
The ``(A, b, c)`` triple of the identities.
"""
O_n = math.zeros((n_modes, n_modes), math.complex128)
I_n = (
math.diag(math.astensor([1.0 + 0j for _ in range(n_modes)]))
.reshape((n_modes, n_modes))
.reshape((n_modes, n_modes))
)

A = math.block([[O_n, I_n], [I_n, O_n]])
b = _vacuum_B_vector(n_modes * 2)
c = 1.0 + 0j

return A, b, c


# ~~~~~~~~~~
# Channels
# ~~~~~~~~~~
Expand Down Expand Up @@ -437,38 +461,6 @@ def amplifier_Abc(g: Union[float, Iterable[float]]) -> Union[Matrix, Vector, Sca
return A, b, c


def identity_Abc(n_modes: int) -> Union[Matrix, Vector, Scalar]:
r"""
The ``(A, b, c)`` triple of a tensor product of identity channels

Args:
n_modes: The number of modes.

Returns:
The ``(A, b, c)`` triple of the Fock damping channels.
"""
O_n = math.zeros((n_modes, n_modes), math.complex128)
I_n = (
math.diag(math.astensor([1.0 + 0j for _ in range(n_modes)]))
.reshape((n_modes, n_modes))
.reshape((n_modes, n_modes))
)

A = math.block(
[
[O_n, I_n, O_n, O_n],
[I_n, O_n, O_n, O_n],
[O_n, O_n, O_n, I_n],
[O_n, O_n, I_n, O_n],
]
)

b = _vacuum_B_vector(n_modes * 4)
c = 1.0 + 0j

return A, b, c


def fock_damping_Abc(n_modes: int) -> Union[Matrix, Vector, Scalar]:
r"""
The ``(A, b, c)`` triple of a tensor product of Fock dampers.
Expand Down
21 changes: 17 additions & 4 deletions tests/test_lab_dev/test_circuit_components_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@

import pytest

from mrmustard import settings
from mrmustard.physics.triples import identity_Abc
from mrmustard.physics.representations import Bargmann
from mrmustard.lab_dev.circuit_components_utils import TraceOut
from mrmustard.lab_dev.states import Coherent
from mrmustard.lab_dev.transformations import Dgate, Attenuator, Unitary
from mrmustard.lab_dev.wires import Wires

# original settings
autocutoff_max0 = settings.AUTOCUTOFF_MAX_CUTOFF


class TestTraceOut:
r"""
Expand All @@ -39,6 +42,16 @@ def test_init(self, modes):
assert tr.wires == Wires(modes_in_bra=set(modes), modes_in_ket=set(modes))
assert tr.representation == Bargmann(*identity_Abc(len(modes)))

def test_trace_out_states(self):
assert Coherent([0, 1, 2], x=1) >> TraceOut([0]) == Coherent([1, 2], x=1).dm()
assert Coherent([0, 1, 2], x=1) >> TraceOut([1, 2]) == Coherent([0], x=1).dm()
def test_trace_out_bargmann_states(self):
state = Coherent([0, 1, 2], x=1)
assert state >> TraceOut([0]) == Coherent([1, 2], x=1).dm()
assert state >> TraceOut([1, 2]) == Coherent([0], x=1).dm()

def test_trace_out_fock_states(self):
settings.AUTOCUTOFF_MAX_CUTOFF = 10

state = Coherent([0, 1, 2], x=1).to_fock_component()
assert state >> TraceOut([0]) == Coherent([1, 2], x=1).to_fock_component().dm()
assert state >> TraceOut([1, 2]) == Coherent([0], x=1).to_fock_component().dm()

settings.AUTOCUTOFF_MAX_CUTOFF = autocutoff_max0
89 changes: 69 additions & 20 deletions tests/test_lab_dev/test_states.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,30 +169,30 @@ def test_expectation_bargmann(self):
res0 = ((dm @ op0) >> TraceOut(dm.modes)).representation.c
res1 = ((dm @ op1) >> TraceOut(dm.modes)).representation.c
res01 = ((dm @ op01) >> TraceOut(dm.modes)).representation.c
assert math.allclose(ket.expectation(op0), res0)
assert math.allclose(ket.expectation(op1), res1)

assert math.allclose(ket.expectation(op0), res0)
assert math.allclose(ket.expectation(op1), res1)
assert math.allclose(ket.expectation(op01), res01)

# def test_expectation_fock(self):
# settings.AUTOCUTOFF_MAX_CUTOFF = 10
def test_expectation_fock(self):
settings.AUTOCUTOFF_MAX_CUTOFF = 10

# ket = Coherent([0, 1], x=1, y=[2, 3]).to_fock_component()
# dm = ket.dm()
ket = Coherent([0, 1], x=1, y=[2, 3]).to_fock_component()
dm = ket.dm()

# op0 = Dgate([1], x=0.1)
# op1 = Dgate([0], x=0.2)
# op01 = Dgate([0, 1], x=[0.3, 0.4])
op0 = Dgate([1], x=0.1)
op1 = Dgate([0], x=0.2)
op01 = Dgate([0, 1], x=[0.3, 0.4])

# res0 = ((dm @ op0) >> TraceOut(dm.modes)).representation.array
# res1 = ((dm @ op1) >> TraceOut(dm.modes)).representation.array
# res01 = ((dm @ op01) >> TraceOut(dm.modes)).representation.array

# assert math.allclose(ket.expectation(op0), res0)
# assert math.allclose(ket.expectation(op1), res1)
# assert math.allclose(ket.expectation(op01), res01)
res0 = ((dm @ op0) >> TraceOut(dm.modes)).representation.array
res1 = ((dm @ op1) >> TraceOut(dm.modes)).representation.array
res01 = ((dm @ op01) >> TraceOut(dm.modes)).representation.array

# settings.AUTOCUTOFF_MAX_CUTOFF = autocutoff_max0
assert math.allclose(ket.expectation(op0), res0)
assert math.allclose(ket.expectation(op1), res1)
assert math.allclose(ket.expectation(op01), res01)

settings.AUTOCUTOFF_MAX_CUTOFF = autocutoff_max0

def test_expectation_error(self):
ket = Coherent([0, 1], x=1, y=[2, 3])
Expand All @@ -209,8 +209,6 @@ def test_expectation_error(self):
with pytest.raises(ValueError, match="Expected an observable defined for modes"):
ket.expectation(op3)



def test_rshift(self):
ket = Coherent([0, 1], 1)
unitary = Dgate([0], 1)
Expand Down Expand Up @@ -360,6 +358,57 @@ def test_purity(self):
assert math.allclose(state.purity, 1)
assert state.is_pure

def test_expectation_bargmann(self):
ket = Coherent([0, 1], x=1, y=[2, 3])
dm = ket.dm()

op0 = Dgate([1], x=0.1)
op1 = Dgate([0], x=0.2)
op01 = Dgate([0, 1], x=[0.3, 0.4])

res0 = ((dm @ op0) >> TraceOut(dm.modes)).representation.c
res1 = ((dm @ op1) >> TraceOut(dm.modes)).representation.c
res01 = ((dm @ op01) >> TraceOut(dm.modes)).representation.c

assert math.allclose(dm.expectation(op0), res0)
assert math.allclose(dm.expectation(op1), res1)
assert math.allclose(dm.expectation(op01), res01)

def test_expectation_fock(self):
settings.AUTOCUTOFF_MAX_CUTOFF = 10

ket = Coherent([0, 1], x=1, y=[2, 3]).to_fock_component()
dm = ket.dm()

op0 = Dgate([1], x=0.1)
op1 = Dgate([0], x=0.2)
op01 = Dgate([0, 1], x=[0.3, 0.4])

res0 = ((dm @ op0) >> TraceOut(dm.modes)).representation.array
res1 = ((dm @ op1) >> TraceOut(dm.modes)).representation.array
res01 = ((dm @ op01) >> TraceOut(dm.modes)).representation.array

assert math.allclose(dm.expectation(op0), res0)
assert math.allclose(dm.expectation(op1), res1)
assert math.allclose(dm.expectation(op01), res01)

settings.AUTOCUTOFF_MAX_CUTOFF = autocutoff_max0

def test_expectation_error(self):
dm = Coherent([0, 1], x=1, y=[2, 3]).dm()

op1 = Attenuator([0])
with pytest.raises(ValueError, match="wires on the bra side"):
dm.expectation(op1)

op2 = CircuitComponent("", None, modes_in_ket=[0], modes_out_ket=[1])
with pytest.raises(ValueError, match="different modes"):
dm.expectation(op2)

op3 = Dgate([2])
with pytest.raises(ValueError, match="Expected an observable defined for modes"):
dm.expectation(op3)

def test_rshift(self):
ket = Coherent([0, 1], 1)
unitary = Dgate([0], 1)
Expand Down
11 changes: 11 additions & 0 deletions tests/test_physics/test_triples.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,17 @@ def test_beamsplitter_gate_Abc(self):
assert math.allclose(b3, np.zeros((4)))
assert math.allclose(c3, 1)

def test_identity_Abc(self):
A1, b1, c1 = triples.identity_Abc(1)
assert math.allclose(A1, [[0, 1], [1, 0]])
assert math.allclose(b1, [0, 0])
assert math.allclose(c1, 1)

A2, b2, c2 = triples.identity_Abc(2)
assert math.allclose(A2, [[0, 0, 1, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 1, 0, 0]])
assert math.allclose(b2, [0, 0, 0, 0])
assert math.allclose(c2, 1)

def test_attenuator_Abc(self):
A1, b1, c1 = triples.attenuator_Abc(0.1)
e = 0.31622777
Expand Down