module Quipper102 where import System.Random import Quipper import Quipper.Libraries.Simulation ex_or :: Qubit -> Qubit -> Circ (Qubit, Qubit, Qubit) ex_or x y = do z <- qinit False z <- qnot z `controlled` x z <- qnot z `controlled` y return (x, y, z) exOrCirc = print_generic Preview ex_or qubit qubit g :: Qubit -> Qubit -> Qubit -> Circ (Qubit, Qubit, Qubit, Qubit, Qubit, Qubit, Qubit) g x y z = do (x, y, q1) <- ex_or x y (x, z, q2) <- ex_or x z (y, z, q3) <- ex_or y z (x, q3, q4)<- ex_or x q3 return (x, y, z, q1, q2, q3, q4) gCirc = print_generic Preview g qubit qubit qubit -- ccz using Clifford+T ccz :: Qubit -> Qubit -> Qubit -> Circ (Qubit, Qubit, Qubit) ccz x y z = do with_computed (g x y z) b where b (x, y, z, q1, q2, q3, q4) = do x <- gate_T x y <- gate_T y z <- gate_T z q1 <- gate_T_inv q1 q2 <- gate_T_inv q2 q3 <- gate_T_inv q3 q4 <- gate_T q4 return (x, y, z) cczCirc = print_generic Preview ccz qubit qubit qubit -- from ccz to ccx toffoli :: Qubit -> Qubit -> Qubit -> Circ (Qubit, Qubit, Qubit) toffoli x y z = do z <- hadamard z (x, y, z) <- ccz x y z z <- hadamard z return (x,y,z) toffoliCirc = print_generic Preview toffoli qubit qubit qubit testToffoli :: IO () testToffoli = do let x = sim_generic (0.0 :: Double) toffoli True True True putStrLn $ show x -- Adder via toffoli bit_add :: Qubit -> Qubit -> Qubit -> Qubit -> Circ (Qubit, Qubit, Qubit, Qubit) bit_add x y c z = do (z, x) <- controlled_not z x (z, y) <- controlled_not z y (z, c) <- controlled_not z c return (x, y, c, z) bit_addCirc = print_generic Preview bit_add qubit qubit qubit qubit majority :: Qubit -> Qubit -> Qubit -> Qubit -> Circ Qubit majority x y carryin carryout = do carryout <- qnot carryout `controlled` (x, y) carryout <- qnot carryout `controlled` (y, carryin) carryout <- qnot carryout `controlled` (x, carryin) return carryout majorityCirc = print_generic Preview majority qubit qubit qubit qubit singleBitAdd :: Qubit -> Qubit -> Qubit -> Qubit -> Qubit -> Circ (Qubit, Qubit) singleBitAdd x y carryin s carryout = do (x, y, carryin, s) <- bit_add x y carryin s carryout <- majority x y carryin carryout return (s, carryout) singleBitCirc = print_generic Preview singleBitAdd qubit qubit qubit qubit qubit twoBitAdd :: Qubit -> Qubit -> Qubit -> Qubit -> Qubit -> Qubit -> Qubit -> Circ (Qubit, Qubit, Qubit) twoBitAdd x1 y1 x2 y2 s1 s2 carryout = do label (x1,y1, x2, y2, s1, s2, carryout) ("x1", "y1", "x2", "y2", "s1", "s2", "carryout") carryin <- qinit False carryout1 <- qinit False label (carryin, carryout1) ("carryin", "carryout1") (s1, carryout1) <- singleBitAdd x1 y1 carryin s1 carryout1 (s2, carryout) <- singleBitAdd x2 y2 carryout1 s2 carryout return (s1,s2, carryout) twoBitCirc = print_generic Preview twoBitAdd qubit qubit qubit qubit qubit qubit qubit bitcopy :: [Qubit] -> [Qubit] -> Circ([Qubit], [Qubit]) bitcopy xs ys = undefined copyCirc = print_generic Preview bitcopy [qubit, qubit, qubit] [qubit, qubit, qubit] -- We assume the length of the input lists is the same. adder :: [Qubit] -> [Qubit] -> [Qubit] -> Qubit -> Qubit -> Circ([Qubit], Qubit) adder xs ys output carryin carryout = undefined adderCirc = print_generic Preview adder [qubit] [qubit] [qubit] qubit qubit testAdder :: IO () testAdder = do g <- newStdGen let x = sim_generic (0.0 :: Double) adder [True, False, False] [True, True, True] [False, False, False] False False putStrLn $ show x