Skip to content

Commit 9082627

Browse files
committed
Adding a screenshot
1 parent 5cc494e commit 9082627

File tree

3 files changed

+82
-30
lines changed

3 files changed

+82
-30
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

ProcessingGroversAlgorithm.pde

Lines changed: 81 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ int n_bits = 4;
33
int v_size = (int)pow(2, n_bits);
44
float state[];
55
int chosen_val;
6+
int other_val;
67
float mean;
78
int n_steps = (int)(sqrt(v_size) * PI / 4) * 2;
89
int current_step;
10+
float best_diff;
11+
int best_diff_step;
912

1013
boolean started;
1114
boolean finished;
15+
boolean addOne;
1216

1317
void setup() {
1418
size(800, 500);
@@ -31,50 +35,95 @@ void resetState() {
3135
started = false;
3236
finished = false;
3337
current_step = 0;
34-
35-
//noLoop();
36-
//redraw();
38+
addOne = false;
39+
best_diff = 0;
40+
best_diff_step = 0;
3741
}
38-
42+
3943
void draw() {
4044
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
4457
state[chosen_val] *= -1;
45-
} else { // Invert about the mean
58+
} else { // Part B: Invert about the mean
4659
mean = getMean(state);
4760
for (int i = 0; i < state.length; i++) {
4861
state[i] = mean + mean - state[i];
4962
}
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+
}
5071
}
5172
// 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";
5576
println("Step " + (int)(current_step / 2) + stp);
56-
int i;
57-
if (chosen_val == 0) i = 1;
58-
else i = 0;
5977
System.out.printf(
6078
"Target (%d) squared magnitude: %.4f\n",
6179
chosen_val,
6280
state[chosen_val] * state[chosen_val]
6381
);
6482
System.out.printf(
6583
"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
7292
current_step++;
7393
}
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+
78127
push();
79128
stroke(200, 100, 250);
80129
strokeWeight(width / (state.length + 2));
@@ -101,6 +150,7 @@ void draw() {
101150
line(0, height/2, width, height/2);
102151
line(0, mapValue(1), width, mapValue(1));
103152
line(0, mapValue(-1), width, mapValue(-1));
153+
104154
// Draw the mean
105155
float mapped_mean = mapValue(mean);
106156
dottedLine(0, mapped_mean, width, mapped_mean, 100);
@@ -112,7 +162,7 @@ void draw() {
112162
text(
113163
bInt(i,n_bits),
114164
map(i+0.5,0,state.length,0,width), // x value
115-
mapValue(1) - 20 // y value
165+
mapValue(1) - 17 // y value
116166
);
117167
text(
118168
nf(state[i] * state[i],1,2),
@@ -127,13 +177,14 @@ void mousePressed() {
127177
started = true;
128178
float chunks = width / state.length;
129179
chosen_val = (int)(mouseX / chunks);
180+
if (chosen_val == 0) other_val = 1;
181+
else other_val = 0;
130182
println("Chosen value: " + chosen_val);
131183
println();
132-
//loop();
133-
//} else if (finished) {
134-
} else {
135-
println("\n\nResetting...\n");
136-
resetState();
184+
} else if (finished) {
185+
//println("\n\nResetting...\n");
186+
//resetState();
187+
addOne = true;
137188
}
138189
}
139190

images/grovers_viz.png

539 KB
Loading

0 commit comments

Comments
 (0)