forked from oreilly-qc/oreilly-qc.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
beta_sandbox.html
1450 lines (1282 loc) · 48.3 KB
/
beta_sandbox.html
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<title>Programming Quantum Computers</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<script src="https://oreilly-qc.github.io/external/ace/src-min-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>
<!-- <script src="./external/jailed/lib/jailed.js" type="text/javascript" charset="utf-8"></script>
-->
<script type="text/javascript">
window.onerror = function(message, source, lineno, colno, error)
{ alert('Beta Error: ' + message + ' src:' + source + ' line:' + lineno + ' col:' + colno); }
</script>
<script type="text/javascript" src="https://machinelevel.github.io/QCEngine/beta/src/qcengine_bitfield.js"></script>
<script type="text/javascript" src="https://machinelevel.github.io/QCEngine/beta/src/qcengine_reg.js"></script>
<script type="text/javascript" src="https://machinelevel.github.io/QCEngine/beta/src/qcengine_int.js"></script>
<script type="text/javascript" src="https://machinelevel.github.io/QCEngine/beta/src/qcengine_basicops.js"></script>
<script type="text/javascript" src="https://machinelevel.github.io/QCEngine/beta/src/qcengine_widgets.js"></script>
<script type="text/javascript" src="https://machinelevel.github.io/QCEngine/beta/src/qcengine_staff.js"></script>
<script type="text/javascript" src="https://machinelevel.github.io/QCEngine/beta/src/qcengine_scriptpanel.js"></script>
<script type="text/javascript" src="https://machinelevel.github.io/QCEngine/beta/src/qcengine_panel.js"></script>
<script type="text/javascript" src="https://oreilly-qc.github.io/sample_shortcuts.js"></script>
<style>
.panel-resizable {
resize: vertical;
overflow: auto;
}
.ej-no-margin {
margin: 0px !important;
/* margin-bottom:50px !important;
*/ padding: 0px !important;
padding-bottom:10px !important;
}
.xxxpad-left-only {
margin: 0px !important;
/* padding: 8px !important;
padding-left: 10px !important;
padding-right: 0px !important;
margin: 0px !important;
margin-left: 10px;
*/ }
.xxxpad-right-only {
padding: 8px !important;
/* padding-left: 0px !important;
padding-right: 10px !important;
margin: 0px !important;
margin-right: 10px;
*/ }
.less-padding {
padding: 0px !important;
margin: 0px !important;
}
.ej-btn-cstyle {
padding: 4px !important;
}
.editor {
/* background-color:#e5e5e5;
padding:0px;
margin-top:1%;
margin-left:1%;
margin-right:1%;
margin-bottom:1%;
float:bottom;
width:98%;
*/
/* resize:vertical;
overflow:auto;
*/
overflow: hidden;
width:100%;
height:400px;
}
.info-button {
background-color: #1c87c9;
border: none;
color: white;
padding: 5px 8px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
</style>
</head>
<body>
<div id="info-buttons-div" style="position:absolute; left:0px; top:0px;">
<table cellpadding="10">
<tr>
<td colspan="3">
<img src="https://oreilly-qc.github.io/images/cs-title-bar-spacer.png" style="width:100%;" />
</td>
</tr>
<tr>
<td style="width:60%;">
</td>
<td>
<a href="https://shop.oreilly.com/product/0636920167433.do" class="info-button" target="_blank">Book Info</a>
</td>
<td>
<a href="https://www.amazon.com/Programming-Quantum-Computers-Essential-Algorithms/dp/1492039683" class="info-button" target="_blank">Buy on Amazon</a>
</td>
</tr>
</table>
</div>
<div id="title-div">
<img src="https://oreilly-qc.github.io/images/cs-title-bar.jpg" width="100%" />
</div>
<br/>
<div class="row">
<div class="col-sm-6">
<div class="container-fluid pad-left-only">
<div class="panel panel-default">
<div class="panel-heading">
<div class="btn-group">
<button type="button" class="btn btn-success" onclick="handle_run_button();">
<span class="glyphicon glyphicon-play"></span>
Run Program
</button>
<div class="btn-group">
<span id="example_choice_span">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
Choose a sample <span class="caret"></span></button>
<ul class="dropdown-menu" role="menu">
</ul>
</span>
</div>
<div class="btn-group">
<span id="engine_choice_span">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
QCEngine <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
</ul>
</span>
</div>
</div>
<div class="btn-group">
<button type="button" class="btn btn-default" onclick="click_editor_zoom(-2);">
<span class="glyphicon glyphicon-zoom-out"></span>
</button>
<button type="button" class="btn btn-default" onclick="click_editor_zoom(2);">
<span class="glyphicon glyphicon-zoom-in"></span>
</button>
</div>
<img src="https://oreilly-qc.github.io/images/icon_question.png" title="About QCEngine" height=20 onclick="do_aboutqce_modal();" />
<img src="https://oreilly-qc.github.io/images/icon_cheatsheet.png" title="Command cheat sheet" height=20 onclick="do_cheatsheet_modal();" />
<span id="bug_span">
</span>
<span id="info_span">
</span>
<span id="example_github_span">
</span>
<span id="sample_info_span" style="display:none;">
<img src="https://oreilly-qc.github.io/images/caution.png" height="28" />
<b>This sample takes a while to run.</b>
</span>
<span id="disable_info_span" style="display:none;">
</span>
</div>
<div id="editor_boot_panel" class="panel-body panel-resizable ej-no-margin">
<div id="editor_div" class="editor"></div>
</div>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="container-fluid pad-right-only">
<div class="row">
<div id="progress_panel" class="col-*-*" style="display:none;">
<div class="container-fluid pad-right-only">
<div class="progress">
<div id="progress_bar" class="progress-bar" role="progressbar" style="width: 25%;" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100">25%</div>
</div>
</div>
</div>
<div class="col-*-*" style="display:none;">
<div class="container-fluid pad-right-only">
<div class="panel panel-default">
<div class="panel-heading">
<b>How to run this sample</b>
<div class="btn-group">
<!-- <span id="example_language_span">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
Choose a Language <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
</ul>
</span>
-->
</div>
<div class="btn-group">
<button type="button" class="btn btn-default" onclick="click_gate_zoom(-1);">
<span class="glyphicon glyphicon-zoom-out"></span>
</button>
<button type="button" class="btn btn-default" onclick="click_gate_zoom(1);">
<span class="glyphicon glyphicon-zoom-in"></span>
</button>
</div>
</div>
<div id="how_to_boot_panel" class="panel-body panel-resizable">
<span id="how-to-span">(text goes here)</span>
</div>
</div>
</div>
</div>
<div id="image_output_div" class="col-*-*" style="display:none;">
<div class="container-fluid pad-right-only">
<div class="panel panel-default">
<div class="panel-heading">
<b>Image output</b>
<div class="btn-group">
<!-- <span id="example_language_span">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
Choose a Language <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
</ul>
</span>
-->
</div>
</div>
<div id="image_boot_panel" class="panel-body panel-resizable">
<span id="image_info_span"></span>
<table id="images_table">
<tr>
<td align="center" style="padding: 10px; border: 4px solid #ddd;">
<canvas id="display_ground_truth"></canvas><br/>
<span id="display_ground_truth_span"></span>
</td>
<td align="center" style="padding: 10px; border: 4px solid #ddd;">
<canvas id="display_qfull_res"></canvas><br/>
<span id="display_qfull_res_span"></span>
</td>
</tr>
<tr>
<td align="center" style="padding: 10px; border: 4px solid #ddd;">
<canvas id="display_monte_carlo"></canvas><br/>
<span id="display_monte_carlo_span"></span>
</td>
<td align="center" style="padding: 10px; border: 4px solid #ddd;">
<canvas id="display_qss"></canvas><br/>
<span id="display_qss_span"></span>
</td>
</tr>
<tr>
<td align="center" style="padding: 10px; border: 4px solid #ddd;">
<canvas id="display_confidence"></canvas><br/>
<span id="display_confidence_span"></span>
</td>
</tr>
</table>
<table>
<tr>
<td colspan="2" align="center" style="padding: 10px; border: 4px solid #ddd;">
<canvas id="display_cwtable"></canvas><br/>
<span id="display_cwtable_span"></span>
</td>
</tr>
</table>
</div>
</div>
</div>
</div>
<div class="col-*-*">
<div class="container-fluid pad-right-only">
<div class="panel panel-default">
<div class="panel-heading">
<b>Program gates</b>
<div class="btn-group">
<button type="button" class="btn btn-default" onclick="click_gate_zoom(-1);">
<span class="glyphicon glyphicon-zoom-out"></span>
</button>
<button type="button" class="btn btn-default" onclick="click_gate_zoom(1);">
<span class="glyphicon glyphicon-zoom-in"></span>
</button>
</div>
<!-- <button type="button" class="btn btn-default">
<span class="glyphicon glyphicon-new-window"></span>
</button>
-->
</div>
<div id="staff_boot_panel" class="panel-body panel-resizable">
<div id="staff_popin_div" class="right" style="resize: both; overflow: auto;">
<!-- <img src="https://oreilly-qc.github.io/images/click_tag.png" height="32" /><br/> -->
<canvas id="draw_gate_canvas"></canvas>
</div>
</div>
</div>
</div>
</div>
<div class="col-*-*">
<div class="container-fluid pad-right-only">
<div class="panel panel-default">
<div class="panel-heading">
<b>State vector</b>
<div class="btn-group">
<button type="button" class="btn btn-default" onclick="click_circle_zoom(-1);">
<span class="glyphicon glyphicon-zoom-out"></span>
</button>
<button type="button" class="btn btn-default" onclick="click_circle_zoom(1);">
<span class="glyphicon glyphicon-zoom-in"></span>
</button>
</div>
<div class="btn-group">
<button type="button" class="btn btn-default ej-btn-cstyle">
<img src="https://oreilly-qc.github.io/images/circ_style_00a.png" height="18" onclick="click_circle_style(0);"></span>
</button>
<button type="button" class="btn btn-default ej-btn-cstyle">
<img src="https://oreilly-qc.github.io/images/circ_style_01a.png" height="18" onclick="click_circle_style(1);"></span>
</button>
<button type="button" class="btn btn-default ej-btn-cstyle">
<img src="https://oreilly-qc.github.io/images/circ_style_10a.png" height="18" onclick="click_circle_style(2);"></span>
</button>
<button type="button" class="btn btn-default ej-btn-cstyle">
<img src="https://oreilly-qc.github.io/images/circ_style_11a.png" height="18" onclick="click_circle_style(3);"></span>
</button>
</div>
<div class="btn-group">
<button type="button" class="btn btn-default ej-btn-cstyle">
<img src="https://oreilly-qc.github.io/images/circ_fill_small.png" height="18" onclick="click_circle_fill(-1);"></span>
</button>
<button type="button" class="btn btn-default ej-btn-cstyle">
<img src="https://oreilly-qc.github.io/images/circ_fill_big.png" height="18" onclick="click_circle_fill(1);"></span>
</button>
</div>
<!-- <button type="button" class="btn btn-default">
<span class="glyphicon glyphicon-new-window"></span>
</button>
-->
</div>
<div id="circle_boot_panel" class="panel-body panel-resizable">
<div id="circle_div" class="right" style="resize: both; overflow: auto;">
<canvas id="circle_canvas"></canvas>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<b>Program output</b>
<div class="btn-group">
<button type="button" class="btn btn-default" onclick="click_output_zoom(-1);">
<span class="glyphicon glyphicon-zoom-out"></span>
</button>
<button type="button" class="btn btn-default" onclick="click_output_zoom(1);">
<span class="glyphicon glyphicon-zoom-in"></span>
</button>
</div>
<div class="btn-group">
<button type="button" class="btn btn-default" onclick="clear_output(false);" title="Clear output">
<span class="glyphicon glyphicon-erase"></span>
</button>
</div>
</div>
<div class="panel-body panel-resizable">
<textarea id="script_output_textarea" style="font-family:monospace; font-size:12pt; width:100%; height:100px; resize:both;">
(program output goes here)
</textarea>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="modal fade" id="AboutQCE_runModal" tabindex="-1" role="dialog" aria-labelledby="AboutQCE_runModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="AboutQCE_runModalLabel">QCEngine Notes</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
QCEngine is intended to be a lightweight quantum computation simulator,
useful for quick sketches and demonstrations.
<br/>
<br/>
It will let you run JavaScript code, simulating the quantum computing as
though you had a real QC, up to about 25 qubits (depending on your browser).
The best way to get familiar with QCEngine is to run the book samples on
this page.
<br/>
<br/>
For more serious development tasks, there are many other simulation engines available:
<br/>
<ul>
<li><b><a href="qiskit.org" target="_default">Qiskit</a></b> -
An open-source software development kit (SDK) for working with OpenQASM and the
IBM Q quantum processors. Create quantum computing programs, compile, and execute them online
in a real quantum processors.</li>
<li><b><a href="https://quantum-computing.ibm.com" target="_default">IBM Q Experience</a></b> -
A complete online environment, containing tools and information for programming quantum
simulations as well as physical quantum computers.</li>
<li><b><a href="https://www.microsoft.com/en-us/quantum/development-kit" target="_default">Microsoft QDK</a></b> -
A complete software development system for developing and running programs for quantum computers.</li>
<li><b><a href="http://www.quantumplayground.net" target="_default">Google QC Playground</a></b> -
An approachable online environment for discovering quantum computing.</li>
</ul>
...just to name a few.
<br/>
<br/>
If you have questions or comments
about running these book samples, the authors would love to hear from you
at <b><a href="mailto:[email protected]">[email protected]</a></b>.
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<div class="modal fade" id="CheatSheet_runModal" tabindex="-1" role="dialog" aria-labelledby="CheatSheet_runModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="CheatSheet_runModalLabel">QCEngine Command Cheat Sheet</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
This sheet provides a quick-reference for most common QCEngine commands.
The best way to become familiar with QCEngine commands is to try running the book
samples in this page.
If you find things missing or unclear, the authors would love to hear from you
at <b><a href="mailto:[email protected]">[email protected]</a></b>.
<br/>
<br/>
<b>"qc." Commands:</b> Commands such as <code>qc.reset()</code> and <code>qc.cnot(t,c)</code> execute
using a quantum computation simulator. In most cases, they take a <b>target mask</b> and a <b>condition mask</b>,
to specify which qubits the instructions act on. Some commands also take a rotation angle.
<br/>
<br/>
<b>Qubit Masks:</b> Qubits are be specified using an integer, where each bit corresponds to one of the
qubits in the quantum computer.
<br/>
<br/>
(...this is under sonstruction)
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<div class="modal fade" id="AddEngine_runModal" tabindex="-1" role="dialog" aria-labelledby="AddEngine_runModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="AddEngine_runModalLabel">Add new engines and samples!</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<b>Have a favorite quantum simulator, or even a real QPU?</b>
<br/>
We'd love to add your sample code here.
<br/>
<br/>
Providing these samples for as many systems as possible helps our readers, so please contact us
at <b><a href="mailto:[email protected]">[email protected]</a></b>.
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<div class="modal fade" id="Qiskit_runModal" tabindex="-1" role="dialog" aria-labelledby="Qiskit_runModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="Qiskit_runModalLabel">Running Qiskit Sample Code</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
The Qiskit versions of examples in this book are written in Python, designed to run
as Qiskit notebooks, at <a href="https://qiskit.org" target="_blank">https://qiskit.org</a>.
<br/><br/>
Qiskit is an open-source software development kit (SDK) for working with OpenQASM and the
IBM Q quantum processors. Create quantum computing programs, compile, and execute them online
in a real quantum processors.
<br/><br/>
To run this sample, go to the Qiskit site and create a new notebook, and then paste this
sample code into it.
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-success" onclick="window.open('https://qiskit.org', '_blank');">Go to Qiskit.org</button>
</div>
</div>
</div>
</div>
<div class="modal fade" id="QASM_runModal" tabindex="-1" role="dialog" aria-labelledby="QASM_runModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="QASM_runModalLabel">Running QASM Sample Code</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
The OpenQASM versions of examples in this book are designed to run
using the <b>IBM Q Experience Quantum Composer</b>, at <a href="https://quantum-computing.ibm.com" target="_blank">https://quantum-computing.ibm.com</a>.
<br/><br/>
The <b>Quantum Composer</b> is a graphical user interface for programming a quantum processor.
<br/><br/>
To run this sample, go to the Q Experience site and create new circuit using the Circuit Composer,
then paste the code into the Circuit Editor.
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-success" onclick="window.open('https://quantum-computing.ibm.com', '_blank');">Go to IBM Q Experience</button>
</div>
</div>
</div>
</div>
<div class="modal fade" id="QSharp_runModal" tabindex="-1" role="dialog" aria-labelledby="QSharp_runModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="QSharp_runModalLabel">Running Q# Sample Code</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
The Q# (pronounced "Q Sharp") versions of examples in this book are designed to run
using the <b>Microsoft QDK</b>, at <a href="https://www.microsoft.com/en-us/quantum/development-kit" target="_blank">https://www.microsoft.com/en-us/quantum/development-kit</a>.
<br/><br/>
Q# is a quantum-focused programming language with native type, operators, and other abstraction.
<br/><br/>
To run this sample, go to the QDK site and create a new IQ# Notebook, and then paste this
sample code into it.
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-success" onclick="window.open('https://www.microsoft.com/en-us/quantum/development-kit', '_blank');">Go to QDK Website</button>
</div>
</div>
</div>
</div>
<!-- <iframe id="sandbox_iframe" src="./sandbox.html" sandbox="allow-scripts"></iframe>
-->
<script>
window.addEventListener('message', function(event) {
// IMPORTANT: Check the origin of the data!
if (1 || ~event.origin.indexOf('http://yoursite.com')) {
// The data has been sent from your site
// The data sent with postMessage is stored in event.data
console.log(event.data);
if (event.data.cmd == 'got_sample')
{
editor.focus();
editor.setValue(event.data.sample_code);
editor.gotoLine(0);
make_github_source_links();
make_engine_menu();
}
else if (event.data.cmd == 'get_sample_failed')
{
do_failed_load_sample(event.data.sample_url);
}
} else {
// The data hasn't been sent from your site!
// Be careful! Do not use it.
return;
}
});
var editor = ace.edit("editor_div");
editor.session.setMode("ace/mode/javascript");
editor.commands.addCommand({
name: "run",
bindKey: {win: "Shift-Enter", mac: "Shift-Enter"},
exec: function(editor) {
handle_run_button();
}
});
var editor_div = document.getElementById('editor_div');
var editor_boot_panel = document.getElementById('editor_boot_panel');
var staff_boot_panel = document.getElementById('staff_boot_panel');
var circle_boot_panel = document.getElementById('circle_boot_panel');
var editor_frame_div = document.getElementById('editor_frame_div');
var example_choice_span = document.getElementById('example_choice_span');
var engine_choice_span = document.getElementById('engine_choice_span');
var example_github_span = document.getElementById('example_github_span');
// var language_choice_span = document.getElementById('language_choice_span');
var script_output_textarea = document.getElementById('script_output_textarea');
var staff_popin_div = document.getElementById('staff_popin_div');
///////////////////////////////////////////////////////////////
// This is a small workaround for the issue where the editor
// doesn't get informed about size changes
var last_editor_div_width = 0;
var last_editor_div_height = 0;
var last_circle_div_width = 0;
var last_circle_div_height = 0;
var check_serial = 0;
function check_for_editor_resize()
{
// Editor
var w = editor_boot_panel.offsetWidth;
var h = editor_boot_panel.offsetHeight;
if (w != last_editor_div_width || h != last_editor_div_height)
{
editor_div.style.height = "" + (h - 10) + "px";
editor.resize();
last_editor_div_width = w;
last_editor_div_height = h;
}
// Circle chart
var w = circle_boot_panel.offsetWidth;
var h = circle_boot_panel.offsetHeight;
if (w != last_circle_div_width || h != last_circle_div_height)
{
qc_options.circle_div.width = w;
qc_options.circle_div.height = h;
qc.panel_chart.draw();
last_circle_div_width = w;
last_circle_div_height = h;
}
var seconds_per_check_for_editor_resize = 0.5;
window.setTimeout(check_for_editor_resize,
1000 * seconds_per_check_for_editor_resize);
}
window.onload = function() {
check_for_editor_resize();
fetch_sample_contents();
make_sample_menu();
};
function click_circle_fill(up_down)
{
var e = {ctrlKey:false, shiftKey:true, deltaY:-up_down};
list = qc.panel_chart.widgets;
for (var i = 0; i < list.length; ++i)
{
var widget = list[i];
// Boost the reaction a bit
if (up_down > 0)
list[i].magScale *= 1.5;
else
list[i].magScale *= 1.0/1.5;
list[i].mouseWheel(e);
}
}
function click_circle_style(style)
{
if (style == 0)
{
qc_options.color_by_phase = false;
qc_options.book_render = false;
}
else if (style == 1)
{
qc_options.color_by_phase = false;
qc_options.book_render = true;
}
else if (style == 2)
{
qc_options.color_by_phase = true;
qc_options.book_render = false;
}
else if (style == 3)
{
qc_options.color_by_phase = true;
qc_options.book_render = true;
}
qc.panel_chart.draw();
}
function click_editor_zoom(up_down)
{
var editor_fontsize = parseInt(editor.getOption('fontSize'));
if (up_down == 0)
editor_fontsize = 12;
else
editor_fontsize += up_down;
if (editor_fontsize < 4)
editor_fontsize = 4;
editor.setOptions({fontSize: '' + editor_fontsize + 'pt'});
}
function click_gate_zoom(up_down)
{
var old_scale = qc.get_param('draw_scale', 1.0);
var new_scale = 1.0;
if (up_down == 1)
new_scale = old_scale * 1.1;
if (up_down == -1)
new_scale = old_scale / 1.1;
qc.set_param('draw_scale', new_scale);
qc.draw();
}
function click_circle_zoom(up_down)
{
var e = {ctrlKey:true, shiftKey:false, deltaY:-up_down};
list = qc.panel_chart.widgets;
for (var i = 0; i < list.length; ++i)
{
var widget = list[i];
list[i].mouseWheel(e);
}
}
function click_output_zoom(up_down)
{
var fontsize = parseInt(script_output_textarea.style.fontSize);
if (up_down == 0)
fontsize = 12;
else
fontsize += up_down;
if (fontsize < 4)
fontsize = 4;
script_output_textarea.style.fontSize = '' + fontsize + 'pt';
}
function load_json_from_url(file_url)
{
fetch(file_url, {cache: 'reload'})
.then(
function(response) {
if (response.status !== 200) {
console.log('Looks like there was a problem. Status Code: ' + response.status);
return false;
}
response.json().then(function(data) {
console.log(data);
});
return true;
}
)
.catch(function(err) {
console.log('Fetch Error :-S', err);
return false;
});
}
function load_code_from_url(file_url)
{
parent.postMessage({cmd:'get_sample',sample_url:file_url}, '*');
return;
var use_fetch = false; // fetch is not supported on Kindles
if (use_fetch)
{
fetch(file_url, {cache: 'reload'})
.then(
function(response) {
if (response.status !== 200) {
console.log('Error loading ' + file_url + '. Status Code: ' + response.status);
return false;
}
response.text().then(function(data) {
editor.focus();
editor.setValue(data);
editor.gotoLine(0);
});
return true;
}
)
.catch(function(err) {
console.log('Fetch Error :-S', err);
return false;
});
}
else
{
var http_request = new XMLHttpRequest();
http_request.open('GET', file_url, true);
// If specified, responseType must be empty string or "text"
http_request.responseType = 'text';
http_request.onload = function ()
{
if (http_request.readyState === http_request.DONE)
{
if (http_request.status === 200)
{
// console.log(http_request.response);
// console.log(http_request.responseText);
editor.focus();
editor.setValue(http_request.responseText);
editor.gotoLine(0);
make_github_source_links();
make_engine_menu();
}
else
{
do_failed_load_sample(file_url);
}
}
};
http_request.onerror = function ()
{
do_failed_load_sample(file_url);
};
try {
http_request.send(null);
}
catch (error) {
do_failed_load_sample(file_url);
}
return true;
}
make_github_source_links();
make_engine_menu();
}
function do_failed_load_sample(file_url)
{
var code_str = '// The sample ' + file_url + '\n// could not be loaded.\n//\n';
code_str += default_program;
editor.focus();
editor.setValue(code_str);
editor.gotoLine(0);
make_github_source_links();
make_engine_menu();
}
qc_options.staff_canvas = document.getElementById('draw_gate_canvas');
qc_options.staff_div = document.getElementById('staff_popin_div');
qc_options.circle_canvas = document.getElementById('circle_canvas');
qc_options.circle_div = document.getElementById('circle_div');
var valid_engine_list = [];
//qc_options.draw_scale = 3.0;
var qc = QPU();
qc.set_param('draw_scale', 1.0);
// qc.set_canvas(document.getElementById('draw_gate_canvas'));
// panel_staff = qc.panel_staff;
// panel_chart = qc.panel_chart;
var current_sample = null;
var current_engine = engine_list[0];
var all_sample_contents = {};
function fetch_one_sample_dir(engine)
{
var base_url = 'https://api.github.com/repos/oreilly-qc/oreilly-qc.github.io/contents/samples/';
var http_request = new XMLHttpRequest();
http_request.open('GET', base_url + engine.name, true);
// If specified, responseType must be empty string or "text"
http_request.responseType = 'text';
http_request.onload = function ()
{
if (http_request.readyState === http_request.DONE)
{
if (http_request.status === 200)
{
engine.dir_list = http_request.responseText;
make_github_source_links();
make_engine_menu();
}
else
{
engine.dir_list = '';
}
}
};
http_request.send(null);
}
function fetch_sample_contents()
{
for (var i = 0; i < engine_list.length; ++i)
fetch_one_sample_dir(engine_list[i]);
}
function make_sample_menu()
{
str = '';
str += '<button id="sample_menu_button" type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">';
str += 'Choose a sample <span class="caret"></span></button>';
str += '<ul class="dropdown-menu" role="menu">';
for (i = 0; i < sample_menu.length; ++i)
{
sample = sample_menu[i];
str += '<li><a href="#" onclick="choose_sample_menu(sample_menu['+i+'], null);">';
str += sample.menu_title;
str += '</a></li>';
}
str += '</ul>'
example_choice_span.innerHTML = str;
try_to_get_program_from_passed_in_url();
}
function set_current_engine(engine)
{
if (engine == null)
engine = engine_list[0];
current_engine = engine;
if (current_engine.name == 'QCEngine')