xiaozqh
[srcbox.git] / quantium.cpp
blob1efeb58973e296314c7d601133ad854f06a597b3
1 #include <iostream>
2 #include <string>
3 #include <math.h>
4 #include <fstream>
5 #define PI 3.141592653
7 class Complex{
8 public:
9 Complex(); //Default constructor~Complex();//Default destructor.
10 void Set(double new_real,double new_imaginary); //Set data members.
11 double Real(); //Return the real part.
12 double Imaginary();//Return the imaginary part.
13 Complex operator+(Complex);//Overloaded+operator
14 Complex operator*(Complex);//Overloaded*operator
15 Complex operator=(Complex);//Overloaded=operator
16 int operator==(Complex);//Overloaded==operator
17 private:
18 double real;
19 double imaginary;
23 class Qubit{
24 public:
25 Qubit();//Default constructor.
26 ~Qubit();//Default destructor.
27 int Measure();//Returns zero_state=0 or one_state=1 in accordanc
28 //with the probabilities of zero_state and one_state
29 void Dump();//Prints our zero_state,and one_state,without
30 //disturbing anything,this operation has no physic
31 //realization,it is only for information and debugin
32 //It should never be used in an algorithm for
33 //information.
34 void SetState(Complex zero_prob,Complex one_prob);
35 //Takes two complex numbers and sets the states t
36 //those values.
37 void SetAverage();//Sets the state to 2^(1/2)zero_state,2^(1/2)
38 //one_state.No imaginary/phase component.
39 double MCC(int state);//Multiply the zero or one state by its complex
40 //conjugate,and return the value.This valu
41 //will always be a real number,with no imagi
42 //component.
43 private:
44 Complex zero_state;
45 //The probability of finding the Qubit in the zero or all imarinary
46 //state.I currently use only the real portion.
47 Complex one_state;
48 //The probability of finding the Qubit in the one or all real state.//I currently use only the real portion.
49 //|zero_state|^2+|one_state|^2 should always be 1.
50 //This notation means z_s*ComplexConjugate(z_s)+o_s*
51 //ComplexConjugate(o_s)=1.
54 class QuReg{
55 public:
56 QuReg(int size);
57 QuReg();//Default Constructor
58 QuReg(const QuReg&);//Copy constructor
59 ~QuReg();//Default destructor.
60 int DecMeasure();//Measures our quantum register,and returns the
61 void Dump(int verbose);
62 void SetState(Complex new_state[]);
63 void SetAverage(int number);
64 void Norm();
65 Complex GetProb(int state);
66 //Return the size of the register.
67 int Size();
68 private:
69 int reg_size;
70 Complex*State;
73 int GetQ(int n)
75 int power=8;//265 is the smallest q ever is.
76 while(pow(2,power)<pow(n,2))
78 power=power+1;
80 return((int)pow(2,power));
85 int modexp(int x,int a,int n)
87 int value=1;int tmp;
88 tmp=x%n;
89 while(a>0)
91 if(a&1)
93 value=(value*tmp)%n;
95 tmp=tmp*tmp%n;
96 a=a>>1;
98 return value;
102 int RegSize(int a){
103 int size=0;
104 while(a!=0){
105 a=a>>1;
106 size++;
108 return(size);
113 int Max(int a,int b)
115 return(a>b?a:b);
120 int GCD(int a,int b)
122 int d;
123 if(b!=0)
125 while(a%b!=0)
127 d=a%b;
128 a=b;
129 b=d;
132 else
134 return-1;
136 return(b);
140 void DFT(QuReg*reg,int q)
142 Complex*init=new Complex[q];
143 Complex tmpcomp;
144 tmpcomp.Set(0,0);
145 int count=0;
146 double tmpreal=0;
147 double tmpimag=0;
148 double tmpprob=0;
149 for(int a=0;a<q;a++){
150 if((pow(reg->GetProb(a).Real(),2)+pow(reg->GetProb(a).Imaginary(),2))>pow(10,-14))
152 for(int c=0;c<q;c++)
154 tmpcomp.Set(pow(q,-.5)*cos(2*PI*a*c/q),pow(q,-.5)*sin(2*PI*a*c/q));
155 init[c]=init[c]+(reg->GetProb(a)*tmpcomp);
158 count++;
159 if(count==100)
163 cout<<"Making progress in Fourier transform,"<<100*((double)a/(double)(q-1))
164 <<"%done!"<<endl<<flush;
165 count=0;
168 reg->SetState(init);
169 reg->Norm();
170 delete[]init;