Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#7 create functions #8

Merged
merged 1 commit into from
Jul 25, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ module Utils
, Matrix
, complexVector
, complexMatrix
, (<*>)
, (<+>)
, entangle
, (|>)
) where

import Data.Complex
import Data.List(transpose)
import Prelude hiding ((<*>),(<+>))


-- Basic new types needed for defining qubits
type Vector a=[a]
Expand All @@ -20,6 +26,26 @@ complexMatrix::Real a=>Matrix a->Matrix(Complex Double)
complexMatrix=map complexVector


(<*>)::Num a=>a->Matrix a->Matrix a
c <*> m= map (map (c *)) m

--tensor product
(<+>)::Num a=>Matrix a->Matrix a->Matrix a
m1 <+> m2=concatMap collateRows $ groups n [c<*>m2|c<-concat m1]
where
n=length $ head m1
groups::Int->[a]->[[a]]
groups i s
| null s=[]
| otherwise=let (h,t)=splitAt i s in h:groups i t
collateRows::[Matrix a]->Matrix a
collateRows=map concat . transpose

entangle::Num a=>Vector a->Vector a->Vector a
entangle q1 q2=[qs1*qs2 | qs1<-q1,qs2<-q2]

apply::Num a=>Matrix a->Vector a->Vector a
apply m v=map(sum . zipWith(*) v) m

(|>)::Num a=>Vector a->Matrix a->Vector a
(|>)=flip apply