ArticlePDF Available

QJava: A Monadic Java Library for Quantum Programming

Authors:

Abstract

To help the understanding and development of quantum algorithms there is an effort focused on the investigation of new semantic models and programming languages for quantum computing. Researchers in computer science have the challenge of deve loping programming languages to support the creation, analysis, modeling and simulation of high level quantum algorithms. Based on previous works that use monads inside the programming language Haskell to elegantly explain the odd characteristics of quantum computation (like superposition and entanglement), in this work we present a monadic Java library for quantum programming. We use the extension of the programming language Java called BGGA Closure, that allow the manipulation of anonymous functions (closures) inside Java. We exemplify the use of the library with an implementation of the Toffoli quantum circuit.
QJava: A Monadic Java Library for Quantum
Programming
Bruno Crestani Calegaro 1
Juliana Kaizer Vizzotto 2
Data submiss˜
ao: 22.10.2014
Data aceitac¸ ˜
ao: 25.04.2015
Sec¸˜
ao melhores artigos WEIT-2013, Rio Grande, Brasil, 2013.
Abstract:
To help the understanding and development of quantum algorithms there is an effort
focused on the investigation of new semantic models and programming languages for
quantum computing. Researchers in computer science have the challenge of deve-
loping programming languages to support the creation, analysis, modeling and simu-
lation of high level quantum algorithms. Based on previous works that use monads
inside the programming language Haskell to elegantly explain the odd characteris-
tics of quantum computation (like superposition and entanglement), in this work we
present a monadic Java library for quantum programming. We use the extension of
the programming language Java called BGGA Closure, that allow the manipulation of
anonymous functions (closures) inside Java. We exemplify the use of the library with
an implementation of the Toffoli quantum circuit.
1 Introduction
Quantum computing is an emerging technology, based on the idea of a computer that
has access to, and can manipulate, quantum information, i.e., information coded in a state
of a quantum physical system. The idea of a quantum computer that takes advantage of the
effects of quantum mechanics was theoretically introduced by Richard Feynman in 1982 [1].
After that, David Deutsch [2] defined a quantum Turing machine, suggesting that if quantum
computers could solve quantum mechanical problems more quickly than classical comput-
ers, they might also could solve classical problems more quickly. In 1994, Peter Shor [3]
invented a quantum algorithm to solve the problem of factoring large numbers in polynomial
time on a quantum computer. Most famous cryptography techniques are based on the fact that
classical algorithms to factor large numbers take exponential time [4]. A practical quantum
1Instituto Federal de Santa Catarina, IFSC
{bruno.calegaro@@ifsc.edu.br}
2Universidade Federal de Santa Maria, UFSM
{juvizzotto@@inf.ufsm.br}
QJava: A Monadic Java Library for Quantum Programming
computer capable of performing Shor’s algorithm would be able to break current cryptogra-
phy techniques. The invention of this algorithm motivated the topic of quantum computing
and stimulated researchers around the world to investigate the creation of practical quantum
computers and of new quantum algorithms applied to different areas.
Also, to help the understanding and development of quantum algorithms there is an
effort focused on the investigation of new semantic models and programming languages for
quantum computing. Researchers in computer science have the challenge of developing pro-
gramming languages to support the creation, analysis, modeling and simulation of high level
quantum algorithms.
In the work [5], the authors presented a semantic model for pure quantum computing
(or more precisely, arbitrary linear functions over complex vector spaces) using the concept of
monads [6]. Monads are an idea from category theory used to model side effects in classical
functional programming languages. The appeal of using monads is that they can elegantly
model side effects, such as global state and input/output, in the context of pure functional
languages. The use of monads allows to elegantly explain the odd characteristics of quantum
computing (like superposition) using tools applied in the development of traditional program-
ming languages (not quantum).
Java is a general propose objected oriented programming language that enables secure
and high performance software development on multiple platforms. It is one of the most
widely used programming language in software development. Based on the previous monadic
model for quantum programming mentioned above, we present in this paper a monadic Java
library for quantum programming. We use the extension of Java called BGGA Closure, that
allow the manipulation of anonymous functions (closures) inside Java. We exemplify the use
of the library with an implementation of the Toffoli quantum circuit.
The practical contribution of our work is a monadic Java library, called QJava, that can
be used to program pure quantum algorithms. Moreover the manuscript has a very detailed
explanation of monads in the context of functional programming languages e its application
to quantum computing.
The present work is organized as follows: Section 2 briefly reviews the basic notions
involved in quantum programming. In Section 3 we explain the main concepts about monads
applied to semantics of programming languages and the definition of a quantum monad.
Section 4 presents the library QJava and examples. Finally, we conclude in Section 5.
2 Quantum Programming
Quantum computing is an information processing paradigm, concerning hardware and
software, where information/data (and maybe control flow) can be encoded in the state of a
RITA Volume 22 N´
umero 1 2015 243
QJava: A Monadic Java Library for Quantum Programming
quantum physical system. Quantum programming is about quantum software design, i.e., to
design algorithms considering the quantum computing paradigm. In this section we briefly
review the formalism needed to understand the basic principles of quantum programming. All
the concepts presented in this section also apply to quantum computing in general, however
we do not discuss quantum hardware here. For a complete reference on quantum computing
see [7].
2.1 Quantum Bits
The basic unit of information in classical computing is the traditional bit, a binary
classical physical system. The bit is usually represented as an scalar that belongs to {1,0}.
In quantum computing the basic unit information is represented by a quantum bit, or
qubit, a binary quantum physical system. The qubit is a vector usually represented using the
Dirac braket 3notation:
|ψi=α|0i+β|1i.
The Dirac notation has the advantage that it labels the basis vectors explicitly. The basic
states |0iand |1ican be explained by analogy with the classical bit, i.e., form a two-level
system and are an orthonormal basis for the vector space where the qubit lives (usually called
the standard or computational basis). The coefficients or also called probability amplitudes,
αand β, are complex numbers, such that |α|2+|β|2= 1. In other words the qubit can be
formalized as a vector in two-dimensional complex vector space (Hilbert space), with norm
(size) equals 1.
The qubit can be seen as a column vector:
α
β
In this representation it is important to note that there is an ordering for the basis
vectors which is relevant: the coefficient for |0i, in this specific case equals α, always appear
in the first row (the row for |0i) and the coefficient for |1i, in this specific case equals β, in
the second row (the row for |1i). This convention can be illustrated as follows:
|0i
|1iα
β
3The name braket comes from the convention that a column vector is called a “ket” and is denoted by | i and a row
vector is called a “bra” and is denoted by h |.
244 RITA Volume 22 N´
umero 1 2015
QJava: A Monadic Java Library for Quantum Programming
As an example, the classical bit 0can be represented as the basis state |0i= 1|0i+0|1i
and the classical bit 1can be represented as the basis state |1i= 0|0i+1|1i. These two states
can also be represented as the following column vectors, respectively:
1
0 0
1
Any other state with different values for αand βis said to be in a quantum superpo-
sition of |0iand |1i, for instance the state 1/2|0i+ 1/2|1i.
The interpretation of the probability amplitudes αand βcan be given by the following:
when we interact or measure a quantum state like α|0i+β|1iwe will see/get the state |0i
with probability |α|2and the state |1iwith probability |β|2.
Now lets understand a composite quantum state with two or more qubits like above.
Again, in analogy with the classical bit, consider a state with two bits, we would have four
alternatives: 00,01,10,11. Then, the state of a pair of qubits is a linear combination of these
four classical states:
α|00i+γ|01i+δ|10i+β|11i
This two qubit state can also be represented as a column vector:
α
γ
δ
β
Note again that there is an ordering for the basis vectors which is relevant: the coefficient for
|00i, in this specific case equals α, always appear in the first row (the row for |00i), the the
coefficient for |01i, in this specific case equals γ, in the second row (the row for |01i), the
coefficient for |10i, in this specific case equals δ, in the third row (the row for |10i), and the
coefficient for |11i, in this specific case equals β, in the last row.
If q=α|0i+β|1iand p=γ|0i+δ|1iare two independent quantum bits, then we
can form a combined state using the tensor operation on vector spaces, , defined as follows:
qp=αγ|00i+αδ|01i+βγ |10i+βδ|11i
However, there are some combined quantum bits which are not of the form qp. For
instance, the state:
1/2|00i+ 1/2|11i
is clearly not of the form qp, for any qand p. This kind of combined state which
can not be described using the tensor product operation is called entangled.
RITA Volume 22 N´
umero 1 2015 245
QJava: A Monadic Java Library for Quantum Programming
2.2 Quantum Operations
There are two kinds of operations on quantum bits: unitary transformations 4and
measurement. The first kind corresponds to reversible operations that change the state of a
qubit without loss of information and the second kind observes the state to find out its value.
A measurement collapses the quantum state making it impossible to recover the original state
after the observation.
2.2.1 Unitary Transformations As we explained above a qubit is a unit vector in a two
dimensional complex vector space (in general spanned by the computational basis |0iand
|1i). A unitary transformation changes the state of the unit vector without loss of information.
As a qubit is usually represented by a column vector a unitary transformation is in general
represented by a unitary matrix. That is, for a state with nqubits we will have a 2n×2n
matrix.
Suppose a state |φi=α|0i+β|1i, then the application of a unitary transformation T
can be represented as usual matrix/vector multiplication:
T|φi=a00 a01
a10 a11 α
β
As an example of a unitary transformation acting in one qubit, consider the quantum
NOT transformation, which flips the amplitudes of |0iand |1i. So, given a quantum state
|φi=α|0i+β|1i, the application of the quantum NOT should return |φ0i=β|0i+α|1i.
The quantum NOT is represented by the following unitary matrix:
NOT =0 1
1 0
Intuitively, the first column of this 2×2matrix says what happens to the input vector
|0iand the second column says what happens to the input vector |1i. Hence, the application
to α|0i+β|1igives:
0 1
1 0 α
β=β
α
i.e., β|0i+α|1i.
4A unitary matrix is a square (n×n) complex matrix Usatisfying the condition UU=U U =In, such that U
is the conjugate transpose (also called the Hermitian adjoint) of U.
246 RITA Volume 22 N´
umero 1 2015
QJava: A Monadic Java Library for Quantum Programming
Other example of an important unitary transformation acting in one qubit is the Hada-
mard quantum operation, which generates a quantum state in a coherent superposition:
H=1
21 1
11
Note the rule of the columns again: the first column says what happens to the input
vector |0i, that is, should return 1
2|0i+1
2|1i, and the second column says what happens to
the input vector |1i, that is, should return 1
2|0i − 1
2|1i. Hence, this operation generates a
qubit in a coherent superposition. Here coherent means that the state has the same probability
amplitude for |0iand for |1i, i.e., |1
2|2= 1/2.
Additionally, there are operations that change the phase factor of a qubit. In quantum
mechanics a phase factor is a complex coefficient that multiplies a vector |ψi. In terms of
classical observation of a qubit the phase factor has no physical meaning. However the phase
factor is an important quantity when considering interference. An example is the phase-flip
transformation:
Z=1 0
01
An example of a quantum operation acting on two qubits is the controlled not, CN OT ,
operation. It is a quantum controlled operation for a state of two qubits (a 4×4matrix). The
first qubit is the controller and the CN OT only applies the N OT on the second qubit if the
first qubit is 1, otherwise it does nothing.
CNOT =
1000
0100
0001
0010
Again, it is interesting to note the rule of the columns, however for a two qubit state:
the first column says what happens if the input vector is |00i, the second column says what
happens if the input vector is |01i, the third says what happens if the input vector is |10iand
the last column says what happens if the input vector is |11i. Note that the input can be in a
superposition, so the output is a combination of each case. Also note that there is an ordering
for the coefficients in the rows (each row represents a basis vector), which is the same as
explained in section above.
It is intriguing to note the orthogonality condition: due to the condition that all these
operations must be unitary/reversible each column of the matrix must represent an orthogonal
basic vector with respect to the other columns.
RITA Volume 22 N´
umero 1 2015 247
QJava: A Monadic Java Library for Quantum Programming
Unitary transformations acting in quantum vector states (or also called pure states) are
called pure quantum computations as no information is lost.
2.2.2 Measurement Intuitively, unitary transformations represent a kind of reversible ro-
tation on the vector which represents the state of the quantum system. A measurement causes
other kind of transformation on the vector, called a collapse, and allows to get some classical
information about the quantum state.
A measurement in a state like α|0i+β|1iwill return the state |0i(or simply 0) with
probability |α|2and the state |1i(or simply 1) with probability |β|2. Moreover, the quantum
state is modified by the measurement: in this case, after the measurement the state will be
|0iif the observed value was 0and |1iif the observed value was 1. Immediately after the
measurement we cannot recover the state before the measurement. This implies that we
cannot collect any additionally information about αand βby repeating the measurement.
In this example, we explained the measurement in the computational basis,|0iand
|1i. However we can measure a quantum state considering any orthonormal basis for the
qubit. For instance, the states |+i= 1/2(|0i+|1i)and |−i = 1/2(|0i−|1i)also form an
orthonormal basis for the qubit. Then, we can rewrite the state in that basis: α0|+i+β0|−i. So
a measurement of a qubit considering that basis will produce |+iwith probability |α0|2, and
|−i with probability |β0|2. As an example, consider we measure the state |ψi=α|0i+β|1i
in the |+i,|−i basis. We first express |ψiin the |+i,|−i basis:
|ψi=α|0i+β|1i
=α1
2(|+i+|−i) + β1
2(|+i − |−i)
=1
2((α+β)|+i+ (|αβ)|−i).
Then the probability of measuring |+iis |1
2(α+β)|2=|α+β|2/2, and the probability of
measuring |−i is |α+β|2/2.
Moreover, we can measure a composite quantum system. For instance, if two qubits
are in a state |ψiand we measure them, then the probability that the first qubit is in a state
i, and the second qubit is in a state jis P(i, j) = hij |ψi, where h|i is the traditional inner
product operation in vector spaces. The inner product returns a scalar quantity for a pair
of vectors. Formally, it is given by the multiplication of a row vector by a column vector.
After the measurement the state of two qubits is |ψ0i=|iji. However, we can still measure
only the first qubit of a two state quantum system. In that case , the outcome is the same
as if we had measured both qubits. That is, the probability that the first qubit is in state iis
P(i) = P0,1
jhij|ψi. The new state after the measurement then consists of those terms in the
superposition that are consistent with the outcome of the measurement, but normalized. This
partial measurement represents a projection of the vector onto the subspace spanned by the
248 RITA Volume 22 N´
umero 1 2015
QJava: A Monadic Java Library for Quantum Programming
states |iji, such that j∈ {0,1}.
Entanglement can also be explained using measurement. Consider the following en-
tangled state: |ψi=1
2|00i+1
2|11i. If we take a measurement in the first qubit, then the
state of the other qubit is determined by the outcome of the measurement. For instance, with
probability 1
2we get |00ias the outcome of the measurement, and in this case, we know that
the state of the system after the measurement must be |00i.
3 Monads in Programming Languages
Monads were first applied to Computer Science by Moggi [6] when he presented a
category-theoretic semantics of computations. As an example of this semantic approach for
computations, Moggi showed a general way to structure various notions of computation in
the computational λ-calculus. The computational λ-calculus is a modification of the typed λ-
calculus, which is correct for proving equivalence of programs independent from any specific
notion of computation. By notion of computation (or also called computational effect) he
means a quantitative description of the denotation of programs. Examples of notions of
computations are: computations with side effects, where a program denotes a map from a
store to a pair, value and modified store; computations with exceptions, where a program
denotes either a value or an exception; partial computations, where a program denotes either
a value or diverges; nondeterministic computations, where a program denotes a set of possible
values.
Typed λ-calculus is the core language for functional programming languages. How-
ever, functional programming languages extend typed λ-calculus with many notions of com-
putation that are indispensable for programming. Besides simple and pure functions, all
realistic programming languages include some kind of computational effects, and there is a
long debate about how to structure the semantics of these computational effects in the con-
text of functional programming languages (see papers about pure and impure functional pro-
gramming languages [8, 9]). In this context, the monadic approach by Moggi was elegantly
used to structure computational effects in pure functional programming languages [10], like
Haskell [11].
In this section we review the category-theoretic concept of a monad, explain why it is
a useful tool to elegantly model notions of computations and review how the approach can be
applied in the context of the functional programming language Haskell.
RITA Volume 22 N´
umero 1 2015 249
QJava: A Monadic Java Library for Quantum Programming
3.1 Monads in Category Theory
When considering the categorical semantics of computations one can consider differ-
ent levels of abstraction. Here we consider the denotational view, where programs can be
viewed as morphisms of a category, whose objects are types.
Considering a simplification of the denotational view, programs can be identified with
total functions from values to values. However, that is a gross simplification as state in [12].
Indeed, a simple program may include notions of computational effects, like input/output,
exceptions, side effects, etc. In this sense, a program can be understood as a function form
values to computations. For instance, a program that receives a value and prints it in the
standard output.
Hence, we take a category Cas a model for functions and develop on top of that a
general understanding of type for values and computations. As explained in [12], we use a
unary operation Ton the objects of C, which maps an object A, viewed as the set of values of
type τ, to an object T A corresponding to the set of computations of type τ. Then a program
from Ato B, i.e., which takes as input a value of type Aand after performing a certain
computation will return a value of type B, can be identified with a morphism form Ato T B
in C. Finally, there is a minimum set of requirements on values and computations so that
programs are the morphism of a suitable category.
These informal discussion on notions of computations and category theory leads to
the use of Kleisli triples for modeling notions of computations and Kleisli categories for
modeling categories of programs.
Definition 3.1 AKleisli triple over a category Cis a triple (T, η, ), where T:Obj(C)
Obj(C)(such that, Obj(C)corresponds to the class of objects from the category C), ηA:
AT A,f:T A T B for f:AT B, and the following equations hold:
η
A=idT A
ηA;f=f
f;g= (f;g)
Intuitively ηAis the inclusion of values into computations and fis the extension of
a function ffrom values to computations to a function from computations to computations.
The axioms amounts exactly to say that programs form a category, the Kleisli category CT,
where the set CT(A, B)of morphisms from Ato Bis C(A, T B ), the identity over Ais ηA
and the composition of f:AT B followed by g:BT C is f;g.
250 RITA Volume 22 N´
umero 1 2015
QJava: A Monadic Java Library for Quantum Programming
Monads are equivalent to Kleisli triples, which, as seen above, are easy to justify
computationally. There is a one-to-one correspondence between Kleisli triples and monads.
However, the formal definition of monads is given in terms of functors and natural transfor-
mations. We are not going to formally define monads here as the alternative definition of
Kleisli triples is enough in the context of denotational semantics of computational effects in
programming languages. Additional information on monads can be found in [13].
3.2 Adding Monadic Effects in Functional Programming
The pure λ-calculus is the core language for functional programming languages. It
offers simple equational reasoning mechanism and referential transparency. Essentially, that
means the equations and inference rules of pure λ-calculus say that “the order of evaluation
of an expression is irrelevant”.
As types are very relevant for the discussion about adding monadic effects to the
language we are going to present pure λ-calculus with simple types and with constants for
numbers and addition. The following grammar generates the set of simple types over the type
of natural numbers:
A::= N|AA
i.e., we have the type of natural numbers and the type of functions. Consider the syntax of
expressions:
e::= x|λx :A.e |e1e2|n|e1+e2
The syntax is given using traditional BNF (Backus-Naur Form) notation. The derivation rule
says the expressions of the language are constituted by simple variables, λ-abstraction with
an explicit type annotation on the bound variable telling us to assume hat the argument will
be of an explicit type A, application, constants for numbers and addition, respectively.
The typing relation for pure λ-calculus, written Γ`e:Aand read in the environment
Γ, the expression ehas type A, where Γis a set of assumptions about the free variables in e,
is defined by a set of inference rules assigning types to expressions, summarized in Figure 1.
Below we show the axioms for the equational semantics for the language.
(λx :A.e)e0=e[e0/x] (β)
λx :A.e x =e(η)
(e1+e2) + e3=e1+ (e2+e3) (S)
e1+e2=e2+e1(C)
n1+n2=nwhere n=n1+n2(A)
RITA Volume 22 N´
umero 1 2015 251
QJava: A Monadic Java Library for Quantum Programming
Type Cons
Γ`n:N
x:AΓType Var
Γ`x:A
Γ, x :A1`e:A2Type Abs
Γ`λx :A1.e :A1A2
Γ`e1:A11 A12 Γ`e2:A11 Type App
Γ`e1e2:A12
Γ`e1 : NΓ`e2:NType Sum
Γ`e1+e2:N
Figure 1. Pure Simply Typed λ-Calculus
First two equations are usual βand ηreduction. The notation e[e0/x]represents substitution,
saying that the final expression will be ewith the free occurrences of xsubstituted by e0. The
three following equations stand for associativity, commutativity and evaluation of addition,
respectively.With the following inference rule:
e1=e2
C[e1] = C[e2]
for any evaluation context C. The evaluation context, or simply context, was introduced
to reduction semantics of programming languages by [14] and controls where evaluation
occurs. A context is an expression or a sub-expression with a placeholder (represented by )
for where the next evaluation step can take place. By any context we mean that the order of
evaluation of an (sub)expression is irrelevant.
For instance, consider the evaluation of the expression (λx.x+x) (4 + 2). Depending
on the evaluation context we consider we can have different orders of evaluation. Considering
C=, the empty context, and e1= (λx.x +x) (4 + 2) we can have the evaluation order on
left below, and considering C= (λx.x +x)and e1= (4 + 2) we can have the evaluation
order on right below.
(λx.x +x) (4 + 2)
=(by β)(4 + 2) + (4 + 2)
= 6 + (4 + 2)
= 6 + 6
= 12
(λx.x +x) (4 + 2)
=(by C)(λx.x +x) (2 + 4)
= (λx.x +x) 6
= 6 + 6
= 12
The example also illustrate the referential transparency property, i.e, the expressions
yield the same value each time they are invoked independent of order of evaluation.
252 RITA Volume 22 N´
umero 1 2015
QJava: A Monadic Java Library for Quantum Programming
Now consider we want to add to the syntax of the language an implicit global location
initialized to 0, and the expressions get and inc:
e::= x|λx :A.e |e1e2|n|e1+e2
inc |get
where get returns the current contents of the global location and inc returns the current con-
tents of the global location and increments it as a side effects.
Then, lets evaluate the expression (λx.x +x)inc:
(λx.x +x)inc
=(by β)inc +inc
= 0 + inc
= 0 + 1
= 1
(λx.x +x)inc
=(by inc)(λx.x +x) 0
= 0 + 0
= 0
Again we can consider the two different orders of evaluation above. However in this example,
as we are considering access to a global variable, the order of evaluation is very relevant.
Depending on the order we can get different results. This is because we are adding some
notion of computational effect to the language. Indeed, inc is not a simple function which
returns a value, the invocation of inc may return different results each time depending on the
global state.
The state of the art in pure functional languages uses the concept of monads explained
in section above to sequencing effects. Monads separate the pure language from the effects
through the type system, and impose minimal constraints on the language used to sequence
effects: must have a composition operator that is associative and a unit for the composition.
Moreover, they provide a generic treatment of a large class of computational effects.
Considering the definition of a monad as a Kleisli triple in section above, we need
to add the tripe (T, η, )for a global location to the language. In this case, the functor
T A = (A×S)S, where Sis a set of states. That is, the functor maps a type Aof values to
computations that are functions from a state/store to a pair of a value together with the modi-
fied state/store: S(A×S). The natural transformation ηis the map a7→ (λs :S.ha, si)
and if f:AT B and cT A, then f(c) = λs :S.(letha, s0i=c(s)in f(a)(s0)). Note
that fis a passing state function.
In the programming language community, the ηis usually called a function return ::
AT A and the composition, , is usually pronounced “bind”, >>= :: T A (A
T B)T B , a function that receives two inputs, a computational effect, T A, and a function
RITA Volume 22 N´
umero 1 2015 253
QJava: A Monadic Java Library for Quantum Programming
from AT B, and returns a computational effect, T B 5. So we add these two constructions
to the language:
e::= x|λx :A.e |e1e2|n|e1+e2
return e |e1>>=e2|inc |get
With the following typing rules:
Type Inc
Γ`inc :T A Type Get
Γ`get :T A
Γ`e:AType Return
Γ`return e :T A
Γ`e1:T A Γ`e2:aT B Type Bind
Γ`e1>>=e2:T B
Figure 2. Types for the Monadic Functions
Hence, from now is illegal to write (λx.x +x)inc because monads separate the pure
language from the effects through the type system. One must use the monadic functions
for sequencing the effects and write one of the following two programs depending on the
intended meaning:
inc >>=λx1.
inc >>=λx2.
return(x1+x2)
inc >>=λx1.
return(x1+x1)
In this way, the value of the global location is propagated and maintained by the
monad. Using the axioms below one can see that in the left expression, x1+x2reduces
to 1, and that in the right expression, x1+x1reduces to 0.
Regarding the axioms and inference rules, the monadic approach states that we should
maintain all the axioms for the pure language, add the generic axioms for monads (the monads
laws reflecting the monadic equations on Definition 3.1), and also add the specific axioms to
the particular effect in question, in this case get and inc.
The generic axioms in the definition of the Kleisli triple can be rewritten in terms of
the monadic functions, return and >>=, as:
1. Left identity: return a >>=f=f a
2. Right identity: m >>=return =m
5Note that the of the arguments of >>=and are the same with a small changing in the order.
254 RITA Volume 22 N´
umero 1 2015
QJava: A Monadic Java Library for Quantum Programming
3. Associativity: (m >>=f)>>=g=m >>= (λx f x >>=g)
The axioms for get and inc are given below:
hinc >>=λx.e, ni=he[n/x], n + 1i(inc)
hget >>=λx.e, ni=he[n/x], ni(get)
Note that we are explicitly showing what happens to the implicit global location (gl) by means
of the notation he, gli. To illustrate the reduction steps we can evaluate the expression with
the explicit global location initialized to 0:
hinc >>=λx1.
return(x1+x1),0i
=(by inc)hreturn(0 + 0),1i
=(by A)hreturn(0),1i
3.3 Monads in Haskell
Haskell [11] is a pure functional programming language. Independently of being
pure 6, Haskell has pure λ-calculus as its core language, which offers simple equational
reasoning mechanism and referential transparency. As a general purpose language, Haskell
offers computational effects via built-in monads for effects like input/output (the IO monad),
lazy lists (the List monad), and failure (the Maybe monad). The approach used in the lan-
guage design is the same as explained in section above.
Moreover, in Haskell, the user can also declare a new type to be a monad. That is, any
computational effect of interest can be added to the language and treated as a monad. To do
so, the user needs to define:
1. A Type constructor M: data M
2. Definition of a function return: return :: aM a
3. Definition of a function bind: >>= :: M a (aM b)M b
More precisely, one can consider the type constructor as a model for functions with
effects. The trivial mapping of values to computations is given in the definition of the func-
tion return. The function >>=must reflect how to sequence/transform effects. Note that a
6We are not going to discuss about pure and impure functional languages in this work, for a nice discussion see [8].
RITA Volume 22 N´
umero 1 2015 255
QJava: A Monadic Java Library for Quantum Programming
transformation of a computation M a in a computation M b must be defined in terms of a
function of type aM b.
Of course, to construct a correct monad, the return and >>=functions must work
together according to the three monadic axioms presented in section above.
Additionally, Haskell has a type class mechanism, which is a type system construct
that supports ad-hoc polymorphism. There is a standard Monad class that defines the names
and signatures of the two monad functions return and >>=. It is not strictly necessary to
make any user defined monad an instance of the Monad class, but it is a good idea if one
wants to overload the name of the functions and also use Haskell’ syntactic sugar for sequence
monadic functions called do-notation.
3.4 Quantum Monad
Quantum bits can be thought as a kind of computation effect. More specifically, quan-
tum bits can be modeled as type of a monad for non-determinism as first suggested by Mu
and Bird [15].
Non-deterministic programs can not be implemented by pure functions as they may
not produce the same value for the same input. However, non-deterministic programs can be
simulated by functions returning the set of all possible solutions. A Kleisli triple (T, η, )
that gives a category of non-deterministic programs is defined as follows: T A =P(A),
where P(A)is the power set over A. The function ηAis the singleton map a7→ {a}, and if
f:AT B and cT A, then f(c) = xcf(x).
Following this idea of using a monad for non-determinism, the second author of the
present paper modeled, in [5], pure quantum vector states and unitary transformations using
a Kleisli triple (T, η, )that gives a category of pure quantum programs defined as follows:
T A =P(A×C), where P(A×C)is the power set over A×C. The function ηAis the singleton
map a7→ {(a, 1)}, and if f:AT B and qT A, then f(q) = (a,c)qlet (a2, c2) =
f(a)in (a2, c c2).
The idea behind this monad is that this kind of non-deterministic monad builds the
quantum state space spanned by the basis set A, and has the meaning of linearity. As we
saw in Section 2.1 a quantum bit is a unit vector in a complex bi-dimensional vector space.
Considering the computational basis A={1,0}, we can write a qubit as:
|ψi=α|0i+β|1i.
where αand βare complex numbers given the respective probability amplitudes for the basic
states, |0iand |1i. So, the functor Tbuilds the quantum state space for the qubit. For instance,
for A={1,0}then P(A×C) = {{(0, α)},{(1, β)},{(0, α),(1, β )}} such that α, β C.
256 RITA Volume 22 N´
umero 1 2015
QJava: A Monadic Java Library for Quantum Programming
In this case, ηbuilds the basis vectors |0iby 07→ {(0,1)}and |1iby 07→ {(1,1)}. The
extension denotes the application of a linear function to an input vector.
Below we summarize the work presented in [5], as a quantum monad implementation
using the Haskell functional programming language. The Java quantum library discussed in
next section is based in this presentation.
type V ec a =aC
return a1a2=if (a1a2)then 1else 0
va>>=f=λb sum [vaaf a b]
The type constructor of the quantum monad named V ec, defines that a underlying
type ais mapped to a complex number. The return function builds trivial quantum vectors,
i.e, just the basis states and the bind specifies how to sequence computations.
Furthermore, vector spaces have additional properties abstracted in the monad with
the plus operation. This supports two additional operations called mzero and mplus which
provide a “zero” computation and an operation to “add” computations:
mzero :: V ec a
mzero =const 0
mplus :: V ec a V ec a V ec a
mplus v1v2a=v1a+v2a
For convenience, it is also possible to define various kinds of products over vectors:
the scalar product is an example:
($) :: KV ec a V ec a
pa $v=λ a pa v a
For instance, consider the basis vectors |0iand |1ican be programmed as over the
basis of Booleans as:
qF alse, q T rue :: V ec Bool
qF alse =return F alse
qT rue =return T rue
Superpositions can be generated using the mplus and scalar product, for instance:
qF T , qF mT :: V ecBool
qF T = (1/2 $(qF alse mplusq T rue)
qF mT = (1/2 $qF alse) ‘mplus‘ (1/2 $qT rue)
RITA Volume 22 N´
umero 1 2015 257
QJava: A Monadic Java Library for Quantum Programming
Given two base sets Aand Ba linear operator fA(Bis a function f:
ABC. Such operators can be represented as functions mapping values to vectors:
Lin a b =aV ec b For example, the quantum version of the boolean negation is:
qnot :: Lin Bool Bool
qnot a =return(6a)
and the hadamard operation can be programmed as:
hadamard :: Lin Bool Bool
hadamard F alse =qF T
hadamard T r ue =q F mT
4 Quantum Monad using Java Closures
Java is a general propose objected oriented programming language that enables secure
and high performance software development on multiple platforms. It is one of the most
widely used programming language in software development. Hence, based on the monadic
model for quantum programming discussed in section above we present in this section a
monadic Java library for quantum programming. We use the extension of Java called BGGA
Closure, that allow the manipulation of anonymous functions (closures) inside Java. We
exemplify the use of the library with an implementation of the Toffoli quantum circuit.
4.1 Anonymous Functions and Closures in Java
Anonymous functions are strongly related to λ-calculus and are (like λ-functions)
functions without a name. A closure is a kind of anonymous functions, it is a function with
an environment containing the bindings of its free variables. The word closure comes from
the term closed expression and refers to a lambda expression with its free variables closed by
a lexical environment [16]. Indeed, when we have a function that is nested inside another, if
it accesses a variable from it’s parent’s scope, we create a closure.
These concepts of anonymous functions and their closures are very common in the
functional programming community, where functions are first class citizens, allowing higher
order programming. The use of anonymous functions in an imperative set provides a way to
express abstractions that are currently quite awkward in non-functional languages. One can
use functions as arguments to other functions (or methods), or define functions dynamically.
Therefore, with the introduction of these concepts in the programming language Java
now we can create a whole new kind of data structures. There are two basic syntax for
anonymous function in BGGA:
258 RITA Volume 22 N´
umero 1 2015
QJava: A Monadic Java Library for Quantum Programming
( p a r a m e t e r s ) >expression}
( p a r a m e t e r s ) >{declarations ;}
The declaration of anonymous function also allows omission of modifiers, return type,
and, in some cases, the types of parameters. For instance:
1 . ( int x , int y ) >x+y
2 . ( x , y ) >xy
3 . ( ) >4 2
4 . ( S t r i n g s ) >S ys te m . o ut . p r i n t l n ( s )
5 . x >2x
6 . c >{int s = c . s i z e ( ) ; c . c l e a r ( ) ; r e t u r n s ; }
The first function takes two integers and returns their sum. The second one receives
two values and returns their difference. The third receives no value and returns the number
42. Fourth receives a string, prints its value in the console, and returns no value. The fifth
receives a number and returns its double. The sixth receives a collection, cleans it and returns
its previous size.
Note that type parameters may be stated explicitly (ex: 1/4) or implicitly inferred
(ex 2/5/6) but not mixed in a single lambda expression. The function body can be a block
(surrounded by braces, ex 6) or an expression (ex 1-5). A function can have a return value or
nothing (void). If the body is just an expression, it can return a value (ex 1/2/3/5) or nothing
(ex 4) without explicitly declare the return statement (return). Parentheses may be omitted
for a single inferred type parameter (ex 5/6).
In Java, to assign a dynamic function to a variable and then use it to call the method
created or just to pass as an argument to another function, the use of functional interfaces7is
required. Such functional interfaces can be seen as any interface that has exactly one abstract
method declared.
Some functional interfaces are already distributed inside the Java platform. Among
many, here are some examples:
p u b l i c i n t e r f a c e R u n n a b l e {
v o i d r u n ( ) ; }
p u b l i c i n t e r f a c e Callable<V>{
V c a l l ( ) throws E x c e p t i o n ; }
p u b l i c i n t e r f a c e ActionListener {
v o i d a c t i o n P e r f o r m e d ( A ct i o n E v e n t e ) ; }
As an example, consider a definition of an anonymous function representing a dy-
namic function that prints any sentence on the console screen. Then, lets assign it to a local
7This is also a new concept included in Java 8
RITA Volume 22 N´
umero 1 2015 259
QJava: A Monadic Java Library for Quantum Programming
variable. In this specific case we can use the already defined functional interface Runnable.
Doing so, means that the invocation of the dynamic function must be done trhough the call-
ing of the method run, the only abstract method of the functional interface. The code below,
prints the sentence ”Hello World” in the console.
R un na b le c = ( ) >{Sy s t e m . o u t . p r i n t l n ( H e l l o W o r l d ) ; };
c . r un ( ) ;
4.2 The Library QJava
In this section we present our library called QJava: Quantum monad in Java. We use
Java Development Kit (JDK version 8), which has support to anonymous functions.
Considering the original definition of the quantum monad presented in Section 3.4, we
can build the following relation between the monad definition and object-oriented program-
ming: the type constructor corresponding to the declaration of the monadic type is a class
declaration; the return function takes the role of the constructor method; and the binding
operation contains the logic necessary to execute its registered callbacks.
Then, in order to implement a monad as a closure in Java we need to define a functional
interface:
p u b l i c i n t e r f a c e V<A>{
Co mp le x i n v o k e ( A b o o l ) ;
}
In this way, we can use a closure to represent an quantum state as a function that maps
a basis type Ato a complex number, i.e., to its probability amplitude. The generic type A
provides a generic implementation, making it possible to create a quantum state of Nqubits.
For example, a quantum state with two qubits would have the type tuple<boolean,boolean>,
a quantum state with three qubits would have the type tuple<boolean,boolean,boolean>, and
so on.
Hence, quantum vectors are represented by the following class Vec:
p u b l i c c l a s s V ec <A>{
V<A>v e c ;
A t y p e ;
Vec (A t y p e , V<A>vec ) {
t h i s . t y p e = ty p e ;
t h i s . vec = vec ;
}
Co mp le x i n v o k e ( A b o o l ) {
r e t u r n v e c . i n v o k e ( b o o l ) ;
260 RITA Volume 22 N´
umero 1 2015
QJava: A Monadic Java Library for Quantum Programming
}
}
Additionally, the class Vec has mzero,mplus and scalar product:
p ub l i c s t a t i c <A>Vec<A>m ze ro ( A t i p o ) {
r e t u r n new Vec<A>( t i p o , (A bo o l ) >Co mp le x . ZERO) ;
}
p ub l i c s t a t i c <A>Vec<A>m pl us ( V ec<A>v a , Ve c<A>v b ) {
r e t u r n new Vec<A>(v a , ( A b o o l ) >
va . i n v o k e ( b o o l ) . p l u s ( v b . i n v o k e ( b o o l ) ) ) ;
}
p ub l i c s t a t i c <A>Vec<A>s c a l a r ( C omp lex c , Vec<A>va ) {
r e t u r n new Vec<A>(v a , ( A b o o l ) >
va . i n v o k e ( b o o l ) . t i m e s ( c ) ) ;
}
The monadic functions, return and bind, are implemented in a class called QMonad.
The return function is a method, qreturn, that receives a value of type Aand construct a
basic vector state over the basis A, as in the return function explained in section 3.4.
p ub l i c s t a t i c <A>Vec<A>q r e t u r n (A ba s e ) {
r e t u r n new Vec<A>(b a s e , ( A b o o l ) >{
r e t u r n b o o l . e q u a l s ( b a se ) ? C om pl ex . ONE: C om pl ex . ZERO;
}) ;
}
The returned type is a functional object representing a quantum state vector. More specifi-
cally, the method receives a type Aand returns a closure, i.e., an instance of the class Vec<A>.
The closure maps all values of the type Ato zero except the value passed as its argument.
Thus, we can create the basis quantum states |0ie|1i, as:
Vec<Boolean>v z er o = QMonad . q r e t u r n ( false ) ;
Vec<Boolean>v on e = QMonad . q r e t u r n ( t r u e ) ;
Linear operators mapping quantum state vectors to new quantum state vectors, are
implemented in Java exactly as described in Section 3.4, i.e, as a function mapping values to
vectors. Then, we define a class Lin<A,B> with the following functional interface:
p u b l i c i n t e r f a c e L<A , B>{
Vec<B>i n v o k e ( A b o o l ) ;
}
This closure takes a value of type Aand returns a Vec<B>.
Now we are ready to explain the implementation of the monadic bind function, which
models the application of a linear operation to a quantum state vector, i.e, how to sequence
RITA Volume 22 N´
umero 1 2015 261
QJava: A Monadic Java Library for Quantum Programming
quantum operations. Then, it is implemented as a method that receives a vector va, of type
Vec<A>, and a linear operation f, of type Lin<A,B>. The method returns a new vector of
type Vec<B> representing the application of the transformation fin va:
p ub l i c s t a t i c <A, B>Vec<B>b i n d ( V ec<A>va , Li n <A, B>f ) {
r e t u r n new Vec<B>( f . i n v o k e ( v a . g e t B a s e ( ) ) , ( B b ) >{
Co mp l ex som a = Co mp le x . ZERO;
for (A a : v a . g e t B a s e s ( ) )
so ma = so ma . p l u s ( v a . i n v o k e ( a ) . t i m e s ( f . i n v o k e ( a ) . i n v o k e ( b ) ) ) ;
r e t u r n soma ;
}) ;
}
Basically, the behaviour of the method bind must capture a matrix vector multipli-
cation. To do so, we need to know all possible values of the basis type A, which is done by
the method getBases(). The methods plus and times are the sum and multiplication
of complex numbers. Therefore, the bind method receives a vector state of type Vec<A>, a
linear operation of type Lin<A,B> and binds the computation to a new vector Vec<B>.
To help the user of the library we define a universal set of quantum operations provided
within the class Lin<A,B>. For instance, there are included the static methods qnot(),
qphase() and qhadamard(). Furthermore, there is a general function controlled(),
which returns the controlled operation of a given linear transformation. We can create the op-
eration cnot for example, with the function call controlled(qnot()).
As a simple example consider the application of the transformation qnot in the quan-
tum state |0i:
Vec<Boolean>v z er o = QMoand . q r e t u r n ( false ) ;
Vec<Boolean>r e s u l t = QMonad . b i n d ( v z e r o , q n o t ( ) ) ;
In this case, the quantum state vresult will be the vector that represent the applica-
tion of the operation qnot in vzero. Since vzero represent the vector |0i, the result of this
operation will be quantum state |1i.
4.2.1 A Toffoli Gate with QJava As an example of how to use the library QJava we
present in this section an implementation of the quantum algorithm for the Toffoli circuit.
The Toffoli gate is a universal logic gate, also known as the “controlled-controlled-not”
gate. It works over a three qubits quantum state as shown in Figure 3.
The gates labeled H,V,VT and NOT represent respectively the quantum operations Hadamard,
Phase,Adjoint Phase and NOT. The bullets connecting another wire represent controlled op-
erations.
This quantum circuit can be implemented in the library QJava as the following method:
262 RITA Volume 22 N´
umero 1 2015
QJava: A Monadic Java Library for Quantum Programming
VH HVVT
Not Not
Figure 3. Circuit for the Toffoli gate.
p ub l i c s t a t i c Lin<Tuple , Tuple>Toffoli () {
/ / Au x l i a r y t y p e c o n s t r u c t o r s
B oo l e a n b o o l = Bo o l e a n . TRUE;
T up l e b o o l b o o l = Tu p le . c r e a t e T u p l e ( B o ol e a n . TRUE, B oo l e an . TRUE ) ;
T up l e b o o l bo o l b o o l = T u pl e . c r e a t e T u p l e (
Bo o l e a n . TRUE, B o o l e a n . TRUE, B o o l e a n . TRUE) ;
/ / I n s t a n c e t h e r e q u i r e d q ua nt um g a t e s
Lin<Tuple , Tuple>cn o t = L in . c o n t r o l l e d ( Li n . q n o t ( ) ) ;
Lin<Tuple , Tuple>cp h as e = L i n . c o n t r o l l e d ( Li n . q p ha s e ( ) ) ;
Lin<Tuple , Tuple>ca p ha s e = L in . c o n t r o l l e d ( L in . a d j o i n t ( L i n . q ph a se ( ) ) ) ;
Lin<Boolean , Boolean>hadama rd = Li n . q hadama rd ( ) ;
r e t u r n new Lin<Tuple , Tuple>( b o o l bo o l bo o l , ( T up l e t0 m0 b0 ) >{
r e t u r n QMonad . b i n d ( h a d a ma r d . i n v o k e ( b 0 ) ,
new Lin<Bo ol ea n , T up le >( b o o l , ( B o o le a n b 1 ) >{
r e t u r n QMonad . b i n d ( c p h a s e . i n v o k e ( mo , b 1 ) ,
new Lin<Tuple , Tuple>( b o o l b o o l , ( T u p le m1b 2 ) >{
r e t u r n QMonad . b i n d ( c n o t . i n v o k e ( t 0 , m1 ) ,
new Lin<Tuple , Tuple>( b o o l b o o l , ( T u p l e t1m 2 ) >{
r e t u r n QMonad . b i n d ( c a p h a s e . i n v o k e ( m2 , b2 ) ,
new Lin<Tuple , Tuple>( b o o l b o o l , ( T u p l e m 3b3 ) >{
r e t u r n QMonad . b i n d ( c n o t . i n v o k e ( t 1 , m3 ) ,
new Lin<Tuple , Tuple>( b o o l b o o l , ( T u p l e t2m 4 ) >{
r e t u r n QMonad . b i n d ( c p h a s e . i n v o k e ( t 2 , b 3 ) ,
new Lin<Tuple , Tuple>( b o o l b o o l , ( T u p l e t 3 b 4 ) >{
r e t u r n QMonad . b i n d ( h a d a ma r d . i n v o k e ( b 4 ) ,
new Lin<Bo ol ea n , T up le >( b o o l , ( B o o le a n b 5 ) >{
r e t u r n QMonad . q r e t u r n ( T u p le . c r e a t e T u p l e ( t 3 , m4 , b 5 ) ) ;
}));
}));
}));
}));
}) )
}));
}));
}) ;
}
We use the names top, middle and bottom to refer the qubits in the quantum circuit. The
RITA Volume 22 N´
umero 1 2015 263
QJava: A Monadic Java Library for Quantum Programming
input state is written as t0m0b0, i.e., a tuple of <boolean, boolean, boolean>. We im-
plement a class Tuple to deal with tuples in Java with default constructor to create tuples
and projections. To simplify the reading of the code above we omit the call of the methods
createTuple and the projection getTthValue. One can observe in the code how to use
the monadic bind to program quantum algorithms with many qubits and how to apply specific
operations to specific qubits. The advantage of using the monad is that it allows the program-
mer to work in parts of the global quantum state. For instance, the code starts applying the
hadamard gate only to the third qubit of the system through hadamard.invoke(b0).
The new value for the bottom qubit is now called b1 and then is passed around through the
bind to be used in the operation cphase.invoke(m0,b1). The computation follows with
this same reasoning until the last hadamard application and then returns.
Finally, this method representing the Toffoli circuit can be applied to a three qubit state
as |111i:
Vec<Tuple>t e s t = QMonad . q r e t u r n ( T u p le . c r e a t e T u p l e ( t r u e ,true ,t r u e ));
t e s t = QMonad . b i nd ( t e s t , T o f f o l i ( ) ) ;
5 Conclusion
In this paper we have presented a monadic library for quantum programming in Java
using closures called QJava: Quantum monad in Java. The library can be used to program
pure quantum algorithms in Java. We believe that one of the main advantages of the library is
that it allows to elegantly express quantum algorithms in Java giving to the programmer the
ability to work in parts of the global quantum state.
Besides this technical contribution the present paper has a detailed explanation of
monads in the context of functional programming languages and its application to quantum
computing. Monads and its variants are a very important concept in the semantic of program-
ming languages. We hope this work can help readers also interested in monads in general.
As future work we intend to implement a parser to use the a syntax like the Haskell
do-notation for monads. The investigation and implementation of a type system and then the
definition of a domain specific language (DSL) for quantum programming inside Java is also
a future work.
Due to well know problems of performance on simulation of quantum algorithms
in classical computers, an implementation of the library using threads and General-Purpose
Computation on Graphics Hardware (GPGPU) is also an idea of future work.
264 RITA Volume 22 N´
umero 1 2015
QJava: A Monadic Java Library for Quantum Programming
References
[1] R. Feynman and P. W. Shor, “Simulating physics with computers, SIAM Journal on
Computing, vol. 26, pp. 1484–1509, 1982.
[2] D. Deutsch, “Quantum theory, the church-turing principle, and the universal quantum
computer,” in Proceedings of the Royal Society of London, vol. 47, pp. 97–117, 1985.
[3] P. W. Shor, “Algorithms for quantum computation: discrete logarithms and factoring,
in In Proc. IEEE Symposium on Foundations of Computer Science, pp. 124–134, 1994.
[4] C. Pomerance, “A tale of two sieves,Notices Amer. Math. Soc., vol. 43, pp. 1473–1485,
1996.
[5] J. K. Vizzotto, T. Altenkirch, and A. Sabry, “Structuring quantum effects: Superopera-
tors as arrows,Journal of Mathematical Structures in Computer Science: special issue
in quantum programming languages, vol. 16, pp. 453–468, 2006.
[6] E. Moggi, “Computational lambda-calculus and monads,” in Proceedings of the Fourth
Annual Symposium on Logic in computer science, pp. 14–23, IEEE Press, 1989.
[7] M. A. Nielsen and I. L. Chuang, Quantum Computation and Quantum Information.
Cambridge University Press, 2000.
[8] A. Sabry, “What is a purely functional language?, Journal of Functional Programming,
vol. 8, pp. 1–22, 1998.
[9] P. Wadler, “Monads for functional programming,” in Advanced Functional Pro-
gramming, First International Spring School on Advanced Functional Programming
Techniques-Tutorial Text, (London, UK, UK), pp. 24–52, Springer-Verlag, 1995.
[10] P. Wadler, “The essence of functional programming,” in Proceedings of the 19th ACM
SIGPLAN-SIGACT symposium on Principles of programming languages, POPL ’92,
(New York, NY, USA), pp. 1–14, ACM, 1992.
[11] P. Hudak, S. Peyton Jones, and P. Wadler (editors), “Report on the Programming Lan-
guage Haskell, A Non-strict Purely Functional Language (Version 1.2),” ACM SIG-
PLAN Notices, vol. 27, May 1992.
[12] E. Moggi, “An Abstract View of Programming Languages,” tech. rep., Edinburgh Uni-
versity, 1989.
[13] M. Barr and C. Wells, Toposes, Triples and Theories. Springer Verlag, 1895.
RITA Volume 22 N´
umero 1 2015 265
QJava: A Monadic Java Library for Quantum Programming
[14] M. Felleisen, M. Felleisen, R. Hieb, and R. Hieb, “The revised report on the syntac-
tic theories of sequential control and state,” Theoretical Computer Science, vol. 103,
pp. 235–271, 1992.
[15] S.-C. Mu and R. S. Bird, “Functional quantum programming,” in APLAS, pp. 75–88,
2001.
[16] J. P. Landin, “The mechanical evaluation of expressions, in The Computer Journal,
1964.
266 RITA Volume 22 N´
umero 1 2015
... Nevertheless, understanding how quantum computers and quantum algorithms work is essential for the development of new quantum algorithms that outperform their classical counterparts. The simulation of quantum algorithms by classical computers is a subject studied by many authors [2] [3][4] [5][6] [7] as a tool for understanding important concepts of quantum computing, focusing on at least two main goals: (i) providing an educational tool to learn how the algorithms would behave under certain conditions, and (ii) studying certain aspects of the algorithms where analytic results are very hard to obtain. The problem of simulating quantum mechanics in classical computers usually have exponential complexity, both in time and space. ...
Article
Full-text available
"Some half dozen persons have written technically on combinatory logic, and most of these, including ourselves, have published something erroneous. Since some of our fellow sinners are among the most careful and competent logicians on the contemporary scene, we regard this as evidence that the subject is refractory. Thus fullness of exposition is necessory for accurary; and excessive condensation would be false economy here, even more than it is ordinarily."
Article
Full-text available
We show that the model of quantum computation based on density matrices and superoperators can be decomposed in a pure classical (functional) part and an effectful part modeling probabilities and measurement. The effectful part can be modeled using a generalization of monads called arrows. We express the resulting executable model of quantum computing in the programming language Haskell using its special syntax for arrow computations. The embedding in Haskell is however not perfect: a faithful model of quantum computing requires type capabilities which are not directly expressible in Haskell.
Article
Factorization of large numbers amounts to trial and retrial. The quadratic and algebraic number sieves produce sophisticated trials which are efficient for numbers up to 150 digits.
Article
The syntactic theories of control and state are conservative extensions of the λυ-calculus for equational reasoning about imperative programming facilities in higher-order languages. Unlike the simple λυ-calculus, the extended theories are mixtures of equivalence relations and compatible congruence relations on the term language, which significantly complicates the reasoning process. In this paper we develop fully compatible equational theories of the same imperative higher-order programming languages. The new theories subsume the original calculi of control and state and satisfy the usual Church–Rosser and Standardization Theorems. With the new calculi, equational reasoning about imperative programs becomes as simple as reasoning about functional programs.
Article
This paper is a contribution to the “theory” of the activity of using computers. It shows how some forms of expression used in current programming languages can be modelled in Church's λ-notation, and then describes a way of “interpreting” such expressions. This suggests a method, of analyzing the things computer users write, that applies to many different problem orientations and to different phases of the activity of using a computer. Also a technique is introduced by which the various composite information structures involved can be formally characterized in their essentials, without commitment to specific written or other representations.
Conference Paper
This paper explores the use monads to structure functional programs. No prior knowledge of monads or category theory is required.
Conference Paper
The λ-calculus is considered a useful mathematical tool in the study of programming languages. However, if one uses βη-conversion to prove equivalence of programs, then a gross simplification is introduced. The author gives a calculus based on a categorical semantics for computations, which provides a correct basis for proving equivalence of programs, independent from any specific computational model