QNLP  v1.0
obrien.py
Go to the documentation of this file.
1 from .encoder_base import EncoderBase
2 
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]
def decode(self, bin_val)
Definition: obrien.py:39
def encode(self, bin_val)
Definition: obrien.py:33