From a1c7386ecec90a531c2009557406c80036aaa0f5 Mon Sep 17 00:00:00 2001 From: Kin Ian Lo Date: Tue, 24 Sep 2024 00:13:23 +0100 Subject: [PATCH 1/7] ensure circuit ansatze are dagger functors --- lambeq/ansatz/circuit.py | 5 ++++- tests/test_circuit.py | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lambeq/ansatz/circuit.py b/lambeq/ansatz/circuit.py index bd1f9eb5..a0ed9d70 100644 --- a/lambeq/ansatz/circuit.py +++ b/lambeq/ansatz/circuit.py @@ -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, @@ -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) diff --git a/tests/test_circuit.py b/tests/test_circuit.py index 29aa80d2..2f2bd54e 100644 --- a/tests/test_circuit.py +++ b/tests/test_circuit.py @@ -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 From cc4bbb9a7bee4e14a222a5bca6b2b3b3ef3b4f77 Mon Sep 17 00:00:00 2001 From: Kin Ian Lo Date: Tue, 24 Sep 2024 02:35:33 +0100 Subject: [PATCH 2/7] fix flake8 --- lambeq/ansatz/circuit.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lambeq/ansatz/circuit.py b/lambeq/ansatz/circuit.py index a0ed9d70..f1ddfbc5 100644 --- a/lambeq/ansatz/circuit.py +++ b/lambeq/ansatz/circuit.py @@ -38,7 +38,7 @@ from sympy import Symbol, symbols from lambeq.ansatz import BaseAnsatz -from lambeq.backend.grammar import Box, Diagram, Functor, Ty, Daggered +from lambeq.backend.grammar import Box, Daggered, Diagram, Functor, Ty from lambeq.backend.quantum import ( Bra, CRz, @@ -127,7 +127,7 @@ def _ob(self, _: Functor, ty: Ty) -> 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) From a188a6e123ca98bfdc46ef48070588c4592c0bbc Mon Sep 17 00:00:00 2001 From: Kin Ian Lo Date: Tue, 24 Sep 2024 19:20:06 +0100 Subject: [PATCH 3/7] use apply_functor to handle dagger --- lambeq/ansatz/circuit.py | 3 --- lambeq/backend/grammar.py | 3 +++ 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lambeq/ansatz/circuit.py b/lambeq/ansatz/circuit.py index f1ddfbc5..0e86c8de 100644 --- a/lambeq/ansatz/circuit.py +++ b/lambeq/ansatz/circuit.py @@ -125,9 +125,6 @@ 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) diff --git a/lambeq/backend/grammar.py b/lambeq/backend/grammar.py index 9b5534e3..d59d9888 100644 --- a/lambeq/backend/grammar.py +++ b/lambeq/backend/grammar.py @@ -1571,6 +1571,9 @@ def rotate(self, z: int) -> Self: def dagger(self) -> Box: return self.box + + def apply_functor(self, functor: Functor) -> Diagrammable: + return functor(self.dagger()).dagger() @classmethod def from_json(cls, data: _JSONDictT | str) -> Self: From e65043c7d38d3508846427472ccad8959c6ef958 Mon Sep 17 00:00:00 2001 From: Kin Ian Lo Date: Tue, 24 Sep 2024 23:55:14 +0100 Subject: [PATCH 4/7] add dagger functor test case --- tests/backend/test_grammar.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/backend/test_grammar.py b/tests/backend/test_grammar.py index 1ba68b3f..661d3884 100644 --- a/tests/backend/test_grammar.py +++ b/tests/backend/test_grammar.py @@ -277,6 +277,7 @@ def ar(func, box): assert func(g.r) == func(g).r == g_z.r assert func(f >> g) == f_z >> g_z assert func(f @ g) == f_z @ g_z + assert func(f.dagger()) == func(f).dagger() def bad_ar(func, box): return Box("BOX", a, c) if box.cod == b else box From 762bae5be8ade0363ac6a40147287a486e2e62e7 Mon Sep 17 00:00:00 2001 From: Kin Ian Lo Date: Tue, 24 Sep 2024 23:55:23 +0100 Subject: [PATCH 5/7] remove unused import --- lambeq/ansatz/circuit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lambeq/ansatz/circuit.py b/lambeq/ansatz/circuit.py index 0e86c8de..bd1f9eb5 100644 --- a/lambeq/ansatz/circuit.py +++ b/lambeq/ansatz/circuit.py @@ -38,7 +38,7 @@ from sympy import Symbol, symbols from lambeq.ansatz import BaseAnsatz -from lambeq.backend.grammar import Box, Daggered, Diagram, Functor, Ty +from lambeq.backend.grammar import Box, Diagram, Functor, Ty from lambeq.backend.quantum import ( Bra, CRz, From 5e7949d26ac56f0bbcb1116965210db745cda362 Mon Sep 17 00:00:00 2001 From: Kin Ian Lo Date: Tue, 24 Sep 2024 23:56:10 +0100 Subject: [PATCH 6/7] remove whitespace --- lambeq/backend/grammar.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lambeq/backend/grammar.py b/lambeq/backend/grammar.py index d59d9888..919f36a5 100644 --- a/lambeq/backend/grammar.py +++ b/lambeq/backend/grammar.py @@ -1571,7 +1571,7 @@ def rotate(self, z: int) -> Self: def dagger(self) -> Box: return self.box - + def apply_functor(self, functor: Functor) -> Diagrammable: return functor(self.dagger()).dagger() From d51e05dd988fdd1dc47bf46a939797e661ccd2b0 Mon Sep 17 00:00:00 2001 From: Kin Ian Lo Date: Thu, 26 Sep 2024 19:56:34 +0100 Subject: [PATCH 7/7] add dagger to Diagrammable --- lambeq/backend/grammar.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lambeq/backend/grammar.py b/lambeq/backend/grammar.py index 919f36a5..2aab6381 100644 --- a/lambeq/backend/grammar.py +++ b/lambeq/backend/grammar.py @@ -362,6 +362,9 @@ def rotate(self, z: int) -> Diagrammable: """ + def dagger(self) -> Diagrammable: + """Implements conjugation of diagrams.""" + def __matmul__(self, rhs: Diagrammable | Ty) -> Diagrammable: """Implements the tensor operator `@` with another diagram."""