QNLP
v1.0
obrien.py
Go to the documentation of this file.
1
from
.encoder_base
import
EncoderBase
2
3
class
OBrienEncoder
(
EncoderBase
):
4
"""
5
O'Brien encoding scheme. Adapated from example on http://www.ahok.de/en/hoklas-code.html
6
Triangle stepping encoding distances when excluding first and last bits.
7
For simplicity, assumes 8 unique binary patterns (aka bases).
8
For bitstrings of length 4, the encoding schemes is defined as:
9
10
0 -> 0000
11
1 -> 0001
12
2 -> 0011
13
3 -> 0010
14
4 -> 0110
15
5 -> 1110
16
6 -> 1010
17
7 -> 1011
18
8 -> 1001
19
9 -> 1000
20
21
for a total of 10 unique patterns. If we ignore patterns 0000, and 1000
22
we can have unity incremented pairwise Hamming distances between 0001
23
and others up to 1110, and decremented thereafter. This leaves us with 8
24
unique bitstrings. Assuming the graph-problem for basis ordering offers
25
a Hamiltonian path, we can (possibly) use this pattern to better represent
26
the distances between words.
27
"""
28
29
def
__init__
(self):
30
self.
enc_mapping
= { 1 : 1, 2 : 3, 3 : 2, 4 : 6, 5 : 14, 6 : 10, 7 : 11, 8 : 9 }
31
self.
dec_mapping
= { v:k
for
k,v
in
self.
enc_mapping
.items() }
32
33
def
encode
(self, bin_val):
34
val_offset = bin_val + 1
35
if
val_offset
not
in
self.
enc_mapping
:
36
raise(ValueError(
"Encoding index value not within expected range. Value={}"
.format(val_offset)))
37
return
self.
enc_mapping
[val_offset]
38
39
def
decode
(self, bin_val):
40
val_offset = bin_val + 1
41
if
val_offset
not
in
self.
dec_mapping
:
42
raise(ValueError(
"Decoding index value not within expected range. Value={}"
.format(val_offset)))
43
return
self.
dec_mapping
[val_offset]
QNLP.encoding.encoder_base.EncoderBase
Definition:
encoder_base.py:3
QNLP.encoding.obrien.OBrienEncoder
Definition:
obrien.py:3
QNLP.encoding.obrien.OBrienEncoder.decode
def decode(self, bin_val)
Definition:
obrien.py:39
QNLP.encoding.obrien.OBrienEncoder.enc_mapping
enc_mapping
Definition:
obrien.py:30
QNLP.encoding.obrien.OBrienEncoder.encode
def encode(self, bin_val)
Definition:
obrien.py:33
QNLP.encoding.obrien.OBrienEncoder.dec_mapping
dec_mapping
Definition:
obrien.py:31
QNLP.encoding.obrien.OBrienEncoder.__init__
def __init__(self)
Definition:
obrien.py:29
intel-qnlp-rc2
modules
py
pkgs
QNLP
encoding
obrien.py
Generated by
1.8.15