-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTEST.java
More file actions
540 lines (496 loc) · 24.1 KB
/
Copy pathTEST.java
File metadata and controls
540 lines (496 loc) · 24.1 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
class TEST {
public static void main (String args[]) {
try{
int errorCounter=0;
int innerErrorCounter=0;
System.out.println("\n*** PRESS ENTER TO BEGIN TESTS ***");
System.out.println("\nNotes: Norm function from QuantumRegister is not used. Maybe should be used with user defined QuantumGate.");
System.in.read();
//test 01
System.out.println(" test 01: First test, checking if 0 is different than 1. Expecting TRUE.");
if(0<1 || 0>1)
PASSED();
else{
NOTPASSED();
errorCounter++;}
//test 02
System.out.println(" test 02: Void constructor of Complex class. Expecting (0,0).");
Complex t02complex = new Complex();
if(t02complex.GetRe()==0.0f & t02complex.GetIm()==0.0f)
PASSED();
else{
NOTPASSED();
errorCounter++;}
//test 03
System.out.println(" test 03: Real constructor of Complex class. Expecting (5,0).");
Complex t03complex = new Complex(5.0f);
if(t03complex.GetRe()==5.0f & t03complex.GetIm()==0.0f)
PASSED();
else{
NOTPASSED();
errorCounter++;}
//test 04
System.out.println(" test 04: Full (Real and Imagine) constructor of Complex class. Expecting (2,8).");
Complex t04complex = new Complex(2.0f, 8.0f);
if(t04complex.GetRe()==2.0f & t04complex.GetIm()==8.0f)
PASSED();
else{
NOTPASSED();
errorCounter++;}
//test 05
System.out.println(" test 05: Negation of one complex number. Expecting (-2,-8).");
Complex t05complex = new Complex(2.0f, 8.0f);
t05complex = t05complex.Negation();
if(t05complex.GetRe()==-2.0f & t05complex.GetIm()==-8.0f)
PASSED();
else{
NOTPASSED();
errorCounter++;}
//test 06
System.out.println(" test 06: Addition of two complex numbers. Expecting (3,15).");
Complex t06complex = new Complex(1.0f, 7.0f);
Complex t06Acomplex = new Complex(2.0f, 8.0f);
t06complex = t06complex.Add(t06Acomplex);
if(t06complex.GetRe()==3.0f & t06complex.GetIm()==15.0f)
PASSED();
else{
NOTPASSED();
errorCounter++;}
//test 07
System.out.println(" test 07: Substraction of two complex numbers. Expecting (1,2).");
Complex t07complex = new Complex(5.0f, 8.0f);
Complex t07Acomplex = new Complex(4.0f, 6.0f);
t07complex = t07complex.Substract(t07Acomplex);
if(t07complex.GetRe()==1.0f & t07complex.GetIm()==2.0f)
PASSED();
else{
NOTPASSED();
errorCounter++;}
//test 08
System.out.println(" test 08: Multiplication of two complex numbers. Expecting (-5,14).");
Complex t08complex = new Complex(3.0f, 2.0f);
Complex t08Acomplex = new Complex(1.0f, 4.0f);
t08complex = t08complex.MultiplyBy(t08Acomplex);
if(t08complex.GetRe()==-5.0f & t08complex.GetIm()==14.0f)
PASSED();
else{
NOTPASSED();
errorCounter++;}
//test 08.01
System.out.println(" test 08.01: Multiplication by single float number. Expecting (12,2).");
Complex t08Bcomplex = new Complex(3.0f, 2.0f);
t08Bcomplex = t08Bcomplex.MultiplyBy(4.0f);
if(t08Bcomplex.GetRe()==12.0f & t08Bcomplex.GetIm()==2.0f)
PASSED();
else{
NOTPASSED();
errorCounter++;}
//test 09
System.out.println(" test 09: Complex modulus (absolute value) of complex number. Expecting |(3,-2)|=3,605.");
Complex t09complex = new Complex(3.0f, -2.0f);
if(t09complex.Mod()==(float)Math.sqrt(13.0f))
PASSED();
else{
NOTPASSED();
errorCounter++;}
//test 10
System.out.println(" test 10: Complex modulus squared (absolute value squered) of complex number. Expecting |(3,-2)|^2=13.");
Complex t10complex = new Complex(3.0f, -2.0f);
if(t10complex.Mod2()==13.0f)
PASSED();
else{
NOTPASSED();
errorCounter++;}
//test 11
System.out.println(" test 11: Phi angle in radians of complex number's polar form. Expecting -0.5880026f. Checking also Real=0 and Imagine > 0 and < 0");
Complex t11complex = new Complex(3.0f, -2.0f);
Complex t11Acomplex = new Complex(0.0f, 2.0f);
Complex t11Bcomplex = new Complex(0.0f, -2.0f);
if(t11complex.Phi()==-0.5880026f & t11Acomplex.Phi()==(((float)Math.PI)/2.0f) & t11Bcomplex.Phi()==(-((float)Math.PI)/2.0f))
PASSED();
else{
NOTPASSED();
errorCounter++;}
//test 12
System.out.println(" test 12: Checking ToString for complex number. Expecting (3.0-2.0i)");
Complex t12complex = new Complex(3.0f, -2.0f);
if(t12complex.ToString().equals(3.0f+""+(-2.0f)+"i"))
PASSED();
else{
NOTPASSED();
errorCounter++;
System.out.println(t12complex.ToString());}
//test 13
System.out.println(" test 13: Checking ToPolarString for complex number. Expecting 3.6055512exp^(-0.5880026)i");
Complex t13complex = new Complex(3.0f, -2.0f);
if(t13complex.ToPolarString().equals(3.6055512f+"exp^("+(-0.5880026f)+")i"))
PASSED();
else{
NOTPASSED();
errorCounter++;}
//test 14
System.out.println(" test 14: Void constructor of Matrix class with n rows/columns. Expecting matrix with 5 rows/columns filled with zeros.");
Matrix t14matrix = new Matrix(5);
innerErrorCounter=0;
for (int i=0; i<=4; i++){
for (int j=0; j<=4; j++){
//System.out.println("["+i+","+j+"]:"+t14matrix.Get(i,j).ToString()); //uncomment for debug
if(t14matrix.Get(i,j).GetRe()==0.0f & t14matrix.Get(i,j).GetIm()==0.0f);
else
innerErrorCounter++;
}
}
if(innerErrorCounter==0)
PASSED();
else{
NOTPASSED();
errorCounter++;}
//test 15
System.out.println(" test 15: Identity Matrix with n rows/columns. Expecting Identity matrix with 5 rows/columns.");
Matrix t15matrix = new Matrix(5);
t15matrix.IdentityMatrix();
innerErrorCounter=0;
for (int i=0; i<=4; i++){
for (int j=0; j<=4; j++){
//System.out.println("["+i+","+j+"]:"+t15matrix.Get(i,j).ToString()); //uncomment for debug
if(i==j){
if(t15matrix.Get(i,j).GetRe()==1.0f & t15matrix.Get(i,j).GetIm()==0.0f);
else
innerErrorCounter++;
}
else
if(t15matrix.Get(i,j).GetRe()==0.0f & t15matrix.Get(i,j).GetIm()==0.0f);
else
innerErrorCounter++;
}
}
if(innerErrorCounter==0)
PASSED();
else{
NOTPASSED();
errorCounter++;}
//test 16
System.out.println(" test 16: Void constructor of Matrix class with n rows and m columns. Expecting Identity matrix with 5 rows and 7 columns filled with zeros.");
Matrix t16matrix = new Matrix(5,7);
innerErrorCounter=0;
for (int i=0; i<=4; i++){
for (int j=0; j<=6; j++){
//System.out.println("["+i+","+j+"]:"+t16matrix.Get(i,j).ToString()); //uncomment for debug
if(t16matrix.Get(i,j).GetRe()==0.0f & t16matrix.Get(i,j).GetIm()==0.0f);
else
innerErrorCounter++;
}
}
if(innerErrorCounter==0)
PASSED();
else{
NOTPASSED();
errorCounter++;}
//test 17
System.out.println(" test 17: Switching rows of matrix. Expecting to switch rows 3 and 5.");
Matrix t17matrix = new Matrix(6,7);
t17matrix.Set(3,1,new Complex(4.0f, 34.0f));
t17matrix.Set(5,1,new Complex(17.0f, 8.0f));
t17matrix.SwitchRow(3,5);
//t17matrix.DebugPrintMatrixDetailsComplexValues(); //uncomment for debug
if(t17matrix.Get(3,1).GetRe()==17.0f & t17matrix.Get(3,1).GetIm()==8.0f & t17matrix.Get(5,1).GetRe()==4.0f & t17matrix.Get(5,1).GetIm()==34.0f)
PASSED();
else{
NOTPASSED();
errorCounter++;}
//test 18
System.out.println(" test 18: Addition of two matrices. Expecting the following result:");
System.out.println(" [1,2][3,4] [5, 6][7, 8] [6, 8][10, 12]");
System.out.println(" [5,6][7,8] + [9,10][11,12] = [14,16][18,20]");
Matrix t18Amatrix = new Matrix(2,2);
Matrix t18Bmatrix = new Matrix(2,2);
t18Amatrix.Set(0,0,new Complex(1.0f, 2.0f));
t18Amatrix.Set(0,1,new Complex(3.0f, 4.0f));
t18Amatrix.Set(1,0,new Complex(5.0f, 6.0f));
t18Amatrix.Set(1,1,new Complex(7.0f, 8.0f));
t18Bmatrix.Set(0,0,new Complex(5.0f, 6.0f));
t18Bmatrix.Set(0,1,new Complex(7.0f, 8.0f));
t18Bmatrix.Set(1,0,new Complex(9.0f, 10.0f));
t18Bmatrix.Set(1,1,new Complex(11.0f, 12.0f));
t18Amatrix = t18Amatrix.Add(t18Bmatrix);
//t18Amatrix.DebugPrintMatrixDetailsComplexValues(); //uncomment for debug
if(t18Amatrix.Get(0,0).GetRe()==6.0f & t18Amatrix.Get(0,0).GetIm()==8.0f & t18Amatrix.Get(0,1).GetRe()==10.0f & t18Amatrix.Get(0,1).GetIm()==12.0f & t18Amatrix.Get(1,0).GetRe()==14.0f & t18Amatrix.Get(1,0).GetIm()==16.0f & t18Amatrix.Get(1,1).GetRe()==18.0f & t18Amatrix.Get(1,1).GetIm()==20.0f)
PASSED();
else{
NOTPASSED();
errorCounter++;}
//test 19
System.out.println(" test 19: Substraction of two matrices. Expecting the following result:");
System.out.println(" [1,2][3,4] [5, 6][7, 8] [-4, -4][-4, -4]");
System.out.println(" [5,6][7,8] - [9,10][11,12] = [-4, -4][-4, -4]");
Matrix t19Amatrix = new Matrix(2,2);
Matrix t19Bmatrix = new Matrix(2,2);
t19Amatrix.Set(0,0,new Complex(1.0f, 2.0f));
t19Amatrix.Set(0,1,new Complex(3.0f, 4.0f));
t19Amatrix.Set(1,0,new Complex(5.0f, 6.0f));
t19Amatrix.Set(1,1,new Complex(7.0f, 8.0f));
t19Bmatrix.Set(0,0,new Complex(5.0f, 6.0f));
t19Bmatrix.Set(0,1,new Complex(7.0f, 8.0f));
t19Bmatrix.Set(1,0,new Complex(9.0f, 10.0f));
t19Bmatrix.Set(1,1,new Complex(11.0f, 12.0f));
t19Amatrix = t19Amatrix.Substract(t19Bmatrix);
//t19Amatrix.DebugPrintMatrixDetailsComplexValues(); //uncomment for debug
if(t19Amatrix.Get(0,0).GetRe()==-4.0f & t19Amatrix.Get(0,0).GetIm()==-4.0f & t19Amatrix.Get(0,1).GetRe()==-4.0f & t19Amatrix.Get(0,1).GetIm()==-4.0f & t19Amatrix.Get(1,0).GetRe()==-4.0f & t19Amatrix.Get(1,0).GetIm()==-4.0f & t19Amatrix.Get(1,1).GetRe()==-4.0f & t19Amatrix.Get(1,1).GetIm()==-4.0f)
PASSED();
else{
NOTPASSED();
errorCounter++;}
//test 20
System.out.println(" test 20: Multiplication of two matrices. Expecting the following result:");
System.out.println(" [1,2][3,4] [5, 6][7, 8] [-20, 82][-24, 102]");
System.out.println(" [5,6][7,8] * [9,10][11,12] = [-28, 202][-32, 254]");
Matrix t20Amatrix = new Matrix(2,2);
Matrix t20Bmatrix = new Matrix(2,2);
t20Amatrix.Set(0,0,new Complex(1.0f, 2.0f));
t20Amatrix.Set(0,1,new Complex(3.0f, 4.0f));
t20Amatrix.Set(1,0,new Complex(5.0f, 6.0f));
t20Amatrix.Set(1,1,new Complex(7.0f, 8.0f));
t20Bmatrix.Set(0,0,new Complex(5.0f, 6.0f));
t20Bmatrix.Set(0,1,new Complex(7.0f, 8.0f));
t20Bmatrix.Set(1,0,new Complex(9.0f, 10.0f));
t20Bmatrix.Set(1,1,new Complex(11.0f, 12.0f));
t20Amatrix = t20Amatrix.MultiplyBy(t20Bmatrix);
//t20Amatrix.DebugPrintMatrixDetailsComplexValues(); //uncomment for debug
if(t20Amatrix.Get(0,0).GetRe()==-20.0f & t20Amatrix.Get(0,0).GetIm()==82.0f & t20Amatrix.Get(0,1).GetRe()==-24.0f & t20Amatrix.Get(0,1).GetIm()==102.0f & t20Amatrix.Get(1,0).GetRe()==-28.0f & t20Amatrix.Get(1,0).GetIm()==202.0f & t20Amatrix.Get(1,1).GetRe()==-32.0f & t20Amatrix.Get(1,1).GetIm()==254.0f)
PASSED();
else{
NOTPASSED();
errorCounter++;}
//test 21
System.out.println(" test 21: Multiplication of two matrices with different number of rows and columns. Expecting the following result:");
System.out.println(" [1, 2][3, 4][5, 6] [5, 6] [-33, 172]");
System.out.println(" [5, 6][7, 8][9, 10] * [7, 8] = [-45, 352]");
System.out.println(" [9, 10] ");
Matrix t21Amatrix = new Matrix(2,3);
Matrix t21Bmatrix = new Matrix(3,1);
Matrix t21resultmatrix = new Matrix(2,1);
t21Amatrix.Set(0,0,new Complex(1.0f, 2.0f));
t21Amatrix.Set(0,1,new Complex(3.0f, 4.0f));
t21Amatrix.Set(0,2,new Complex(5.0f, 6.0f));
t21Amatrix.Set(1,0,new Complex(5.0f, 6.0f));
t21Amatrix.Set(1,1,new Complex(7.0f, 8.0f));
t21Amatrix.Set(1,2,new Complex(9.0f, 10.0f));
t21Bmatrix.Set(0,0,new Complex(5.0f, 6.0f));
t21Bmatrix.Set(1,0,new Complex(7.0f, 8.0f));
t21Bmatrix.Set(2,0,new Complex(9.0f, 10.0f));
t21resultmatrix = t21Amatrix.MultiplyBy(t21Bmatrix);
//t21resultmatrix.DebugPrintMatrixDetailsComplexValues(); //uncomment for debug
if(t21resultmatrix.Get(0,0).GetRe()==-33.0f & t21resultmatrix.Get(0,0).GetIm()==172.0f & t21resultmatrix.Get(1,0).GetRe()==-45.0f & t21resultmatrix.Get(1,0).GetIm()==352.0f)
PASSED();
else{
NOTPASSED();
errorCounter++;}
//test 22
System.out.println(" test 22: Constructing Quantum Register as extension of Matrix class.");
QuantumRegister t22qregist = new QuantumRegister(1);
t22qregist.Set(0,0,new Complex(4.0f, 34.0f)); //setter with row and column
t22qregist.Set(1,new Complex(17.0f, 8.0f)); //setter only with row
//t22qregist.DebugPrintMatrixDetailsComplexValues(); //uncomment for debug
if(t22qregist.Get(0,0).GetRe()==4.0f & t22qregist.Get(0,0).GetIm()==34.0f & t22qregist.Get(1,0).GetRe()==17.0f & t22qregist.Get(1,0).GetIm()==8.0f)
PASSED();
else{
NOTPASSED();
errorCounter++;}
//test 23
System.out.println(" test 23: Testing utility class BinaryFormOfState which contains table with binary form of all states.");
System.out.println(" Checking state 6 for 5 qubits. Expecting result: [00110] (qubit number zero is last).");
System.out.println(" Checking state 19 for 5 qubits. Expecting result: [10011] (qubit number zero is last).");
BinaryFormOfState test23binaryform = new BinaryFormOfState(5);
//test23binaryform.PrintResults(); //uncomment for debug
if(test23binaryform.QubitIsOneInState(0,6)==false & test23binaryform.QubitIsOneInState(1,6)==true & test23binaryform.QubitIsOneInState(2,6)==true & test23binaryform.QubitIsOneInState(3,6)==false & test23binaryform.QubitIsOneInState(4,6)==false &
test23binaryform.QubitIsOneInState(0,19)==true & test23binaryform.QubitIsOneInState(1,19)==true & test23binaryform.QubitIsOneInState(2,19)==false & test23binaryform.QubitIsOneInState(3,19)==false & test23binaryform.QubitIsOneInState(4,19)==true)
PASSED();
else{
NOTPASSED();
errorCounter++;}
//test 24
System.out.println(" test 24: Using Pauli X quantum gate for one qubit circuit.");
QuantumGate t24qgate = new QuantumGate(1);
t24qgate.PauliX(0,0);
t24qgate.PauliX(0,1);
//t24qgate.DebugPrintMatrixDetailsOnlyRealValues();//uncomment for debug
if(t24qgate.Get(0,0).GetRe()==0.0f & t24qgate.Get(0,1).GetRe()==1.0f & t24qgate.Get(1,0).GetRe()==1.0f & t24qgate.Get(1,1).GetRe()==0.0f)
PASSED();
else{
NOTPASSED();
errorCounter++;}
//test 25
System.out.println(" test 25: Using Pauli X quantum gate for 3 qubits circuit. Gate is on the qubit in the middle.");
QuantumGate t25qgate = new QuantumGate(3);
for (int q=0; q<=7; q++){
t25qgate.PauliX(1,q);
}
//t25qgate.DebugPrintMatrixDetailsOnlyRealValues();//uncomment for debug
if(t25qgate.Get(0,2).GetRe()==1.0f & t25qgate.Get(2,0).GetRe()==1.0f & t25qgate.Get(3,1).GetRe()==1.0f & t25qgate.Get(1,3).GetRe()==1.0f &
t25qgate.Get(4,6).GetRe()==1.0f & t25qgate.Get(6,4).GetRe()==1.0f & t25qgate.Get(5,7).GetRe()==1.0f & t25qgate.Get(7,5).GetRe()==1.0f)
PASSED();
else{
NOTPASSED();
errorCounter++;}
//test 26
System.out.println(" test 26: Creating and calculating Quantum Circuit for one qubit and with Pauli X gate.");
QuantumCircuit t26qcirc = new QuantumCircuit(1);
t26qcirc.AddGate(0,0,1); //(targetqubit,step,gateId)
//System.out.println("Initial Register:"); //uncomment for debug
//t26qcirc.initialRegister.DebugPrintMatrixDetailsOnlyRealValues();//uncomment for debug
//System.out.println("Quantum Gate before Calculate:"); //uncomment for debug
//t26qcirc.circuitGates.DebugPrintMatrixDetailsOnlyRealValues();
t26qcirc.Calculate();
//System.out.println("Quantum Gate after Calculate:"); //uncomment for debug
//t26qcirc.circuitGates.DebugPrintMatrixDetailsOnlyRealValues();//uncomment for debug
//System.out.println("Final Register:"); //uncomment for debug
//t26qcirc.finalRegister.DebugPrintMatrixDetailsOnlyRealValues();//uncomment for debug
if(t26qcirc.GetResult(0).GetRe()==0.0f & t26qcirc.GetResult(0).GetIm()==0.0f & t26qcirc.GetResult(1).GetRe()==1.0f & t26qcirc.GetResult(1).GetIm()==0.0f)
PASSED();
else{
NOTPASSED();
errorCounter++;}
//test 27
System.out.println(" test 27: Creating only Quantum Circuit for three qubits with initial state 5. Checking if initial register has correct state.");
QuantumCircuit t27qcirc = new QuantumCircuit(3,5);
//t27qcirc.initialRegister.DebugPrintMatrixDetailsOnlyRealValues();//uncomment for debug
if(t27qcirc.initialRegister.Get(0,0).GetRe()==0.0f & t27qcirc.initialRegister.Get(0,0).GetIm()==0.0f & t27qcirc.initialRegister.Get(5,0).GetRe()==1.0f & t27qcirc.initialRegister.Get(5,0).GetIm()==0.0f)
PASSED();
else{
NOTPASSED();
errorCounter++;}
//test 28
System.out.println(" test 28: Creating and calculating Quantum Circuit for one qubit and with Pauli Y gate.");
QuantumCircuit t28qcirc = new QuantumCircuit(1);
t28qcirc.AddGate(0,0,2); //(targetqubit,step,gateId)
//System.out.println("Initial Register:"); //uncomment for debug
//t28qcirc.initialRegister.DebugPrintMatrixDetailsComplexValues();//uncomment for debug
//System.out.println("Quantum Gate before Calculate:"); //uncomment for debug
//t28qcirc.circuitGates.DebugPrintMatrixDetailsComplexValues(); //uncomment for debug
t28qcirc.Calculate();
//System.out.println("Quantum Gate after Calculate:"); //uncomment for debug
//t28qcirc.circuitGates.DebugPrintMatrixDetailsComplexValues();//uncomment for debug
//System.out.println("Final Register:"); //uncomment for debug
//t28qcirc.finalRegister.DebugPrintMatrixDetailsComplexValues();//uncomment for debug
if(t28qcirc.GetResult(0).GetRe()==0.0f & t28qcirc.GetResult(0).GetIm()==0.0f & t28qcirc.GetResult(1).GetRe()==0.0f & t28qcirc.GetResult(1).GetIm()==1.0f)
PASSED();
else{
NOTPASSED();
errorCounter++;}
//test 29
System.out.println(" test 29: Creating and calculating Quantum Circuit for one qubit and with Pauli Z gate.");
QuantumCircuit t29qcirc = new QuantumCircuit(1,1);
t29qcirc.AddGate(0,0,3); //(targetqubit,step,gateId)
//System.out.println("Initial Register:"); //uncomment for debug
//t29qcirc.initialRegister.DebugPrintMatrixDetailsComplexValues();//uncomment for debug
//System.out.println("Quantum Gate before Calculate:"); //uncomment for debug
//t29qcirc.circuitGates.DebugPrintMatrixDetailsComplexValues(); //uncomment for debug
t29qcirc.Calculate();
//System.out.println("Quantum Gate after Calculate:"); //uncomment for debug
//t29qcirc.circuitGates.DebugPrintMatrixDetailsComplexValues();//uncomment for debug
//System.out.println("Final Register:"); //uncomment for debug
//t29qcirc.finalRegister.DebugPrintMatrixDetailsComplexValues();//uncomment for debug
if(t29qcirc.GetResult(0).GetRe()==0.0f & t29qcirc.GetResult(0).GetIm()==0.0f & t29qcirc.GetResult(1).GetRe()==-1.0f & t29qcirc.GetResult(1).GetIm()==0.0f)
PASSED();
else{
NOTPASSED();
errorCounter++;}
//test 30
System.out.println(" test 30: Creating and calculating Quantum Circuit for one qubit and with Hadamard gate.");
QuantumCircuit t30qcirc = new QuantumCircuit(2,1);
t30qcirc.AddGate(0,0,4); //(targetqubit,step,gateId)
//System.out.println("Initial Register:"); //uncomment for debug
//t30qcirc.initialRegister.DebugPrintMatrixDetailsComplexValues();//uncomment for debug
//System.out.println("Sqrt(2)= "+(float)(1/Math.sqrt(2)));// for debug
//System.out.println("Quantum Gate before Calculate:"); //uncomment for debug
//t30qcirc.circuitGates.DebugPrintMatrixDetailsComplexValues(); //uncomment for debug
t30qcirc.Calculate();
//System.out.println("Quantum Gate after Calculate:"); //uncomment for debug
//t30qcirc.circuitGates.DebugPrintMatrixDetailsComplexValues();//uncomment for debug
//System.out.println("Final Register:"); //uncomment for debug
//t30qcirc.finalRegister.DebugPrintMatrixDetailsComplexValues();//uncomment for debug
if(t30qcirc.circuitGates.Get(0,0).GetRe()==0.70710677f & t30qcirc.circuitGates.Get(1,0).GetRe()==0.70710677f & t30qcirc.circuitGates.Get(0,1).GetRe()==0.70710677f & t30qcirc.circuitGates.Get(1,1).GetRe()==-0.70710677f)
PASSED();
else{
NOTPASSED();
errorCounter++;}
//test 31
System.out.println(" test 31: Creating and calculating Quantum Circuit for one qubit and with SqrtNOT gate.");
QuantumCircuit t31qcirc = new QuantumCircuit(1,1);
t31qcirc.AddGate(0,0,5); //(targetqubit,step,gateId)
//System.out.println("Initial Register:"); //uncomment for debug
//t31qcirc.initialRegister.DebugPrintMatrixDetailsComplexValues();//uncomment for debug
//System.out.println("Quantum Gate before Calculate:"); //uncomment for debug
//t31qcirc.circuitGates.DebugPrintMatrixDetailsComplexValues(); //uncomment for debug
t31qcirc.Calculate();
//System.out.println("Quantum Gate after Calculate:"); //uncomment for debug
//t31qcirc.circuitGates.DebugPrintMatrixDetailsComplexValues();//uncomment for debug
//System.out.println("Final Register:"); //uncomment for debug
//t31qcirc.finalRegister.DebugPrintMatrixDetailsComplexValues();//uncomment for debug
if(t31qcirc.circuitGates.Get(0,0).GetRe()==0.70710677f & t31qcirc.circuitGates.Get(1,0).GetRe()==0.70710677f & t31qcirc.circuitGates.Get(0,1).GetRe()==-0.70710677f & t31qcirc.circuitGates.Get(1,1).GetRe()==0.70710677f)
PASSED();
else{
NOTPASSED();
errorCounter++;}
//test 32
System.out.println(" test 32: Creating and calculating Quantum Circuit for one qubit and with two gates: Pauli Y and Hadamard.");
QuantumCircuit t32qcirc = new QuantumCircuit(1,1);
t32qcirc.AddGate(0,0,4); //(targetqubit,step,gateId)
t32qcirc.AddGate(0,1,2); //(targetqubit,step,gateId)
//System.out.println("Initial Register:"); //uncomment for debug
//t32qcirc.initialRegister.DebugPrintMatrixDetailsComplexValues();//uncomment for debug
//System.out.println("Quantum Gate before Calculate:"); //uncomment for debug
//t32qcirc.circuitGates.DebugPrintMatrixDetailsComplexValues(); //uncomment for debug
t32qcirc.Calculate();
//System.out.println("Quantum Gate after Calculate:"); //uncomment for debug
//t32qcirc.circuitGates.DebugPrintMatrixDetailsComplexValues();//uncomment for debug
//System.out.println("Final Register:"); //uncomment for debug
//t32qcirc.finalRegister.DebugPrintMatrixDetailsComplexValues();//uncomment for debug
if(t32qcirc.circuitGates.Get(0,0).GetIm()==0.70710677f & t32qcirc.circuitGates.Get(1,0).GetIm()==-0.70710677f & t32qcirc.circuitGates.Get(0,1).GetIm()==-0.70710677f & t32qcirc.circuitGates.Get(1,1).GetIm()==-0.70710677f)
PASSED();
else{
NOTPASSED();
errorCounter++;}
/*
//SPECIAL TEST OF EFFICIENCY - uncomment below to proceed
long spteststarttime;
long sptestelapsedtime;
starttime=System.nanoTime();
int specialTestNumberOfQubits = 7;
int repeat = 1;
System.out.println(" ***SPECIAL EFFICIENCY TEST***");
System.out.println(" *** starting test for "+specialTestNumberOfQubits+" qubits and repeating "+repeat+" times.");
QuantumGate sptqgate = new QuantumGate(specialTestNumberOfQubits);
for(int r=1; r<=repeat; r++){
sptqgate.IdentityMatrix();
for (int q=0; q<=(int)Math.pow(2,specialTestNumberOfQubits)-1; q++){
sptqgate.PauliZ(1,q);
System.out.println(" *** changing state*** "+q);
}
System.out.println(" *** repeating " + r);
}
System.out.println(" *** done ");
sptestelapsedtime= System.nanoTime()-spteststarttime;
System.out.println("Elapsed time: "+sptestelapsedtime/1000000+" miliseconds");
*/
if(errorCounter==0)
System.out.println("\nGreat work!");
else
System.out.println("\nFound "+errorCounter+" errors.");
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Element not found");
System.exit(-1);
}
catch(Exception e){
System.out.println("Other error");
System.exit(-1);
}
}
public static void PASSED(){
System.out.println("PASSED");
}
public static void NOTPASSED(){
System.out.println("*** NOT PASSED");
}
}