-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deutsch Jozsa.slq
61 lines (49 loc) · 1.43 KB
/
Deutsch Jozsa.slq
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
// Deutsch-Jozsa algorithm
// - Returns : | 0 if f is balanced (#{x, f(x)=0} = #{x, f(x)=1})
// | 1 if f is constant (∀x f(x)=0 or ∀x f(x)=1)
def deutsch_jozsa[n:!ℕ](f: const int[n] !→ lifted 𝔹):!𝔹{
cand := 0:int[n];
for k in [0..n) { cand[k] := H(cand[k]); }
// state ignoring normalization:
// ∑ᵥ|v⟩
if f(cand) {
phase(π);
}
// state ignoring normalization:
// ∑ᵥ(-1)^f(v)|v⟩
for k in [0..n) { cand[k] := H(cand[k]); }
// state ignoring normalization:
// ∑ᵥ(-1)^f(v) ∑ᵤ (-1)^(u·v)|u⟩
result := measure(cand);
// probability to measure 0 is:
// - 1 if f constant (constructive interference)
// - 0 if f balanced (destructive interference)
return result==0;
}
/* TEST */
def test_balanced() {
def balanced(x:int[2])lifted:𝔹{
if (x[0]==1) {
return 1:𝔹;
}
else {
return 0:𝔹;
}
} // implements a balanced function (outputs half 0 and half 1)
x := deutsch_jozsa(balanced);
assert(x == 0);
return x;
}
def test_constant() {
def constant(x:int[2])lifted:𝔹{
return 1:𝔹;
} // implements a constant function (outputs only 1)
x := deutsch_jozsa(constant);
assert(x == 1);
return x;
}
def main() {
print(test_balanced()); // DJ on balanced function should output 0
print(test_constant()); // DJ on constant function should output 1
return;
}