Source-linked AI summary
lambeq: An Efficient High-Level Python Library for Quantum NLP
Dimitri Kartsaklis, Ian Fan, Richie Yeung, Anna Pearson, Robin Lorenz, Alexis Toumi, Giovanni de Felice, Konstantinos Meichanetzidis, Stephen Clark, Bob Coecke
TL;DR
QNLP research seeks practical NLP models that use quantum phenomena on quantum hardware, but early experiments remain small-scale. lambeq is an open-source Python toolkit for building sentence-to-diagram and circuit or tensor-network pipelines, and the paper demonstrates classical and quantum experiments using it.
Problem
QNLP is a young area, and practical experience is needed to help close the gap between theory and practice.
Source: §1 · p. 1Method
lambeq provides a modular Python toolkit for sentence-to-string-diagram and quantum-circuit or tensor-network pipelines, with compositional models spanning different degrees of syntax sensitivity.
Source: §1 · pp. 1–2Results
The toolkit is demonstrated through classical and quantum experiments on a meaning-classification dataset, including tensor-network training and classical, noiseless, and noisy quantum-pipeline simulations.
Source: §1 · p. 3Takeaways & Limitations
The toolkit provides abstractions and tools for implementing the stages needed to convert sentences into quantum circuits and tensor networks.
Source: §8 · p. 14The toolkit does not yet include a native machine-learning module or its own optimisation and training features, relying instead on external libraries.
Source: §1 · p. 3, §6.4 · p. 10
Abstract
from arXiv · showhide
We present lambeq, the first high-level Python library for Quantum Natural Language Processing (QNLP). The open-source toolkit offers a detailed hierarchy of modules and classes implementing all stages of a pipeline for converting sentences to string diagrams, tensor networks, and quantum circuits ready to be used on a quantum computer. lambeq supports syntactic parsing, rewriting and simplification of string diagrams, ansatz creation and manipulation, as well as a number of compositional models for preparing quantum-friendly representations of sentences, employing various degrees of syntax sensitivity. We present the generic architecture and describe the most important modules in detail, demonstrating the usage with illustrative examples. Further, we test the toolkit in practice by using it to perform a number of experiments on simple NLP tasks, implementing both classical and quantum pipelines.
1 Introduction
QNLP research needs practical experience, and lambeq provides an extensible Python toolkit for building experimental pipelines from sentences to quantum circuits or tensor networks. It offers compositional models with different degrees of syntax sensitivity and demonstrates classical and quantum experiments.
1 Introduction
- lambeq provides tools for implementing an experimental QNLP pipeline that converts sentences into quantum circuits or tensor networks.The pipeline includes parsing when needed, conversion to string diagrams, rewriting, and conversion to a circuit or tensor network.Source: §1 · pp. 1–2
- Three compositional models span no syntactic information, word order, and full syntax, while the toolkit can accommodate other models that encode sentences as string diagrams.The syntax-based model follows the compositional distributional framework from Coecke et al. (2010).Source: §1 · p. 2
- This first version does not include its own optimisation or training features.Optimisation and training are identified as future goals.Source: §1 · p. 3
- The toolkit is demonstrated on a meaning-classification dataset through classical tensor-network training, a classical quantum-pipeline simulation, and noiseless and noisy shot-based simulations.The classical experiment uses PyTorch; the classical simulation uses JAX, and the shot-based simulations use Qiskit’s Aer simulator with tket.Source: §1 · p. 3
2 Background
Prior work spans quantum and quantum-inspired approaches to language tasks, while practical QNLP experiments have begun at small scale. lambeq is presented as the first programming toolkit specifically aimed at QNLP research.
2 Background
- Quantum approaches to language include Grover-based parsing speedups, Fock-space language representations, quantum-computer NLP solutions, and quantum-inspired classical models.Other work also interpreted parse trees as tensor networks and proposed instantiating them as quantum circuits.Source: §2 · p. 3
- Earlier practical QNLP work trained a classifier on 16 sentences, then scaled to datasets of 100–130 sentences and reported convergence and statistically significant results over a random baseline.The later work conducted the first complete small-scale NLP experiments on quantum hardware.Source: §2 · p. 3
- String diagrams and monoidal categories underpin DisCoCat, and subsequent work connected the framework to a quadratic speedup and NISQ-device implementations.The passage attributes the original language representation to Coecke et al. (2010), the speedup to Zeng and Coecke (2016), and NISQ foundations to Meichanetzidis et al. (2021).Source: §2 · pp. 3–4
- lambeq is described as the first programming toolkit specifically for QNLP, alongside products aimed at generic quantum machine learning.Examples named are TensorFlow Quantum and PennyLane.Source: §2 · p. 4
3 Design goals
lambeq’s design goals emphasize Python’s scientific ecosystem, open-source participation, modular extensibility, and interoperability with external tools and formats.
3 Design goals
- Python gives lambeq access to an open-source ecosystem and scientific and numerical libraries including NumPy, SciPy, PyTorch, and JAX.The paper cites Python’s prototyping abilities, extensibility, and academic and scientific-computing community as reasons for the choice.Source: §3 · p. 4
- The Apache 2.0 open-source release is intended to support code transparency, communication, issue reporting, and community contributions.The authors aim for QNLP researchers to participate in extending and improving the toolkit.Source: §3 · p. 4
- Low coupling between modules and a flexible class hierarchy are intended to support independent use and the addition of compositional models, ansätze, and rewrite rules.The design also supports interoperability through parser wrappers, JSON export, tket conversion, and tensor-network formats and backends.Source: §3 · p. 4
4 String diagrams
lambeq uses string diagrams as an abstract sentence representation that remains close to quantum computation while allowing structure to be modified. Pregroup types encode grammatical interactions and map to vector and tensor spaces for semantic interpretation.
4.1 Motivation and connection to tensor networks
- String diagrams offer an abstract sentence representation that can be viewed as an enriched tensor network, while preserving word order and enabling structural rewriting.They express computations in a monoidal category, an abstraction suited to modeling quantum-computer operations.Source: §4.1 · p. 5
- String diagrams and tensor networks remain independent of low-level choices such as quantum gates while staying close to quantum circuits.This abstraction is useful because programming quantum gates directly is difficult for NLP tasks with multiple levels of abstraction.Source: §4.1 · pp. 4–5
4.2 Pregroup grammars
- Pregroup types label diagram wires with atomic types and adjoints; n denotes a noun or noun phrase, while s denotes a sentence.For example, n·nl denotes a determiner such as “a” that expects a noun on its right to return a noun phrase.Source: §4.2 · p. 5
- Mapping atomic types to vector spaces and composite types to tensor products gives words typed tensor representations, with cups denoting tensor contractions.A concrete diagram requires dimensions for each atomic-type space; in the quantum case, these fix the number of qubits.Source: §4.2 · pp. 5–6
5 Main components
lambeq combines an external CCG parser with DisCoPy to implement a QNLP pipeline, while providing substantial language-related functionality of its own. Its parser interface converts CCG derivations into pregroup-like string diagrams, and DisCoPy supplies the underlying diagram and circuit operations.
5 Main components
- lambeq extends beyond middleware that merely connects applications: it adds language-task functionality, while DisCoPy supplies low-level support across its components.DisCoPy is the underlying engine, not just an output format.Source: §5 · p. 6, §5.2 · p. 7
5.1 CCG parser
- Yeung and Kartsaklis (2021) provide a passage from CCG derivations to pregroup-like string diagrams, addressing the lack of a wide-coverage pregroup parser.The approach enables conversion of large corpora with arbitrary sentence lengths and syntactic structures.Source: §5.1 · p. 6
- lambeq connects to external CCG parsers rather than providing its own, and ships with support for DepCCG by default.The interface converts parser output into CCGTree format.Source: §5.1 · p. 6
5.2 DisCoPy
- DisCoPy computes with monoidal categories and provides string-diagram structures, pregroup and DisCoCat features, and abstractions for standard quantum gates and circuits.lambeq uses DisCoPy for its internal sentence representation and final pipeline stages.Source: §5.2 · p. 7
6 Detailed architecture
The detailed architecture converts sentences into diagrams, transforms them with rewriting rules, and parameterises them as circuits or tensor networks. Its modules support alternative compositional models, reduce diagram complexity, and provide tensor forms for classical experiments; training uses external ML and optimisation libraries.
6.1 Sentence input
- CCGParser output is converted to a string diagram by ccg2discocat; additional parsers can be integrated through wrapper subclasses, and other compositional schemes can be added via readers.The spiders reader composes words as a bag of words, while cups composes them in sequence; CCGBankParser converts the corpus’s 49,000 annotated CCG trees into diagrams.Source: §6.1 · pp. 7–8
6.2 Rewriting
- Rewriting the prepositional phrase reduces the preposition tensor’s order by 2, and lambeq includes standard rules for several word and phrase types.The library’s rewriting rules cover auxiliary verbs, connectors, adverbs, determiners, and prepositional phrases.Source: §6.2 · p. 8, Table 1 · p. 9
6.3 Parameterisation
- An ansatz maps abstract string diagrams to circuits or tensor networks by choosing wire dimensions and parameterised word states; IQPAnsatz supports quantum circuits.TensorAnsatz supports classical experiments, while CircuitAnsatz supports quantum experiments.Source: §6.3 · p. 9
- For classical experiments, lambeq provides MPS ansätze that split potentially very large word tensors into smaller tensors linked by spiders or standard contractions.Some tensors can have order 12 or higher, making efficient execution impossible; the Figure 7 MPSAnsatz example has maximum tensor order 3.Source: §6.3 · p. 10, Figure 7 · p. 10
6.4 Optimisation/training
- lambeq has no native machine-learning module, but works with external ML and optimisation libraries including PyTorch and JAX.The toolkit documentation includes notebooks demonstrating classical and quantum pipelines.Source: §6.4 · p. 10
7 lambeq in practice
lambeq was tested on a 130-sentence food-versus-IT classification task across classical and quantum-pipeline simulations. All four settings achieved perfect test accuracy, and the toolkit was also used for hardware experiments, a web tool, and a large DisCoCat resource.
7 lambeq in practice
- The experiments classified 130 grammar-generated sentences as food or IT using classical, classical-simulated quantum, noiseless shot-based, and noisy shot-based pipelines.The dataset includes simple sentences such as “skillful programmer creates software” and “chef prepares delicious meal.”Source: §7 · pp. 10–11
- Perfect test accuracy was achieved in all four pipeline settings; noiseless runs varied, while the noisy simulation required more iterations to converge.The settings were a classical tensor-network pipeline, a classical simulation of a quantum pipeline, and noiseless and noisy shot-based simulations.Source: §7 · pp. 11–12, Figure 11 · p. 13, Figure 10 · p. 13
7.1 Other uses of lambeq
- An early toolkit version supported Lorenz et al.’s small-scale quantum-hardware experiments (2021), implementing the paper’s pipeline.Source: §7.1 · p. 12
- A web tool based on ccg2discocat converted sentences into string diagrams or quantum circuits and supported multiple output formats.Source: §7.1 · p. 12
- Yeung and Kartsaklis (2021) used lambeq to create a DisCoCat resource of more than 3,000 diagrams by parsing Alice in Wonderland.Figure 12 shows some of the sentences in diagrammatic form.Source: §7.1 · p. 12, Figure 12 · p. 14
8 Conclusions and future work
The conclusion presents lambeq as a high-level, open-source QNLP toolkit for building pipelines that produce quantum circuits and tensor networks. Planned work includes more models and parsers, quantum-friendly training, document-level tasks, and continued DisCoPy development.
8 Conclusions and future work
- lambeq provides abstractions and tools for the stages of a pipeline that converts sentences into quantum circuits and tensor networks.Source: §8 · p. 14
- The team plans to add compositional models and ansätze for classical and quantum pipelines, plus support for additional parsers including a transformer-based CCG parser.These additions are described as priorities or future package extensions.Source: §8 · p. 14
- A long-term goal is a quantum-friendly training module; integrating DisCoPy’s diagrammatic differentiation is a priority toward a more automated quantum machine-learning pipeline.Source: §8 · p. 14
- Another longer-term goal is support for paragraph- or document-level tasks using DisCoCirc, encoding documents into circuits as functional words dynamically modify nouns.Source: §8 · p. 14
- The team also intends to support, maintain, and contribute further to DisCoPy, lambeq’s low-level backend.Source: §8 · p. 14
A Appendix
The appendix illustrates lambeq workflows for parsing sentences into diagrams, rewriting diagrams, and converting them into quantum circuits or tensor-network forms.
A Appendix
- A parser example converts “John gave Mary a flower” into a string diagram, while reader examples create diagrams using spiders and cups readers.Source: A Appendix · p. 18
- The appendix shows loading CCGBank sections with CCGBankParser and applying a prepositional-phrase rewrite rule to a parsed diagram.Source: A Appendix · p. 18
- An IQPAnsatz example converts a parsed sentence diagram into a quantum circuit and then into tket form.Source: A Appendix · pp. 18–19
- TensorAnsatz, MPSAnsatz, and SpiderAnsatz examples construct tensor diagrams from a parsed sentence, with noun and sentence dimensions specified.Source: A Appendix · p. 19