-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.html
105 lines (95 loc) · 2.32 KB
/
test.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
<html>
<head>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/highlight.min.js"></script>
<script src="quantum.js"></script>
<title>Grover's Example</title>
</head>
<style>
body {
margin: 0;
padding: 1em;
background: #000;
}
pre {
margin:0;
heigth:100%;
}
code {
margin:0;
height:100%;
font-size: 14pt;
color:white;
}
h3 { color:white; }
</style>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/styles/agate.min.css">
<body><h3>Source</h3><pre><code class="javascript"></code></pre>
<br>
<h3>Compiled</h3>
<pre><code class="php"></code></pre></body>
<script id="src">
function getNCXMask(values) {
var mask = [1,1,0];
for (var i = 2; i < values.length; i++) {
mask.push(1); mask.push(0);
}
return mask;
}
function invertMask(mask) {
var imask = [];
for (var i = 0; i < mask.length; i++) {
imask[i] = (mask[i] == 0) ? 1 : 0;
}
return imask;
}
function ncx(n) {
for (var i = 0; i <= n; i+=2) {
Q.bit(i).ccx(i+1, i+2);
}
var l = (n % 2 == 0) ? 0 : 1;
for (var i = (n-l); i >= 2; i-=2) {
Q.bit(i-2).ccx(i-1, i);
}
}
function grover(input, iters) {
var mask = getNCXMask(input);
var imask = invertMask(mask);
Q.comment("Walsch Hadamard Block: Preparing uniform superposition");
Q.mask(mask, bit => bit.h());
Q.brk();
Q.comment("Oracle");
Q.mask(imask, bit => bit.s());
Q.bit(mask.length-1).h();
Q.fnc.ncx(input.length);
Q.bit(mask.length-1).h();
Q.mask(imask, bit => bit.s());
Q.brk().brk();
Q.comment("Grover Iterator");
for (var i = 0; i < iters; i++) {
Q.comment("Iter " + i);
Q.mask(mask, bit => bit.h());
Q.mask(mask, bit => bit.x());
Q.bit(mask.length-1).h();
Q.fnc.ncx(input.length);
Q.bit(mask.length-1).h();
Q.mask(mask, bit => bit.x());
Q.mask(mask, bit => bit.h());
Q.brk();
}
}
var Q = new QuantumJS();
var input = [1,1,1,1];
Q.addFunction('ncx', ncx);
Q.addFunction('grover', grover);
Q.fnc.grover(input, 4);
Q.comment("Measurement");
Q.bit().measure();
Q.compile(function(compiled) {
$('.php').text(compiled);
$('.javascript').text(($("#src")[0]).innerHTML);
hljs.initHighlightingOnLoad();
});
</script>
</html>