Keywords

1 Introduction

Finite automata are real models of computers, which have only limited amount of memory. Finite automata are also interesting models themselves, due to their simplicity but at the same time rich structure and interesting properties [7]. Since the introduction of finite automata in 1959 by Rabin and Scott [13], the theme has been quite well recognized and approached from various aspects. Connections with different domains, including algebra and logics, have also been established.

The picture for theory of quantum automata and languages generated by them is less clear, and various important problems remain open [2, 11]. The task of quantum finite automata is to recognise quantum languages. Studying these languages is useful in establishing the computational and expressive power of quantum machines in general. However, such devices are not yet available and simulators have to be used instead. Because of that, we developed a library written in Python, running on a classical computer and providing implementation of several types of quantum finite automata.

This paper presents the library for simulating quantum finite automata. The library can help in exploring hypotheses on unknown relations between classes of quantum finite automata and quantum languages by providing evidence about accepting probabilities of particular words and sets of words. The library could also be useful for teaching students courses on finite automata in a quantum context.

2 Related Work

Simulation of classical finite automata is a mature area. A comprehensive survey of simulators of classical finite automata is given in [5] and JFLAP emerges as the most mature and popular tool [14].

On the other hand, there have been many libraries focused on bringing quantum computation onto the classical architectures, perhaps the best known being Q#Footnote 1. The low-level libraries include QuirkFootnote 2, with graphical interface available through a web browser, or Quantum++Footnote 3, a high performance library written in C++11. The high-level libraries provide similar functionalities but focus more on code expressiveness. They often enable using real life simulators or quantum computers as their back-ends. Examples of such libraries include ProjectQFootnote 4, QiskitFootnote 5 and the aforementioned Q#.

However, we have not found a solution focused solely on quantum finite automata. The available pieces of software were either too low-level, focusing on quantum gates and quantum phenomena in micro-scale, or too abstract, providing interfaces for developing quantum algorithms in general, but without tools dedicated specifically for quantum automata.

3 Quantum Finite Automata

In this work we consider only one-way finite automata, i.e., in each step of a simulation the head reads one symbol from the tape and moves forward. Backward or empty moves of the head are forbidden. The paper defines all automata in a uniform framework, which means that for classical finite automata notation differs slightly from the one adopted widely in literature.

Preliminaries. An input alphabet \(\varSigma \) is a finite set of symbols. The working alphabet \(\varGamma \) equals \(\varSigma \cup \{\$\}\), where $ denotes a special end-marker symbol outside the input alphabet. Set Q is a finite set of states and \(q_I \in Q\) is a distinguished state, called the initial state. Each classical state \(q \in Q\) has a quantum counterpart \(|q\rangle \). A pure quantum state \(|\psi \rangle \) of quantum automaton is defined as

$$\begin{aligned} |\psi \rangle = \left( \begin{matrix} \alpha _1 \\ \vdots \\ \alpha _n \end{matrix} \right) = \sum _{i=1}^n \alpha _i |q_i\rangle , \end{aligned}$$
(1)

where \(\alpha _1, \dots , \alpha _n \in \mathbb {C}\) and \(\sum _{i=1}^n |\alpha _i|^2 = 1\).

Vector \(\mathbf {1}\) denotes column vector consisting of |Q| ones. Matrix \(I_{|Q|}\) denotes square \(|Q| \times |Q|\) matrix containing ones on a diagonal and zeros elsewhere.

Definition 1

Nondeterministic Finite Automaton (NFA) [13] is a 5-tuple \(\mathcal {A} = (\varSigma ,Q,q_I,Q_\text {acc},\{M_\sigma \}_{\sigma \in \varSigma })\), where \(Q_\text {acc} \subseteq Q\) is a set of accepting states, and for all \(\sigma \in \varSigma \) transition matrix \(M_\sigma \) satisfies \(M_\sigma \in \{0,1\}^{|Q| \times |Q|}\). If transition matrices \(M_\sigma \) additionally satisfy for all \(\sigma \in \varSigma \) condition

$$\begin{aligned} M_\sigma \mathbf {1} = \mathbf {1} \;, \end{aligned}$$
(2)

then an automaton is Deterministic Finite Automaton (DFA). Classes of NFAs and DFAs are equivalent as they recognize the same class of languages, i.e., class of regular languages.

Definition 2

Probabilistic Finite Automaton (PFA) [12] is a 5-tuple \(\mathcal {A} = (Q,\varSigma ,I,F,\{M_\sigma \}_{\sigma \in \varSigma })\), where vector I is a stochastic column vector describing initial distribution of states, i.e., \(I \in [0,1]^{|Q| \times 1}\) and \(\sum _{i=1}^{|Q|} I_i = 1\). Vector F is a column vector of size |Q| with i-th entry equal 1 if \(q_i\) is a accepting state and 0 otherwise. For all \(\sigma \in \varSigma \) transition matrix \(M_\sigma \) is Markovian, i.e., its rows define probability distribution. Thus, for all \(\sigma \in \varSigma \) we have \(M_\sigma \in [0,1]^{|Q| \times |Q|}\) and \(M_\sigma \) satisfies (2).

