QNLP  v1.0
QNLP.encoding.utils Namespace Reference

Functions

def pow2bits (bin_val)
 
def get_type_offsets (encoding_dict)
 
def get_type_masks (encoding_dict, type_offsets=None)
 
def bin_to_sentence (bin_val, encoding_dict, decoding_dict, type_offsets=None)
 
def gen_state_string (l)
 
def HammingInt (int i1, int i2)
 
def num_qubits (dict enc_dict)
 
def encode_binary_pattern ((str, str, str) pattern, dict enc_dict)
 
def encode_binary_pattern_direct ([int, int, int] pattern, dict enc_dict)
 
def decode_binary_pattern (int pattern, dict dec_dict)
 

Function Documentation

◆ bin_to_sentence()

def QNLP.encoding.utils.bin_to_sentence (   bin_val,
  encoding_dict,
  decoding_dict,
  type_offsets = None 
)
Map the integer binary value to the result from the encoded basis mappings.

Definition at line 38 of file utils.py.

38 def bin_to_sentence(bin_val, encoding_dict, decoding_dict, type_offsets=None):
39  """
40  Map the integer binary value to the result from the encoded basis mappings.
41  """
42  if type_offsets == None:
43  l_ns, l_v, l_no = get_type_offsets(encoding_dict)
44  else:
45  l_ns, l_v, l_no = type_offsets
46 
47  bitmask_ns, bitmask_v, bitmask_no = get_type_masks(encoding_dict, type_offsets=(l_ns, l_v, l_no) )
48 
49  ns_val = (bin_val & bitmask_ns) >> (l_v[1] + l_no[1])
50  v_val = (bin_val & bitmask_v) >> l_no[1]
51  no_val = (bin_val & bitmask_no)
52 
53  return decoding_dict["ns"][ns_val], decoding_dict["v"][v_val], decoding_dict["no"][no_val]
54 
55 
def get_type_masks(encoding_dict, type_offsets=None)
Definition: utils.py:24
def bin_to_sentence(bin_val, encoding_dict, decoding_dict, type_offsets=None)
Definition: utils.py:38
def get_type_offsets(encoding_dict)
Definition: utils.py:18

References QNLP.encoding.utils.get_type_masks(), and QNLP.encoding.utils.get_type_offsets().

Here is the call graph for this function:

◆ decode_binary_pattern()

def QNLP.encoding.utils.decode_binary_pattern ( int  pattern,
dict  dec_dict 
)
Pass in the pattern given as the encoded integer.
The pattern will be deconstructed into the appropriate token tuple
and output for plotting/printing. Inverse of encode_binary_pattern.

Definition at line 105 of file utils.py.

105 def decode_binary_pattern(pattern : int, dec_dict : dict):
106  """
107  Pass in the pattern given as the encoded integer.
108  The pattern will be deconstructed into the appropriate token tuple
109  and output for plotting/printing. Inverse of encode_binary_pattern.
110  """
111  num_ns, num_v, num_no = num_qubits(dec_dict)
112 
113  t_ns = pattern & (2**num_ns -1)
114  t_v = ( pattern >> num_ns ) & (2**num_v -1)
115  t_no = ( pattern >> (num_ns + num_v) ) & (2**num_no -1)
116 
117  return (dec_dict["ns"][t_ns], dec_dict["v"][t_v], dec_dict["no"][t_no])
def decode_binary_pattern(int pattern, dict dec_dict)
Definition: utils.py:105
int num_qubits
Definition: simple_MPI.py:10

References QNLP.encoding.utils.num_qubits().

Here is the call graph for this function:

◆ encode_binary_pattern()

def QNLP.encoding.utils.encode_binary_pattern ( (str,str,str)  pattern,
dict  enc_dict 
)
Pass in the pattern given by the basis token values as (ns,v,no)
The pattern will be constructed into the appropriate binary string
and output for encoding.

Definition at line 80 of file utils.py.

80 def encode_binary_pattern(pattern : (str,str,str), enc_dict : dict):
81  """
82  Pass in the pattern given by the basis token values as (ns,v,no)
83  The pattern will be constructed into the appropriate binary string
84  and output for encoding.
85  """
86  num_ns, num_v, num_no = num_qubits(enc_dict)
87  result = enc_dict["ns"][pattern[0]] | \
88  enc_dict["v"][pattern[1]] << num_ns | \
89  enc_dict["no"][pattern[2]] << (num_ns + num_v)
90 
91  return result
92 
def encode_binary_pattern((str, str, str) pattern, dict enc_dict)
Definition: utils.py:80
int num_qubits
Definition: simple_MPI.py:10

References QNLP.encoding.utils.num_qubits().

Here is the call graph for this function:

◆ encode_binary_pattern_direct()

def QNLP.encoding.utils.encode_binary_pattern_direct ( [int,int,int]  pattern,
dict  enc_dict 
)
Pass in the pattern given by the basis token values as (ns,v,no)
The pattern will be constructed into the appropriate binary string
and output for encoding.

Definition at line 93 of file utils.py.

