The Quantum IO Monad
QIO
Alexander S. Greenasg@cs.nott.ac.uk
Foundations of Programming Group,
School of Computer Science & IT,
University of Nottingham

The Quantum IO Monad – p.1/19

Introduction• We would like to model Quantum Computations.

The Quantum IO Monad – p.2/19

Introduction• We would like to model Quantum Computations.
• The QIO Monad, can be thought of as a register ofQubits that plugs into your classical computer.

The Quantum IO Monad – p.2/19

Introduction• We would like to model Quantum Computations.
• The QIO Monad, can be thought of as a register ofQubits that plugs into your classical computer.
• It provides a framework for constructing quantum
computations...

The Quantum IO Monad – p.2/19

Introduction• We would like to model Quantum Computations.
• The QIO Monad, can be thought of as a register ofQubits that plugs into your classical computer.
• It provides a framework for constructing quantum
computations...
• ... and simulates the running of these computations.

The Quantum IO Monad – p.2/19

Haskell and Monads• Haskell is a pure functional programming language, soany computations that may involve side effects makeuse of Monads.

The Quantum IO Monad – p.3/19

Haskell and Monads• Haskell is a pure functional programming language, so
any computations that may involve side effects makeuse of Monads.
• Monads are defined by a return function, and a bindfunction denoted ( >>= )The Quantum IO Monad – p.3/19

Haskell and Monads• Haskell is a pure functional programming language, so
any computations that may involve side effects makeuse of Monads.
• Monads are defined by a return function, and a bindfunction denoted ( >>= )•class Monad m where(>>=) :: m a → (a → m b) → m b
return :: a → m a

The Quantum IO Monad – p.3/19

Haskell and Monads• Haskell is a pure functional programming language, so
any computations that may involve side effects makeuse of Monads.
• Monads are defined by a return function, and a bindfunction denoted ( >>= )•class Monad m where(>>=) :: m a → (a → m b) → m b
return :: a → m a• Haskell provides the do notation to make monadic
programming easier.

The Quantum IO Monad – p.3/19’do’ notation• IO in Haskell takes place in the IO Monad.

The Quantum IO Monad – p.4/19

’do’ notation• IO in Haskell takes place in the IO Monad.
• For example, echoing a character to the screen
getChar :: IO Char
putChar :: Char → IO ()The Quantum IO Monad – p.4/19

’do’ notation• IO in Haskell takes place in the IO Monad.
• For example, echoing a character to the screen
getChar :: IO Char
putChar :: Char → IO ()•echo :: IO ()echo = getChar >>= (λc → putChar c) > >echo

The Quantum IO Monad – p.4/19

’do’ notation• IO in Haskell takes place in the IO Monad.
• For example, echoing a character to the screen
getChar :: IO Char
putChar :: Char → IO ()•echo :: IO ()echo = getChar >>= (λc → putChar c) > >echo• or in do notation

The Quantum IO Monad – p.4/19

’do’ notation• IO in Haskell takes place in the IO Monad.
• For example, echoing a character to the screen
getChar :: IO Char
putChar :: Char → IO ()•echo :: IO ()echo = getChar >>= (λc → putChar c) > >echo• or in do notationecho = do c ← getCharputChar c•echo

The Quantum IO Monad – p.4/19

The QIO Monad• The QIO Monad has been designed so that Quantum
computations can be defined within Haskell.

The Quantum IO Monad – p.5/19

The QIO Monad• The QIO Monad has been designed so that Quantum
computations can be defined within Haskell.
• The do notation provided by Haskell is very useful forthis purpose.

The Quantum IO Monad – p.5/19

The QIO Monad• The QIO Monad has been designed so that Quantum
computations can be defined within Haskell.
• The do notation provided by Haskell is very useful forthis purpose.

•trueBit :: QIO Bool
trueBit = do qb ← mkQbit Truex ← measQbit qb
return x

The Quantum IO Monad – p.5/19

API• What can we do in the QIO Monad?

The Quantum IO Monad – p.6/19

API• What can we do in the QIO Monad?
•mkQbit :: Bool → QIO Qbit

The Quantum IO Monad – p.6/19

API• What can we do in the QIO Monad?
•mkQbit :: Bool → QIO Qbit

• When initialising a qubit the user must define which of
the base states, True or False (|0i or |1i), to initialise it
into.

The Quantum IO Monad – p.6/19

API• What can we do in the QIO Monad?
•

mkQbit :: Bool → QIO Qbit

• When initialising a qubit the user must define which of
the base states, True or False (|0i or |1i), to initialise it
into.
•measQbit :: Qbit → QIO BoolThe Quantum IO Monad – p.6/19

API• What can we do in the QIO Monad?
•mkQbit :: Bool → QIO Qbit

• When initialising a qubit the user must define which of
the base states, True or False (|0i or |1i), to initialise itinto.
•measQbit :: Qbit → QIO Bool

• The measurement of a qubit always results in aboolean value.

The Quantum IO Monad – p.6/19