Definition 3

Measure-Once Quantum Finite Automaton (MO-QFA) [9] is a 5-tuple \(\mathcal {A} = (Q,\varSigma ,q_I,Q_\text {acc}, \{U_\sigma \}_{\sigma \in \varSigma })\), where \(Q_\text {acc} \subseteq Q\) is a set of accepting states. Transition matrices \(\{U_\sigma \}_{\sigma \in \varSigma }\) satisfy \(U_\sigma \in \mathbb {C}^{|Q| \times |Q|}\) for all \(\sigma \in \varSigma \) and are unitary, i.e., for all \(\sigma \in \varSigma \) we have

$$\begin{aligned} U_{\sigma }^{\dagger }U_{\sigma } = U_{\sigma }U_{\sigma }^{\dagger } = I_{|Q|} . \end{aligned}$$
(3)

The set of accepting states corresponds to a projective operator:

$$\begin{aligned} P_\text {acc} = \sum _{q \in Q_\text {acc}} |q \rangle \langle q| \;. \end{aligned}$$
(4)

Definition 4

Measure-Many Quantum Finite Automaton (MM-QFA) [8] is a 6-tuple \(\mathcal {A} = (Q,\varSigma ,q_I,Q_\text {acc},Q_\text {rej}, \{U_\sigma \}_{\sigma \in \Gamma })\), where \(Q_\text {rej} \subseteq Q\) is a set of rejecting states, and \(U_\sigma \in \mathbb {C}^{|Q| \times |Q|}\) are transition matrices satisfying (3).

The automaton partitions set Q into \(Q = Q_\text {acc} \cup Q_\text {rej} \cup Q_\text {non}\), where \(Q_\text {non}\) is a set of nonhalting (neutral) states. Sets \(Q_\text {acc}, Q_\text {rej}\) and \(Q_\text {non}\) should be pairwise disjoint.

In a manner analogous to (4), projective operators \(P_\text {rej}\) and \(P_\text {non}\) are defined as follows:

$$\begin{aligned} P_\text {rej} = \sum _{q \in Q_\text {rej}} |q \rangle \langle q| \;, \end{aligned}$$
(5)
$$\begin{aligned} P_\text {non} = \sum _{q \in Q_\text {non}} |q \rangle \langle q| \;. \end{aligned}$$
(6)

Definition 5

General Quantum Finite Automaton (GQFA) [10] is a 6-tuple \(\mathcal {A} = (Q,\varSigma ,q_I,Q_\text {acc},Q_\text {rej}, \{U_\sigma \}_{\sigma \in \Gamma })\). The model is similar to MM-QFA, but the transition matrices \(U_\sigma \) are more general – they are a composition of a finite sequence of applications of unitary transformations followed by orthogonal measurements. Note, that there is a more general definition of GQFA, provided by Hirvensalo [6], but it is not considered in our work.

For a broader description of these and other types of quantum automata we refer a reader to [3, 7, 11].

3.1 Language Acceptance Modes

Let \(P_\mathcal {A}(x)\) denote probability of accepting word \(x \in \varSigma ^*\) by automaton \(\mathcal {A}\) and let \(\lambda \) be a real number such that \(\lambda \in [0,1)\). Given \(x = \sigma _1\dots \sigma _n\), probability \(P_\mathcal {A}(x)\) is computed as \(\Vert P_\text {acc} U_{\sigma _n} \dots U_{\sigma _1} |q_0\rangle \Vert ^2\) for MO-QFA, but for MM-QFA is more complicated.

There exist several modes of language acceptance:

  • with a cut-point \(\lambda \in [0,1)\), if for all \(x \in L\), we have \(P_\mathcal {A}(x) > \lambda \) and for all \(x \notin L\), we have \(P_\mathcal {A}(x) \le \lambda \). This mode of acceptance is also called with an unbounded error.

  • with an isolated cut-point \(\lambda \in [0,1)\), if there exists \(\varepsilon \ge 0\), such, that for all \(x \in L\), we have \(P_\mathcal {A}(x) \ge \lambda + \varepsilon \) and for all \(x \notin L\), we have \(P_\mathcal {A}(x) \le \lambda - \varepsilon \).

  • with a bounded error \(\varepsilon \in [0, \frac{1}{2})\), if for all \(x \in L\), we have \(P_\mathcal {A}(x) \ge 1 - \varepsilon \) and for all \(x \notin L\), we have \(P_\mathcal {A}(x) \le \varepsilon \). This mode of acceptance is equivalent to acceptance with an isolated cut-point, where cut-point \(\lambda = \frac{1}{2}\) is isolated with value \(\frac{1}{2} - \varepsilon \).

  • with a positive one-sided unbounded error if for all \(x \in L\), we have \(P_\mathcal {A}(x) > 0\).

  • with a negative one-sided unbounded error if for all \(x \in L\), we have \(P_\mathcal {A}(x) = 1\).

  • Monte Carlo acceptance [4], if there exists \(\varepsilon \in (0, \frac{1}{2}]\) such, that for all \(x \in L\), we have \(P_\mathcal {A}(x) = 1\) and for all \(x \notin L\), we have \(P_\mathcal {A}(x) \le \varepsilon \). Such \(\mathcal {A}\) is called Monte Carlo QFA for L.

