mqt.problemsolver.qubo_tools.pathfinder¶
This module implements the pathfinding functionalities of the qubo_tools.
This, in particular, includes all pathfinding-related cost functions and the specialized QUBO generator for pathfinding problems.
Typical usage example:
```python import mqt.problemsolver.qubo_tools.pathfinder as pf
graph = pf.Graph(5, [(1, 2, 5), (2, 3, 3), (3, 4, 9), (4, 5, 8), (5, 1, 6)]) settings = pf.PathFindingQuboGeneratorSettings(pf.EncodingType.ONE_HOT, 1, 5, True) generator = pf.PathFindingQuboGenerator(pf.MinimizePathLength([1]), graph, settings) generator.add_constraint(pf.PathIsValid([1])) generator.add_constraint(pf.PathContainsVerticesExactlyOnce(graph.all_vertices, [1]))
Submodules¶
Package Contents¶
- class CompositeCostFunction(*parts: tuple[CostFunction, int])[source]¶
Bases:
CostFunctionA composite cost function that is the sum of multiple cost functions.
- summands¶
A list of tuples of cost functions and their weights.
- Type:
list[tuple[CostFunction, int]]
- summands: list[tuple[CostFunction, int]]¶
- __str__() str[source]¶
Returns a string representation of the cost function.
- Returns:
A string representation of the cost function.
- Return type:
- get_formula(graph: Graph, settings: PathFindingQuboGeneratorSettings) sympy.Expr[source]¶
Translates the cost function into a QUBO expression.
- Parameters:
graph (Graph) – The graph on which the cost function should be applied.
settings (PathFindingQuboGeneratorSettings) – The settings of the QUBO generator.
- Returns:
An expression representing the cost function as a QUBO function.
- Return type:
sp.Expr
- get_formula_general(graph: Graph, settings: PathFindingQuboGeneratorSettings, get_variable_function: GetVariableFunction) sympy.Expr[source]¶
Returns the QUBO expression for the cost function in the general case.
In the general case, the cost function may access the blackbox function get_variable_function(p, v, i) that returns 1 if vertex v is located at position i in path p and 0 otherwise.
- Parameters:
graph (Graph) – The graph on which the cost function should be applied.
settings (PathFindingQuboGeneratorSettings) – The settings of the QUBO generator.
get_variable_function (GetVariableFunction) – The blackbox function for accessing the encoding variables.
- Returns:
An expression representing the cost function as a QUBO function.
- Return type:
sp.Expr
- class CostFunction[source]¶
Bases:
abc.ABCAn abstract base class for cost functions.
Represents a cost function that can be translated into a QUBO expression.
- get_formula(graph: Graph, settings: PathFindingQuboGeneratorSettings) sympy.Expr[source]¶
Translates the cost function into a QUBO expression.
- Parameters:
graph (Graph) – The graph on which the cost function should be applied.
settings (PathFindingQuboGeneratorSettings) – The settings of the QUBO generator.
- Returns:
An expression representing the cost function as a QUBO function.
- Return type:
sp.Expr
- abstractmethod get_formula_general(graph: Graph, settings: PathFindingQuboGeneratorSettings, get_variable_function: GetVariableFunction) sympy.Expr[source]¶
Returns the QUBO expression for the cost function in the general case.
In the general case, the cost function may access the blackbox function get_variable_function(p, v, i) that returns 1 if vertex v is located at position i in path p and 0 otherwise.
- Parameters:
graph (Graph) – The graph on which the cost function should be applied.
settings (PathFindingQuboGeneratorSettings) – The settings of the QUBO generator.
get_variable_function (GetVariableFunction) – The blackbox function for accessing the encoding variables.
- Returns:
An expression representing the cost function as a QUBO function.
- Return type:
sp.Expr
- get_formula_one_hot(graph: Graph, settings: PathFindingQuboGeneratorSettings) sympy.Expr[source]¶
Computes the QUBO expression for the cost function for One-Hot encoding.
- Parameters:
graph (Graph) – The graph on which the cost function should be applied.
settings (PathFindingQuboGeneratorSettings) – The settings of the QUBO generator.
- Returns:
An expression representing the cost function as a QUBO function.
- Return type:
sp.Expr
- get_formula_domain_wall(graph: Graph, settings: PathFindingQuboGeneratorSettings) sympy.Expr[source]¶
Computes the QUBO expression for the cost function for Domain-Wall encoding.
- Parameters:
graph (Graph) – The graph on which the cost function should be applied.
settings (PathFindingQuboGeneratorSettings) – The settings of the QUBO generator.
- Returns:
An expression representing the cost function as a QUBO function.
- Return type:
sp.Expr
- get_formula_binary(graph: Graph, settings: PathFindingQuboGeneratorSettings) sympy.Expr[source]¶
Computes the QUBO expression for the cost function for Binary encoding.
- Parameters:
graph (Graph) – The graph on which the cost function should be applied.
settings (PathFindingQuboGeneratorSettings) – The settings of the QUBO generator.
- Returns:
An expression representing the cost function as a QUBO function.
- Return type:
sp.Expr
- class EncodingType[source]¶
Bases:
enum.EnumThe encoding types supported for the QUBO generation.
- ONE_HOT = 1¶
- DOMAIN_WALL = 2¶
- BINARY = 3¶
- class MaximizePathLength(path_ids: list[int])[source]¶
Bases:
PathBoundA cost function that penalises paths based on their length.
A lower total weight causes a bigger penalty.
- get_formula_general(graph: Graph, settings: PathFindingQuboGeneratorSettings, get_variable_function: GetVariableFunction) sympy.Expr[source]¶
Returns the QUBO expression for the cost function in the general case.
In the general case, the cost function may access the blackbox function get_variable_function(p, v, i) that returns 1 if vertex v is located at position i in path p and 0 otherwise.
- Parameters:
graph (Graph) – The graph on which the cost function should be applied.
settings (PathFindingQuboGeneratorSettings) – The settings of the QUBO generator.
get_variable_function (GetVariableFunction) – The blackbox function for accessing the encoding variables.
- Returns:
An expression representing the cost function as a QUBO function.
- Return type:
sp.Expr
- class MinimizePathLength(path_ids: list[int])[source]¶
Bases:
PathBoundA cost function that penalises paths based on their length.
A bigger total weight causes a bigger penalty.
- get_formula_general(graph: Graph, settings: PathFindingQuboGeneratorSettings, get_variable_function: GetVariableFunction) sympy.Expr[source]¶
Returns the QUBO expression for the cost function in the general case.
In the general case, the cost function may access the blackbox function get_variable_function(p, v, i) that returns 1 if vertex v is located at position i in path p and 0 otherwise.
- Parameters:
graph (Graph) – The graph on which the cost function should be applied.
settings (PathFindingQuboGeneratorSettings) – The settings of the QUBO generator.
get_variable_function (GetVariableFunction) – The blackbox function for accessing the encoding variables.
- Returns:
An expression representing the cost function as a QUBO function.
- Return type:
sp.Expr
- class PathBound(path_ids: list[int])[source]¶
Bases:
CostFunctionAn abstract cost function for penalties limited to an individual path.
- class PathComparison(path_one: int, path_two: int)[source]¶
Bases:
CostFunctionAn abstract cost function for penalties that compare two paths.
path_two (int): The second path.
- class PathContainsEdges(min_occurrences: int, max_occurrences: int, edges: list[tuple[int, int]], path_ids: list[int])[source]¶
Bases:
CostFunctionA cost function that penalises paths that do not contain a given set of edges.
- class PathContainsEdgesAtLeastOnce(edges: list[tuple[int, int]], path_ids: list[int])[source]¶
Bases:
PathContainsEdgesA cost function that penalises paths that do not contain a given set of edges at least once.
- get_formula_general(graph: Graph, settings: PathFindingQuboGeneratorSettings, get_variable_function: GetVariableFunction) sympy.Expr[source]¶
Returns the QUBO expression for the cost function in the general case.
In the general case, the cost function may access the blackbox function get_variable_function(p, v, i) that returns 1 if vertex v is located at position i in path p and 0 otherwise.
- Parameters:
graph (Graph) – The graph on which the cost function should be applied.
settings (PathFindingQuboGeneratorSettings) – The settings of the QUBO generator.
get_variable_function (GetVariableFunction) – The blackbox function for accessing the encoding variables.
- Returns:
An expression representing the cost function as a QUBO function.
- Return type:
sp.Expr
- class PathContainsEdgesAtMostOnce(edges: list[tuple[int, int]], path_ids: list[int])[source]¶
Bases:
PathContainsEdgesA cost function that penalises paths that do not contain a given set of edges at most once.
- get_formula_general(graph: Graph, settings: PathFindingQuboGeneratorSettings, get_variable_function: GetVariableFunction) sympy.Expr[source]¶
Returns the QUBO expression for the cost function in the general case.
In the general case, the cost function may access the blackbox function get_variable_function(p, v, i) that returns 1 if vertex v is located at position i in path p and 0 otherwise.
- Parameters:
graph (Graph) – The graph on which the cost function should be applied.
settings (PathFindingQuboGeneratorSettings) – The settings of the QUBO generator.
get_variable_function (GetVariableFunction) – The blackbox function for accessing the encoding variables.
- Returns:
An expression representing the cost function as a QUBO function.
- Return type:
sp.Expr
- class PathContainsEdgesExactlyOnce(edges: list[tuple[int, int]], path_ids: list[int])[source]¶
Bases:
PathContainsEdgesA cost function that penalises paths that do not contain a given set of edges exactly once.
- get_formula_general(graph: Graph, settings: PathFindingQuboGeneratorSettings, get_variable_function: GetVariableFunction) sympy.Expr[source]¶
Returns the QUBO expression for the cost function in the general case.
In the general case, the cost function may access the blackbox function get_variable_function(p, v, i) that returns 1 if vertex v is located at position i in path p and 0 otherwise.
- Parameters:
graph (Graph) – The graph on which the cost function should be applied.
settings (PathFindingQuboGeneratorSettings) – The settings of the QUBO generator.
get_variable_function (GetVariableFunction) – The blackbox function for accessing the encoding variables.
- Returns:
An expression representing the cost function as a QUBO function.
- Return type:
sp.Expr
- class PathContainsVertices(min_occurrences: int, max_occurrences: int, vertex_ids: list[int], path_ids: list[int])[source]¶
Bases:
CostFunctionA cost function that penalises paths that do not contain a given set of vertices.
- class PathContainsVerticesAtLeastOnce(vertex_ids: list[int], path_ids: list[int])[source]¶
Bases:
PathContainsVerticesA cost function that penalises paths that do not contain a given set of vertices at least once.
- get_formula_general(graph: Graph, settings: PathFindingQuboGeneratorSettings, get_variable_function: GetVariableFunction) sympy.Expr[source]¶
Returns the QUBO expression for the cost function in the general case.
In the general case, the cost function may access the blackbox function get_variable_function(p, v, i) that returns 1 if vertex v is located at position i in path p and 0 otherwise.
- Parameters:
graph (Graph) – The graph on which the cost function should be applied.
settings (PathFindingQuboGeneratorSettings) – The settings of the QUBO generator.
get_variable_function (GetVariableFunction) – The blackbox function for accessing the encoding variables.
- Returns:
An expression representing the cost function as a QUBO function.
- Return type:
sp.Expr
- class PathContainsVerticesAtMostOnce(vertex_ids: list[int], path_ids: list[int])[source]¶
Bases:
PathContainsVerticesA cost function that penalises paths that do not contain a given set of vertices at most once.
- get_formula_general(graph: Graph, settings: PathFindingQuboGeneratorSettings, get_variable_function: GetVariableFunction) sympy.Expr[source]¶
Returns the QUBO expression for the cost function in the general case.
In the general case, the cost function may access the blackbox function get_variable_function(p, v, i) that returns 1 if vertex v is located at position i in path p and 0 otherwise.
- Parameters:
graph (Graph) – The graph on which the cost function should be applied.
settings (PathFindingQuboGeneratorSettings) – The settings of the QUBO generator.
get_variable_function (GetVariableFunction) – The blackbox function for accessing the encoding variables.
- Returns:
An expression representing the cost function as a QUBO function.
- Return type:
sp.Expr
- class PathContainsVerticesExactlyOnce(vertex_ids: list[int], path_ids: list[int])[source]¶
Bases:
PathContainsVerticesA cost function that penalises paths that do not contain a given set of vertices exactly once.
- get_formula_general(graph: Graph, settings: PathFindingQuboGeneratorSettings, get_variable_function: GetVariableFunction) sympy.Expr[source]¶
Returns the QUBO expression for the cost function in the general case.
In the general case, the cost function may access the blackbox function get_variable_function(p, v, i) that returns 1 if vertex v is located at position i in path p and 0 otherwise.
- Parameters:
graph (Graph) – The graph on which the cost function should be applied.
settings (PathFindingQuboGeneratorSettings) – The settings of the QUBO generator.
get_variable_function (GetVariableFunction) – The blackbox function for accessing the encoding variables.
- Returns:
An expression representing the cost function as a QUBO function.
- Return type:
sp.Expr
- class PathEndsAt(vertex_ids: list[int], path: int)[source]¶
Bases:
CostFunctionA cost function that penalises paths that do not end at a given vertex.
- vertex_ids¶
The list of vertices, one of which must be located at the end of the path.
- get_formula_general(graph: Graph, settings: PathFindingQuboGeneratorSettings, get_variable_function: GetVariableFunction) sympy.Expr[source]¶
Returns the QUBO expression for the cost function in the general case.
In the general case, the cost function may access the blackbox function get_variable_function(p, v, i) that returns 1 if vertex v is located at position i in path p and 0 otherwise.
- Parameters:
graph (Graph) – The graph on which the cost function should be applied.
settings (PathFindingQuboGeneratorSettings) – The settings of the QUBO generator.
get_variable_function (GetVariableFunction) – The blackbox function for accessing the encoding variables.
- Returns:
An expression representing the cost function as a QUBO function.
- Return type:
sp.Expr
- class PathIsValid(path_ids: list[int])[source]¶
Bases:
PathBoundA cost function that penalises paths that are not valid for a given encoding.
- A path may be invalid if
it contains an edge that is not in the graph,
- it has an assignment incompatible with the encoding:
for One-Hot encoding, multiple vertices are assigned to a single position
for Domain-Wall encoding, a position bit string is of the form 1…10…1…0
- get_formula_general(graph: Graph, settings: PathFindingQuboGeneratorSettings, get_variable_function: GetVariableFunction) sympy.Expr[source]¶
Returns the QUBO expression for the cost function in the general case.
In the general case, the cost function may access the blackbox function get_variable_function(p, v, i) that returns 1 if vertex v is located at position i in path p and 0 otherwise.
- Parameters:
graph (Graph) – The graph on which the cost function should be applied.
settings (PathFindingQuboGeneratorSettings) – The settings of the QUBO generator.
get_variable_function (GetVariableFunction) – The blackbox function for accessing the encoding variables.
- Returns:
An expression representing the cost function as a QUBO function.
- Return type:
sp.Expr
- get_formula_one_hot(graph: Graph, settings: PathFindingQuboGeneratorSettings) sympy.Expr[source]¶
Computes the QUBO expression for the cost function for One-Hot encoding.
- Parameters:
graph (Graph) – The graph on which the cost function should be applied.
settings (PathFindingQuboGeneratorSettings) – The settings of the QUBO generator.
- Returns:
An expression representing the cost function as a QUBO function.
- Return type:
sp.Expr
- get_formula_domain_wall(graph: Graph, settings: PathFindingQuboGeneratorSettings) sympy.Expr[source]¶
Computes the QUBO expression for the cost function for Domain-Wall encoding.
- Parameters:
graph (Graph) – The graph on which the cost function should be applied.
settings (PathFindingQuboGeneratorSettings) – The settings of the QUBO generator.
- Returns:
An expression representing the cost function as a QUBO function.
- Return type:
sp.Expr
- get_formula_binary(graph: Graph, settings: PathFindingQuboGeneratorSettings) sympy.Expr[source]¶
Computes the QUBO expression for the cost function for Binary encoding.
- Parameters:
graph (Graph) – The graph on which the cost function should be applied.
settings (PathFindingQuboGeneratorSettings) – The settings of the QUBO generator.
- Returns:
An expression representing the cost function as a QUBO function.
- Return type:
sp.Expr
- class PathPositionIs(position: int, vertex_ids: list[int], path: int)[source]¶
Bases:
CostFunctionA cost function that penalises paths that do not contain a given vertex at a given position.
- vertex_ids¶
The list of vertices, one of which must be located at the given position.
- get_formula_general(graph: Graph, settings: PathFindingQuboGeneratorSettings, get_variable_function: GetVariableFunction) sympy.Expr[source]¶
Returns the QUBO expression for the cost function in the general case.
In the general case, the cost function may access the blackbox function get_variable_function(p, v, i) that returns 1 if vertex v is located at position i in path p and 0 otherwise.
- Parameters:
graph (Graph) – The graph on which the cost function should be applied.
settings (PathFindingQuboGeneratorSettings) – The settings of the QUBO generator.
get_variable_function (GetVariableFunction) – The blackbox function for accessing the encoding variables.
- Returns:
An expression representing the cost function as a QUBO function.
- Return type:
sp.Expr
Bases:
PathComparisonA cost function that penalises paths that share edges.
Returns the QUBO expression for the cost function in the general case.
In the general case, the cost function may access the blackbox function get_variable_function(p, v, i) that returns 1 if vertex v is located at position i in path p and 0 otherwise.
- Parameters:
graph (Graph) – The graph on which the cost function should be applied.
settings (PathFindingQuboGeneratorSettings) – The settings of the QUBO generator.
get_variable_function (GetVariableFunction) – The blackbox function for accessing the encoding variables.
- Returns:
An expression representing the cost function as a QUBO function.
- Return type:
sp.Expr
Bases:
PathComparisonA cost function that penalises paths that share vertices.
Returns the QUBO expression for the cost function in the general case.
In the general case, the cost function may access the blackbox function get_variable_function(p, v, i) that returns 1 if vertex v is located at position i in path p and 0 otherwise.
- Parameters:
graph (Graph) – The graph on which the cost function should be applied.
settings (PathFindingQuboGeneratorSettings) – The settings of the QUBO generator.
get_variable_function (GetVariableFunction) – The blackbox function for accessing the encoding variables.
- Returns:
An expression representing the cost function as a QUBO function.
- Return type:
sp.Expr
- class PathStartsAt(vertex_ids: list[int], path: int)[source]¶
Bases:
PathPositionIsA cost function that penalises paths that do not start at a given vertex.
- class PrecedenceConstraint(pre: int, post: int, path_ids: list[int])[source]¶
Bases:
PathBoundA cost function that penalises paths that do not satisfy a given precedence constraint.
- get_formula_general(graph: Graph, settings: PathFindingQuboGeneratorSettings, get_variable_function: GetVariableFunction) sympy.Expr[source]¶
Returns the QUBO expression for the cost function in the general case.
In the general case, the cost function may access the blackbox function get_variable_function(p, v, i) that returns 1 if vertex v is located at position i in path p and 0 otherwise.
- Parameters:
graph (Graph) – The graph on which the cost function should be applied.
settings (PathFindingQuboGeneratorSettings) – The settings of the QUBO generator.
get_variable_function (GetVariableFunction) – The blackbox function for accessing the encoding variables.
- Returns:
An expression representing the cost function as a QUBO function.
- Return type:
sp.Expr
- class Graph(n_vertices: int, edges: list[Edge])[source]¶
Represents a graph to be used with qubo_tools.
- property non_edges: list[tuple[int, int]]¶
A list of all pairs (i, j) that are not edges in the graph.
- static read(file: TextIOWrapper) Graph[source]¶
Reads a graph from a file.
- Parameters:
file (TextIOWrapper) – The file to read the graph from.
- Returns:
The graph read from the file.
- Return type:
- store(file: TextIOWrapper) None[source]¶
Stores the graph in a file.
- Parameters:
file (TextIOWrapper) – The file to store the graph in.
- static from_adjacency_matrix(adjacency_matrix: numpy.typing.NDArray[int_ | numpy.float64] | list[list[int]] | list[list[float]]) Graph[source]¶
Creates a graph from an adjacency matrix.
- serialize() str[source]¶
Serializes the graph into a string.
- Returns:
The serialized graph as a string.
- Return type:
- class PathFindingQuboGenerator(objective_function: CostFunction | None, graph: Graph, settings: PathFindingQuboGeneratorSettings)[source]¶
Bases:
mqt.problemsolver.qubo_tools.QuboGeneratorA class for generating QUBOs for pathfinding problems.
Extends the QuboGenerator class with methods for generating QUBOs for pathfinding problems.
- graph¶
The graph on which the pathfinding problem is defined.
- settings¶
The settings for the QUBO generator.
- settings: PathFindingQuboGeneratorSettings¶
- static suggest_encoding(json_string: str, graph: Graph) EncodingType[source]¶
Suggests an encoding type for a given pathfinding problem.
The suggestion is based on the number of binary variables required for a specific problem.
- Parameters:
json_string – A JSON string describing the pathfinding problem.
graph – The graph on which the pathfinding problem is defined.
- Returns:
The suggested encoding type.
- static from_json(json_string: str, graph: Graph) PathFindingQuboGenerator[source]¶
Creates a PathFindingQuboGenerator object from its JSON format.
- Parameters:
json_string – The JSON string describing the pathfinding problem.
graph – The graph on which the pathfinding problem is defined.
- Returns:
The constructed QUBO generator.
- add_constraint(constraint: CostFunction, weight: float | None = None) PathFindingQuboGenerator[source]¶
Add a pathfinding constraint to the QUBO generator.
- Parameters:
constraint – The constraint to be added.
weight – The desired weight of the constraint. Defaults to None.
- Returns:
The current instance of the QUBO generator.
- add_constraint_if_exists(constraint: CostFunction | None, weight: float | None = None) PathFindingQuboGenerator[source]¶
Add a pathfinding constraint to the QUBO generator.
- Parameters:
constraint – The constraint to be added.
weight – The desired weight of the constraint. Defaults to None.
- Returns:
The current instance of the QUBO generator.
- get_variable_index(variable: sympy.Expr) int[source]¶
For a given variable, returns its index in the QUBO matrix.
- Parameters:
variable – The variable to investigate.
- Returns:
The index of the variable.
- decode_bit_array(_array: list[int]) Any[source]¶
Given an assignment, decodes it into a meaningful result. May be extended by subclasses.
- Parameters:
_array – The binary assignment.
- Returns:
The decoded result as a (set of) path(s).
- decode_bit_array_domain_wall(array: list[int]) list[list[int]][source]¶
Decodes an assignment for domain_wall encoding.
- Parameters:
array – The assignment to decode.
- Returns:
The decoded assignment as a (set of) path(s).