Skip to content

Commit

Permalink
improving tests for codecov
Browse files Browse the repository at this point in the history
  • Loading branch information
arsalan-motamedi committed Oct 15, 2024
1 parent 3c8261b commit a02ffe8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
5 changes: 4 additions & 1 deletion mrmustard/lab_dev/circuit_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,11 +693,13 @@ def _apply_btops_for_change_of_rep(self, i) -> CircuitComponent:
r"""
Helper function for change of representation in to_bargmann()
"""

from .circuit_components_utils import BtoPS

name = self._index_representation[i][0]
ret = copy.deepcopy(self)
if name == "PS":

_, arg = self._index_representation[i]
ret._index_representation[i] = ("B", None)
m = self.wires.index_to_mode_dict[i]
Expand All @@ -709,14 +711,15 @@ def _apply_btops_for_change_of_rep(self, i) -> CircuitComponent:
friend_index = self.wires.index_dicts[2][m]
ret._index_representation[friend_index] = ("B", None)
ret = ret @ BtoPS([m], s=arg).adjoint.inverse()

if i in self.wires.input.bra.indices:
if m not in self.wires.input.ket.modes:
raise ValueError(
f"The object does not have a consistent representation. Mode {m} with PS representation has appeared only on the input bra."
)
friend_index = self.wires.index_dicts[3][m]
ret._index_representation[friend_index] = ("B", None)
ret = BtoPS([m], s=arg).dual.adjoint.inverse() @ ret
ret = BtoPS([m], s=arg).dual.inverse() @ ret

return ret

Expand Down
9 changes: 9 additions & 0 deletions tests/test_lab_dev/test_circuit_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,15 @@ def tets_matmul_respects_representations(self):
2: ("Q", 0),
}

rho = DM.random([0, 1])
sigma = DM.random([0])
assert rho >> BtoPS([0], 0) >> sigma.dual == rho >> sigma.dual

rho = DM.random([0, 1])
sigma = DM.random([0])

sigma >> (BtoPS([0], 0).dual >> rho.dual) == sigma >> rho.dual

def test_matmul_scalar(self):
d0 = Dgate([0], x=0.1, y=0.1)
result = d0 @ 0.8
Expand Down
32 changes: 32 additions & 0 deletions tests/test_lab_dev/test_circuit_components_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,32 @@ def testBtoPS_contraction_with_state(self):
psi = Ket.random([0])
assert math.allclose((psi >> BtoPS([0], 1)).representation([0, 0]), [1.0])

def test_Bto_S_index_representation(self):
r"""
Tests the assingments of the index_representration of a BtoPS and its variants
"""
btops_1 = BtoPS([0], 0.1)
assert btops_1._index_representation == {
0: ("PS", 0.1),
1: ("B", None),
2: ("PS", 0.1),
3: ("B", None),
}

btops_dual = BtoPS([0], 0.1).dual
assert btops_dual._index_representation == {
0: ("B", None),
1: ("PS", 0.1),
2: ("B", None),
3: ("PS", 0.1),
}

btops_adjoint = btops_1.adjoint
assert btops_adjoint._index_representation == btops_1._index_representation

btops_inv = btops_1.inverse()
assert btops_inv._index_representation == btops_dual._index_representation


class TestBtoQ:
r"""
Expand Down Expand Up @@ -293,3 +319,9 @@ def test_BtoQ_index_representatioin(self):

btoq_adj_dual = btoq_1.adjoint.dual
assert btoq_adj_dual._index_representation == {0: ("B", None), 1: ("Q", 0)}

btoq_inv = btoq_1.inverse()
assert btoq_inv._index_representation == btoq_dual._index_representation

btoq_adj_inv = btoq_1.adjoint.inverse()
assert btoq_adj_inv._index_representation == {0: ("B", None), 1: ("Q", 0)}

0 comments on commit a02ffe8

Please sign in to comment.