Skip to content

Commit

Permalink
ex
Browse files Browse the repository at this point in the history
  • Loading branch information
melsman committed Nov 23, 2024
1 parent 3a929a1 commit 8918552
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
36 changes: 34 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Quantum Circuits in Standard ML
# Quantum Circuits in Standard ML [![CI](https://github.com/diku-dk/atpl-sml-quantum/workflows/CI/badge.svg)](https://github.com/diku-dk/atpl-sml-quantum/actions)

This repository demonstrates the design and development of a simple quantum
circuit simulation framework in Standard ML. The source code is structured to
Expand All @@ -7,7 +7,8 @@ semantics and an evaluation framework for the circuits.

## Dependencies

The framework build with both the MLKit and MLton compilers and uses
The framework builds with both the [MLKit](https://github.com/melsman/mlkit) and
[MLton](http://mlton.org/) compilers and uses
[`smlpkg`](https://github.com/diku-dk/smlpkg) to fetch relevant libraries,
including [`sml-complex`](https://github.com/diku-dk/sml-complex) and
[`sml-matrix`](https://github.com/diku-dk/sml-matrix), libraries for easily
Expand All @@ -16,12 +17,43 @@ working with complex numbers and matrices in Standard ML.
On macos, you may install `smlpkg` and `mlkit` using `brew install smlpkg
mlkit`, assuming you have Homebrew installed.

On Linux, you may download binaries from the respective repositories.

## Compiling the Source Code

To compile the source code and run the tests, just execute `make test` in the
source directory. The default is to use `mlkit` as a compiler. If you must use
MLton, execute `MLCOMP=mlton make test` instead.

## Example Run

Here is an example run:
```
$ cd src
$ mlkit quantum_ex1.mlb
...
$ ./run
Circuit for d = I ** H oo CX oo Z ** Z oo CX oo I ** H:
.---.
----------*---| Z |---*----------
| '---' |
| |
.---. .-+-. .---. .-+-. .---.
--| H |-| X |-| Z |-| X |-| H |--
'---' '---' '---' '---' '---'
Semantics of d:
0 1 0 0
1 0 0 0
0 0 0 1
0 0 1 0
Result distribution when evaluating d on |01> :
|00> : 1
|01> : 0
|10> : 0
|11> : 0
...
```

## Relevant Literature

Phillip Kaye, Raymond Laflamme, and Michele Mosca. [An Introduction to Quantum
Expand Down
4 changes: 4 additions & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
run
MLB
*~
*.exe
5 changes: 4 additions & 1 deletion src/diagram.sml
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,8 @@ struct
mapi (padi w) b
end
fun seq (a:t,b:t) : t = mapi2 (fn (i,a,b) => a ^ sep i ^ b) (a,b)
fun toString (a:t) : string = String.concatWith "\n" a
fun toString (a:t) : string =
let val a = mapi (padi (width a + 4)) a
in String.concatWith "\n" a
end
end
4 changes: 4 additions & 0 deletions src/quantum_ex1.mlb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
local $(SML_LIB)/basis/basis.mlb
quantum.mlb
in quantum_ex1.sml
end
15 changes: 15 additions & 0 deletions src/quantum_ex1.sml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

open Circuit Diagram Semantics
infix 3 oo
infix 4 **

fun run c k =
(print ("Circuit for d = " ^ pp c ^ ":\n");
print (draw c ^ "\n");
print ("Semantics of d:\n" ^ pp_mat(sem c) ^ "\n");
print ("Result distribution when evaluating d on " ^ pp_ket k ^ " :\n");
print (pp_dist(measure_dist(eval c (init k))) ^ "\n\n"))

val () = run (I ** H oo C X oo Z ** Z oo C X oo I ** H) (ket[0,1])

val () = run (I ** X) (ket[0,1])

0 comments on commit 8918552

Please sign in to comment.