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

New multirep PR initiated #496

Open
wants to merge 84 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
84 commits
Select commit Hold shift + click to select a range
60a477f
New multirep PR initiated
arsalan-motamedi Oct 2, 2024
e5ae472
working on the errors
arsalan-motamedi Oct 4, 2024
6c56fa8
updates from Anthony's ntoqInv branch
arsalan-motamedi Oct 4, 2024
63bf520
updating btoq and btops
arsalan-motamedi Oct 4, 2024
9222ccd
all CC tests pass :)
arsalan-motamedi Oct 7, 2024
38fb967
making codefactor happy
arsalan-motamedi Oct 7, 2024
9d4195e
conflict resolved
arsalan-motamedi Oct 7, 2024
bc6782a
not sure what's happening
arsalan-motamedi Oct 7, 2024
78e80ed
Docstrings added
arsalan-motamedi Oct 7, 2024
f79b6ca
minimal changes
arsalan-motamedi Oct 7, 2024
c2b932c
Merge branch 'develop' of https://github.com/XanaduAI/MrMustard into …
apchytr Oct 7, 2024
f8fe74e
fixing errors: BtoPS.s.value
arsalan-motamedi Oct 7, 2024
0f22fdf
fixing BtoPS errors
arsalan-motamedi Oct 7, 2024
aa7a438
BtoQ phi issues fixed
arsalan-motamedi Oct 7, 2024
60b912e
BtoPS s issues fixed
arsalan-motamedi Oct 7, 2024
d4cdd95
changing stuff regarding BtoQ/BtoPS
arsalan-motamedi Oct 9, 2024
fbb636e
formatting
arsalan-motamedi Oct 9, 2024
96a525e
Some weird issues with BtoQ and BtoPS (Constant / float / etc) fixed
arsalan-motamedi Oct 9, 2024
8ab3750
formatting
arsalan-motamedi Oct 9, 2024
92df65f
slight changes
arsalan-motamedi Oct 9, 2024
b2deb23
tests fixed
arsalan-motamedi Oct 10, 2024
43ee95b
removed a print command :)
arsalan-motamedi Oct 10, 2024
615c5e3
Adding the ability to handle BtoQ.inverse and update the representation
arsalan-motamedi Oct 10, 2024
698e426
WIP
arsalan-motamedi Oct 10, 2024
9408efc
Ket matmul revised
arsalan-motamedi Oct 10, 2024
9190c4f
formatting
arsalan-motamedi Oct 10, 2024
f8b14f7
deepcopy change
arsalan-motamedi Oct 11, 2024
c5578b8
removing print statements
arsalan-motamedi Oct 11, 2024
364ac77
Now it's safe to do dual
arsalan-motamedi Oct 11, 2024
fe64727
formatting
arsalan-motamedi Oct 11, 2024
5a51d0e
test to take care of dual matmul
arsalan-motamedi Oct 11, 2024
9458c67
merge conflict resolution
arsalan-motamedi Oct 11, 2024
5af0008
formatting
arsalan-motamedi Oct 11, 2024
5cd6170
the "on" method of circuitcomponent revised (with multirep)
arsalan-motamedi Oct 11, 2024
d0b8091
added adjoint
arsalan-motamedi Oct 11, 2024
4174725
some tests regarding adoint, dual, and "on"
arsalan-motamedi Oct 11, 2024
01791a2
formatting
arsalan-motamedi Oct 11, 2024
218f3c8
test improved
arsalan-motamedi Oct 11, 2024
8e4775c
tests + code improvements (added copying when doing to_bargmann in ma…
arsalan-motamedi Oct 11, 2024
81104f2
weird tests added for logic of matmul
arsalan-motamedi Oct 11, 2024
18b22e3
minor improvement
arsalan-motamedi Oct 11, 2024
265b854
monir
arsalan-motamedi Oct 11, 2024
9095417
added wire tests
arsalan-motamedi Oct 11, 2024
8ad4249
formatting
arsalan-motamedi Oct 11, 2024
e07761e
codefactor changes
arsalan-motamedi Oct 11, 2024
b6d2a71
making codefactor a but happier
arsalan-motamedi Oct 11, 2024
43d20cd
addition's test
arsalan-motamedi Oct 11, 2024
6557de6
Adding representation to BtoQ/BtoPS and removing if statements on the…
arsalan-motamedi Oct 11, 2024
ce28399
Merge branch 'develop' of https://github.com/XanaduAI/MrMustard into …
arsalan-motamedi Oct 15, 2024
1bd62aa
Improving btoq
arsalan-motamedi Oct 15, 2024
e895788
added btoq representation tests
arsalan-motamedi Oct 15, 2024
0942b9c
formatting
arsalan-motamedi Oct 15, 2024
d25a08a
making codefactor happy
arsalan-motamedi Oct 15, 2024
7145fd7
telling codefactor to let go of "too-many-instance-attributes" error
arsalan-motamedi Oct 15, 2024
c42e20f
just restarging
arsalan-motamedi Oct 15, 2024
56e88e5
idk what happened-- just brought it back to a few commits ago
arsalan-motamedi Oct 15, 2024
4afbd1e
making codefactor happy
arsalan-motamedi Oct 15, 2024
7497575
nothing important
arsalan-motamedi Oct 15, 2024
4f88920
formatting
arsalan-motamedi Oct 15, 2024
f2d4f65
Paring wire updates on BtoPS rshifts + DM rshift updated + neater for…
arsalan-motamedi Oct 15, 2024
f843f04
fixing a typo in DM rshift test
arsalan-motamedi Oct 15, 2024
3132357
fixing serialize issue
arsalan-motamedi Oct 15, 2024
3ef97b0
making codefactor happy
arsalan-motamedi Oct 15, 2024
17629d3
formatting
arsalan-motamedi Oct 15, 2024
af395f4
added to_quadrature tests for checking the change of representation
arsalan-motamedi Oct 15, 2024
42f13e8
formatting
arsalan-motamedi Oct 15, 2024
7a76877
some important changes (some error fixed bc I forgot to put .modes an…
arsalan-motamedi Oct 15, 2024
a62200b
a fatal error in btops inverse fixed
arsalan-motamedi Oct 15, 2024
3c8261b
btoq inverse improved
arsalan-motamedi Oct 15, 2024
a02ffe8
improving tests for codecov
arsalan-motamedi Oct 15, 2024
62bc1b6
added forgotten assert
arsalan-motamedi Oct 15, 2024
5b7ad47
now scalar product remembers the representation
arsalan-motamedi Oct 15, 2024
fdc50d0
reformatting
arsalan-motamedi Oct 15, 2024
a8c7c17
Addressing some of Anthony's comments
arsalan-motamedi Oct 16, 2024
4ba09e7
removed an extra file I had generated
arsalan-motamedi Oct 16, 2024
512072a
Merge branch 'develop' into multirep-v2
arsalan-motamedi Oct 16, 2024
fdfbe02
Merge branch 'develop' of https://github.com/XanaduAI/MrMustard into …
arsalan-motamedi Oct 16, 2024
932c9e7
_index_representation
apchytr Oct 17, 2024
af1552e
Merge branch 'develop' of https://github.com/XanaduAI/MrMustard into …
arsalan-motamedi Oct 17, 2024
7316083
removed redundancies
arsalan-motamedi Oct 17, 2024
8a5c93e
Corrected the issue of having a multiplication of BtoQ stuff
arsalan-motamedi Oct 22, 2024
7940a29
Final improvements
arsalan-motamedi Oct 22, 2024
983c275
btoq update
arsalan-motamedi Oct 22, 2024
23acbda
codefactor improvement
arsalan-motamedi Oct 22, 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
84 changes: 76 additions & 8 deletions mrmustard/lab_dev/circuit_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def __init__(
self._name = name
self._parameter_set = ParameterSet()
self._representation = representation

if isinstance(wires, Wires):
self._wires = wires
else:
Expand Down Expand Up @@ -110,6 +110,13 @@ def __init__(
if self._representation:
self._representation = self._representation.reorder(tuple(perm))


@property
def _index_representation(self):
apchytr marked this conversation as resolved.
Show resolved Hide resolved
if not hasattr(self, '_index_representation_'):
self._index_representation_ = {i: ('B', None) for i in self.wires.indices}
return self._index_representation_

def _serialize(self) -> tuple[dict[str, Any], dict[str, ArrayLike]]:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you need to incorporate _index_representation into the serialization of the object?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hmm.. not sure about that. But I guess it makes some sense to keep _index_representationas a hidden attribute since there are many calls to circuit component initializer and I might be missing correcting some when doing the change.

"""
Inner serialization to be used by Circuit.serialize().
Expand Down Expand Up @@ -585,7 +592,7 @@ def to_fock(self, shape: int | Sequence[int] | None = None) -> CircuitComponent:
del ret.manual_shape
return ret

def to_bargmann(self) -> CircuitComponent:
def to_bargmann(self, indices: Sequence[int] | None = None) -> CircuitComponent:
r"""
Returns a new circuit component with the same attributes as this and a ``Bargmann`` representation.
.. code-block::
Expand All @@ -602,9 +609,39 @@ def to_bargmann(self) -> CircuitComponent:
>>> assert d_bargmann.wires == d.wires
>>> assert isinstance(d_bargmann.representation, Bargmann)
"""
if isinstance(self.representation, Bargmann):
from .circuit_components_utils import BtoQ, BtoPS
if isinstance(self.representation, Bargmann):# TODO: better name for Bargmann class
# check cc rep
if not indices:
indices = self.wires.indices
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
indices = self.wires.indices
indices = indices if indices else self.wires.indices


for i in indices:
name, arg = self._index_representation[i]
if name == 'Q':
self._index_representation[i] = ('B', None)
if i in self.wires.output.bra.indices:
self = self @ BtoQ([self.wires.index_to_mode_dict[i]], phi = arg).adjoint.inverse()
if i in self.wires.output.ket.indices:
self = self @ BtoQ([self.wires.index_to_mode_dict[i]], phi = arg).inverse()
if i in self.wires.input.bra.indices:
self = BtoQ([self.wires.index_to_mode_dict[i]], phi = arg).dual.adjoint.inverse() @ self
if i in self.wires.input.bra.indices:
self = BtoQ([self.wires.index_to_mode_dict[i]], phi = arg).dual.inverse() @ self


if name == 'PS':
arsalan-motamedi marked this conversation as resolved.
Show resolved Hide resolved
self._index_representation[i] = ('B', None)
if i in self.wires.output.bra.indices:
self = self @ BtoPS([self.wires.index_to_mode_dict[i]], s = arg).adjoint.inverse()
if i in self.wires.output.ket.indices:
self = self @ BtoPS([self.wires.index_to_mode_dict[i]], s = arg).inverse()
if i in self.wires.input.bra.indices:
self = BtoPS([self.wires.index_to_mode_dict[i]], s = arg).dual.adjoint.inverse() @ self
if i in self.wires.input.bra.indices:
self = BtoPS([self.wires.index_to_mode_dict[i]], s = arg).dual.inverse() @ self

return self
else:
apchytr marked this conversation as resolved.
Show resolved Hide resolved
elif isinstance(self.representation, Fock):
if self.representation.ansatz._original_abc_data:
A, b, c = self.representation.ansatz._original_abc_data
else:
Expand Down Expand Up @@ -717,21 +754,52 @@ def __matmul__(self, other: CircuitComponent | Scalar) -> CircuitComponent:
>>> att = Attenuator([0], 0.5)
>>> assert (coh @ att).wires.input.bra # the input bra is still uncontracted
"""


if isinstance(other, (numbers.Number, np.ndarray)):
return self * other

wires_result, perm = self.wires @ other.wires
idx_z, idx_zconj = self._matmul_indices(other)
if type(self.representation) == type(other.representation):

if type(self.representation) == type(other.representation) == Fock:
arsalan-motamedi marked this conversation as resolved.
Show resolved Hide resolved
self_rep = self.representation
other_rep = other.representation
else:
self_rep = self.to_bargmann().representation
other_rep = other.to_bargmann().representation
index_self, index_other = self.wires.contracted_indices(other.wires)
self_rep = self.to_bargmann(index_self).representation
other_rep = other.to_bargmann(index_other).representation

rep = self_rep[idx_z] @ other_rep[idx_zconj]
rep = rep.reorder(perm) if perm else rep
return CircuitComponent._from_attributes(rep, wires_result, None)
result = CircuitComponent._from_attributes(rep, wires_result, None)
result._helper_update_wire_rep(other)

return result

def _helper_update_wire_rep(self,other):

from .circuit_components_utils import BtoQ, BtoPS
if isinstance(other, BtoQ):
btoq_param = math.atleast_1d(other.phi.value)
if other.wires.bra:
for k, m in enumerate(other.modes):
i = self.wires.index_dicts[0][m]
self._index_representation[i] = ('Q', float(btoq_param[k]))
arsalan-motamedi marked this conversation as resolved.
Show resolved Hide resolved
elif other.wires.ket:
for k, m in enumerate(other.modes):
i = self.wires.index_dicts[2][m]
self._index_representation[i] = ('Q', float(btoq_param[k]))

if isinstance(other, BtoPS):
if other.wires.bra:
for k, m in enumerate(other.modes):
i = self.wires.index_dicts[0][m]
self._index_representation[i] = ('PS', float(other.s.value[k]))
elif other.wires.ket:
for k, m in enumerate(other.modes):
i = self.wires.index_dicts[2][m]
self._index_representation[i] = ('PS', float(other.s.value[k]))

def __mul__(self, other: Scalar) -> CircuitComponent:
r"""
Expand Down
5 changes: 5 additions & 0 deletions mrmustard/lab_dev/wires.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,11 @@ def indices(self) -> tuple[int, ...]:
self.index_dicts[t][m] for t, modes in enumerate(self.sorted_args) for m in modes
)


@cached_property
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why are we caching this? This seems unnecessary as it's just a reordering of an already cached property i.e. duplicate information being cached

def index_to_mode_dict(self) -> dict[int, int]:
return {i: m for d in self.index_dicts for m,i in d.items()}

@cached_property
def input(self) -> Wires:
r"""
Expand Down
Loading