Acceptance with a cut-point and acceptance with an isolated cut-point are the most import modes of language acceptance, and are implemented in our library.

4 Library

The simulation libraryFootnote 6 provides its interface through a number of classes, each representing one of the automata models. It also uses modules LanguageGenera-tor, LanguageChecker and Plotter. Figure 1 presents main components of the simulation library, which offers the following functionality.

Fig. 1.
figure 1

Model of the quantum finite automata library

Automaton Definition. A new automaton is constructed with an object belonging to a class representing implemented automaton (one of PFA, MO_1QFA, MM_1QFA, GQFA) and data defining chosen automaton, such as the alphabet, transition matrices and matrices of projective measurements are passed. A user has to assure unitarity of transition matrices.

Generation of Language Samples. The LanguageGenerator module is responsible for this functionality. For finite languages, a user can provide a list of words which entirely define the language. To account for the infinite regular languages, a language can be defined with a regular expression. Generation of samples of stochastic languages is not implemented in the current version of the library.

Automaton Simulation. The LanguageChecker module of the library enables simulation of an automaton run on a single word or on a random or user-defined sample of defined language. A simulation is determined by a transition matrix and one simulation of the automaton on a given word is sufficient to obtain all information about word acceptance or rejection. Thus, there is no need to repeat a simulation since transition matrices are stationary. Simulation results are returned with respect to two modes of language acceptance: with a cut-point and with an isolated cut-point.

Results Visualisation. The library has a dedicated Plotter module to plot histograms of counts of words accepted and words rejected with a given probability. A cut-point and an isolation interval can also be shown.

5 Simulations and Results

There exist two ways of simulating systems of a probabilistic nature. One way is the strong simulation, which requires calculating the exact probability of an outcome. The other way is the weak simulation, which is based on a sampling from the output distribution in order to approximate the probability. The latter is the only tractable way of simulating quantum computers because of the complexity of the task. However, our library performs a strong simulation, which is adequate because of the simpler nature of quantum automata.

5.1 Experiments

We have performed a handful of experiments to determine the time complexity of the simulations depending on various parameters.

Automaton size. During our research we concluded that the most important factor in the time complexity of a simulation is the number of automaton states. In a true QFA, the relation between the number of states and computation time would be linear. In our simulator the time complexity is polynomial which agrees with the result proven in [3]. Figure 2 shows simulation time of GQFA with the growing number of its states. The results are quite close to the values predicted theoretically. Simulation time is reported as the arithmetic mean from 5 simulations for each automaton size.

Fig. 2.
figure 2

Simulation time of GQFA in a function of the number of states

Alphabet Size. Figure 3 presents simulation time of GQFA as a function of the size of alphabet, over which automaton is defined. For each alphabet size, simulation time was measured as the arithmetic mean over 500 random words. It should be noted that the scale in ordinate axis (y-axis) does not start from 0. Figure 3 shows that there is no dependence between the size of the alphabet and the time of simulation. This is because the change of the alphabet size influences only the amount of input data (transition matrices and projective measurement matrices) required to define an automaton but does not impact computation time at all.

Fig. 3.
figure 3

Simulation time of GQFA in a function of the alphabet size

Word Length. The relation between the time of computation and the length of an input word is linear. This is not surprising, because with each letter of the input word, an automaton performs a fixed number of matrix multiplications. The size of these matrices is determined by the number of states of automaton. For each letter a transition function must be applied and a measurement may be performed, depending on the type of an automaton. Figure 4 shows simulation time of GQFA as a function of the length of simulated words. For each word length, 100 random words were simulated and simulation time was taken as the arithmetic mean over these 100 words.

5.2 Usage Example

Listing 1.1 presents exemplary code for defining a MM-QFA automaton. This automaton is well-known in literature and was proposed by Ambainis and Freivalds [1] as a part of the proof that there exists a one-way QFA, which recognizes language \(a^*b^*\) with probability \(p=0.68..\), where p is the real root of the equation \(p^3 + p = 1\). Figure 5 visualizes obtained acceptance probabilities and their corresponding word counts.

figure a
Fig. 4.
figure 4

Simulation time of GQFA in a function of the length of simulated words

Fig. 5.
figure 5

Visualization of acceptance probabilities and acceptance modes for MM-QFA

6 Conclusions

The library provides a simple API for functionality required to simulate quantum finite automata. We hope the library will encourage research at the intersection of quantum computations and theory of formal languages and automata. We have experimentally shown that the time complexity of simulating a quantum finite automaton is polynomial in relation to the size of the automaton. Nevertheless, we believe that the library may be useful for researchers, lecturers and students as a tool to prove or disprove certain properties of quantum automata and languages in a reasonable time. We have successfully used our library and thus shown that it returns expected results for examples taken from literature.

The scope of the project can be broadened in several directions, e.g. by adding new types of automata.

Acknowledgments. The research presented in this paper was supported by the funds assigned to AGH University of Science and Technology by the Polish Ministry of Science and Higher Education.