API• What can we do in the QIO Monad?
•mkQbit :: Bool → QIO Qbit

• When initialising a qubit the user must define which of
the base states, True or False (|0i or |1i), to initialise itinto.
•measQbit :: Qbit → QIO Bool

• The measurement of a qubit always results in aboolean value.
• Wht else can be done with these qubits?

The Quantum IO Monad – p.6/19

Unitaries.
• It is possible to construct unitary operators, and apply
them to the relevent qubits.

The Quantum IO Monad – p.7/19

Unitaries.
• It is possible to construct unitary operators, and apply
them to the relevent qubits.
•applyU :: U → QIO ()The Quantum IO Monad – p.7/19

Unitaries.
• It is possible to construct unitary operators, and apply
them to the relevent qubits.
•applyU :: U → QIO ()• There are 5 unitary constructors that are available:

The Quantum IO Monad – p.7/19

Unitaries.
• It is possible to construct unitary operators, and apply
them to the relevent qubits.
•applyU :: U → QIO ()• There are 5 unitary constructors that are available:•unot :: Qbit → U
which will rotate the given qubit by 180o as in the Not
rotation.

The Quantum IO Monad – p.7/19

Unitaries.
• It is possible to construct unitary operators, and applythem to the relevent qubits.
•applyU :: U → QIO ()• There are 5 unitary constructors that are available:
unot :: Qbit → U
which will rotate the given qubit by 180o as in the Notrotation.
 0 1•
1 0•

The Quantum IO Monad – p.7/19

Unitaries..
•uhad :: Qbit → U
which will rotate the given qubit by 90o as in theHadamard rotation.

The Quantum IO Monad – p.8/19

Unitaries..
uhad :: Qbit → U
which will rotate the given qubit by 90o as in theHadamard rotation.

1 11√•2 1 −1•

The Quantum IO Monad – p.8/19

Unitaries..
uhad :: Qbit → U
which will rotate the given qubit by 90o as in theHadamard rotation.

1 11
√
•
2 1 −1••uphase :: Qbit → Float → U
which will rotate the given qubit by the given phasechange (φ).

The Quantum IO Monad – p.8/19

Unitaries..
uhad :: Qbit → U
which will rotate the given qubit by 90o as in theHadamard rotation.


1 11
√
•
2 1 −1•uphase :: Qbit → Float → U
which will rotate the given qubit by the given phasechange (φ).

1 0•0 e2πiφ•

The Quantum IO Monad – p.8/19

Unitaries...
•swap :: Qbit → Qbit → U
which simply swaps the two given qubits.

The Quantum IO Monad – p.9/19

Unitaries...
•swap :: Qbit → Qbit → U
which simply swaps the two given qubits.

•

cond :: Qbit → (Bool → U ) → U
which given a control qubit, will conditionally do thecorresponding unitary given by the function.
(The control qubit must not be effected by theunitaries)

The Quantum IO Monad – p.9/19

Unitaries...
•swap :: Qbit → Qbit → U
which simply swaps the two given qubits.

•cond :: Qbit → (Bool → U ) → U
which given a control qubit, will conditionally do thecorresponding unitary given by the function.
(The control qubit must not be effected by theunitaries)• It is this conditional operation that can be used toentangle qubits.

The Quantum IO Monad – p.9/19

Unitaries...
•swap :: Qbit → Qbit → U
which simply swaps the two given qubits.

•cond :: Qbit → (Bool → U ) → U
which given a control qubit, will conditionally do thecorresponding unitary given by the function.
(The control qubit must not be effected by theunitaries)• It is this conditional operation that can be used toentangle qubits.
• The U datatype of unitaries, also forms a Monoid
meaning there is an append operation for combining
uniatries sequentially.

The Quantum IO Monad – p.9/19

Running Quantum Computations?
• Along with creating quantum computations, the QIO
Monad also provides two ways of evaluating them.

The Quantum IO Monad – p.10/19

Running Quantum Computations?
• Along with creating quantum computations, the QIO
Monad also provides two ways of evaluating them.
•run :: QIO a → IO a

The Quantum IO Monad – p.10/19

Running Quantum Computations?
• Along with creating quantum computations, the QIO
Monad also provides two ways of evaluating them.
•run :: QIO a → IO a

• Running a quantum computation returns aprobabilistic result for each measurement.

The Quantum IO Monad – p.10/19

Running Quantum Computations?
• Along with creating quantum computations, the QIO
Monad also provides two ways of evaluating them.
•run :: QIO a → IO a

• Running a quantum computation returns aprobabilistic result for each measurement.
•sim :: QIO a → Prob a

The Quantum IO Monad – p.10/19

Running Quantum Computations?
• Along with creating quantum computations, the QIO
Monad also provides two ways of evaluating them.
•run :: QIO a → IO a

• Running a quantum computation returns aprobabilistic result for each measurement.
•sim :: QIO a → Prob a• Simulating a quantum computation returns aprobability distribution of all the possible
measurement outcomes.

The Quantum IO Monad – p.10/19

