-
Notifications
You must be signed in to change notification settings - Fork 0
/
Qubits_package.m
84 lines (63 loc) · 1.76 KB
/
Qubits_package.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
(* ::Package:: *)
(*Parameters*)
PI=N[\[Pi],12];
EPS=1.*^-12;
(*Pauli operators*)
\[Sigma]I={{1.,0.},{0.,1.}};
\[Sigma]X={{0.,1.},{1.,0.}};
\[Sigma]Y={{0.,-1.I},{1.I,0.}};
\[Sigma]Z={{1.,0.},{0.,-1.}};
funPX[i_]:=Module[{PX},(
If[i==0,PX=\[Sigma]X,PX=\[Sigma]I];
Do[(
If[i==k,PX=KroneckerProduct[\[Sigma]X,PX],PX=KroneckerProduct[\[Sigma]I,PX]];
),{k,1,Nq-1}];
Return[PX]
)]
funPY[i_]:=Module[{PY},(
If[i==0,PY=\[Sigma]Y,PY=\[Sigma]I];
Do[(
If[i==k,PY=KroneckerProduct[\[Sigma]Y,PY],PY=KroneckerProduct[\[Sigma]I,PY]];
),{k,1,Nq-1}];
Return[PY]
)]
funPZ[i_]:=Module[{PZ},(
If[i==0,PZ=\[Sigma]Z,PZ=\[Sigma]I];
Do[(
If[i==k,PZ=KroneckerProduct[\[Sigma]Z,PZ],PZ=KroneckerProduct[\[Sigma]I,PZ]];
),{k,1,Nq-1}];
Return[PZ]
)]
funPO[ps_]:=Module[{q,p,PO},(
q=0;
p=StringTake[ps,{q+1}];
If[StringMatchQ[p,"I"],PO=\[Sigma]I];
If[StringMatchQ[p,"X"],PO=\[Sigma]X];
If[StringMatchQ[p,"Y"],PO=\[Sigma]Y];
If[StringMatchQ[p,"Z"],PO=\[Sigma]Z];
Do[(
p=StringTake[ps,{q+1}];
If[StringMatchQ[p,"I"],PO=KroneckerProduct[\[Sigma]I,PO]];
If[StringMatchQ[p,"X"],PO=KroneckerProduct[\[Sigma]X,PO]];
If[StringMatchQ[p,"Y"],PO=KroneckerProduct[\[Sigma]Y,PO]];
If[StringMatchQ[p,"Z"],PO=KroneckerProduct[\[Sigma]Z,PO]];
),{q,1,Nq-1}];
Return[PO]
)]
(*Hamiltonian*)
funHamiltonianQubit[Model_]:=Module[{Ham},(
Ham=0.;
Do[(
Ham=Ham+Model[[i,1]]*funPO[Model[[i,2]]];
),{i,1,Length[Model]}];
Return[Ham]
)]
(*Spectrum*)
funSpectrum[Ham_]:=Module[{vals,vecs},(
{vals,vecs}=Eigensystem[Ham];
vals=Re[vals];
{vals,vecs}=Transpose@SortBy[Transpose[{vals,vecs}],First];
(*Print[Total[Total[Abs[Transpose[vecs].DiagonalMatrix[vals].Conjugate[vecs]-Ham]]]];*)
(*Print[Total[Total[Abs[DiagonalMatrix[vals]-Conjugate[vecs].Ham.Transpose[vecs]]]]];*)
Return[{vals,vecs}]
)]