(* Mathematica Notebook of Standard Quantum Gates
      This is a notebook of the standard quantum gates as listed below. This is open source and available for anyone who wants to use it under  the GPL
Copyright (c) Thirring Institute for Applied Gravitational Research.  As we are a small privately funded research instutute, it would be appreciated if we are sited by anyone using
this though there is no obligation to do so!
 *)

1. Identity Gate 
Id[|0⟩]=|0⟩
Id[|1⟩]=|1⟩

Id[Ket[0]]:=Ket[0]

Id[Ket[1]]:=Ket[1]

Id[a_+b_]:=Id[a]+Id[b]

Id[-Ket[a_]]:=-Ket[a]

Id[a_ Ket[b_]]:=a Ket[b]

Id[c_*(a_+b_)]:=c*Id[a+b]

2. X(Not) Gate
X[|0⟩]=|1⟩
X[|1⟩]=|0⟩

X[Ket[0]]:=Ket[1]

X[Ket[1]]:=Ket[0]

X[a_+b_]:=X[a]+X[b]

X[-Ket[a_]]:=-X[Ket[a]]

X[a_ Ket[b_]]:=a X[Ket[b]]

X[c_*(a_+b_)]:=c*X[a+b]

3. Pauli Y Gate
Y[|0⟩]=i*|1⟩
Y[|1⟩]=-i*|0⟩

Y[Ket[0]]:=I Ket[1]

Y[Ket[1]]:=- I Ket[0]

Y[a_+b_]:=Y[a]+Y[b]

Y[-Ket[a_]]:=-Y[Ket[a]]

Y[a_ Ket[b_]]:=a Y[Ket[b]]

Y[c_*(a_+b_)]:=c*Y[a+b]

4. Z Gate
Z[|0⟩]=|0⟩
Z[|1⟩]=-i*|1⟩

Z[Ket[0]]:=Ket[0]

Z[Ket[1]]:=-I Ket[1]

Z[a_+b_]:=Z[a]+Z[b]

Z[-Ket[a_]]:=-Z[Ket[a]]

Z[a_ Ket[b_]]:=a Z[Ket[b]]

Z[c_*(a_+b_)]:=c*Z[a+b]

5. S Gate
S[|0⟩]=|0⟩
S[|1⟩]=i*|1⟩

S[Ket[0]]:=Ket[0]

S[Ket[1]]:=I Ket[1]

S[a_+b_]:=S[a]+S[b]

S[-Ket[a_]]:=-S[Ket[a]]

S[a_ Ket[b_]]:=a S[Ket[b]]

S[c_*(a_+b_)]:=c*S[a+b]

6. T Gate
T[|0⟩]=|0⟩
T[|1⟩=(e^((iπ)/4))(Ket[1])

T[Ket[0]]:=Ket[0]

T[Ket[1]]:=(E^(I (π/4)))Ket[1]
T[a_+b_]:=T[a]+T[b]

T[-Ket[a_]]:=-T[Ket[a]]

T[a_ Ket[b_]]:=a T[Ket[b]]

T[c_*(a_+b_)]:=c*T[a+b]

7.R phase gate
(Subscript[R, z])(θ)(Ket[0])=(Ket[0])
   (Subscript[R, z])(θ)(Ket[1])=(e^(iθ))(Ket[1])

(Subscript[R, z])[Ket[0]]:=Ket[0]

(Subscript[R, z])[Ket[1]]:=Exp[I θ] Ket[1]

(Subscript[R, z])[a_+b_]:=(Subscript[R, z])[a]+(Subscript[R, z])[b]

(Subscript[R, z])[-Ket[a_]]:=-(Subscript[R, z])[Ket[a]]

(Subscript[R, z])[a_ Ket[b_]]:=a (Subscript[R, z])[Ket[b]]

(Subscript[R, z])[c_*(a_+b_)]:=c*(Subscript[R, z])[a+b]

8. H (Hadamard) Gate
H[(Ket[0])]:=(((Ket[0])+(Ket[1]))/(Sqrt[2]))
H[(Ket[1])]:=(((Ket[0])-(Ket[1]))/(Sqrt[2]))

H[Ket[0]]:=(1/(Sqrt[2]))(Ket[0]+Ket[1])

H[Ket[1]]:=(1/(Sqrt[2]))(Ket[0]-Ket[1])

H[a_+b_]:=H[a]+H[b]

H[-Ket[a_]]:=-H[Ket[a]]

H[a_ Ket[b_]]:=a H[Ket[b]]

H[c_*(a_+b_)]:=c*H[a+b]

1. Various Qubit Structures
({{(Ket[-]):=(((Ket[0])-(Ket[1]))/(Sqrt[2]))}, { }, {(Ket[+]):=(((Ket[0])+(Ket[1]))/(Sqrt[2]))}})

(Ket[i]):=(((Ket[0])+i (Ket[1]))/(Sqrt[2]))

(Ket[-i]):=(((Ket[0])-i (Ket[1]))/(Sqrt[2]))

Ket["+"]:=(1/(Sqrt[2]))(Ket[0]+Ket[1])

Ket["-"]:=(1/(Sqrt[2]))(Ket[0]-Ket[1])

Ket["-I"]:=(1/(Sqrt[2]))(Ket[0]-I Ket[1])

Ket["I"]:=(1/(Sqrt[2]))(Ket[0]+I Ket[1])