93 def encode_binary_pattern_direct(pattern : [int,int,int], enc_dict : dict):
94  """
95  Pass in the pattern given by the basis token values as (ns,v,no)
96  The pattern will be constructed into the appropriate binary string
97  and output for encoding.
98  """
99  num_ns, num_v, num_no = num_qubits(enc_dict)
100  result = pattern[0] | pattern[1] << num_ns | pattern[2] << (num_ns + num_v)
101 
102  return result
103 
104 
int num_qubits
Definition: simple_MPI.py:10
def encode_binary_pattern_direct([int, int, int] pattern, dict enc_dict)
Definition: utils.py:93

References QNLP.encoding.utils.num_qubits().

Here is the call graph for this function:

◆ gen_state_string()

def QNLP.encoding.utils.gen_state_string (   l)
Given a list of strings, generate the latex '\\vert {} \\rangle' representation of this in superposition.

Definition at line 56 of file utils.py.

56 def gen_state_string(l):
57  """Given a list of strings, generate the latex '\\vert {} \\rangle' representation of this in superposition."""
58  result = ""
59  str_template = r"\vert {} \rangle"
60  for idx,i in enumerate(l):
61  result += r"b_{{{}}}\vert \textrm{{{}}} \rangle ".format(idx, i[0])
62  if i != l[-1]:
63  result += " + "
64  return result
65 
def gen_state_string(l)
Definition: utils.py:56

◆ get_type_masks()

def QNLP.encoding.utils.get_type_masks (   encoding_dict,
  type_offsets = None 
)

Definition at line 24 of file utils.py.

24 def get_type_masks(encoding_dict, type_offsets=None):
25  # Create the bit_masks of the required length to decode the value from bin_val
26  if type_offsets == None:
27  l_ns, l_v, l_no = get_type_offsets(encoding_dict)
28  else:
29  l_ns, l_v, l_no = type_offsets
30 
31  bitmask_ns = (l_ns[0]-1) << (l_v[1] + l_no[1])
32  bitmask_v = (l_v[0]-1) << l_no[1]
33  bitmask_no = (l_no[0]-1)
34 
35  return bitmask_ns, bitmask_v, bitmask_no
36 
37 
def get_type_masks(encoding_dict, type_offsets=None)
Definition: utils.py:24
def get_type_offsets(encoding_dict)
Definition: utils.py:18

References QNLP.encoding.utils.get_type_offsets().

Referenced by QNLP.encoding.utils.bin_to_sentence().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_type_offsets()

def QNLP.encoding.utils.get_type_offsets (   encoding_dict)

Definition at line 18 of file utils.py.

18 def get_type_offsets(encoding_dict):
19  l_ns = pow2bits( int(np.max( list(encoding_dict['ns'].values()))) )
20  l_v = pow2bits( int(np.max( list(encoding_dict['v'].values()))) )
21  l_no = pow2bits( int(np.max( list(encoding_dict['no'].values()))) )
22  return l_ns, l_v, l_no
23 
def pow2bits(bin_val)
Definition: utils.py:4
def get_type_offsets(encoding_dict)
Definition: utils.py:18

References QNLP.encoding.utils.pow2bits().

Referenced by QNLP.encoding.utils.bin_to_sentence(), and QNLP.encoding.utils.get_type_masks().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ HammingInt()

def QNLP.encoding.utils.HammingInt ( int  i1,
int  i2 
)
Simple integer based hamming distance for bits

Definition at line 66 of file utils.py.

66 def HammingInt(i1 : int, i2 : int):
67  """Simple integer based hamming distance for bits"""
68  return bin(i1 ^ i2).count("1")
69 
def HammingInt(int i1, int i2)
Definition: utils.py:66

References QNLP_Python_MPI.count.

◆ num_qubits()

def QNLP.encoding.utils.num_qubits ( dict  enc_dict)
Assuming simple.py encoding and give qubits as num_tokens/2 per meaning space

Definition at line 70 of file utils.py.

70 def num_qubits(enc_dict : dict):
71  """
72  Assuming simple.py encoding and give qubits as num_tokens/2 per meaning space
73  """
74 
75  num_ns = len(enc_dict['ns'])//2
76  num_v = len(enc_dict['v'])//2
77  num_no = len(enc_dict['no'])//2
78  return num_ns, num_v, num_no
79 
int num_qubits
Definition: simple_MPI.py:10

Referenced by QNLP.encoding.utils.decode_binary_pattern(), QNLP.encoding.utils.encode_binary_pattern(), and QNLP.encoding.utils.encode_binary_pattern_direct().

Here is the caller graph for this function:

◆ pow2bits()

def QNLP.encoding.utils.pow2bits (   bin_val)
Convert the integer value to a tuple of the next power of two, 
and number of bits required to represent it.

Definition at line 4 of file utils.py.

4 def pow2bits(bin_val):
5  """
6  Convert the integer value to a tuple of the next power of two,
7  and number of bits required to represent it.
8  """
9  result = -1
10  bits = (bin_val).bit_length()
11  if bin_val > 1:
12  result = 2**(bits)
13  else:
14  result = 1
15  bits = 1
16  return result, bits
17 
def pow2bits(bin_val)
Definition: utils.py:4

Referenced by QNLP.encoding.utils.get_type_offsets().

Here is the caller graph for this function: