@@ -3,12 +3,16 @@ int n_bits = 4;
3
3
int v_size = (int )pow (2 , n_bits);
4
4
float state[];
5
5
int chosen_val;
6
+ int other_val;
6
7
float mean;
7
8
int n_steps = (int )(sqrt (v_size) * PI / 4 ) * 2 ;
8
9
int current_step;
10
+ float best_diff;
11
+ int best_diff_step;
9
12
10
13
boolean started;
11
14
boolean finished;
15
+ boolean addOne;
12
16
13
17
void setup () {
14
18
size (800 , 500 );
@@ -31,50 +35,95 @@ void resetState() {
31
35
started = false ;
32
36
finished = false ;
33
37
current_step = 0 ;
34
-
35
- // noLoop() ;
36
- // redraw() ;
38
+ addOne = false ;
39
+ best_diff = 0 ;
40
+ best_diff_step = 0 ;
37
41
}
38
-
42
+
39
43
void draw () {
40
44
background (50 );
41
-
42
- if (started && current_step < n_steps) {
43
- if ((current_step & 1 ) == 0 ) { // Phase Shift
45
+ // Draw the title
46
+ push();
47
+ textAlign (CENTER ,CENTER );
48
+ textSize (18 );
49
+ text (" Grover's Algorithm Visualization" ,
50
+ width / 2 ,15 );
51
+ pop();
52
+
53
+ // Check if making changes during this draw cycle
54
+ // (step < suggested repetitions or user wants to go again)
55
+ if (started && (! finished || addOne)) {
56
+ if (current_step % 2 == 0 ) { // Part A: Phase Shift
44
57
state[chosen_val] *= - 1 ;
45
- } else { // Invert about the mean
58
+ } else { // Part B: Invert about the mean
46
59
mean = getMean(state);
47
60
for (int i = 0 ; i < state. length; i++ ) {
48
61
state[i] = mean + mean - state[i];
49
62
}
63
+ // Check if the new probability difference is
64
+ // better than previous
65
+ float pdiff = abs (state[chosen_val]* state[chosen_val] -
66
+ state[other_val]* state[other_val]);
67
+ if (pdiff > best_diff) {
68
+ best_diff = pdiff;
69
+ best_diff_step = current_step;
70
+ }
50
71
}
51
72
// Print the current status
52
- char stp;
53
- if (current_step % 2 == 0 ) stp = ' a ' ;
54
- else stp = ' b ' ;
73
+ String stp;
74
+ if (current_step % 2 == 0 ) stp = " a – Phase Rotation " ;
75
+ else stp = " b – Invert About the Mean " ;
55
76
println (" Step " + (int )(current_step / 2 ) + stp);
56
- int i;
57
- if (chosen_val == 0 ) i = 1 ;
58
- else i = 0 ;
59
77
System . out. printf(
60
78
" Target (%d) squared magnitude: %.4f\n " ,
61
79
chosen_val,
62
80
state[chosen_val] * state[chosen_val]
63
81
);
64
82
System . out. printf(
65
83
" Other squared magnitudes: %.4f\n " ,
66
- state[i] * state[i]
67
- );
68
- if (current_step % 2 == 0 ) println ();
69
-
70
-
71
- // Increment the step
84
+ state[other_val] * state[other_val]);
85
+ if (current_step % 2 == 1 ) println (); // Linebreak after part b
86
+
87
+ // Done with suggested repetitions?
88
+ if (current_step > n_steps) finished = true ;
89
+ // User adds another step
90
+ if (addOne && (current_step % 2 == 1 )) addOne = false ;
91
+ // Increment the current step
72
92
current_step++ ;
73
93
}
74
-
75
- // Draw the magnitudes
76
- // float x_start = width * 0.05;
77
- // float x_end = width - x_start;
94
+
95
+ if (! started) {
96
+ push();
97
+ textAlign (CENTER ,CENTER );
98
+ textSize (18 );
99
+ text (" Click a Column to Start" ,
100
+ width / 2 ,height - 35 );
101
+ pop();
102
+ } else {
103
+ push();
104
+ textAlign (LEFT ,CENTER );
105
+ textSize (15 );
106
+ // Column 1
107
+ text (" Chosen Value: " + chosen_val,10 ,height - 45 );
108
+ text (" Number of Qubits: " + n_bits,10 ,height - 25 );
109
+ // Column 2
110
+ text (" Current Steps: " + (current_step/ 2 ),195 ,height - 45 );
111
+ text (" Suggested Repetitions: " + (n_steps/ 2 ),195 ,height - 25 );
112
+ // Column 3
113
+ float prob_diff = abs (state[chosen_val]* state[chosen_val] -
114
+ state[other_val]* state[other_val]);
115
+ String fpdiff = String . format(java.util. Locale . US ," %.2f" , prob_diff);
116
+ String fmean = String . format(java.util. Locale . US ," %.2f" , mean);
117
+ text (" Prob Difference: " + fpdiff,400 ,height - 45 );
118
+ text (" Mean: " + fmean,400 ,height - 25 );
119
+ // Column 2
120
+ String fbest_diff = String . format(java.util. Locale . US ," %.2f" ,best_diff);
121
+ text (" Best Difference: " + fbest_diff,600 ,height - 45 );
122
+ text (" from Step: " + (best_diff_step/ 2 ),600 ,height - 25 );
123
+ pop();
124
+ }
125
+
126
+
78
127
push();
79
128
stroke (200 , 100 , 250 );
80
129
strokeWeight (width / (state. length + 2 ));
@@ -101,6 +150,7 @@ void draw() {
101
150
line (0 , height / 2 , width , height / 2 );
102
151
line (0 , mapValue(1 ), width , mapValue(1 ));
103
152
line (0 , mapValue(- 1 ), width , mapValue(- 1 ));
153
+
104
154
// Draw the mean
105
155
float mapped_mean = mapValue(mean);
106
156
dottedLine(0 , mapped_mean, width , mapped_mean, 100 );
@@ -112,7 +162,7 @@ void draw() {
112
162
text (
113
163
bInt(i,n_bits),
114
164
map (i+ 0.5 ,0 ,state. length,0 ,width ), // x value
115
- mapValue(1 ) - 20 // y value
165
+ mapValue(1 ) - 17 // y value
116
166
);
117
167
text (
118
168
nf (state[i] * state[i],1 ,2 ),
@@ -127,13 +177,14 @@ void mousePressed() {
127
177
started = true ;
128
178
float chunks = width / state. length;
129
179
chosen_val = (int )(mouseX / chunks);
180
+ if (chosen_val == 0 ) other_val = 1 ;
181
+ else other_val = 0 ;
130
182
println (" Chosen value: " + chosen_val);
131
183
println ();
132
- // loop();
133
- // } else if (finished) {
134
- } else {
135
- println (" \n\n Resetting...\n " );
136
- resetState();
184
+ } else if (finished) {
185
+ // println("\n\nResetting...\n");
186
+ // resetState();
187
+ addOne = true ;
137
188
}
138
189
}
139
190
0 commit comments