-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQuantumGate.java
More file actions
178 lines (164 loc) · 8.06 KB
/
Copy pathQuantumGate.java
File metadata and controls
178 lines (164 loc) · 8.06 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
class QuantumGate extends Matrix {
private BinaryFormOfState binaryForm;
private int totalQubits;
private static final Complex imUnit = new Complex(0,1);
private static final Complex minusOneReal = new Complex(-1);
private static final Complex OneDividedBySqrtOfTwo = new Complex((float)(1/Math.sqrt(2)));
public QuantumGate(int givenTotalQubits){
super((int)Math.pow(2,givenTotalQubits),(int)Math.pow(2,givenTotalQubits));
binaryForm = new BinaryFormOfState(givenTotalQubits);
this.IdentityMatrix();
totalQubits = givenTotalQubits;
}
public void PauliX (int targetQubit, int state){
if(binaryForm.QubitIsOneInState(targetQubit, state))
SwitchRow(state,state-(int)Math.pow(2,targetQubit));
}
/* public void PauliY (int targetQubit, int state){
if(binaryForm.QubitIsOneInState(targetQubit, state)){
SwitchRow(state,state-(int)Math.pow(2,targetQubit));
for(int column=0; column<=this.totalColumns-1; column++){
Set(state, column, Get(state, column).MultiplyBy(imUnit));
Set(state-(int)Math.pow(2,targetQubit), column, Get(state-(int)Math.pow(2,targetQubit), column).MultiplyBy(imUnit.Negation()));
}
}
}*/
public void PauliY (int targetQubit, int state){
Matrix hardCopyOfThisState = new Matrix(1,totalColumns);
int rowVariable=0;
int columnVariable=0;
for(int column=0; column<=this.totalColumns-1;column++){
hardCopyOfThisState.Set(0, column, this.Get(state,column));
}
for(int columnOfThisState=0; columnOfThisState<=this.totalColumns-1; columnOfThisState++){
this.Set(state, columnOfThisState, new Complex(0.0f));
if(binaryForm.QubitIsOneInState(targetQubit, columnOfThisState))
columnVariable=(int)Math.pow(2,targetQubit);
else
columnVariable=0;
for(int rowOfMultipliedGate=0; rowOfMultipliedGate<=this.totalRows-1; rowOfMultipliedGate++){
if(binaryForm.QubitIsOneInState(targetQubit, rowOfMultipliedGate))
rowVariable=(int)Math.pow(2,targetQubit);
else
rowVariable=0;
if((rowOfMultipliedGate-rowVariable)==(columnOfThisState-columnVariable)){
if(binaryForm.QubitIsOneInState(targetQubit, columnOfThisState) && !binaryForm.QubitIsOneInState(targetQubit, rowOfMultipliedGate))
this.Set(state, columnOfThisState, this.Get(state, columnOfThisState).Add(hardCopyOfThisState.Get(0, rowOfMultipliedGate).MultiplyBy(imUnit.Negation())));
if(!binaryForm.QubitIsOneInState(targetQubit, columnOfThisState) && binaryForm.QubitIsOneInState(targetQubit, rowOfMultipliedGate))
this.Set(state, columnOfThisState, this.Get(state, columnOfThisState).Add(hardCopyOfThisState.Get(0, rowOfMultipliedGate).MultiplyBy(imUnit)));
}
}
}
}
/* public void PauliZ (int targetQubit, int state){
if(binaryForm.QubitIsOneInState(targetQubit, state)){
for(int column=0; column<=this.totalColumns-1; column++){
Set(state, column, Get(state, column).MultiplyBy(minusOneReal));
}
}
}*/
public void PauliZ (int targetQubit, int state){
Matrix hardCopyOfThisState = new Matrix(1,totalColumns);
int rowVariable=0;
int columnVariable=0;
for(int column=0; column<=this.totalColumns-1;column++){
hardCopyOfThisState.Set(0, column, this.Get(state,column));
}
for(int columnOfThisState=0; columnOfThisState<=this.totalColumns-1; columnOfThisState++){
this.Set(state, columnOfThisState, new Complex(0.0f));
if(binaryForm.QubitIsOneInState(targetQubit, columnOfThisState))
columnVariable=(int)Math.pow(2,targetQubit);
else
columnVariable=0;
for(int rowOfMultipliedGate=0; rowOfMultipliedGate<=this.totalRows-1; rowOfMultipliedGate++){
if(binaryForm.QubitIsOneInState(targetQubit, rowOfMultipliedGate))
rowVariable=(int)Math.pow(2,targetQubit);
else
rowVariable=0;
if((rowOfMultipliedGate-rowVariable)==(columnOfThisState-columnVariable)){
if(!binaryForm.QubitIsOneInState(targetQubit, columnOfThisState) && !binaryForm.QubitIsOneInState(targetQubit, rowOfMultipliedGate))
this.Set(state, columnOfThisState, this.Get(state, columnOfThisState).Add(hardCopyOfThisState.Get(0, rowOfMultipliedGate)));
if(binaryForm.QubitIsOneInState(targetQubit, columnOfThisState) && binaryForm.QubitIsOneInState(targetQubit, rowOfMultipliedGate))
this.Set(state, columnOfThisState, this.Get(state, columnOfThisState).Add(hardCopyOfThisState.Get(0, rowOfMultipliedGate).MultiplyBy(minusOneReal)));
}
}
}
}
public void Hadamard (int targetQubit, int state){
Matrix hardCopyOfThisState = new Matrix(1,totalColumns);
int rowVariable=0;
int columnVariable=0;
for(int column=0; column<=this.totalColumns-1;column++){
hardCopyOfThisState.Set(0, column, this.Get(state,column));
}
for(int columnOfThisState=0; columnOfThisState<=this.totalColumns-1; columnOfThisState++){
this.Set(state, columnOfThisState, new Complex(0.0f));
if(binaryForm.QubitIsOneInState(targetQubit, columnOfThisState))
columnVariable=(int)Math.pow(2,targetQubit);
else
columnVariable=0;
for(int rowOfMultipliedGate=0; rowOfMultipliedGate<=this.totalRows-1; rowOfMultipliedGate++){
if(binaryForm.QubitIsOneInState(targetQubit, rowOfMultipliedGate))
rowVariable=(int)Math.pow(2,targetQubit);
else
rowVariable=0;
if((rowOfMultipliedGate-rowVariable)==(columnOfThisState-columnVariable)){
if(binaryForm.QubitIsOneInState(targetQubit, columnOfThisState) && binaryForm.QubitIsOneInState(targetQubit, rowOfMultipliedGate))
this.Set(state, columnOfThisState, this.Get(state, columnOfThisState).Add(hardCopyOfThisState.Get(0, rowOfMultipliedGate).MultiplyBy(OneDividedBySqrtOfTwo.Negation())));
else
this.Set(state, columnOfThisState, this.Get(state, columnOfThisState).Add(hardCopyOfThisState.Get(0, rowOfMultipliedGate).MultiplyBy(OneDividedBySqrtOfTwo)));
}
}
}
}
public void SqrtNOT (int targetQubit, int state){
Matrix hardCopyOfThisState = new Matrix(1,totalColumns);
int rowVariable=0;
int columnVariable=0;
for(int column=0; column<=this.totalColumns-1;column++){
hardCopyOfThisState.Set(0, column, this.Get(state,column));
}
for(int columnOfThisState=0; columnOfThisState<=this.totalColumns-1; columnOfThisState++){
this.Set(state, columnOfThisState, new Complex(0.0f));
if(binaryForm.QubitIsOneInState(targetQubit, columnOfThisState))
columnVariable=(int)Math.pow(2,targetQubit);
else
columnVariable=0;
for(int rowOfMultipliedGate=0; rowOfMultipliedGate<=this.totalRows-1; rowOfMultipliedGate++){
if(binaryForm.QubitIsOneInState(targetQubit, rowOfMultipliedGate))
rowVariable=(int)Math.pow(2,targetQubit);
else
rowVariable=0;
if((rowOfMultipliedGate-rowVariable)==(columnOfThisState-columnVariable)){
if(binaryForm.QubitIsOneInState(targetQubit, columnOfThisState) && !binaryForm.QubitIsOneInState(targetQubit, rowOfMultipliedGate))
this.Set(state, columnOfThisState, this.Get(state, columnOfThisState).Add(hardCopyOfThisState.Get(0, rowOfMultipliedGate).MultiplyBy(OneDividedBySqrtOfTwo.Negation())));
else
this.Set(state, columnOfThisState, this.Get(state, columnOfThisState).Add(hardCopyOfThisState.Get(0, rowOfMultipliedGate).MultiplyBy(OneDividedBySqrtOfTwo)));
}
}
}
}
public QuantumRegister MultiplyBy (QuantumRegister register){
QuantumRegister result = new QuantumRegister(totalQubits);
result.Set(0, 0, new Complex(0.0f));
try{
if (this.totalColumns != register.totalRows)
{
throw new ArrayIndexOutOfBoundsException();
}
for (int i=0; i<=this.totalRows-1; i++){
for (int j=0; j<=register.totalColumns-1; j++){
for (int k=0; k<=this.totalColumns-1; k++){
result.Set(i,j,result.Get(i,j).Add(this.Get(i,k).MultiplyBy(register.Get(k,j))));
}
//System.out.println("["+i+","+j+"]:"+result.Get(i,j).ToString()); //uncomment for debug
}
}
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Error: Column of first Matrix should be the same lenght as row of second Matrix.");
System.exit(-1);
}
return result;
}
}