Skip to content

Commit

Permalink
docs: Add example of magic state distillation (#40)
Browse files Browse the repository at this point in the history
Co-authored-by: Alan Lawrence <[email protected]>
  • Loading branch information
croyzor and acl-cqc authored Oct 24, 2024
1 parent 462a480 commit c6c6f83
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ The [`brat/examples`](brat/examples) directory contains some examples of BRAT pr
For example:
- Quantum teleportation: [teleportation.brat](brat/examples/teleportation.brat)
- Quantum Fourier transform: [teleportation.brat](brat/examples/teleportation.brat)
- Magic State Distillation: [magic-state-distillation.brat](brat/examples/magic-state-distillation.brat)
- Simple Repeat-Until-Success: [rus.brat](brat/examples/rus.brat)
- Ising Hamiltonian: [ising.brat](brat/examples/ising.brat)

Expand Down
12 changes: 12 additions & 0 deletions brat/examples/lib/functional.brat
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- TODO: Fill this with holes once we can guess them
map(X :: $, Y :: $, n :: #, f :: { X -o Y }) -> { Vec(X, n) -o Vec(Y, n) }
map(_, _, _, _) = { [] => [] }
map(X, Y, succ(n), f) = { cons(x,xs) => cons(f(x), map(X, Y, n, f)(xs)) }

fold(X :: $
,{ X, X -o X }
,{ -o X }
,n :: #
) -> { Vec(X, n) -o X }
fold(X, f, x, 0) = { [] => x() }
fold(X, f, x, succ(n)) = { cons(y, ys) => f(y, fold(X, f, x, n)(ys)) }
6 changes: 6 additions & 0 deletions brat/examples/lib/math.brat
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pi :: Float
pi = 3.1415926

-- Things that we need to find a way to support
ext "TODO" arccos :: { Float -> Float }
ext "TODO_sqrt" sqrt :: { Float -> Float }
71 changes: 71 additions & 0 deletions brat/examples/magic-state-distillation.brat
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
open import lib.functional
open import lib.kernel
open import lib.math

or(Bit, Bit) -o Bit
or(true, _) = true
or(_, y) = y

any(n :: #) -> { Vec(Bit, n) -o Bit }
any(n) = fold(Bit, or, { => false }, n)

-- Decoder for the [[5,3,1]] "perfect" error code
-- TODO: Add vectorisation and fanin/out
decoder(Vec(Qubit, 5)) -o Vec(Qubit, 5)
decoder = {
[/\];
CZ, CZ, |;
|, CZ, CZ;
swap, |, swap;
|, swap, ..;
|, |, CZ, |;
|, swap, ..;
swap, |, swap;
H, H, H, H, H;
[\/]
}

phi :: Float
phi = arccos(1.0 / sqrt(3.0))

prepare_noisy_1(Money) -o Qubit
prepare_noisy_1 = {
fresh;
Ry(phi);
Rz(pi / 4.0)
}

measure_all(n :: #) -> { Vec(Qubit, n) -o Vec(Bit, n), Vec(Money, n) }
measure_all(0) = { [] => [], [] }
measure_all(succ(n)) = { q ,- qs =>
let m, b = measure(q) in
let bs, ms = measure_all(n)(qs) in
b ,- bs, m ,- ms
}

measure_syndrome(n :: #)
-> { Vec(Qubit, n)
-o Bit, Vec(Money, n)
}
measure_syndrome(n) = {
measure_all(n);
any(n), |
}

prepare_noisy(n :: #) -> { Vec(Money, n) -o Vec(Qubit, n) }
prepare_noisy(n) = map(Money, Qubit, n, prepare_noisy_1)

redistill_if_bad(Qubit, Bit, Vec(Money, 4))
-o Qubit, Vec(Money, 4)
redistill_if_bad(q, false, ms) = q, ms
redistill_if_bad(q, true, ms) = let m, _ = measure(q) in
distill(cons(m, ms))

distill(Vec(Money, 5)) -o Qubit, Vec(Money, 4)
distill = {
prepare_noisy(5);
decoder;
uncons(4);
|, measure_syndrome(4);
redistill_if_bad
}
14 changes: 6 additions & 8 deletions brat/examples/rus.brat
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
open import lib.kernel


rus(Money, Qubit) -o Money, Qubit
rus = {
rus(timeout :: Nat) -> { Money, Qubit -o Money, Qubit }
rus(0) = { .. }
rus(succ(n)) = {
fresh, ..;
(H;T), ..;
CX;
H, ..;
CX;
(T;H), ..;
measure, ..;
rus'
( m, false, q => m, q
| m, true, q => rus(n)(m, q)
)
}

rus'(Money, Bit, Qubit) -o Money, Qubit
rus'(m, false, q) = m, q -- success
rus'(m, true, q) = rus(m, q) -- try again
3 changes: 2 additions & 1 deletion brat/test/Test/Compile/Hugr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ nonCompilingExamples = (expectedCheckingFails ++ expectedParsingFails ++
-- signatures causes `kindCheck` to call `abstract`, creating "Selector"
-- nodes, which we don't attempt to compile because we want to get rid of them
,"vec-pats"
-- Victims of #389
-- Victims of #13
,"arith"
,"bell"
,"cqcconf"
,"imports"
,"ising"
,"klet"
,"magic-state-distillation" -- also makes selectors
,"rus"
,"teleportation"
,"vlup_covering"
Expand Down

0 comments on commit c6c6f83

Please sign in to comment.