XanaduAI/MrMustard

Using a tuple for modes= argument gives ValueError

Closed this issue · 1 comments

Before posting a bug report

  • I have searched exisisting GitHub issues to make sure the issue does not already exist.

Expected behavior

I generally use tuples for immutable structures, and so expected that modes could be given as such. But it seems to be only working for lists.

Actual behavior

Shouldn't give a value error and should work just as if the modes= argument were a list.

Reproduces how often

Always.

System information

Mr Mustard: a differentiable bridge between phase space and Fock space.

Copyright 2021 Xanadu Quantum Technologies Inc.

Python version:            3.9.5

Platform info:             Linux-6.2.2-arch1-1-x86_64-with-glibc2.31

Installation path:         /usr/local/lib/python3.9/dist-packages/mrmustard

Mr Mustard version:        0.4.0

Numpy version:             1.23.5

Numba version:             0.56.4

Scipy version:             1.8.0

The Walrus version:        0.19.0

TensorFlow version:        2.10.1

Source code

from mrmustard.lab import BSgate
gate_tuple = BSgate(modes=(1,2)) >> BSgate(modes=(0,1))
gate_list = BSgate(modes=[1,2]) >> BSgate(modes=[0,1])
unitary = gate_list.U(cutoffs=(3,3,3)) # works
unitary = gate_tuple.U(cutoffs=(3,3,3))

Tracebacks

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[19], line 1
----> 1 unitary = gate_tuple.U(cutoffs=(3,3,3))

File /usr/local/lib/python3.9/dist-packages/mrmustard/lab/abstract/transformation.py:218, in Transformation.U(self, cutoffs)
    216 if not self.is_unitary:
    217     return None
--> 218 X, _, d = self.XYd
    219 return fock.wigner_to_fock_U(
    220     X if X is not None else math.eye(2 * self.num_modes),
    221     d if d is not None else math.zeros((2 * self.num_modes,)),
    222     shape=cutoffs * 2 if len(cutoffs) == self.num_modes else cutoffs,
    223 )

File /usr/local/lib/python3.9/dist-packages/mrmustard/lab/circuit.py:76, in Circuit.XYd(self)
     74 for op in self._ops:
     75     opx, opy, opd = op.XYd
---> 76     opX = XPMatrix.from_xxpp(opx, modes=(op.modes, op.modes), like_1=True)
     77     opY = XPMatrix.from_xxpp(opy, modes=(op.modes, op.modes), like_0=True)
     78     opd = XPVector.from_xxpp(opd, modes=op.modes)

File /usr/local/lib/python3.9/dist-packages/mrmustard/utils/xptensor.py:561, in XPMatrix.from_xxpp(cls, tensor, like_0, like_1, modes)
    559     tensor = math.reshape(tensor, [_ for n in tensor.shape for _ in (2, n // 2)])
    560     tensor = math.transpose(tensor, (1, 3, 0, 2))
--> 561 return XPMatrix(tensor, like_0, like_1, modes)

File /usr/local/lib/python3.9/dist-packages/mrmustard/utils/xptensor.py:538, in XPMatrix.__init__(self, tensor, like_0, like_1, modes)
    534     raise ValueError(f"like_0 and like_1 can't both be {like_0}")
    535 if not (
    536     isinstance(modes, tuple) and len(modes) == 2 and all(isinstance(m, list) for m in modes)
    537 ):
--> 538     raise ValueError("modes should be a tuple containing two lists (outmodes and inmodes)")
    539 if len(modes[0]) == 0 and len(modes[1]) == 0 and tensor is not None:
    540     if (
    541         tensor.shape[0] != tensor.shape[1] and like_0
    542     ):  # NOTE: we can't catch square coherences if no modes are specified

ValueError: modes should be a tuple containing two lists (outmodes and inmodes)

Additional information

This is actually on the develop branch at 1abad670dc76a83efd73f8189f3875bd7915c234.  But I see the same behaviour using v0.4.0.

closing, feel free to reopen if needed