Skip to content

Commit

Permalink
ensure circuit ansatze are dagger functors
Browse files Browse the repository at this point in the history
  • Loading branch information
kinianlo committed Sep 23, 2024
1 parent 8b80d2c commit a1c7386
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lambeq/ansatz/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from sympy import Symbol, symbols

from lambeq.ansatz import BaseAnsatz
from lambeq.backend.grammar import Box, Diagram, Functor, Ty
from lambeq.backend.grammar import Box, Diagram, Functor, Ty, Daggered
from lambeq.backend.quantum import (
Bra,
CRz,
Expand Down Expand Up @@ -125,6 +125,9 @@ def _ob(self, _: Functor, ty: Ty) -> Ty:
return self.ob_map[ty]

def _ar(self, _: Functor, box: Box) -> Circuit:
if isinstance(box, Daggered):
return self._ar(_, box.dagger()).dagger()

label = self._summarise_box(box)
dom, cod = self.ob_size(box.dom), self.ob_size(box.cod)

Expand Down
17 changes: 17 additions & 0 deletions tests/test_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,20 @@ def test_lambeq_tket_conversion():
def test_special_characters(box, expected_sym_count):
ansatz = Sim15Ansatz({n_ty: 2, comma_ty: 2, space_ty: 2}, n_layers=1)
assert(len(ansatz(box).free_symbols) == expected_sym_count)


def test_ansatz_is_dagger_functor():
ansatz = IQPAnsatz({N: 1, S: 1}, n_layers=1)
diagram = Word('John', N)
circuit1 = ansatz(diagram).dagger()
circuit2 = ansatz(diagram.dagger())
assert circuit1 == circuit2

def test_ansatz_is_dagger_functor_sentence():
ansatz = IQPAnsatz({N: 1, S: 1}, n_layers=1)
diagram = (Word('Alice', N) @ Word('runs', N >> S) >>
Cup(N, N.r) @ S)

circuit1 = ansatz(diagram).dagger().normal_form()
circuit2 = ansatz(diagram.dagger()).normal_form()
assert circuit1 == circuit2

0 comments on commit a1c7386

Please sign in to comment.