From 1b094763c49c2b4d5484b1b888c6293d4a3f6302 Mon Sep 17 00:00:00 2001 From: SamFerracin Date: Fri, 23 Feb 2024 14:21:11 -0500 Subject: [PATCH] conflicts --- ...est_bargmann_utils.py => test_bargmann.py} | 16 -- .../test_bargmann/test_bargmann_repr.py | 177 ------------------ 2 files changed, 193 deletions(-) rename tests/test_physics/{test_bargmann/test_bargmann_utils.py => test_bargmann.py} (89%) delete mode 100644 tests/test_physics/test_bargmann/test_bargmann_repr.py diff --git a/tests/test_physics/test_bargmann/test_bargmann_utils.py b/tests/test_physics/test_bargmann.py similarity index 89% rename from tests/test_physics/test_bargmann/test_bargmann_utils.py rename to tests/test_physics/test_bargmann.py index 8de117c89..05bb5be80 100644 --- a/tests/test_physics/test_bargmann/test_bargmann_utils.py +++ b/tests/test_physics/test_bargmann.py @@ -16,22 +16,6 @@ from mrmustard.physics import triples -def test_reorder_abc(): - """Test that the reorder_abc function works correctly""" - A = np.array([[1, 2], [2, 3]]) - b = np.array([4, 5]) - c = np.array(6) - same = reorder_abc((A, b, c), (0, 1)) - assert all(np.allclose(x, y) for x, y in zip(same, (A, b, c))) - flipped = reorder_abc((A, b, c), (1, 0)) - assert all(np.allclose(x, y) for x, y in zip(flipped, (A[[1, 0], :][:, [1, 0]], b[[1, 0]], c))) - c = np.array([[6, 7], [8, 9]]) - flipped = reorder_abc((A, b, c), (1, 0)) # test transposition of c - assert all( - np.allclose(x, y) for x, y in zip(flipped, (A[[1, 0], :][:, [1, 0]], b[[1, 0]], c.T)) - ) - - def test_reorder_abc(): """Test that the reorder_abc function works correctly""" A = np.array([[1, 2], [2, 3]]) diff --git a/tests/test_physics/test_bargmann/test_bargmann_repr.py b/tests/test_physics/test_bargmann/test_bargmann_repr.py deleted file mode 100644 index e1680a61f..000000000 --- a/tests/test_physics/test_bargmann/test_bargmann_repr.py +++ /dev/null @@ -1,177 +0,0 @@ -import numpy as np - -from mrmustard import math -from mrmustard.lab import Attenuator, Coherent, Gaussian, Ggate, Dgate -from mrmustard.physics.bargmann import contract_two_Abc, reorder_abc, wigner_to_bargmann_rho -from mrmustard.physics.representations import Bargmann -from tests.random import random_Ggate, single_mode_unitary_gate, n_mode_mixed_state, Abc_triple -from hypothesis import given - - -def test_make_cat(): - r"test adding two coherent states via the Bargmann representation" - cat = Bargmann(*Coherent(1.0).bargmann()) + Bargmann(*Coherent(-1.0).bargmann()) - assert np.allclose(cat.A[0], cat.A[1]) - assert np.allclose(cat.b[0], -cat.b[1]) - - -def test_muldiv_with_another_Bargmann(): - r"test multiplying and dividing two Bargmann representations" - Abc1 = Bargmann(*(Gaussian(1) >> Dgate(0.1, 0.2)).bargmann()) - Abc2 = Bargmann(*(Gaussian(1) >> Dgate(0.4, 0.1)).bargmann()) - s1 = Abc1 * Abc2 - s2 = Abc1 / Abc2 - assert np.allclose(s1.A[0], Abc1.A[0] + Abc2.A[0]) - assert np.allclose(s1.b[0], Abc1.b[0] + Abc2.b[0]) - assert np.allclose(s1.c[0], Abc1.c[0] * Abc2.c[0]) - assert np.allclose(s2.A[0], Abc1.A[0] - Abc2.A[0]) - assert np.allclose(s2.b[0], Abc1.b[0] - Abc2.b[0]) - assert np.allclose(s2.c[0], Abc1.c[0] / Abc2.c[0]) - - -def test_muldiv_with_scalar(): - r"test multiplying and dividing a Bargmann representation with a scalar" - s1 = Bargmann(*Coherent(1.0).bargmann()) * 2.0 - s2 = Bargmann(*Coherent(1.0).bargmann()) / 3.0 - s3 = 4.0 * Bargmann(*Coherent(1.0).bargmann()) - assert np.allclose(s1.c, Coherent(1.0).bargmann()[2] * 2.0) - assert np.allclose(s2.c, Coherent(1.0).bargmann()[2] / 3.0) - assert np.allclose(s3.c, Coherent(1.0).bargmann()[2] * 4.0) - - -@given(Abc=Abc_triple(3)) -def test_reorder_indices(Abc): - r"""Test that we can reorder the indices of the A matrix and b vector of an (A,b,c) triple""" - barg = Bargmann(*Abc) - barg = barg.reorder((0, 2, 1)) - assert np.allclose(barg.A[0], Abc[0][[0, 2, 1], :][:, [0, 2, 1]]) - assert np.allclose(barg.b[0], Abc[1][[0, 2, 1]]) - - -@given(Abc=Abc_triple()) -def test_call(Abc): - r"""Test that we can call the PolyExpAnsatz object""" - A, b, c = Abc - barg = Bargmann(A, b, c) - assert np.allclose(barg(z=math.zeros_like(b)), c) - - -def test_subtract(): - r"test subtracting two coherent states via the Bargmann representation" - cat = Bargmann(*Coherent(1.0).bargmann()) - Bargmann(*Coherent(-1.0).bargmann()) - assert np.allclose(cat.A[0], cat.A[1]) - assert np.allclose(cat.b[0], -cat.b[1]) - - -def test_abc_contraction_2mode_psi_U(): - "tests that the abc contraction works for U|psi>" - psi = Gaussian(2) - U = Ggate(2) - A1, b1, c1 = psi.bargmann() # out1ket, out2ket - A2, b2, c2 = U.bargmann() # out1ket, out2ket, in1ket, in2ket - A_abc, b_abc, c_abc = contract_two_Abc((A1, b1, c1), (A2, b2, c2), (0, 1), (2, 3)) - A_mm, b_mm, c_mm = (psi >> U).bargmann() - assert np.allclose(A_abc, A_mm) - assert np.allclose(b_abc, b_mm) - assert np.allclose(abs(c_abc), abs(c_mm)) - - -def test_abc_contraction_2mode_rho_phi(): - "tests that the abc contraction works for rho >> phi" - rho = Gaussian(2) >> Attenuator([0.1, 0.2]) >> Ggate(2) >> Attenuator([0.4, 0.9]) - phi = Ggate(2) >> Attenuator([0.3, 0.4]) >> Ggate(2) - # out1bra, out2bra, out1ket, out2ket - A1, b1, c1 = rho.bargmann() - # out1bra, out2bra, in1bra, in2bra, out1ket, out2ket, in1ket, in2ket - A2, b2, c2 = phi.bargmann() - - A_abc, b_abc, c_abc = contract_two_Abc((A1, b1, c1), (A2, b2, c2), (0, 1, 2, 3), (2, 3, 6, 7)) - - A_mm, b_mm, c_mm = (rho >> phi).bargmann() - - assert np.allclose(A_abc, A_mm) - assert np.allclose(b_abc, b_mm) - assert np.allclose(c_abc, c_mm) - - -def test_abc_contraction_3mode_rho_2mode_U(): - "tests that the abc contraction works for U rho U_dagger" - rho = Gaussian(3) >> Attenuator([0.1, 0.2, 0.4]) >> Ggate(3) >> Attenuator([0.4, 0.5, 0.9]) - U = Ggate(2) - # out1bra, out2bra, out3bra, out1ket, out2ket, out3ket - A1, b1, c1 = rho.bargmann() - # out1ket, out2ket, in1ket, in2ket - A2, b2, c2 = U.bargmann() - A_abc, b_abc, c_abc = contract_two_Abc( - (A2, b2, c2), (A1, b1, c1), (2, 3), (4, 5) - ) # left in out1ket_U, out2ket_U, out1bra_rho, out2bra_rho, out3bra_rho, out1ket_rho - A_abc, b_abc, c_abc = contract_two_Abc( - (A_abc, b_abc, c_abc), - (math.conj(A2), math.conj(b2), math.conj(c2)), - (3, 4), - (2, 3), - ) # left in out1ket_U, out2ket_U, out1bra_rho, out1ket_rho, out1bra_U, out2bra_U - A_abc, b_abc, c_abc = reorder_abc((A_abc, b_abc, c_abc), (2, 4, 5, 3, 0, 1)) - A_mm, b_mm, c_mm = (rho >> U[1, 2]).bargmann() - assert np.allclose(A_abc, A_mm) - assert np.allclose(b_abc, b_mm) - assert np.allclose(c_abc, c_mm) - - -def test_Bargmann_2mode_psi_U(): - "tests that the Bargmann representation works for U|psi>" - psi = Gaussian(2) - U = Ggate(2) - A1, b1, c1 = psi.bargmann() # out1ket, out2ket - A2, b2, c2 = U.bargmann() # out1ket, out2ket, in1ket, in2ket - Abc1 = Bargmann(A1, b1, c1) - Abc2 = Bargmann(A2, b2, c2) - psiU = Abc1[0, 1] @ Abc2[2, 3] - A_abc, b_abc, c_abc = psiU.A[0], psiU.b[0], psiU.c[0] - A_mm, b_mm, c_mm = (psi >> U).bargmann() - assert np.allclose(A_abc, A_mm) - assert np.allclose(b_abc, b_mm) - assert np.allclose(abs(c_abc), abs(c_mm)) - - -@given(G1=random_Ggate(num_modes=1), G2=random_Ggate(num_modes=1)) -def test_composition_GG(G1, G2): - r"""Test that the composition of two G gates is the same - as the composition of their Bargmann representations""" - a12, b12, c12 = (G1 >> G2).bargmann() - composed = Bargmann(*G2.bargmann())[1] @ Bargmann(*G1.bargmann())[0] - assert np.allclose(composed.A[0], a12) - assert np.allclose(composed.b[0], b12) - assert np.allclose(np.abs(composed.c[0]), np.abs(c12)) - - -@given(G1=single_mode_unitary_gate(), G2=single_mode_unitary_gate()) -def test_composition_all(G1, G2): - r"""Test that the composition of any two gates is the same - as the composition of their Bargmann representations""" - a12, b12, c12 = (G1 >> G2).bargmann() - composed = Bargmann(*G2.bargmann())[1] @ Bargmann(*G1.bargmann())[0] - assert np.allclose(composed.A[0], a12) - assert np.allclose(composed.b[0], b12) - assert np.allclose(np.abs(composed.c[0]), np.abs(c12)) - - -@given(rho=n_mode_mixed_state(num_modes=2)) -def test_partial_trace_2mode_state(rho): - r"""Test that the partial trace of a 2-mode state works""" - rho01 = Bargmann(*wigner_to_bargmann_rho(rho.cov, rho.means)) - rho1 = rho01.trace([0], [2]) - rho0 = rho01.trace([1], [3]) - assert rho1 == Bargmann(*rho.get_modes(1).bargmann()) - assert rho0 == Bargmann(*rho.get_modes(0).bargmann()) - - -@given(rho=n_mode_mixed_state(num_modes=3)) -def test_partial_trace_3mode_state(rho): - r"""Test that the partial trace of a 3-mode state works""" - rho = rho >> Attenuator([0.9, 0.9, 0.9]) - rho012 = Bargmann(*wigner_to_bargmann_rho(rho.cov, rho.means)) - rho12 = rho012.trace([0], [3]) - rho2 = rho012.trace([0, 1], [3, 4]) - assert np.allclose(rho12.b, Bargmann(*rho.get_modes([1, 2]).bargmann()).b) - assert rho2 == Bargmann(*rho.get_modes(2).bargmann())