-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstate_machine.py
39 lines (32 loc) · 1.14 KB
/
state_machine.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
# TurnstileExample
# Implementing the Turnstile state machine with Hypervectors
# The state machine: https://en.wikipedia.org/wiki/Finite-state_machine#Example:_coin-operated_turnstile
from bhv.vanilla import VanillaBHV as BHV, VanillaPermutation as Perm
# states
locked = BHV.rand()
unlocked = BHV.rand()
# input symbols
token = BHV.rand()
push = BHV.rand()
# next state permutation
PNext = Perm.random()
# inverse for querying the next state
QNext = ~PNext
transition = BHV.majority([
(push ^ locked ^ PNext(locked)),
(token ^ locked ^ PNext(unlocked)),
(push ^ unlocked ^ PNext(locked)),
(token ^ unlocked ^ PNext(unlocked))
])
# note this doesn't exactly give the right state
def next_state(state, input):
return QNext(transition ^ input ^ state)
# so we make a noisy lookup table
table = [locked, unlocked]
def closest(noisy):
return min(table, key=noisy.hamming)
# and check if the transition system works as expected
assert closest(next_state(locked, push)) == locked
assert closest(next_state(locked, token)) == unlocked
assert closest(next_state(unlocked, push)) == locked
assert closest(next_state(unlocked, token)) == unlocked