-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgates.py
More file actions
145 lines (130 loc) · 3.56 KB
/
gates.py
File metadata and controls
145 lines (130 loc) · 3.56 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
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
from ast import Bytes
import random
class qbit:
def __init__(self):
self.q = [0, 0, 1]
def rotY90(self):
x, y, z = self.q
# 90 deg rot on y axis
if z == 1 and x == 0:
self.q[0] += 1
self.q[2] -= 1
elif z == 0 and x == 1:
self.q[0] -= 1
self.q[2] -= 1
elif z == -1 and x == 0:
self.q[0] -= 1
self.q[2] += 1
elif z == 0 and x == -1:
self.q[0] += 1
self.q[2] += 1
def rotX90(self):
x, y, z = self.q
# 90 deg rot on y axis
if z == 1 and y == 0:
self.q[1] += 1
self.q[2] -= 1
elif z == 0 and y == 1:
self.q[1] -= 1
self.q[2] -= 1
elif z == -1 and y == 0:
self.q[1] -= 1
self.q[2] += 1
elif z == 0 and y == -1:
self.q[1] += 1
self.q[2] += 1
def rotZ90(self):
x, y, z = self.q
# 90 deg rot on y axis
if x == 1 and y == 0:
self.q[0] -= 1
self.q[1] -= 1
elif x == 0 and y == -1:
self.q[0] -= 1
self.q[1] += 1
elif x == -1 and y == 0:
self.q[0] += 1
self.q[1] += 1
elif x == 0 and y == 1:
self.q[0] += 1
self.q[1] -= 1
def X(self):
self.rotX90()
self.rotX90()
return self.q
def Y(self):
self.rotY90()
self.rotY90()
return self.q
def Z(self):
self.rotZ90()
self.rotZ90()
return self.q
def getState(self):
return self.q
def H(self):
self.rotY90()
self.rotX90()
self.rotX90()
return self.q
def CNOT(self, z):
if z == 0:
if random.randint(0, 1) == 1:
self.X()
elif z == -1:
self.X()
return
def Measure(self):
if self.q[-1] == 0:
if random.randint(0, 1) == 0:
self.q[-1] = 1
else:
self.q[-1] = -1
self.q[0] = 0
return self.q
def reset(self):
self.q = [0, 0, 1]
def run(s, shots, circuit):
possibleStates = 2 ** len(s)
print(f"{possibleStates} Possible States\n\n")
statesFound = []
states = []
MeasurementHistory = []
for _ in range(shots):
StateChangeHistory = []
for l, i in enumerate(circuit):
if i[0] == "H":
i[1].H()
if i[0] == "X":
i[1].X()
if i[0] == "Y":
i[1].Y()
if i[0] == "Z":
i[1].Z()
if i[0] == "CNOT":
i[1].CNOT(i[2].q[2])
if i[0] == "CZ":
i[1].H()
i[1].CNOT(i[2].Measure()[2])
i[1].H()
qbitStates = []
for i in s:
m = i.Measure()[2]
if m == 1:
qbitStates.append(0)
elif m == -1:
qbitStates.append(1)
elif m == 0:
qbitStates.append(random.randint(0, 1))
i.reset()
MeasurementHistory.append(qbitStates)
for i, z in enumerate(MeasurementHistory):
if z not in states:
statesFound.append([
round((MeasurementHistory.count(z) / shots) * 100, 2), z])
states.append(z)
statesFound.sort(key=lambda x: x[0], reverse=True)
num = int(''.join(str(e) for e in statesFound[0][1]), 2)
for i in statesFound:
if i[0] != 0:
print(i)