-
Notifications
You must be signed in to change notification settings - Fork 0
/
toy_arr_model.py
201 lines (160 loc) · 4.31 KB
/
toy_arr_model.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 10 10:00:01 2022
@author: jogib
todo:
-step generating function
fix up selection for pairs
-actual op for do op
-integrate scalar bits for testing
-typing
-change to density matrix for ciruit eventually
"""
import numpy as np
from pprint import pprint
np.set_printoptions(suppress=True)
#%%
class density_matrix:
def __init__(self, num_elems):
self.value = np.Identity(num_elems)
class circuit:
"""
contains the bits and mixing functions
Returns
-------
None.
"""
def __init__(self, num_elems):
self.num_elems = num_elems
self.elem_arr = [bits() for x in range(self.num_elems)]
self.step_num = 0
self.pairs = []
def gen_step(self, eoo):
self.gen_pairs(eoo)
for i in self.pairs:
self.do_operation(i)
def gen_pairs(self, eoo):
self.pairs = []
# some control over where indexing starts
if eoo:
i = 0
else:
i = 1
# get pairs
while i + 2 <= self.num_elems:
self.pairs.append([i, i + 1])
i = i + 2
def do_operation(self, pair):
angle = np.pi / 4
op = np.array([[np.cos(angle), -np.sin(angle)], [np.sin(angle), np.cos(angle)]])
self.elem_arr[pair[0]].value = np.dot(op, self.elem_arr[pair[0]].value)
self.elem_arr[pair[1]].value = np.dot(op, self.elem_arr[pair[1]].value)
def print_arr(self):
print()
for i in self.elem_arr:
pprint(i.value)
#%%
circ = circuit(6)
circ.gen_step(0)
print(circ.pairs)
circ.print_arr()
circ.gen_step(1)
circ.print_arr()
########################################################33
#%%
class bits:
def __init__(self):
self.value = np.array([[1, 0], [0, 1]])
class circuit:
"""
contains the bits and mixing functions
Returns
-------
None.
"""
def __init__(self, num_elems):
self.num_elems = num_elems
self.elem_arr = [bits() for x in range(self.num_elems)]
self.step_num = 0
self.pairs = []
def gen_step(self, eoo):
self.gen_pairs(eoo)
for i in self.pairs:
self.do_operation(i)
def gen_pairs(self, eoo):
self.pairs = []
# some control over where indexing starts
if eoo:
i = 0
else:
i = 1
# get pairs
while i + 2 <= self.num_elems:
self.pairs.append([i, i + 1])
i = i + 2
def do_operation(self, pair):
angle = np.pi / 4
op = np.array([[np.cos(angle), -np.sin(angle)], [np.sin(angle), np.cos(angle)]])
self.elem_arr[pair[0]].value = np.dot(op, self.elem_arr[pair[0]].value)
self.elem_arr[pair[1]].value = np.dot(op, self.elem_arr[pair[1]].value)
def print_arr(self):
print()
for i in self.elem_arr:
pprint(i.value)
#%%
circ = circuit(6)
circ.gen_step(0)
print(circ.pairs)
circ.print_arr()
circ.gen_step(1)
circ.print_arr()
#%%
"""
scalar bits
"""
class bits:
def __init__(self):
self.value = 1
class circuit:
"""
contains the bits and mixing functions
Returns
-------
None.
"""
def __init__(self, num_elems):
self.num_elems = num_elems
self.elem_arr = [bits() for x in range(self.num_elems)]
self.step_num = 0
self.pairs = []
def gen_step(self, eoo):
self.gen_pairs(eoo)
for i in self.pairs:
self.do_operation(i)
def gen_pairs(self, eoo):
self.pairs = []
# some control over where indexing starts
if eoo:
i = 0
else:
i = 1
# get pairs
while i + 2 <= self.num_elems:
self.pairs.append([i, i + 1])
i = i + 2
def do_operation(self, pair):
angle = np.pi / 4
op = np.array([[np.cos(angle), -np.sin(angle)], [np.sin(angle), np.cos(angle)]])
# get
val1 = self.elem_arr[pair[0]].value
val2 = self.elem_arr[pair[1]].value
# op
val1 = val1 + 1
val2 = val2 + 1
# write
self.elem_arr[pair[0]].value = val1
self.elem_arr[pair[1]].value = val2
def print_arr(self):
print()
for i in self.elem_arr:
print(i.value, " ", end="")