-
Notifications
You must be signed in to change notification settings - Fork 5
/
variants.py
214 lines (178 loc) · 6.45 KB
/
variants.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
202
203
204
205
206
207
208
209
210
211
212
213
from typing import Generic, TypeVar, Type
from copy import deepcopy
from .abstract import *
HV = TypeVar("HV", bound=BooleanAlgBHV, covariant=True)
class LinearBHVModel(Generic[HV]):
def __init__(self, bhv: Type[HV]):
self.bhv = bhv
self.used = IdSet()
def spawn(self, one: 'HV.ONE', zero: 'HV.ZERO') -> (HV, HV):
assert one not in self.used and zero not in self.used
self.used.add(one); self.used.add(zero)
# I O -> P N
# 1 0 -> 1 0 | 0 1
r = self.bhv.select_rand(one, zero)
return (r, ~r)
def xor(self, x: HV, y: HV) -> (HV, HV, HV):
assert x not in self.used and y not in self.used
self.used.add(x); self.used.add(y)
# L R -> X L R
# 0 0 -> 0 0 0
# 0 1 -> 1 0 0
# 1 0 -> 1 0 0
# 1 1 -> 0 1 1
b = x & y
return (x ^ y, b, deepcopy(b))
def thresholds3(self, x: 'HV', y: 'HV', z: 'HV') -> ('HV', 'HV', 'HV'):
assert x not in self.used and y not in self.used and z not in self.used
self.used.add(x); self.used.add(y); self.used.add(z)
# x y z -> + M -
# x y z -> + M -
# 0 0 0 -> 0 0 0
# 0 0 1 -> 0 0 1
# 0 1 0 -> 0 0 1
# 1 0 0 -> 0 0 1
# 0 1 1 -> 0 1 1
# 1 0 1 -> 0 1 1
# 1 1 0 -> 0 1 1
# 1 1 1 -> 1 1 1
return (x & y & z, self.bhv.majority([x, y, z]), x | y | z)
def and_or(self, x: 'HV', y: 'HV') -> ('HV', 'HV'):
assert x not in self.used and y not in self.used
self.used.add(x); self.used.add(y)
# x y -> & |
# 0 0 -> 0 0
# 0 1 -> 0 1
# 1 0 -> 0 1
# 1 1 -> 1 1
return (x & y, x | y)
def switch(self, cond: 'HV', left: 'HV', right: 'HV') -> ('HV', 'HV', 'HV'):
assert cond not in self.used and left not in self.used and right not in self.used
self.used.add(cond); self.used.add(left); self.used.add(right)
# C L R -> C P N
# 0 0 0 -> 0 0 0
# 0 0 1 -> 0 0 1
# 0 1 0 -> 0 1 0
# 0 1 1 -> 0 1 1
# 1 0 0 -> 1 0 0
# 1 0 1 -> 1 1 0
# 1 1 0 -> 1 0 1
# 1 1 1 -> 1 1 1
pos = self.bhv.select(cond, left, right)
neg = self.bhv.select(cond, right, left)
return (deepcopy(cond), pos, neg)
def invert(self, x: 'HV', one: 'HV.ONE', zero: 'HV.ZERO') -> ('HV', 'HV', 'HV'):
# P I O -> N A B
# 0 1 0 -> 1 0 0
# 1 1 0 -> 0 1 1
(self_1, self_2, inverted) = self.switch(x, one, zero)
return (inverted, self_1, self_2)
def permute(self, x: 'HV', permutation_id: int) -> 'HV':
assert x not in self.used
self.used.add(x)
return self.bhv.permute(permutation_id)
class Tank:
def __init__(self, capacity: float, fill=None, check=True, record=False):
self.capacity = capacity
self.level = capacity if fill is None else fill
self.check = check
self.record = record
self.updates = []
def update(self, amount: float):
if self.check:
if amount < 0:
assert self.level >= amount, "not enough to drain"
else:
assert amount <= self.capacity, "too much to fill"
if self.record:
self.updates.append(amount)
self.level += amount
def historical_levels(self):
assert self.record, "history not recorded, please pass record=True"
level = self.level
record = [level]
for update in reversed(self.updates):
level -= update
record.append(level)
record.reverse()
return record
class AdiabaticBHVModel:
def __init__(self, tank: Tank, bhv: Type[HV]):
self.tank = tank
self.bhv = bhv
self.used = IdSet()
def get_one(self) -> 'HV.ONE':
self.tank.update(-DIMENSION)
return deepcopy(self.bhv.ONE)
def get_zero(self) -> 'HV.ZERO':
self.tank.update(0)
return deepcopy(self.bhv.ZERO)
def spawn(self) -> ('HV', 'HV'):
# Note: maybe there's a more efficient way to generate random vectors in this context
# I O -> P N
# 1 0 -> 1 0 | 0 1
r = self.bhv.rand()
self.tank.update(-DIMENSION)
return (r, ~r)
def xor(self, x: 'HV', y: 'HV') -> 'HV':
assert x not in self.used and y not in self.used
self.used.add(x); self.used.add(y)
# L R -> X
# 0 0 -> 0
# 0 1 -> 1
# 1 0 -> 1
# 1 1 -> 0
b = (x & y).active()
self.tank.update(2*b)
return x ^ y
def majority3(self, x: 'HV', y: 'HV', z: 'HV') -> 'HV':
assert x not in self.used and y not in self.used and z not in self.used
self.used.add(x); self.used.add(y); self.used.add(z)
# x y z -> M
# 0 0 0 -> 0
# 0 0 1 -> 0
# 0 1 0 -> 0
# 1 0 0 -> 0
# 0 1 1 -> 1
# 1 0 1 -> 1
# 1 1 0 -> 1
# 1 1 1 -> 1
self.tank.update((x & y & z).active() + (x | y | z).active())
return self.bhv.majority([x, y, z])
def and_or(self, x: 'HV', y: 'HV') -> ('HV', 'HV'):
assert x not in self.used and y not in self.used
self.used.add(x); self.used.add(y)
# x y -> & |
# 0 0 -> 0 0
# 0 1 -> 0 1
# 1 0 -> 0 1
# 1 1 -> 1 1
self.tank.update(0)
return (x & y, x | y)
def switch(self, cond: 'HV', left: 'HV', right: 'HV') -> ('HV', 'HV', 'HV'):
assert cond not in self.used and left not in self.used and right not in self.used
self.used.add(cond); self.used.add(left); self.used.add(right)
# C L R -> C P N
# 0 0 0 -> 0 0 0
# 0 0 1 -> 0 0 1
# 0 1 0 -> 0 1 0
# 0 1 1 -> 0 1 1
# 1 0 0 -> 1 0 0
# 1 0 1 -> 1 1 0
# 1 1 0 -> 1 0 1
# 1 1 1 -> 1 1 1
pos = cond.select(left, right)
neg = cond.select(right, left)
self.tank.update(0)
return (deepcopy(cond), pos, neg)
def invert(self, x: 'HV') -> 'HV':
assert x not in self.used
self.used.add(x)
# P I O -> N A B
# 0 1 0 -> 1 0 0
# 1 1 0 -> 0 1 1
self.tank.update(2*(x.active() - DIMENSION//2))
return ~x
def permute(self, permutation_id: int) -> 'HV':
self.tank.update(0)
return self.bhv.permute(permutation_id)