Created
June 12, 2019 01:54
-
-
Save olivia-fl/37f14ff4e3160c66639d5dbb6fdc6661 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Quipper | |
| import Quipper.Monad | |
| import Control.Monad | |
| import QuipperLib.Simulation | |
| import QuipperLib.QFT | |
| import Data.Bits | |
| import Debug.Trace | |
| diffusion :: [Qubit] -> Circ () | |
| diffusion searchSpace = do | |
| mapM_ hadamard searchSpace | |
| mapM_ qnot searchSpace | |
| hadamard $ head searchSpace | |
| qnot_at (head searchSpace) `controlled` tail searchSpace | |
| hadamard $ head searchSpace | |
| mapM_ qnot searchSpace | |
| mapM_ hadamard searchSpace | |
| grover :: Int -> [Qubit] -> Qubit -> Circ () -> Circ () | |
| grover numIters searchSpace result oracle = do | |
| replicateM_ numIters $ do | |
| oracle | |
| diffusion searchSpace | |
| return () | |
| xnor :: Qubit -> Qubit -> Circ Qubit | |
| xnor a b = do | |
| qnot a `controlled` b | |
| qnot a | |
| oracleXnor :: [Qubit] -> [Qubit] -> Qubit -> Circ () | |
| oracleXnor as bs result = do | |
| mapM (uncurry xnor) $ zip as bs | |
| qnot result `controlled` as | |
| -- mirror | |
| mapM_ (uncurry xnor) $ zip as bs | |
| oracleToffoli :: [Qubit] -> [Qubit] -> Qubit -> Circ () | |
| oracleToffoli as bs result = do | |
| qnot result `controlled` as ++ bs | |
| return () | |
| oracle = oracleXnor | |
| groverCircuit :: Circ [Bit] | |
| groverCircuit = do | |
| as <- qinit [False, False] | |
| bs <- qinit [False, False] | |
| result <- qinit True | |
| mapM_ hadamard (as ++ bs) | |
| hadamard result | |
| grover 4 (as ++ bs) result (oracle as bs result) | |
| hadamard result | |
| measure (as ++ bs) | |
| countingCircuit :: Circ [Bit] | |
| countingCircuit = do | |
| as <- qinit [False, False] | |
| bs <- qinit [False, False] | |
| result <- qinit True | |
| rs <- qinit $ replicate 6 False | |
| mapM_ hadamard rs | |
| mapM_ hadamard (as ++ bs) | |
| hadamard result | |
| forM_ (zip rs [0..]) $ \(r, n) -> do | |
| grover (2 ^ n) (as ++ bs) result (oracle as bs result) `controlled` r | |
| (reverse_generic qft_little_endian rs) rs | |
| named_gate_at "trace" rs | |
| measure rs | |
| fromBits :: [Bool] -> Int | |
| fromBits bits = foldl (.|.) 0 $ map (Data.Bits.bit . fst) $ | |
| filter snd $ zip [0..] $ reverse bits | |
| countResult :: [Bool] -> Int -> Float | |
| countResult result searchSpace = mn * fromIntegral searchSpace | |
| where | |
| mn = 1 - (sin $ theta / 2) ^ 2 | |
| n = length result | |
| theta = 2 * pi * (fromIntegral $ fromBits result) / (fromIntegral $ 2^n) | |
| mainTrace = do | |
| trace <- run_generic_trace_io (undefined :: Double) countingCircuit | |
| let Vector trace' = head trace | |
| forM_ trace' $ \(state, prob) -> putStrLn $ (concatMap (show . fromEnum) state) ++ ": " ++ show prob | |
| mainSim = do | |
| result <- run_generic_io (undefined :: Double) countingCircuit | |
| print $ concatMap (show . fromEnum) result | |
| print $ countResult result (2 ^ 4) | |
| mainSimGrover = do | |
| result <- run_generic_io (undefined :: Double) groverCircuit | |
| print $ concatMap (show . fromEnum) result | |
| mainPrint = print_simple PDF countingCircuit | |
| main :: IO () | |
| main = mainSim |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment