Tensor

from lambeq import BobcatParser

parser = BobcatParser()
diagram = parser.sentence2diagram('Alice gives Claire the flowers')
diagram.draw()
../_images/9ae78c7cdc23e49e936ca099aad4f3dad3f12c8914de33cc9fec0a0949b58896.png
from lambeq import AtomicType, MPSAnsatz, SpiderAnsatz, TensorAnsatz
from lambeq.backend.tensor import Dim

N = AtomicType.NOUN
S = AtomicType.SENTENCE
TensorAnsatz({N: Dim(4), S: Dim(3)})(diagram).draw(figsize=(8, 2))
../_images/ca2ad4eba9558da931fd3861cc9a8e133e4f4027462454df3ff42f774df71855.png
MPSAnsatz({N: Dim(4), S: Dim(3)}, bond_dim=5)(diagram).draw(figsize=(10, 2))
../_images/ce0c3204f6a453558831977e360b0e7f386c9837654fd7efcf8bd5c6fbeb8330.png
SpiderAnsatz({N: Dim(4), S: Dim(3)})(diagram).draw(figsize=(10, 2))
../_images/d24a81cf71a89ee6a5cad0c20d4485dc2b8d5eb2a7dda839e06504290a3e0d4e.png
import torch as th
from sympy import default_sort_key

d = SpiderAnsatz({N: Dim(4), S: Dim(3)}, max_order=2)(diagram)

syms = sorted(d.free_symbols, key=default_sort_key)
sym_dict = {k: th.ones(k.size) for k in syms}
subbed_diagram = d.lambdify(*syms)(*sym_dict.values())
import tensornetwork as tn
subbed_diagram.eval(contractor=tn.contractors.auto)
array([256., 256., 256.], dtype=float32)