Running Quantum Computations?
• Along with creating quantum computations, the QIO
Monad also provides two ways of evaluating them.
•run :: QIO a → IO a

• Running a quantum computation returns aprobabilistic result for each measurement.
•sim :: QIO a → Prob a• Simulating a quantum computation returns aprobability distribution of all the possible
measurement outcomes.
• We would also like to be able to display the internal
state of the system at any time, possibly by showing
the complex amplitudes for each base state.

The Quantum IO Monad – p.10/19

Computations.
qPlus :: QIO Qbit
qPlus = do qa ← mkQbit False
applyU (uhad qa)
return qa
randBit :: QIO Bool
randBit = do qa ← qPlus
x ← measQbit qa
return x

The Quantum IO Monad – p.11/19

Computations..
share :: Qbit → QIO Qbit
share qa = do qb ← mkQbit False
applyU (cond qa (λa → if a then (unot qb)
else mempty))
return qb
bell :: QIO (Qbit, Qbit )bell = do qa ← qPlus
qb ← share qareturn (qa, qb)

The Quantum IO Monad – p.12/19

Computations..
test bell :: QIO (Bool , Bool )test bell = do qb ← bellb ← measQ qb
return b

The Quantum IO Monad – p.13/19

Teleportation.
alice :: Qbit → Qbit → QIO (Bool , Bool )alice aq bsq = do applyU (cond aq(λa → if a then (unot bsq)
else mempty))
applyU (uhad aq)
cd ← measQ (aq, bsq)
return cd

The Quantum IO Monad – p.14/19

Teleportation..
uZ :: Qbit → U
uZ qb = (uphase qb 0.5)bobsU :: (Bool , Bool ) → Qbit → U
bobsU (False, False) qb = mempty
bobsU (False, True) qb = (unot qb)
bobsU (True, False) qb = (uZ qb)
bobsU (True, True) qb = ((unot qb) ‘mappend ‘ (uZ qb))
bob :: Qbit → (Bool , Bool ) → QIO Qbit
bob bsq cd = do applyU (bobsU cd bsq)
return bsq

The Quantum IO Monad – p.15/19

Teleportation...
teleportation :: Qbit → QIO Qbit
teleportation iq = do (bsq1 , bsq2 ) ← bell
cd ← alice iq bsq1tq ← bob bsq2 cd
return tq

The Quantum IO Monad – p.16/19

Qdata.
• There is a symmetry between initialising a qubit, andmeasuring a qubit.

The Quantum IO Monad – p.17/19

Qdata.
• There is a symmetry between initialising a qubit, andmeasuring a qubit.
• Larger quantum data structures can be defined using
qubits, in the same way classical data structures aredefined using bits.

The Quantum IO Monad – p.17/19

Qdata.
• There is a symmetry between initialising a qubit, andmeasuring a qubit.
• Larger quantum data structures can be defined using
qubits, in the same way classical data structures aredefined using bits.
• We have defined a class of quantum data types, Qdata
For which an mkQ initialisation function and a measQ
measurement function must be defined, between thequantum datatype and its classical counter-part.

The Quantum IO Monad – p.17/19

Qdata.
• There is a symmetry between initialising a qubit, andmeasuring a qubit.
• Larger quantum data structures can be defined using
qubits, in the same way classical data structures aredefined using bits.
• We have defined a class of quantum data types, Qdata
For which an mkQ initialisation function and a measQ
measurement function must be defined, between thequantum datatype and its classical counter-part.
•instance Qdata Bool Qbit where
mkQ = mkQbit
measQ = measQbit

The Quantum IO Monad – p.17/19

Qdata..
instance (Qdata a qa, Qdata b qb)⇒ Qdata (a, b) (qa, qb) where
mkQ (a, b) =do qa ← mkQ aqb ← mkQ b
return (qa, qb)
measQ (qa, qb) =do a ← measQ qab ← measQ qb
return (a, b)

The Quantum IO Monad – p.18/19

Further Work• We are going to implement some of the famous
quantum algorithms, such as Shor’s quantum
factorisation algorithm.

The Quantum IO Monad – p.19/19

Further Work• We are going to implement some of the famous
quantum algorithms, such as Shor’s quantum
factorisation algorithm.
• We are going to use the QIO Monad to start reasoning
about quantum computation in general.

The Quantum IO Monad – p.19/19

Further Work• We are going to implement some of the famousquantum algorithms, such as Shor’s quantum
factorisation algorithm.
• We are going to use the QIO Monad to start reasoning
about quantum computation in general.
• We are going to model other forms of quantum
computer within the QIO Monad, such as theMeasurment based model of quantum computations.

The Quantum IO Monad – p.19/19

Further Work• We are going to implement some of the famous
quantum algorithms, such as Shor’s quantum
factorisation algorithm.
• We are going to use the QIO Monad to start reasoning
about quantum computation in general.
• We are going to model other forms of quantum
computer within the QIO Monad, such as theMeasurment based model of quantum computations.
• Thank you all for listening!

The Quantum IO Monad – p.19/19