Advertisement
Guest User

сукаблядьговнохуйсосиебанатахахахахашоманшоман

a guest
Oct 15th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 3.25 KB | None | 0 0
  1. open System
  2. open System.Numerics
  3. open MathNet.Numerics
  4. open MathNet.Numerics.LinearAlgebra
  5.  
  6. type Qregister =
  7.     Qregister of Matrix<Complex>
  8.         member this.measure () =
  9.             let asArray =
  10.                 match this with
  11.                 | (Qregister x) -> x.ToColumnMajorArray ()
  12.  
  13.             let probs =
  14.                 Array.map (fun x -> x ** 2.0) asArray
  15.                 |> List.ofArray
  16.                 |> List.map (fun x -> x.Real)
  17.  
  18.             let diceRoll =
  19.                 let r = new Random () in
  20.                     r.NextDouble ()
  21.            
  22.             let makeCumulativeProbs xs (a : float) =
  23.                 let support = function
  24.                     | [] -> []
  25.                     | (head::tail) -> (head+a)::head::tail
  26.            
  27.                 support xs
  28.            
  29.             let selectelem (ready, last) (a) =
  30.                 if ready then (ready, last)
  31.                 else
  32.                     if diceRoll < a then
  33.                         (true, a)
  34.                     else
  35.                         (false, a)
  36.            
  37.             let cumulativeProbs =
  38.                 probs
  39.                 |> List.fold makeCumulativeProbs [0.0]
  40.                 |> List.rev
  41.                 |> (fun x -> x.Tail)
  42.            
  43.             cumulativeProbs
  44.             |> List.fold selectelem (false, 0.0)
  45.             |> (fun (_, x) -> List.findIndex ((=) x) cumulativeProbs)
  46.  
  47.        
  48. let qreg n =
  49.     let ket = matrix [[Complex (1.0, 0.0)]
  50.                       [Complex (0.0, 0.0)]]
  51.    
  52.     let rec support = function
  53.     | 1 -> ket
  54.     | n when n > 0 -> ket.KroneckerProduct (support (n-1))
  55.     | _ -> failwith "degree must be greater than 0"
  56.    
  57.     Qregister (support n)
  58.  
  59. type Gate =
  60.     Gate of Matrix<Complex>
  61.         static member (*) (Gate a, Gate b) = Gate (a.KroneckerProduct b)
  62.         static member (+) (a, b) = Binding [a; b]
  63.        
  64.         static member (+) (a, b) = a + (Binding [b])
  65.         static member (+) (a, b) = (Binding [a]) + b
  66.  
  67.         static member apply (Gate a) (Qregister s) = Qregister (a * s)
  68.         static member (?-) (state : Qregister, gate : Gate) =
  69.             Gate.apply gate state
  70.  
  71. and Binding =
  72.     Binding of List<Gate>
  73.         static member (+) (Binding a, Binding b) = Binding (List.append a b)
  74.  
  75.         static member (?-) (state, binding) =
  76.             match binding with
  77.             | Binding [] -> state
  78.             | Binding (head::tail) ->
  79.                 (Gate.apply head state) ?- (Binding tail)
  80.  
  81. // lol
  82.  
  83. let I = matrix [[ Complex (1.0, 0.0); Complex (0.0, 0.0) ]
  84.                 [ Complex (0.0, 0.0); Complex (1.0, 0.0) ]] |> Gate
  85.  
  86. let H = (Complex (1.0/sqrt 2.0, 0.0)) *
  87.         matrix [[ Complex (1.0, 0.0); Complex (1.0,  0.0)]
  88.                 [ Complex (1.0, 0.0); Complex (-1.0, 0.0)]] |> Gate
  89.  
  90. let X = matrix [[Complex (0.0, 0.0); Complex (0.0, -1.0)]
  91.                 [Complex (0.0, 1.0); Complex (0.0,  0.0)]] |> Gate
  92.  
  93. let CNOT =
  94.     let unit = Complex (1.0, 0.0)
  95.     let zero = Complex (0.0, 0.0)
  96.     matrix [[unit; zero; zero; zero]
  97.             [zero; unit; zero; zero]
  98.             [zero; zero; zero; unit]
  99.             [zero; zero; unit; zero]] |> Gate
  100.  
  101. (qreg 2) ?- (H * H + CNOT + I * H)
  102. |> (fun x -> x.measure ())
  103. |> printfn "%A"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement