QIO-1.2: The Quantum IO Monad is a library for defining quantum computations in Haskell

Safe HaskellNone



This module defines the Syntax of the Quantum IO Monad, which is an embedded language for writing quantum computations.



type RR = DoubleSource

For Real numbers, we simply use the built in Double type

type CC = Complex RRSource

For Complex numbers, we use the built in Complex numbers, over our Real number type (i.e. Double)

amp :: CC -> RRSource

The amplitude of a complex number is the magnitude squared.

newtype Qbit Source

The type of Qubits in QIO are simply integer references.


Qbit Int 


Enum Qbit 
Eq Qbit 
Num Qbit 
Ord Qbit 
Show Qbit

We can display a qubit reference

Heap HeapMap

A HeapMap is an instance of the Heap type class, where the Heap functions can make use of the underlying Map functions.

Qdata Bool Qbit

The lowest-level instance of Qdata is the relation between Booleans and Qubits.

type Rotation = (Bool, Bool) -> CCSource

A rotation is in essence a two-by-two complex valued matrix

data U Source

The underlying data type of a U unitary operation


Rot Qbit Rotation U 
Swap Qbit Qbit U 
Cond Qbit (Bool -> U) U 
Ulet Bool (Qbit -> U) U 


Show U

We can display a representation of a unitary

Monoid U

The type U forms a Monoid

data QIO a Source

The underlying data type of a QIO Computation


QReturn a 
MkQbit Bool (Qbit -> QIO a) 
ApplyU U (QIO a) 
Meas Qbit (Bool -> QIO a) 


Monad QIO

The QIO type forms a Monad

rot :: Qbit -> Rotation -> USource

Apply the given rotation to the given qubit

swap :: Qbit -> Qbit -> USource

Swap the state of the two given qubits

cond :: Qbit -> (Bool -> U) -> USource

Apply the conditional unitary, depending on the value of the given qubit

ulet :: Bool -> (Qbit -> U) -> USource

Introduce an Ancilla qubit in the given state, for use in the sub-unitary

urev :: U -> USource

Returns the inverse (or reverse) of the given unitary operation

unot :: Qbit -> USource

Apply a not rotation to the given qubit

uhad :: Qbit -> USource

Apply a hadamard rotation to the given qubit

uphase :: Qbit -> RR -> USource

Apply a phase rotation (of the given angle) to the given qubit

mkQbit :: Bool -> QIO QbitSource

Initialise a qubit in the given state (adding it to the overall quantum state)

applyU :: U -> QIO ()Source

Apply the given unitary operation to the current quantum state

measQbit :: Qbit -> QIO BoolSource

Measure the given qubit, and return the measurement outcome (note that this operation may affect the overall quantum state, as a measurement is destructive)

rid :: RotationSource

The identity rotation

rnot :: RotationSource

The not rotation

rhad :: RotationSource

The hadamard rotation

rphase :: RR -> RotationSource

The phase rotation

rrev :: Rotation -> RotationSource

Returns the inverse (or reverse) of the given rotation

show' :: U -> Int -> Int -> StringSource

A helper function for the show instance of U

spaces :: Int -> StringSource

A helper function that returns a string of 4x spaces.