-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_grover.py
executable file
·58 lines (51 loc) · 1.59 KB
/
test_grover.py
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
#!/usr/bin/env python
from circuit import *
from states import *
from gates import *
from grover import *
def test_oracle():
print 'Oracle test'
c = Circuit(3)
print c.state[-1] # |000>
c.add_gate(H, [1,])
c.add_gate(H, [2,])
c.add_gate(H, [3,])
print c.add_gate(Z, [3,]) # |++->
print oracle(c, 2, ['00', '11']) # |000> goes to |001> and vice versa;
# |110> goes to |111> and vice versa
def test_cond_phase_shift():
print 'Conditional phase shift test'
print 'Test 1'
c = Circuit(3)
print c.state[-1].transpose() # |000>
print cond_phase_shift(c).transpose() # -|000>
c.add_gate(X, [1,])
print cond_phase_shift(c).transpose() # -|100>
c.add_gate(X, [2,])
print cond_phase_shift(c).transpose() # -|110>
c.add_gate(X, [1,])
print cond_phase_shift(c).transpose() # -|010>
print 'Test 2'
c = Circuit(5)
print c.state[-1].transpose() # |00000>
print cond_phase_shift(c).transpose() # -|00000>
c.add_gate(X, [2,])
c.add_gate(X, [4,])
print cond_phase_shift(c).transpose() # -|01010>
for i in range(1, 6):
c.add_gate(X, [i,])
print cond_phase_shift(c).transpose() # -|10101>
def test_iterator():
print 'Iterator test'
c = Circuit(3)
print c.state[-1].transpose()
c.add_gate(X, [3,])
for i in range(1, 4):
c.add_gate(H, [i,])
print iterator(c, 1, ['00',])
if __name__ == '__main__':
test_oracle()
print
test_cond_phase_shift()
print
test_iterator()