Advertisement
Guest User

sucks

a guest
Oct 14th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.44 KB | None | 0 0
  1. open System.Numerics
  2. open MathNet.Numerics
  3. open MathNet.Numerics.LinearAlgebra
  4.  
  5. type Qregister = Qregister of Matrix<Complex>
  6. let qreg n =
  7.     let ket = matrix [[Complex (1.0, 0.0)]
  8.                       [Complex (0.0, 0.0)]]
  9.     let rec kronPow = function
  10.     | 1 -> ket
  11.     | n when n > 0 -> ket.KroneckerProduct (kronPow (n - 1))
  12.     | _ -> failwith "degree must be greater than 0"
  13.  
  14.     Qregister (kronPow n)
  15.  
  16. type Gate =
  17.     Gate of Matrix<Complex>
  18.         static member (*) (Gate a, Gate b) = Gate (a.KroneckerProduct b)
  19.         static member (><) (Gate a, Qregister s) = Qregister (a * s)
  20.         static member (+) (a, b) = Binding [a; b]
  21. and Binding =
  22.     Binding of List<Gate>
  23.         static member (+) (Binding a, Binding b) = Binding (List.append a b)
  24.  
  25.         static member (?-) (state, binding) =
  26.             match binding with
  27.             | Binding [] -> state
  28.             | Binding (head::tail) -> (head >< state) ?- (Binding tail)
  29.  
  30. // lol
  31.  
  32. let I = matrix [[ Complex (1.0, 0.0); Complex (0.0, 0.0) ]
  33.                 [ Complex (0.0, 0.0); Complex (1.0, 0.0) ]] |> Gate
  34. let H = (Complex (1.0/sqrt 2.0, 0.0)) *
  35.         matrix [[ Complex (1.0, 0.0); Complex (1.0,  0.0)]
  36.                 [ Complex (1.0, 0.0); Complex (-1.0, 0.0)]] |> Gate
  37.  
  38. let PauliX = matrix [[Complex (0.0, 0.0); Complex (0.0, -1.0)]
  39.                      [Complex (0.0, 1.0); Complex (0.0,  0.0)]] |> Gate
  40.  
  41. (qreg 2) ?- (I * H + H * I)
  42. |> printfn "%A"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement