|
| 1 | + |
| 2 | +int n_bits = 4; |
| 3 | +int v_size = (int)pow(2, n_bits); |
| 4 | +float state[]; |
| 5 | +int chosen_val; |
| 6 | +float mean; |
| 7 | +int n_steps = (int)(sqrt(v_size) * PI / 4) * 2; |
| 8 | +int current_step = 0; |
| 9 | + |
| 10 | +boolean started = false; |
| 11 | +boolean finished = false; |
| 12 | + |
| 13 | +void setup() { |
| 14 | + size(800, 500); |
| 15 | + state = new float[v_size]; |
| 16 | + float n = 1 / sqrt(v_size); |
| 17 | + for (int i = 0; i < state.length; i++) { |
| 18 | + state[i] = n; |
| 19 | + } |
| 20 | + mean = n; |
| 21 | + chosen_val = -1; |
| 22 | + |
| 23 | + //println("Starting vector:"); |
| 24 | + //printArray(state); |
| 25 | + |
| 26 | + println("Number of qubits: " + n_bits); |
| 27 | + println("Vector length: " + v_size); |
| 28 | + |
| 29 | + frameRate(2); |
| 30 | + noLoop(); |
| 31 | +} |
| 32 | + |
| 33 | +void draw() { |
| 34 | + background(50); |
| 35 | + |
| 36 | + if (started && current_step < n_steps) { |
| 37 | + if ((current_step & 1) == 0) { |
| 38 | + // Phase Shift |
| 39 | + state[chosen_val] *= -1; |
| 40 | + } else { |
| 41 | + // Invert about the mean |
| 42 | + mean = getMean(state); |
| 43 | + for (int i = 0; i < state.length; i++) { |
| 44 | + state[i] = mean + mean - state[i]; |
| 45 | + } |
| 46 | + } |
| 47 | + // Print the current status |
| 48 | + char stp; |
| 49 | + if (current_step % 2 == 0) stp = 'a'; |
| 50 | + else stp = 'b'; |
| 51 | + println("Step " + (int)(current_step / 2) + stp); |
| 52 | + int i; |
| 53 | + if (chosen_val == 0) i = 1; |
| 54 | + else i = 0; |
| 55 | + System.out.printf( |
| 56 | + "Target (%d) squared magnitude: %.4f\n", |
| 57 | + chosen_val, |
| 58 | + state[chosen_val] * state[chosen_val] |
| 59 | + ); |
| 60 | + System.out.printf( |
| 61 | + "Other squared magnitudes: %.4f\n", |
| 62 | + state[i] * state[i] |
| 63 | + ); |
| 64 | + if (current_step % 2 == 0) println(); |
| 65 | + |
| 66 | + |
| 67 | + // Increment the step |
| 68 | + current_step++; |
| 69 | + } |
| 70 | + |
| 71 | + // Draw the magnitudes |
| 72 | + //float x_start = width * 0.05; |
| 73 | + //float x_end = width - x_start; |
| 74 | + push(); |
| 75 | + stroke(200, 100, 250); |
| 76 | + strokeWeight(width / (state.length + 2)); |
| 77 | + strokeCap(SQUARE); |
| 78 | + float x, y; |
| 79 | + for (int i = 0; i < state.length; i++) { |
| 80 | + x = map(i+0.5, 0, state.length, 0, width); |
| 81 | + y = mapValue(state[i]); // NOTE: Remember to show the probs, too |
| 82 | + if (i == chosen_val) { |
| 83 | + push(); |
| 84 | + stroke(255, 130, 170); |
| 85 | + line(x, mapValue(0), x, y); |
| 86 | + pop(); |
| 87 | + } else { |
| 88 | + line(x, mapValue(0), x, y); |
| 89 | + } |
| 90 | + } |
| 91 | + pop(); |
| 92 | + |
| 93 | + // Draw lines at y = 0, 1, -1 |
| 94 | + noFill(); |
| 95 | + stroke(255, 100); |
| 96 | + strokeWeight(1); |
| 97 | + line(0, height/2, width, height/2); |
| 98 | + line(0, mapValue(1), width, mapValue(1)); |
| 99 | + line(0, mapValue(-1), width, mapValue(-1)); |
| 100 | + // Draw the mean |
| 101 | + float mapped_mean = mapValue(mean); |
| 102 | + dottedLine(0, mapped_mean, width, mapped_mean, 30); |
| 103 | +} |
| 104 | + |
| 105 | +void mousePressed() { |
| 106 | + if (!started) { |
| 107 | + started = true; |
| 108 | + float chunks = width / state.length; |
| 109 | + chosen_val = (int)(mouseX / chunks); |
| 110 | + println("Chosen value: " + chosen_val); |
| 111 | + println(); |
| 112 | + loop(); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +void dottedLine(float x0, float y0, float x1, float y1, float n_dots) { |
| 117 | + float x, y, i; |
| 118 | + float radius = 2; |
| 119 | + noStroke(); |
| 120 | + fill(255); |
| 121 | + for (i = 0; i < n_dots; i++) { |
| 122 | + x = map(i, 0, n_dots, x0, x1); |
| 123 | + y = map(i, 0, n_dots, y0, y1); |
| 124 | + ellipse(x, y, radius, radius); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +float mapValue(float n) { |
| 129 | + float h = height * 0.75; |
| 130 | + float half_h = h / 2; |
| 131 | + return map( |
| 132 | + n, |
| 133 | + -1, 1, |
| 134 | + height/2 + half_h, |
| 135 | + height/2 - half_h |
| 136 | + ); |
| 137 | +} |
| 138 | + |
| 139 | +float getMean(float arr[]) { |
| 140 | + float total = 0; |
| 141 | + for (float v : arr) { |
| 142 | + total += v; |
| 143 | + } |
| 144 | + return total / arr.length; |
| 145 | +} |
0 commit comments