QNLP Using Lambeq Toolkit
QNLP Using Lambeq Toolkit
Abstract—Sentiment classification is one the best use case the error rate is directly proportional to the number of qubits
of classical natural language processing (NLP) where we can and information a qubit contains can be lost easily which is
witness its power in various daily life domains such as banking, why quantum computers are stored at very cool temperatures
business and marketing industry. We already know how classical
AI and machine learning can change and improve technology. and requires great maintainence.
Quantum natural language processing (QNLP) is a young and QNLP is different from classical NLP. QNLP has its ori-
gradually emerging technology which has the potential to provide gins in abstract mathematical theory which includes category
quantum advantage for NLP tasks. In this paper we show the first theory - especially monoidal categories, diagramatic quantum
application of QNLP for sentiment analysis and achieve perfect theory and ZX calculus. To gain more understanding about
test set accuracy for three different kinds of simulations and a
decent accuracy for experiments ran on a noisy quantum device. the concepts of diagrammatic quantum theory, the reader can
We utilize the lambeq QNLP toolkit and t|ket > by Cambridge refer to [2] which explains the fundamentals of diagrammatic
Quantum (Quantinuum) to bring out the results. reasoning for quantum theory and is the core of QNLP.
Index Terms—Quantum Computing, Quantum Natural Lan- Since a model of natural language is being equivalent to
guage Processing, lambeq a model which explains quantum mechanical phenomena,
this approach makes QNLP quantum-native. By this process
I. I NTRODUCTION linguistic structure can be encoded easily where as encoding
Taking computational speeds and performance into con- grammer in classical is very costly.
sideration, quantum computers are exponentially faster than In this paper we are going to see how accurately quantum
the present generation classical computers. Until the last two computers can predict the sentiments, where they are already
decades or the end of the 20th century quantum computer was trained with around 130 sentences. We will also see how
a fictional story developed by great mathematicians and physi- classical computers and quantum computers with embedded
cists such as Richard Feynman, Erwin Schrodinger, David noise will give the results and compare them to get a better
Deutsch, etc. In the early 21st century quantum computers understanding of why we need quantum computers and how
gained its importance and the fictional story was greatly read, powerful they are.
understood and was theoretically well developed. Recently, the The rest of the paper is ordered as follows: section 2
first quantum computers have been built, some of them have gives an introduction to the related work that is done and the
been made publicly available, and they have already gained research going on in this field; section 3 gives a clear picture
its significance and showed its power in various domains and brief intuition on QNLP and also explains the sentiment
like machine learning, chemistry, natural language processing, classification experiment; In section 4 we discuss the results
biomedicine, etc. In this paper we will see how quantum of classical, quantum and quantum with noise devices, section
computers can help us in improving the domain of natural 5 summarises the work and also proposes some future lines
language processing. of work in the domain of QNLP.
Quantum computing is itself a nascent field so is Quan-
tum Natural Language Processing (QNLP), we take the phe- II. R ELATED W ORK
nomenon of superposition, entanglement, interference to our As researchers, scientists, enthusiasts identified the capabil-
own advantage and run NLP models or language related tasks ity of quantum devices and the power they have got, more
on the hardware. As of now we are currently in the Noisy effort and time is put into this domain. Despite of QNLP
Intermediate-Scale Quantum (NISQ) [1] computers era, where being a new and emerging field, Noisy Intermediate Scale
Quantum(NISQ) devices have already led to some propitious
results [3] and also appiled to divergent field such as quantum
music [4].
As we all know that the applications of classical NLP is
already seen in our day-to-day lives. Voice assistants such
as Siri and Alexa are the best examples. The problem and
constraint with classical NLP is that it can only read and
decipher bits but cannot deeply understand the meaning of
the language and that is where there is scope for quantum to
do this in a meaning-aware manner.
In QNLP, the sentences are depicted by variational quantum
circuits and each and every word in a sentence is transformed
into quantum states by using parameterized quantum gates.
With the help of the variational circuit technique, QNLP
becomes NISQ-friendly [5].
Scientists and researchers at Cambridge Quantum developed
the first high level python framework for QNLP named lam-
Fig. 1. String diagram of ”siva hates thrilling comics”.
beq. This unique toolkit is a open source package which offers
the functionality of converting sentences into quantum circuits
[6]. the words together to give the meaning of the sentence, which
The first and foremost effort of designing and execution is the grammar. The juxtaposition of the atomic types reduces
of natural language models on a real quantum computer to ’s’ which signifies that the sentence is grammatically
was accomplished by Cambridge Quantum where they used correct. This juxtaposition is solved in (1).
an intermediate dataset containing sentences of two different
classes - Food or IT and results obtained were profound as n · nr · s · nl · n · nl · n → 1 · s · 1 · 1 → s (1)
given in [7]. Due to today’s NISQ devices i.e. small number of qubits,
In this work, we present the first application of QNLP qubit decoherece and since the string diagrams themselves are
for sentiment analysis on an intermediate level dataset where resource intensive, they need to be rewritten into a more NISQ
we achieve successful results in a binary classification of friendly version which is going to consume less number of
sentiments - positive and negative sentences. We demonstrate qubits to represent the sentences.
that for both classical and quantum simulations we achieve
proper convergence.
III. M ETHODOLOGY AND E XPERIMENTS
We have utilized the Distributional Compositonal Cate-
gorical (DisCoCat) [8] framework for our task of sentiment
classification. The DisCoCat framework provides a unique way
of combining different constitutent words to form the meaning
of a whole sentence. It follows a compositional mechanism for
the grammar to entangle word meanings which are distributed
in a vector space. Lambek’s pregroup grammar is used in
DisCoCat to retrieve the meaning of a sentence from the
meaning of the words.
A. Compositional Model of Meaning
The compositional model of meaning is inspired from
Lambek’s pregroup grammar. In this formalism, we assign
atomic types ’n’ for noun and ’s’ for sentence to the words
present in a sentence which assists in the composition of the Fig. 2. Rewritten string diagram of ”siva hates thrilling comics”.
word meanings together. The grammatical rules to compose
different types of sentences can be found in [9] and the string Since cups consume twice the number of qubits assigned
diagrams shown have utilized those rules given by Lambek. to either ’n’ or ’s’, therefore, removing the cups from the
Fig. 1 shows the string diagram for a sentence ”siva hates original string diagrams results in a diagram which actually
thrilling comics” where the words are represented by boxes consumes less number of resources and is better adapted to
or alternatively triangle shaped boxes and the wires (cups and today’s NISQ hardware. From Fig. 2 it can be seen that the
straight wires) represent the entagling effect which composes diagram has reduced in size by removing the cups.
B. Language into Quantum Circuits
After we have designed the string diagrams for the lan-
guage, we have to transform them into quantum circuits in
order to run them on simulators and quantum hardware. The
compositional model of meaning described before follows
a bottom-up approach i.e. composing words to form the
meaning of sentence. On the contrary, language in the form
of quantum circuits follows a top-down approach i.e. inferring
the meaning of words using the meaning of sentence. This top-
down approach is valid because for training quantum circuits
a dataset of sentences with labels is provided and from that
the meaning of words is inferred.