quast_decisiontree.utils.functions
quast_decisiontree.utils.functions
utility functions
logger
module-attribute
logger = logging.getLogger('dt_logger')
bitstring_to_list
bitstring_to_list(bitstring)
converts a binary bitstring to a list of 0's and 1's Reverses order.
Source code in src/quast_decisiontree/utils/functions.py
30 31 32 33 34 | |
eager_import
eager_import(module_dict, package)
performs eager import of all names in the module_dict from the given modules
Module names are assumed to be relative.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module_dict
|
Mapping
|
A dictionary with keys corresponding to relative module names, and the values being a list of attributes to be imported from those modules. |
required |
package
|
str
|
Base package where to load from. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
a dictionary of the imported objects |
Source code in src/quast_decisiontree/utils/functions.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
to_dict
to_dict(eigenstate)
Converts an eigenstate given as a list or dictionary into a dictionary suited for JSON serialization.
The input is never mutated; a new dictionary is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
eigenstate
|
Union[Mapping, Sequence]
|
Either a list with amplitudes in lexicographic order or a dictionary of (bitstring, amplitude) pairs. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the given eigenstate is a list whose length isn't a power of 2. |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
A dict with entries (bitstring, value) where value will either be a real number (amplitude) or a tuple of real numbers (real, imag) representing a complex number. |
Source code in src/quast_decisiontree/utils/functions.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | |
list_to_dict
list_to_dict(perhaps_a_dict)
converts a list to a dictionary by using the indices as keys
Parameters: perhaps_a_dict - list or dict. If dict, function will return the input. If list, input will be converted to a dict
Returns a dictionary equivalent to the input list or equal to the input dictionary.
Source code in src/quast_decisiontree/utils/functions.py
101 102 103 104 105 106 107 108 109 110 111 112 | |
load_results
load_results(filename)
loads results from the specified path to a list of dictionaries
Source code in src/quast_decisiontree/utils/functions.py
115 116 117 118 119 120 | |
get_most_likely_states
get_most_likely_states(state_dict, num_states=1)
fetches and returns the num_states most likely states from a dictionary of the form bitstring: value
Parameters: state_dict dictionary comprised of bitstrings as keys and probability values as values num_states the number of most likely states to fetch
Returns a dict with the most likely states in the same dict form
Source code in src/quast_decisiontree/utils/functions.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
to_iterable
to_iterable(perhaps_a_list)
returns the argument if it is iterable and not a string (since we assume we don't want to naively iterate over strings) and a list with the argument otherwise
Source code in src/quast_decisiontree/utils/functions.py
141 142 143 144 145 146 147 | |
get_minimal_difference
get_minimal_difference(values, zero_threshold=1e-08)
Determines the minimum nonzero distance between the two closest elements in an array, treating differences below zero_threshold as zero.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
Sequence
|
The array containing the elements to compare. |
required |
zero_threshold
|
float
|
The threshold below which two elements of the array will be considered as equal. Defaults to 1e-8. |
1e-08
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The minimum nonzero distance between elements in the array. |
Source code in src/quast_decisiontree/utils/functions.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
qubo_tensor_to_matrix
qubo_tensor_to_matrix(qubo_tensor)
Reshapes a qubo tensor (an object characterized by more than 2 indices allowing for binary variables addressed by multiple indices) into a simple QUBO matrix by serializing the binary variables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
qubo_tensor
|
ndarray
|
The input tensor. It needs to have an even number of indices, and the shape needs to be of form (x_1, ... ,x_n, x_1, ..., x_n) |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the qubo tensor has an invalid shape. |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: The qubo matrix with serialized indices. |
Source code in src/quast_decisiontree/utils/functions.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | |
is_qubo_matrix
is_qubo_matrix(opt_problem)
checks if the input opt_problem is a qubo matrix (that is, can be cast to a numpy array, is quadratic and 2D)
Source code in src/quast_decisiontree/utils/functions.py
215 216 217 218 219 220 221 222 223 224 225 | |
get_ising_offset
get_ising_offset(qubo_matrix)
Computes the Ising offset of a QUBO matrix (or flattened tensor).
The offset is given by 1/4 * (sum_of_all_elements + sum_of_diagonal).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
qubo_matrix
|
ndarray
|
A QUBO matrix or tensor. If a tensor (ndim > 2), it will be flattened to a matrix first. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The Ising offset. |
Source code in src/quast_decisiontree/utils/functions.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | |
get_qubo_value
get_qubo_value(qubo_matrix, sample)
Calculates the value a sample generates with the given QUBO formulation
Parameters qubo_matrix the matrix of the qubo formulation sample bitstring with the same amounts of bits as the length of the dimensions of the qubo matrix
Returns qubo_value Value the sample generates
Source code in src/quast_decisiontree/utils/functions.py
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | |
get_qaoa_scaling_factor
get_qaoa_scaling_factor(
qubo_matrix,
*,
mixer_spectral_width=None,
mixer_spacing=None,
strategy="eigenvalue_spacing",
)
Attempts to find the scaling factor according to the specified strategy.
Returns 1 if the strategy fails due to the matrix having only one (distinct) eigenvalue.
Parameters: qubo_matrix - the qubo matrix representing the problem Hamiltonian mixer_spectral_width - the spectral width of the mixer. If None, defaults to 2*num_nodes mixer_spacing - the minimal spacing between eigenvalues of the mixer. Defaults to 2. strategy - "spectral_width", "eigenvalue_spacing", or "ground_state_gap"
Returns a scaling factor according to the specified strategy.
Source code in src/quast_decisiontree/utils/functions.py
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | |
find_qubo_penalty
find_qubo_penalty(
problem_instance,
initial_penalty=0,
penalty_step=1,
safety_margin=2,
scaling_factor=1,
num_reads=1,
mode="QUBO_condensed",
strategy="tabu",
)
Attempts to find a good penalty value by classically solving the problem such that the solution is feasible, then adding a safety margin to it.
Parameters: problem_instance The instance of an optimization problem initial_penalty Starting value for the penalty penalty_step how much to increase the penalty at each loop safety_margin safety_margin*penalty_step will be added at the end scaling_factor the scaling factor of the cost function num_reads number of reads the tabu search algorithm uses mode Mode to be used by the formulate_problem function strategy "tabu", "brute_force", or "max_cost"
Returns the penalty value determined by the selected strategy.
Source code in src/quast_decisiontree/utils/functions.py
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 | |
build_varname
build_varname(indices, basename='x')
returns a string of the form x_i_j for the specified set of indices
Source code in src/quast_decisiontree/utils/functions.py
466 467 468 | |
build_indices_from_varname
build_indices_from_varname(varname)
returns the indices from a string varname (inverse to build_varname) as a tuple
Source code in src/quast_decisiontree/utils/functions.py
471 472 473 | |
orthonormalize
orthonormalize(matrix)
returns an orthonormalized version of the matrix with row vectors (!) normalized and orthogonalized by Gram-Schmidt
Source code in src/quast_decisiontree/utils/functions.py
476 477 478 479 480 481 482 483 484 485 486 487 488 489 | |
to_rgb
to_rgb(color)
returns a string with the rgb format that can be used to specify color in plotly
Source code in src/quast_decisiontree/utils/functions.py
492 493 494 495 496 497 498 499 500 501 502 | |
binary_to_ising
binary_to_ising(vector)
converts a binary vector to an Ising vector
Source code in src/quast_decisiontree/utils/functions.py
505 506 507 508 509 510 511 512 513 514 515 | |
ising_to_binary
ising_to_binary(vector)
converts an Ising vector to a binary vector.
Source code in src/quast_decisiontree/utils/functions.py
518 519 520 521 522 523 524 525 526 527 528 | |
optimality_ratio
optimality_ratio(result, optimality_test, feasibility_test)
computes the optimality ratio (optimal occurrence over feasible occurrence)
Returns 0 (the worst possible ratio) if no feasible state is present.
Arguments: result: A dictionary with an "eigenstate" item as {bitstring: value} feasibility_test: function(bitstring) -> bool optimality_test: function(bitstring) -> bool
Source code in src/quast_decisiontree/utils/functions.py
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 | |
one_hot_to_integer
one_hot_to_integer(bitstring, _num_nodes=None)
converts a one-hot encoded bitstring to a list of integers.
Attention: Returned integers are 1-based to facilitate transforming to reduced qubo indices for TSP (which prepends a zero)
Source code in src/quast_decisiontree/utils/functions.py
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 | |
binary_to_integer
binary_to_integer(bitstring, num_nodes)
converts a binary encoded bitstring to a list of integers.
Attention: Returned integers are 1-based.
Source code in src/quast_decisiontree/utils/functions.py
601 602 603 604 605 606 607 608 609 610 611 612 | |
edge_to_integer
edge_to_integer(bitstring, _num_nodes=None)
Converts a bitstring which marks all occurring edges to an integer array of visited nodes in respective order. The first city is set to zero.
Source code in src/quast_decisiontree/utils/functions.py
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 | |
integer_to_one_hot
integer_to_one_hot(integer_list)
converts an integer list to a one-hot encoded binary array
Source code in src/quast_decisiontree/utils/functions.py
634 635 636 637 638 639 640 641 642 643 | |
integer_to_binary
integer_to_binary(integer_list)
Converts a path in the form of a list of integers into a binary encoding
Source code in src/quast_decisiontree/utils/functions.py
646 647 648 649 650 651 652 653 654 655 656 657 658 | |
integer_to_edge
integer_to_edge(integer_list)
Converts an integer list of visited nodes to a binary list marking edges.
Source code in src/quast_decisiontree/utils/functions.py
661 662 663 664 665 666 667 668 669 670 671 672 673 | |
recursive_list_shape
recursive_list_shape(list_like)
returns a tuple describing the list shape if it is n-dimensional rectangular
Source code in src/quast_decisiontree/utils/functions.py
676 677 678 679 680 681 682 683 684 685 686 687 688 689 | |
list_shape
list_shape(list_like)
Makes sure all elements of list_like have the same shape as given by the length attribute.
Returns the length of the list items if they are all equal, False otherwise.
Source code in src/quast_decisiontree/utils/functions.py
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 | |
feasibility_ratio
feasibility_ratio(
eigenstate,
feasibility_test,
length=None,
encoding="one-hot",
)
Calculates the feasibility ratio of an eigenstate.
Returns 0 (the worst possible ratio) if the eigenstate carries no weight.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
eigenstate
|
Mapping
|
The eigenstate given as a dictionary {bitstring: value} |
required |
feasibility_test
|
Callable
|
Function(bitstring, length, encoding) -> bool |
required |
length
|
Optional[float]
|
Range of integer variables (needed for binary encoding) |
None
|
encoding
|
str
|
Encoding of the bitstring. Defaults to "one-hot". |
'one-hot'
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The feasibility ratio of the eigenstate. |
Source code in src/quast_decisiontree/utils/functions.py
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 | |
filter_results
filter_results(results, property_name, property_value)
filters a list of results by searching for a nested property value
Arguments: results: a list of results in dictionary form property_name: nested dict key with "." separator (e.g. "metadata.id") property_value: the value to match
Source code in src/quast_decisiontree/utils/functions.py
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 | |
get_bitstring_from_tabu_result
get_bitstring_from_tabu_result(tabu_result)
produces the most likely state from a tabu result as a bit string
Source code in src/quast_decisiontree/utils/functions.py
774 775 776 | |
normalize_counts
normalize_counts(counts)
Normalize measurement results to probabilities (sum to 1.0).
Accepts both integer counts and probability distributions.
Source code in src/quast_decisiontree/utils/functions.py
779 780 781 782 783 784 785 786 787 | |