-
Notifications
You must be signed in to change notification settings - Fork 0
/
qs2.py
168 lines (157 loc) · 5.59 KB
/
qs2.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
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
import argparse
class WeightedKet():
def __init__(self, size) -> None:
self.size = size
self.amplitude = 1+0j
self.ket = [0] * size
def getket(self, i):
return self.ket[i]
def flip(self, i):
# i = self.size -1 - i
self.ket[i] = [1, 0][self.ket[i]]
def getbin(self):
return int("".join([str(i) for i in self.ket]), 2)
def getstr(self):
return "".join([str(i) for i in self.ket])
def __str__(self) -> str:
return "".join([str(i) for i in self.ket])
class Op():
def __init__(self, type, op1, op2 = None) -> None:
self.type = type
self.op1 = op1
self.op2 = op2
def simulate(instr):
instr = instr.split("\n")
qmap = {}
qset = set()
qsize = 0
csize = 0
ops = []
state = {}
for line in instr:
if len(line) == 0:
continue
tokens = line.strip().split()
if tokens[0] == "OPENQASM" or tokens[0] == "include":
pass
elif tokens[0] == "qreg":
pass
elif tokens[0] == "creg":
csize = int(tokens[1][2:-2])
elif tokens[0] == "h":
op1 = int(tokens[1][2:-2])
qsize = max(qsize, op1 + 1)
ops.append(Op("h", op1))
qset.add(op1)
elif tokens[0] == "x":
op1 = int(tokens[1][2:-2])
qsize = max(qsize, op1 + 1)
ops.append(Op("x", op1))
qset.add(op1)
elif tokens[0] == "t":
op1 = int(tokens[1][2:-2])
qsize = max(qsize, op1 + 1)
ops.append(Op("t", op1))
qset.add(op1)
elif tokens[0] == "tdg":
op1 = int(tokens[1][2:-2])
qsize = max(qsize, op1 + 1)
ops.append(Op("tdg", op1))
qset.add(op1)
elif tokens[0] == "cx":
s1, s2 = tokens[1].split(",")
op1 = int(s1[2:-1])
op2 = int(s2[2:-2])
qsize = max(qsize, op1 + 1, op2 + 1)
qset.add(op1)
qset.add(op2)
ops.append(Op("cx", op1, op2))
start = 0
qqmap = {}
qsize = len(qset)
for i in sorted(qset):
qqmap[i] = start
start += 1
state["0"*qsize] = 1+0j
# print(*state)
for op in ops:
newstate = {}
for ket in state:
if op.type == "h":
if ket[qqmap[op.op1]] == "0":
newket = ket[:qqmap[op.op1]] + "1" + ket[qqmap[op.op1] + 1:]
newamp = state[ket] * ((1/2)**.5)
if ket in newstate:
newstate[ket] += newamp
else:
newstate[ket] = newamp
if newket in newstate:
newstate[newket] += newamp
else:
newstate[newket] = newamp
else:
newket = ket[:qqmap[op.op1]] + "0" + ket[qqmap[op.op1] + 1:]
newamp = state[ket] * ((1/2)**.5)
if ket in newstate:
newstate[ket] += -newamp
else:
newstate[ket] = -newamp
if newket in newstate:
newstate[newket] += newamp
else:
newstate[newket] = newamp
elif op.type == "x":
newket = ket[:qqmap[op.op1]] + ["1", "0"][int(ket[qqmap[op.op1]])] + ket[qqmap[op.op1] + 1:]
if newket in newstate:
newstate[newket] += state[ket]
else:
newstate[newket] = state[ket]
elif op.type == "t":
if ket[qqmap[op.op1]] == "1":
newamp = ((1/2)**.5 + ((1/2)**.5)*1j) * state[ket]
if ket in newstate:
newstate[ket] += newamp
else:
newstate[ket] = newamp
else:
if ket in newstate:
newstate[ket] += state[ket]
else:
newstate[ket] = state[ket]
elif op.type == "tdg":
if ket[qqmap[op.op1]] == "1":
newamp = ((1/2)**.5 - ((1/2)**.5)*1j) * state[ket]
if ket in newstate:
newstate[ket] += newamp
else:
newstate[ket] = newamp
else:
if ket in newstate:
newstate[ket] += state[ket]
else:
newstate[ket] = state[ket]
elif op.type == "cx":
if ket[qqmap[op.op1]] == "1":
newket = newket = ket[:qqmap[op.op2]] + ["1", "0"][int(ket[qqmap[op.op2]])] + ket[qqmap[op.op2]+ 1:]
else:
newket = ket
if newket in newstate:
newstate[newket] += state[ket]
else:
newstate[newket] = state[ket]
# newstate = sorted(newstate,key = lambda x: x.bin())
# print(nsd)
state = newstate
# statevec = [0] * (int(max(nsd.keys(), key=lambda x: int(x ,2)), 2) + 1)
statevec = [0] * pow(2,qsize)
for i in state:
statevec[int(i, 2)] = round(state[i].real, 3) + round(state[i].imag, 3) * 1j
# print(statevec)
return statevec
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("file")
file = parser.parse_args().file
with open(file, "r") as f:
code = f.read()
simulate(code)