Issues (33)

main.js (7 issues)

Labels
1
(function(){
2
  var W = 2;
3
  var N = 2;
4
  var state = [];
5
  var sort_show_flag = false;
6
  var num_of_state = 4;
7
  const Operator_name = [
8
    ["I", "Measure", "X", "Y", "Z", "H", "S", "Sd", "T", "Td", "Rx", "Ry", "Rz"],
9
    ["CI", "SCI", "CX", "CY", "CZ", "CH", "CS", "CSd", "CT", "CTd", "CRx", "CRy", "CRz"],
10
    ["I", "Fs", "Fe"]
11
  ];
12
13
  var Sim = new Simulate();
0 ignored issues
show
The variable Simulate seems to be never declared. If this is a global, consider adding a /** global: Simulate */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
14
15
  const X_SHIFT = 0;
16
  const Y_SHIFT = 150;
17
18
  //{{{ Operator_Cell
19
  function Operator_Cell(iIdx,iIdy){
20
    var self = this;
21
    this.mIdx = iIdx;
22
    this.mIdy = iIdy;
23
    this.Operator_id = 0;
24
    this.line_flag = 0;
25
    this.theta = 0.0;
26
    this.for_num = 0;
27
    this.for_count = 0;
28
29
    var controll_flag = document.getElementById("controll"+iIdx).checked;
30
    if(controll_flag){
31
      this.line_flag += 1;
32
    }
33
    var for_flag = document.getElementById("for"+iIdx).checked;
34
    console.log(iIdx, iIdy, for_flag);
35
    if(for_flag){
36
      this.line_flag = 2;
37
      if(this.mIdy>0){
38
        this.Operator_id = document.getElementById("c"+iIdx+"-"+0).object.Operator_id;
39
      }
40
    }
41
42
    this.mCanvas = document.getElementById("c"+iIdx+"-"+iIdy);
43
44
    this.Click = function(e){
45
      console.log(""+self.mIdx+"-"+self.mIdy+" is clicked ");
46
      var controll_checkbox = document.getElementById("controll"+self.mIdx);
47
      var for_checkbox = document.getElementById("for"+self.mIdx);
48
49
      if(for_checkbox.checked){
50
        self.Operator_id += 1;
51
        self.Operator_id %= Operator_name[2].length;
52
        for(var y=0; y<N; ++y){
53
          var another_canvas = document.getElementById("c"+self.mIdx+"-"+y);
54
          another_canvas.object.Operator_id = self.Operator_id;
55
          show_operator(another_canvas);
56
        }
57
        if(self.Operator_id==2){
58
          make_input_fornum(self.mIdx);
59
        }else{
60
          delete_input_fornum(self.mIdx);
61
        }
62
      }else if(controll_checkbox.checked){
63
        self.Operator_id += 1;
64
        self.Operator_id %= Operator_name[self.line_flag].length;
65
        delete_input_theta(self.mIdx,self.mIdy);
66
        if(self.Operator_id-1>=9 && self.Operator_id-1<=11){
67
          make_input_theta(self.mIdx,self.mIdy,this);
68
        }
69
        show_operator(self.mCanvas);
70
      }else{
71
        self.Operator_id += 1;
72
        self.Operator_id %= Operator_name[0].length;
73
        delete_input_theta(self.mIdx,self.mIdy);
74
        if(self.Operator_id-1>=9 && self.Operator_id-1<=11){
75
          make_input_theta(self.mIdx,self.mIdy,this);
76
        }
77
        show_operator(self.mCanvas);
78
      }
79
    }
80
    this.mCanvas.onclick = this.Click;
81
  }
82
  //}}}
83
84
  //{{{ make_button
85
  function make_button(class_name, left, top, id_name, text){
86
    var button = document.createElement("button");
87
    button.setAttribute("id", id_name);
88
    button.setAttribute("class", class_name);
89
    button.setAttribute("type", "button");
90
    button.setAttribute("style", "cursor:pointer");
91
    button.innerHTML = text;
92
    document.body.appendChild(button);
93
94
    button.style.position = "absolute";
95
    button.style.left = left + X_SHIFT + "px";
96
    button.style.top = top + Y_SHIFT + "px";
97
  };
98
  //}}}
99
100
  //{{{ delete_button
101
  function delete_button(id_name){
102
    var button = document.getElementById(id_name);
103
    if(button!=null){
104
      button.remove();
105
    }
106
  }
107
  //}}}
108
109
  //{{{ make_zero
110
  function make_zero(y){
111
    var canvas = document.createElement("canvas");
112
    canvas.setAttribute("class", "zero");
113
    canvas.setAttribute("id", "z"+y);
114
    canvas.style.position = "absolute";
115
    canvas.style.left = X_SHIFT + "px";
116
    canvas.style.top = 100*(y+1) + Y_SHIFT + "px";
117
118
    var width = canvas.width;
119
    var height = canvas.height;
120
    var ctx = canvas.getContext('2d');
121
    var img = new Image();
0 ignored issues
show
The variable Image seems to be never declared. If this is a global, consider adding a /** global: Image */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
122
    img.src = "./Figures/Zero.png";
123
    img.onload = function(){
124
      ctx.drawImage(img,0,0,width,height);
125
    }
126
    document.body.appendChild(canvas);
127
  }
128
  //}}}
129
130
  //{{{ delete_zero
131
  function delete_zero(y){
132
    var canvas = document.getElementById("z"+y);
133
    if(canvas!=null){
134
      canvas.remove();
135
    }
136
  }
137
  //}}}
138
139
  //{{{ show_operator
140
  function show_operator(canvas){
141
    var width = canvas.width;
142
    var height = canvas.height;
143
    var ctx = canvas.getContext('2d');
144
    var img = new Image();
0 ignored issues
show
The variable Image seems to be never declared. If this is a global, consider adding a /** global: Image */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
145
    var id = canvas.object.Operator_id;
146
    var line_flag = canvas.object.line_flag;
147
    if(line_flag>2){
148
      line_flag = 2;
149
    }
150
    img.src = "./Figures/" + Operator_name[line_flag][id] + ".png";
151
    ctx.clearRect(0, 0, width, height);
152
    img.onload = function(){
153
      ctx.drawImage(img, 0, 0, width, height);
154
    }
155
  }
156
  //}}}
157
158
  //{{{ make_operator
159
  function make_operator(x, y){
160
    var canvas = document.createElement("canvas");
161
    canvas.setAttribute("class", "operator");
162
    canvas.setAttribute("id", "c"+x+"-"+y);
163
    canvas.setAttribute("style", "cursor:pointer");
164
    canvas.style.position = "absolute";
165
    canvas.style.left = 100*(x+1) + X_SHIFT + "px";
166
    canvas.style.top = 100*(y+1) + Y_SHIFT + "px";
167
    document.body.appendChild(canvas);
168
    canvas.object = new Operator_Cell(x,y);
169
    show_operator(canvas);
170
  }
171
  //}}}
172
173
  //{{{ delete_operator
174
  function delete_operator(x,y){
175
    var canvas = document.getElementById("c"+x+"-"+y);
176
    if(canvas!=null){
177
      canvas.remove();
178
    }
179
    delete_input_theta(x,y);
180
  }
181
  //}}}
182
183
  //{{{ make_input_theta
184
  function make_input_theta(iIdx,iIdy,canvas){
185
    var input = document.createElement("input");
186
    input.setAttribute("type", "text");
187
    input.setAttribute("name", "input");
188
    input.setAttribute("class", "theta");
189
    input.setAttribute("id", "theta-"+iIdx+"-"+iIdy);
190
    input.setAttribute("value", "0");
191
192
    input.style.position = "absolute";
193
    input.style.left = 100*(iIdx+1) + X_SHIFT + 25 + "px";
194
    input.style.top = 100*(iIdy+1) + Y_SHIFT + 80 + "px";
195
196
    input.addEventListener('keyup', () => {
197
      canvas.object.theta = input.value;
198
    })
199
    document.body.appendChild(input);
200
  }
201
  //}}}
202
203
  //{{{ delete_input_theta
204
  function delete_input_theta(iIdx,iIdy){
205
    var canvas = document.getElementById("theta-"+iIdx+"-"+iIdy);
206
    if(canvas!=null){
207
      canvas.remove();
208
    }
209
  }
210
  //}}}
211
212
  //{{{ make_controll_checkbox
213
  function make_controll_checkbox(iIdx){
214
    var checkbox = document.createElement("input");
215
    checkbox.setAttribute("type", "checkbox");
216
    checkbox.setAttribute("class", "controll-checkbox");
217
    checkbox.setAttribute("id", "controll"+iIdx);
218
    checkbox.checked = false;
219
220
    checkbox.addEventListener('click', () => {
221
      var controll_flag = checkbox.checked;
222
      for(var i=0; i<N; ++i){
223
        var canvas = document.getElementById("c"+iIdx+"-"+i);
224
        if((canvas.object.line_flag)%2==0){
225
          canvas.object.line_flag+=1;
226
        }else{
227
          canvas.object.line_flag-=1;
228
        }
229
        show_operator(canvas);
230
      }
231
    });
232
233
    document.body.appendChild(checkbox);
234
235
    var label = document.createElement("label");
236
    label.setAttribute("for", "controll"+iIdx);
237
    label.setAttribute("class", "controll-label");
238
    label.setAttribute("id", "controll-label"+iIdx);
239
    label.setAttribute("style", "cursor:pointer");
240
    label.innerHTML = "Controll";
241
242
    label.style.position = "absolute";
243
    label.style.left = 100*(iIdx+1) + X_SHIFT + 25 + "px";
244
    label.style.top = 5 + Y_SHIFT + "px";
245
246
    document.body.appendChild(label);
247
  }
248
  //}}}
249
250
  //{{{ delete_controll_checkbox
251
  function delete_controll_checkbox(iIdx){
252
    var checkbox = document.getElementById("controll"+iIdx);
253
    if(checkbox!=null){
254
      checkbox.remove();
255
    }
256
    var label = document.getElementById("controll-label"+iIdx);
257
    if(label!=null){
258
      label.remove();
259
    }
260
  }
261
  //}}}
262
263
  //{{{ make_for_checkbox
264
  function make_for_checkbox(iIdx){
265
    var checkbox = document.createElement("input");
266
    checkbox.setAttribute("type", "checkbox");
267
    checkbox.setAttribute("class", "for-checkbox");
268
    checkbox.setAttribute("id", "for"+iIdx);
269
    checkbox.checked = false;
270
271
    checkbox.addEventListener('click', () => {
272
      delete_input_fornum(iIdx);
273
      for(var i=0; i<N; ++i){
274
        var canvas = document.getElementById("c"+iIdx+"-"+i);
275
        if(canvas.object.line_flag<2){
276
          canvas.object.line_flag+=2;
277
        }else{
278
          canvas.object.line_flag-=2;
279
        }
280
        canvas.object.Operator_id = 0;
281
        delete_input_theta(iIdx,i);
282
        show_operator(canvas);
283
      }
284
    });
285
286
    document.body.appendChild(checkbox);
287
288
    var label = document.createElement("label");
289
    label.setAttribute("for", "for"+iIdx);
290
    label.setAttribute("class", "for-label");
291
    label.setAttribute("id", "for-label"+iIdx);
292
    label.setAttribute("style", "cursor:pointer");
293
    label.innerHTML = "For";
294
295
    label.style.position = "absolute";
296
    label.style.left = 100*(iIdx+1) + X_SHIFT + 25 + "px";
297
    label.style.top = 40 + Y_SHIFT + "px";
298
299
    document.body.appendChild(label);
300
  }
301
  //}}}
302
303
  //{{{ delete_for_checkbox
304
  function delete_for_checkbox(iIdx){
305
    var checkbox = document.getElementById("for"+iIdx);
306
    if(checkbox!=null){
307
      checkbox.remove();
308
    }
309
    var label = document.getElementById("for-label"+iIdx);
310
    if(label!=null){
311
      label.remove();
312
    }
313
  }
314
  //}}}
315
316
  //{{{ make_input_fornum
317
  function make_input_fornum(iIdx){
318
    var input = document.createElement("input");
319
    input.setAttribute("type", "text");
320
    input.setAttribute("name", "input");
321
    input.setAttribute("class", "fornum");
322
    input.setAttribute("id", "fornum-"+iIdx);
323
    input.setAttribute("value", "0");
324
325
    input.style.position = "absolute";
326
    input.style.left = 100*(iIdx+1) + X_SHIFT + 25 + "px";
327
    input.style.top = Y_SHIFT + 80 + "px";
328
329
    input.addEventListener('keyup', () => {
330
      var ob = document.getElementById("c"+iIdx+"-0").object;
331
      ob.for_num = input.value;
332
    })
333
334
    document.body.appendChild(input);
335
  }
336
  //}}}
337
338
  //{{{ delete_input_fornum
339
  function delete_input_fornum(iIdx){
340
    var canvas = document.getElementById("fornum-"+iIdx);
341
    if(canvas!=null){
342
      canvas.remove();
343
    }
344
  }
345
  //}}}
346
347
  //{{{ index_converter_to_string
348
  function index_converter_to_string(index, show_elements_length){
349
    var str="";
350
    var index_copy = index;
351
    for(var i=0; i<show_elements_length; ++i){
352
      str = str + String(index_copy%2);
353
      index_copy=Math.floor(index_copy/2+0.1);
354
    }
355
    return str;
356
  }
357
  //}}}
358
359
  //{{{ make_result_bar
360
  function make_result_bar(index, pos, val, width, shift){
361
    var canvas = document.createElement("canvas");
362
    canvas.setAttribute("class", "result-bar");
363
    canvas.setAttribute("id", "result-bar-"+index);
364
365
    canvas.style.position = "absolute";
366
    canvas.style.left = 100 + pos*width + X_SHIFT + "px";
367
    canvas.style.top = 100*(N+1) + Y_SHIFT + 300*shift + "px";
368
    canvas.width = width;
369
    var height = canvas.height;
370
    var ctx = canvas.getContext('2d');
371
    ctx.fillStyle = 'blue';
372
    ctx.fillRect(width/4, height*(1-val), width/2, height*val);
373
    document.body.appendChild(canvas);
374
  }
375
  //}}}
376
377
  //{{{ make_result_index
378
  function make_result_index(index, pos, name, width, shift){
379
    var canvas = document.createElement("canvas");
380
    canvas.setAttribute("class", "result-index");
381
    canvas.setAttribute("id", "result-index-"+index);
382
383
    canvas.style.position = "absolute";
384
    canvas.style.left = 100 + pos*width + X_SHIFT + "px";
385
    canvas.style.top = 100*(N+1) + 200 + Y_SHIFT + 300*shift + "px";
386
    var ctx = canvas.getContext('2d');
387
    ctx.font = '64px serif';
388
    ctx.fillStyle = '#404040';
389
    ctx.textBaseline = 'middle';
390
    ctx.textAlign = 'center';
391
    ctx.fillText(name, canvas.width/2, canvas.height/2);
392
    document.body.appendChild(canvas);
393
  }
394
  //}}}
395
396
  //{{{ make_result
397
  function make_result(index, pos, val, show_elements_length, shift){
398
    var width = 100;
399
    make_result_bar(index, pos, val, width, shift);
400
    var index_string = index_converter_to_string(index, show_elements_length);
401
    index_string = "|" + index_string + ">";
402
    make_result_index(index, pos, index_string, width, shift);
403
  }
404
  //}}}
405
406
  //{{{ make_results
407
  function make_results(measure_List){
408
409
    for(var j=0; j<measure_List.length; ++j){
410
      var state_all = [];
411
      var List = measure_List[j];
412
      List.forEach((value, key)=>{
413
        state_all.push([key, value]);
0 ignored issues
show
The variable state_all is changed as part of the for loop for example by [] on line 410. Only the value of the last iteration will be visible in this function if it is called after the loop.
Loading history...
414
        //console.log(key, value);
415
      });
416
      if(sort_show_flag){
417
        state_all.sort(function(a,b){return b[1]-a[1]});
418
      }else{
419
        state_all.sort(function(a,b){return a[0]-b[0]});
420
      }
421
      var measure_indices_length = 0;
422
      for(var i=0; (1<<i)<state_all.length; ++i){
423
        measure_indices_length++;
424
      }
425
      for(var i=0; i<state_all.length; ++i){
426
        make_result(state_all[i][0], i, state_all[i][1], measure_indices_length, j);
427
      }
428
      delete_button("sort-show"+j);
429
      make_button("sort_show", 20, 100*(N+3)+300*j+10, "sort-show"+j, "SORT");
430
      document.getElementById("sort-show"+j).addEventListener('click', () => {
431
        sort_show_flag = !sort_show_flag;
432
        delete_results();
433
        make_results(measure_List);
434
      })
435
    }
436
  }
437
  //}}}
438
439
  //{{{ delete_results
440
  function delete_results(){
441
    var canvases = document.querySelectorAll(".result-bar");
442
    for(var i=0; i<canvases.length; ++i){
443
      if(canvases[i]!=null){
444
        canvases[i].remove();
445
      }
446
    }
447
    canvases = document.querySelectorAll(".result-index");
448
    for(var i=0; i<canvases.length; ++i){
449
      if(canvases[i]!=null){
450
        canvases[i].remove();
451
      }
452
    }
453
    canvases = document.querySelectorAll(".result_show");
454
    for(var i=0; i<canvases.lenth; ++i){
455
      if(canvases[i]!=null){
456
        canvases[i].remove();
457
      }
458
    }
459
    canvases = document.querySelectorAll(".sort_show");
460
    for(var i=0; i<canvases.length; ++i){
461
      if(canvases[i]!=null){
462
        canvases[i].remove();
463
      }
464
    }
465
  }
466
  //}}}
467
468
  //{{{ draw_state
469
  function draw_state(state){
470
    delete_state();
471
472
    var canvas = document.createElement("font");
473
    canvas.setAttribute("class", "result-state");
474
    canvas.setAttribute("id", "result-state");
475
    canvas.setAttribute("size", "+2");
476
477
    canvas.style.position = "absolute";
478
    canvas.style.left = 20 + X_SHIFT + "px";
479
    canvas.style.top = 100*(N+1) + Y_SHIFT + "px";
480
481
    canvas.innerHTML = "";
482
483
    for(var i=0; i<state.length; ++i){
484
      var index = index_converter_to_string(i, N);
485
      var real = String(Math.abs(state[i].real).toFixed(8));
486
      if(state[i].real >= 0.0){
487
        real = "&nbsp;&nbsp;&nbsp;" + real;
488
      }else{
489
        real = "&nbsp;-&nbsp;" + real;
490
      }
491
      var imag = String(Math.abs(state[i].imag).toFixed(8)) + " i";
492
      if(state[i].imag >= 0.0){
493
        imag = "&nbsp;+&nbsp;" + imag;
494
      }else{
495
        imag = "&nbsp;-&nbsp;" + imag;
496
      }
497
      var prob = state[i].real * state[i].real + state[i].imag * state[i].imag;
498
      var prob_text = String(prob.toFixed(8));
499
      var text = "|" + index + ">&nbsp;:&nbsp;" + real + imag + "&nbsp;&nbsp;&nbsp;,&nbsp;&nbsp;Prob&nbsp;:&nbsp;" + prob_text + "<br>";
500
      console.log(text);
501
      canvas.innerHTML = canvas.innerHTML + text;
502
    }
503
504
    document.body.appendChild(canvas);
505
  }
506
  //}}}
507
508
  //{{{ delete_state
509
  function delete_state(){
510
    var canvas = document.getElementById("result-state");
511
    if(canvas!=null){
512
      canvas.remove();
513
    }
514
  }
515
  //}}}
516
517
  //{{{ sort_indices
518
  function sort_indices(array){
519
    var show_elements_length = 0;
520
    for(var i=0; (1<<i)<array.length; ++i){
521
      show_elements_length++;
522
    }
523
    var new_array = [];
524
    for(var i=0; i<array.length; ++i){
525
      var tmp = 0;
526
      var i_copy = i;
527
      for(var j=0; j<show_elements_length; ++j){
528
        tmp = tmp*2;
529
        tmp += i_copy%2;
530
        i_copy = Math.floor(i_copy/2+0.1);
531
      }
532
      new_array.push([tmp, array[i]]);
533
    }
534
    new_array.sort(function(a,b){return a[0]-b[0]});
535
    sorted_array = [];
0 ignored issues
show
The variable sorted_array seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.sorted_array.
Loading history...
536
    for(var i=0; i<new_array.length; ++i){
537
      sorted_array.push(new_array[i][1]);
538
    }
539
    return sorted_array;
540
  }
541
  //}}}
542
543
  //{{{ init
544
  function init(){
545
    document.getElementById("wbutton1").addEventListener('click', () => {
546
      make_controll_checkbox(W);
547
      make_for_checkbox(W);
548
      for(var y=0; y<N; ++y){
549
        make_operator(W, y);
550
      }
551
      W++;
552
    });
553
554
    document.getElementById("wbutton2").addEventListener('click', () => {
555
      W--;
556
      if(W==0){
557
        W=1;
558
      }else{
559
        for(var y=0; y<N; ++y){
560
          delete_operator(W, y);
561
          delete_input_theta(W, y);
562
        }
563
        delete_input_fornum(W);
564
        delete_controll_checkbox(W);
565
        delete_for_checkbox(W);
566
      }
567
    });
568
569
    document.getElementById("nbutton1").addEventListener('click', () => {
570
      delete_results();
571
      delete_state();
572
      make_zero(N);
573
      for(var x=0; x<W; ++x){
574
        make_operator(x, N);
575
      }
576
      N++;
577
      num_of_state *= 2;
578
    });
579
580
    document.getElementById("nbutton2").addEventListener('click', () => {
581
      delete_results();
582
      delete_state();
583
      N--;
584
      if(N==0){
585
        N=1;
586
      }else{
587
        num_of_state /= 2;
588
        delete_zero(N);
589
        for(var x=0; x<W; ++x){
590
          delete_operator(x, N);
591
          delete_input_theta(x, N);
592
        }
593
      }
594
    })
595
596
    document.getElementById("simulate").addEventListener('click', () => {
597
      sort_show_flag = false;
598
      var results = Sim.simulate(W,N,num_of_state);
599
      var state = results[0];
600
      var measure_List = results[1];
601
      delete_results();
602
      delete_state()
603
      if(measure_List.length>0){
604
        measure_List_new = [];
0 ignored issues
show
The variable measure_List_new seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.measure_List_new.
Loading history...
605
        for(var j=0; j<measure_List.length; ++j){
606
          var new_List = new Map();
607
          var show_elements_length = 0;
608
          for(var i=0; (1<<i)<measure_List[j].size; ++i){
609
            show_elements_length++;
610
          }
611
          measure_List[j].forEach((value, key) => {
612
            var new_key = 0;
613
            var key_copy = key;
614
            for(var k=0; k<show_elements_length; ++k){
615
              new_key *= 2;
616
              new_key += key_copy%2;
617
              key_copy = Math.floor(key_copy/2+0.1);
618
            }
619
            new_List.set(new_key, value);
0 ignored issues
show
The variable new_List is changed as part of the for loop for example by new Map() on line 606. Only the value of the last iteration will be visible in this function if it is called after the loop.
Loading history...
620
          })
621
          measure_List_new.push(new_List);
622
        }
623
        make_results(measure_List_new);
624
      }else{
625
        state = sort_indices(state);
626
        draw_state(state);
627
      }
628
      scroll();
629
    })
630
631
    for(var x=0; x<W; ++x){
632
      make_controll_checkbox(x);
633
      make_for_checkbox(x);
634
    }
635
    for(var y=0; y<N; ++y){
636
      make_zero(y);
637
      for(var x=0; x<W; ++x){
638
        make_operator(x,y);
639
      }
640
    }
641
  };
642
  //}}}
643
644
  //{{{ scroll
645
  function scroll(){
646
    //TODO
647
  }
648
  //}}}
649
650
  window.onload = function(){
651
    init();
652
  };
653
654
})();
655