Circuit

from pytket.circuit.display import render_circuit_jupyter

from lambeq import AtomicType, BobcatParser, IQPAnsatz

N = AtomicType.NOUN
S = AtomicType.SENTENCE
parser = BobcatParser()
diagram = parser.sentence2diagram('Alice runs')
diagram.draw()
../_images/a856ab54d56dbf3e433c3e279914986f155cd0f44cc9161e39c84b0a2351a9e5.png
ansatz = IQPAnsatz({N: 1, S: 1}, n_layers=2)
circuit = ansatz(diagram)
circuit.draw(figsize=(8, 8))
../_images/7b5acf03d56038564c171816a89aff56d34a798672f1394c8271d77db590a877.png
tket_circuit = ansatz(diagram).to_tk()

# This does not render properly on GitHub, please view it at:
# https://cqcl.github.io/lambeq/examples/circuit.html
render_circuit_jupyter(tket_circuit)
from sympy import default_sort_key

# Make sure you sort your symbols as they are returned as a set.
parameters = sorted(tket_circuit.free_symbols(), key=default_sort_key)

param_dict = {p: i * 0.001 for i, p in enumerate(parameters)}
param_dict
{Alice__n_0: 0.0,
 Alice__n_1: 0.001,
 Alice__n_2: 0.002,
 runs__n.r@s_0: 0.003,
 runs__n.r@s_1: 0.004}
tket_circuit.symbol_substitution(param_dict)

# This does not render properly on GitHub, please view it at:
# https://cqcl.github.io/lambeq/examples/circuit.html
render_circuit_jupyter(tket_circuit)