From d5f9f3adb7fac6843c9b77b9bc78c39ca698a1cf Mon Sep 17 00:00:00 2001 From: Anthony Date: Tue, 8 Oct 2024 13:41:27 -0400 Subject: [PATCH] cleanup --- mrmustard/lab_dev/circuit_components.py | 4 ++-- mrmustard/lab_dev/states/base.py | 2 +- mrmustard/lab_dev/transformations/dgate.py | 4 ++-- mrmustard/physics/representations.py | 8 ++++---- tests/test_lab_dev/test_circuit_components.py | 2 +- tests/test_lab_dev/test_states/test_states_base.py | 6 +++--- tests/test_lab_dev/test_transformations/test_dgate.py | 2 +- tests/test_math/test_lattice.py | 8 ++++---- 8 files changed, 18 insertions(+), 18 deletions(-) diff --git a/mrmustard/lab_dev/circuit_components.py b/mrmustard/lab_dev/circuit_components.py index bb9255799..b44060ccf 100644 --- a/mrmustard/lab_dev/circuit_components.py +++ b/mrmustard/lab_dev/circuit_components.py @@ -463,7 +463,7 @@ def bargmann_triple( """ return self._representation.bargmann_triple(batched) - def fock(self, shape: int | Sequence[int] | None = None, batched=False) -> ComplexTensor: + def fock_array(self, shape: int | Sequence[int] | None = None, batched=False) -> ComplexTensor: r""" Returns an array representation of this component in the Fock basis with the given shape. If the shape is not given, it defaults to the ``auto_shape`` of the component if it is @@ -477,7 +477,7 @@ def fock(self, shape: int | Sequence[int] | None = None, batched=False) -> Compl Returns: array: The Fock representation of this component. """ - return self._representation.fock(shape or self.auto_shape(), batched) + return self._representation.fock_array(shape or self.auto_shape(), batched) def on(self, modes: Sequence[int]) -> CircuitComponent: r""" diff --git a/mrmustard/lab_dev/states/base.py b/mrmustard/lab_dev/states/base.py index 317e6a671..db96a9186 100644 --- a/mrmustard/lab_dev/states/base.py +++ b/mrmustard/lab_dev/states/base.py @@ -324,7 +324,7 @@ def fock_distribution(self, cutoff: int) -> ComplexTensor: Returns: The Fock distribution. """ - fock_array = self.fock(cutoff) + fock_array = self.fock_array(cutoff) if isinstance(self, Ket): probs = ( math.astensor( diff --git a/mrmustard/lab_dev/transformations/dgate.py b/mrmustard/lab_dev/transformations/dgate.py index 48835ef56..eeb0616df 100644 --- a/mrmustard/lab_dev/transformations/dgate.py +++ b/mrmustard/lab_dev/transformations/dgate.py @@ -100,7 +100,7 @@ def __init__( self.wires, ) - def fock(self, shape: int | Sequence[int] = None, batched=False) -> ComplexTensor: + def fock_array(self, shape: int | Sequence[int] = None, batched=False) -> ComplexTensor: r""" Returns the unitary representation of the Displacement gate using the Laguerre polynomials. If the shape is not given, it defaults to the ``auto_shape`` of the component if it is @@ -145,7 +145,7 @@ def fock(self, shape: int | Sequence[int] = None, batched=False) -> ComplexTenso return arrays def to_fock(self, shape: int | Sequence[int] | None = None) -> Dgate: - fock = ArrayAnsatz(self.fock(shape, batched=True), batched=True) + fock = ArrayAnsatz(self.fock_array(shape, batched=True), batched=True) fock._original_abc_data = self.ansatz.triple ret = self._getitem_builtin(self.modes) ret._representation = Representation(fock, self.wires) diff --git a/mrmustard/physics/representations.py b/mrmustard/physics/representations.py index 1a8d1e8ee..b7d569858 100644 --- a/mrmustard/physics/representations.py +++ b/mrmustard/physics/representations.py @@ -125,7 +125,7 @@ def bargmann_triple( except AttributeError as e: raise AttributeError("No Bargmann data for this component.") from e - def fock(self, shape: int | Sequence[int], batched=False) -> ComplexTensor: + def fock_array(self, shape: int | Sequence[int], batched=False) -> ComplexTensor: r""" Returns an array representation of this component in the Fock basis with the given shape. If the shape is not given, it defaults to the ``auto_shape`` of the component if it is @@ -172,7 +172,7 @@ def fock(self, shape: int | Sequence[int], batched=False) -> ComplexTensor: def to_bargmann(self) -> Representation: r""" - Returns a new circuit component with the same attributes as this and a ``Bargmann`` representation. + Converts this representation to a Bargmann representation. """ if isinstance(self.ansatz, PolyExpAnsatz): return self @@ -187,14 +187,14 @@ def to_bargmann(self) -> Representation: def to_fock(self, shape: int | Sequence[int]) -> Representation: r""" - Returns a new representation with an ``ArrayAnsatz``. + Converts this representation to a Fock representation. Args: shape: The shape of the returned representation. If ``shape``is given as an ``int``, it is broadcasted to all the dimensions. If ``None``, it defaults to the value of ``AUTOSHAPE_MAX`` in the settings. """ - fock = ArrayAnsatz(self.fock(shape, batched=True), batched=True) + fock = ArrayAnsatz(self.fock_array(shape, batched=True), batched=True) try: if self.ansatz.polynomial_shape[0] == 0: fock._original_abc_data = self.ansatz.triple diff --git a/tests/test_lab_dev/test_circuit_components.py b/tests/test_lab_dev/test_circuit_components.py index 7865820c9..5c9bb7776 100644 --- a/tests/test_lab_dev/test_circuit_components.py +++ b/tests/test_lab_dev/test_circuit_components.py @@ -451,7 +451,7 @@ def test_to_fock_keeps_bargmann(self): def test_fock_component_no_bargmann(self): "tests that a fock component doesn't have a bargmann representation by default" coh = Coherent([0], x=1.0) - CC = Ket.from_fock([0], coh.fock(20), batched=False) + CC = Ket.from_fock([0], coh.fock_array(20), batched=False) with pytest.raises(AttributeError): CC.bargmann_triple() # pylint: disable=pointless-statement diff --git a/tests/test_lab_dev/test_states/test_states_base.py b/tests/test_lab_dev/test_states/test_states_base.py index 58895c328..31b7f6a08 100644 --- a/tests/test_lab_dev/test_states/test_states_base.py +++ b/tests/test_lab_dev/test_states/test_states_base.py @@ -139,7 +139,7 @@ def test_normalize(self, modes, x, y, coeff): def test_to_from_fock(self, modes): state_in = Coherent(modes, x=1, y=2) state_in_fock = state_in.to_fock(5) - array_in = state_in.fock(5, batched=True) + array_in = state_in.fock_array(5, batched=True) assert math.allclose(array_in, state_in_fock.ansatz.array) @@ -522,7 +522,7 @@ def test_from_fock_error(self): state01 = Coherent([0, 1], 1).dm() state01 = state01.to_fock(2) with pytest.raises(ValueError): - DM.from_fock([0], state01.fock(5), "my_dm", True) + DM.from_fock([0], state01.fock_array(5), "my_dm", True) def test_bargmann_Abc_to_phasespace_cov_means(self): # The init state cov and means comes from the random state 'state = Gaussian(1) >> Dgate([0.2], [0.3])' @@ -596,7 +596,7 @@ def test_normalize(self, modes, x, y, coeff): def test_to_from_fock(self, modes): state_in = Coherent(modes, x=1, y=2) >> Attenuator([modes[0]], 0.8) state_in_fock = state_in.to_fock(5) - array_in = state_in.fock(5, batched=True) + array_in = state_in.fock_array(5, batched=True) assert math.allclose(array_in, state_in_fock.ansatz.array) diff --git a/tests/test_lab_dev/test_transformations/test_dgate.py b/tests/test_lab_dev/test_transformations/test_dgate.py index bbcaebd99..b2ff27a5e 100644 --- a/tests/test_lab_dev/test_transformations/test_dgate.py +++ b/tests/test_lab_dev/test_transformations/test_dgate.py @@ -52,7 +52,7 @@ def test_to_fock_method(self): # displacement gate in fock representation for large displacement dgate = Dgate([0], x=10.0).to_fock(150) assert (state.to_fock() >> dgate).probability < 1 - assert np.all(math.abs(dgate.fock(150)) < 1) + assert np.all(math.abs(dgate.fock_array(150)) < 1) def test_representation(self): rep1 = Dgate(modes=[0], x=0.1, y=0.1).ansatz diff --git a/tests/test_math/test_lattice.py b/tests/test_math/test_lattice.py index 0c33bdb37..4433793b3 100644 --- a/tests/test_math/test_lattice.py +++ b/tests/test_math/test_lattice.py @@ -122,14 +122,14 @@ def test_diagonalbatchNumba_vs_diagonalNumba(batch_size): def test_bs_schwinger(): "test that the schwinger method to apply a BS works correctly" - G = mmld.Ket.random([0, 1]).fock([20, 20]) + G = mmld.Ket.random([0, 1]).fock_array([20, 20]) G = math.asnumpy(G) BS = beamsplitter((20, 20, 20, 20), 1.0, 1.0) manual = np.einsum("ab, cdab", G, BS) G = apply_BS_schwinger(1.0, 1.0, 0, 1, G) assert np.allclose(manual, G) - Gg = mmld.Unitary.random([0, 1]).fock([20, 20, 20, 20]) + Gg = mmld.Unitary.random([0, 1]).fock_array([20, 20, 20, 20]) Gg = math.asnumpy(Gg) BS = beamsplitter((20, 20, 20, 20), 2.0, -1.0) manual = np.einsum("cdab, abef", BS, Gg) @@ -157,10 +157,10 @@ def test_vanilla_stable(): "tests the vanilla stable against other known stable methods" settings.STABLE_FOCK_CONVERSION = True assert np.allclose( - mmld.Dgate([0], x=4.0, y=4.0).fock([1000, 1000]), + mmld.Dgate([0], x=4.0, y=4.0).fock_array([1000, 1000]), displacement((1000, 1000), 4.0 + 4.0j), ) - sgate = mmld.Sgate([0], r=4.0, phi=2.0).fock([1000, 1000]) + sgate = mmld.Sgate([0], r=4.0, phi=2.0).fock_array([1000, 1000]) assert np.max(np.abs(sgate)) < 1 settings.STABLE_FOCK_CONVERSION = False