cana.cutils module
- cana.cutils.binstate_compare(binstate1, binstate2)
Compare each element in two binary states
- Parameters:
binstate1 – the two binary states to be compared
binstate2 – the two binary states to be compared
- Returns:
a list of comparisons
- Return type:
c (list, bool)
- cana.cutils.binstate_pinned_to_binstate(binstate, pinned_binstate, pinned_var)
Combines two binstates based on the locations of pinned variables.
- Parameters:
binstate (str) – the binary state of non-pinned variables
pinned_binstate (str) – the binary states of the pinned variables
pinned_var (list of int) – the list of pinned variables
- Returns:
The combined binary state.
- Return type:
string
See also
:attr:’statenum_to_binstate’
- cana.cutils.binstate_to_constantbinstate(binstate, constant_template)
- cana.cutils.binstate_to_density(binstate)
Converts from binary state to density
- Parameters:
binstate (string) – The binary state
- Returns:
int
Example
>>> binstate_to_density('1110') >>> 3
- cana.cutils.binstate_to_statenum(binstate)
Converts from binary state to state number.
- Parameters:
binstate (string) – The binary state.
- Returns:
The state number.
- Return type:
int
Example
'000' -> 0 '001' -> 1 '010' -> 2 ...
See also
- cana.cutils.constantbinstate_to_statenum(constantbinstate, constant_template)
- cana.cutils.expand_logic_line(line)
This generator expands a logic line containing don’t-care bits (
2preferred,-also accepted; e.g.002 0or0-0 1) to a series of logic lines containing only0and1.- Parameters:
line (string) – The logic line. Format is <binary-state><space><output>.
- Returns:
a series of logic lines
- Return type:
generator
Example
>>> expand_logic_line('122 0') >>> 100 0 >>> 101 0 >>> 110 0 >>> 111 0
- cana.cutils.flip_binstate_bit(binstate, idx)
Flips the binary value of a bit in a binary state. :param binstate: A string of binary states. :type binstate: string :param idx: The index of the bit to flip. :type idx: int
- Returns:
New binary state.
- Return type:
(string)
Example
- cana.cutils.flip_bit(bit)
Flips the binary value of a state.
- Parameters:
bit (string/int/bool) – The current bit position
- Returns:
The flipped bit
- Return type:
same as input
- cana.cutils.hamming_distance(s1, s2)
Calculates the hamming distance between two configurations strings.
- Parameters:
s1 (string) – First string
s2 (string) – Second string
- Returns:
The Hamming distance
- Return type:
float
Example
>>> hamming_distance('001','101') >>> 1
- cana.cutils.outputs_to_binstates_of_given_type(outputs, output, k)
Converts a node output list into a list of binary states given a specific output type. For instance, for the AND boolean function, with outputs [0, 0, 0, 1] it returns [‘00’, ‘01’, ‘10’] when output_type = 0 and [‘11’] when output_type = 1.
- Parameters:
outputs (list/str) – the list of outputs of a given node.
output (int/str) – a certain output type. 0 or 1 in the case of Binary outputs.
k (int) – input degree
- Returns:
a list containing all the binstates of a certain output type
- Return type:
list
- cana.cutils.random_binstate(length, random_seed=None)
Generates a random binary state of a given length
- Parameters:
length (int) – the length of the binary state
- Returns:
a random binary state
- Return type:
binstate (str)
- cana.cutils.recursive_map(f, d)
Normal python map, but recursive
- Parameters:
f (function) – a function to be applied to every item of the iterable
d (iterable) – the iterable to which f will be applied itemwise.
- cana.cutils.statenum_to_binstate(statenum, base)
Converts an interger into the binary string.
- Parameters:
statenum (int) – The state number.
base (int) – The binary base
- Returns:
The binary state.
- Return type:
string
Example
0 -> '00' (base 2) 1 -> '01' (base 2) 2 -> '10' (base 2) ... 0 -> '000' (base 3) 1 -> '001' (base 3) 2 -> '010' (base 3)
See also
- cana.cutils.statenum_to_density(statenum)
Converts from state number to density
- Parameters:
statenum (int) – The state number
- Returns:
The density of
1in that specific binary state number.- Return type:
int
Example
>>> statenum_to_binstate(14, base=2) >>> '1110' >>> statenum_to_density(14) >>> 3
- cana.cutils.statenum_to_output_list(statenum, base)
Converts an interger into a list of 0 and 1, thus can feed to BooleanNode.from_bit_lut()
- Parameters:
statenum (int) – the state number
base (int) – the length of output list
- Returns:
a list of length base, consisting of 0 and 1
- Return type:
list
See also
:attr:’statenum_to_binstate’