Quickstart¶
After installing lambeq
, you can quickly check if everything went OK by using the command-line interface. Run the following command in a terminal:
$ lambeq "John gave Mary a flower"
The first time you run lambeq
, it will need a few minutes to download Bobcat’s statistical model. After this, you should see in the terminal the pregroup diagram for the given sentence:
John gave Mary a flower
──── ───────────── ──── ───── ──────
n n.r·s·n.l·n.l n n·n.l n
╰─────╯ │ │ ╰────╯ │ ╰─────╯
│ ╰─────────────╯
You are now ready for some real lambeq
coding! Let’s see how the above result can be achieved programmatically. Open your Python interpreter or a Jupyter notebook and type the following:
from lambeq import BobcatParser
Let’s create a new parser object that we’ll use to get the pregroup diagram of the sentence.
parser = BobcatParser()
diagram = parser.sentence2diagram("John gave Mary a flower")
You can display the diagram by using its draw()
method.
diagram.draw()

To use the above diagram on a quantum computer, we need to first convert it into a quantum circuit. This can be done with the help of an ansatz.
from lambeq import IQPAnsatz, AtomicType
For this example we will use an ansatz that generates an IQP circuit. You can think ansatze as mappings from string diagrams to quantum circuits. For this mapping to work, we need to provide the number of actual qubits we want to use for each wire. For example, in the above diagram note that each wire is annotated with a type, n
representing nouns and s
the entire sentence; so, to create the ansatz, we pass a dictionary from noun and sentence types to a specified number of qubits. We also have to provide the number of IQP layers we want for the circuit, as well as the number of rotations that will represent each qubit.
mapping = { AtomicType.NOUN: 1, AtomicType.SENTENCE: 1 }
ansatz = IQPAnsatz(mapping, n_layers=1, n_single_qubit_params=3)
Getting the circuit now is just a matter of applying the ansatz to the string diagram.
circuit = ansatz(diagram)
circuit.draw(figsize=(10,6))

Congratulations on creating your first quantum circuit from natural language text! You are now ready to try some more detailed tutorials - check the links below for suggestions.
See also: