From d68c34c0a16698b3fa64a00e16e66b850974bec8 Mon Sep 17 00:00:00 2001 From: Arsalan Motamedi Date: Fri, 8 Nov 2024 15:23:11 -0500 Subject: [PATCH] tests for bargmanneigenstate added --- .../lab_dev/states/bargmanneigenstate.py | 4 +- .../transformations/realinterferometer.py | 2 +- mrmustard/physics/triples.py | 2 +- .../test_states/test_bargmanneigenstate.py | 44 +++++++++++++++++++ 4 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 tests/test_lab_dev/test_states/test_bargmanneigenstate.py diff --git a/mrmustard/lab_dev/states/bargmanneigenstate.py b/mrmustard/lab_dev/states/bargmanneigenstate.py index 2863f15b9..5e852c04b 100644 --- a/mrmustard/lab_dev/states/bargmanneigenstate.py +++ b/mrmustard/lab_dev/states/bargmanneigenstate.py @@ -46,9 +46,7 @@ def __init__( ): super().__init__(name="BargmannEigenstate") - alphas = list(reshape_params(len(modes), alphas=alpha)) - self._add_parameter(make_parameter(alpha_trainable, alphas, "alpha", alpha_bounds)) - print(self.alpha.value) + self._add_parameter(make_parameter(alpha_trainable, alpha, "alpha", alpha_bounds)) self._representation = self.from_ansatz( modes=modes, ansatz=PolyExpAnsatz.from_function( diff --git a/mrmustard/lab_dev/transformations/realinterferometer.py b/mrmustard/lab_dev/transformations/realinterferometer.py index fb80e3000..b3f98bbb7 100644 --- a/mrmustard/lab_dev/transformations/realinterferometer.py +++ b/mrmustard/lab_dev/transformations/realinterferometer.py @@ -56,7 +56,7 @@ def __init__( num_modes = len(modes) if orthogonal is not None and orthogonal.shape[-1] != num_modes: raise ValueError( - f"The size of the unitary must match the number of modes: {orthogonal.shape[-1]} =/= {num_modes}" + f"The size of the orthogonal matrix must match the number of modes: {orthogonal.shape[-1]} =/= {num_modes}" ) if orthogonal is None: diff --git a/mrmustard/physics/triples.py b/mrmustard/physics/triples.py index c46e1d13e..a790183a5 100644 --- a/mrmustard/physics/triples.py +++ b/mrmustard/physics/triples.py @@ -99,7 +99,7 @@ def bargmann_eigenstate_Abc(x: Union[float, Iterable[float]]) -> Union[Matrix, V The Abc triple of a Bargmann eigenstate. """ x = list(_reshape(x=x)) - nmodes = len(x) + nmodes = len(x[0]) A = _vacuum_A_matrix(nmodes) b = x c = 1 diff --git a/tests/test_lab_dev/test_states/test_bargmanneigenstate.py b/tests/test_lab_dev/test_states/test_bargmanneigenstate.py new file mode 100644 index 000000000..25095d99f --- /dev/null +++ b/tests/test_lab_dev/test_states/test_bargmanneigenstate.py @@ -0,0 +1,44 @@ +# Copyright 2024 Xanadu Quantum Technologies Inc. + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Tests for the BargmannEigenstate class.""" + +# pylint: disable=unspecified-encoding, missing-function-docstring, expression-not-assigned, pointless-statement + +import pytest + +from mrmustard import math +from mrmustard.lab_dev.states import BargmannEigenstate + + +class TestBargmannEigenstate: + r""" + Tests for the ``BargmannEigenstate`` class. + """ + + def test_init(self): + "Tests the initialization." + be = BargmannEigenstate([0, 1], [0.1j, 0.2]) + assert be.name == "BargmannEigenstate" + assert math.allclose(be.alpha.value, [0.1j, 0.2]) + assert be.modes == [0, 1] + assert math.allclose(be.ansatz.b[0], [0.1j, 0.2]) + assert math.allclose(be.ansatz.A[0], math.zeros((2, 2))) + assert be.ansatz.c[0] == 1.0 + + @pytest.mark.parametrize("alpha", [0.1, 0.5, 1]) + def test_numerial(self, alpha): + "A numerical test." + be = BargmannEigenstate([0], alpha) + assert be >> be.dual == math.exp(complex(alpha**2))