-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeutsch.py
More file actions
101 lines (85 loc) · 2.75 KB
/
deutsch.py
File metadata and controls
101 lines (85 loc) · 2.75 KB
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
'''
Deutsh algorithm
To practice, let's make the deutsh algorithm
I'm all done, but I'm left with a question:
how do we map from arbitrary real function f
to a controlled unitary matrix cF in the circuit
'''
import sys
import math
import numpy as np
from pyquil.quil import Program
import pyquil.api as api
from pyquil.gates import *
######################################################################################
# Message Stuff
######################################################################################
def introduction():
mes = """
We will implement Deutsch's algorithm for some practice with pyquil
Enter the one bit gate that you would like to investigate with Deutsch's:
choices: X, I, 1, 0
"""
print(mes)
def parseChoice():
if len(sys.argv) < 2:
print('enter a command line argument for the gate you want to investigate: \
\n \'X\', \'I\', \'1\', or \'0\'')
exit(1)
if str(sys.argv[1]) != 'X' and str(sys.argv[1]) != 'I' and str(sys.argv[1]) != '0' and str(sys.argv[1]) != '1':
print('enter a command line argument for the gate you want to investigate: \
\n \'X\', \'I\', \'1\', or \'0\'')
exit(1)
# we set f to the function entered by the user
# These are all possible functions from 0,1 -> 0,1
if str(sys.argv[1]) == 'X':
f = lambda x: (x + 1)%2
elif str(sys.argv[1]) == 'I':
f = lambda x: x
elif str(sys.argv[1]) == '1':
f = lambda x: 1
elif str(sys.argv[1]) == '0':
f = lambda x: 0
return f
######################################################################################
# Quantum Stuff
######################################################################################
# We now define a 4 x 4 matrix that corresponds to a controlled f(x) on the second bit
# based on https://cs.uwaterloo.ca/~watrous/LectureNotes/CPSC519.Winter2006/04.pdf
# Note, we can't simply make a controlled U gate out of f() because f() does not
# define a unitary operator
def controlled(u):
print('Your controlled f gate then corresponds to:')
cu = np.array([[(f(0) + 1)%2, f(0)%2, 0, 0],
[f(0)%2, (1 + f(0))%2, 0, 0],
[0, 0, (1 + f(1))%2, f(1)%2],
[0, 0, f(1)%2, (1 + f(1))%2]])
print(cu)
return cu
def parseResult(result):
if result[0][0] == 1:
print('looks like this is a balenced function!')
else:
print('it\'s a constant function!')
def runAlg(cf):
qvm = api.QVMConnection()
p = Program()
p.defgate('cF', cf)
p.inst(
# Prepare 0 as a superposition |0> + |1>
# Prepare 1 as the Fourier Transform of |0> - |1>
H(0), X(1), H(1),
# one application of cF gate
('cF', 0, 1),
# project and measure
H(0), MEASURE(0, 0))
print(p)
result = qvm.run(p, [0])
print(result)
parseResult(result)
if __name__ == "__main__":
introduction()
f = parseChoice()
cf = controlled(f)
runAlg(cf)
quit()