Vertex Cover

class qubovert.problems.VertexCover(*args, **kwargs)

VertexCover.

Class to manage converting Vertex Cover to and from its QUBO and QUSO formluations. Based on the paper “Ising formulations of many NP problems”, hereforth designated as [Lucas].

The goal of the VertexCover problem is to find the smallest number of verticies that can be colored such that every edge of the graph is incident to a colored vertex.

VertexCover inherits some methods and attributes from the Problem class. See help(qubovert.problems.Problem).

Example

>>> from qubovert.problems import VertexCover
>>> from any_module import qubo_solver
>>> # or you can use my bruteforce solver...
>>> # from qubovert.utils import solve_qubo_bruteforce as qubo_solver
>>> edges = {("a", "b"), ("a", "c"), ("c", "d"), ("a", "d")}
>>> problem = VertexCover(edges)
>>> Q  = problem.to_qubo()
>>> obj, sol = qubo_solver(Q)
>>> solution = problem.convert_solution(sol)
>>> print(solution)
{"a", "c"}
>>> print(problem.is_solution_valid(solution))
True  # since each edge is adjacent to either "a" or "c".
>>> print(obj == len(solution))
True

__init__.

The goal of the VertexCover problem is to find the smallest number of verticies that can be coloredsuch that every edge of the graph is incident to a colored vertex. All naming conventions follow the names in the paper [Lucas].

Parameters

edges (set of two element tuples.) – Describes edges of the graph.

Examples

>>> edges = {("a", "b"), ("a", "c")}
>>> problem = VertexCover(edges)
>>> edges = {(0, 1), (0, 2)}
>>> problem = VertexCover(edges)
property E

A copy of the set of edges of the graph. Updating the copy will not update the instance set.

Returns

E – A copy of the edge set defining the Vertex Cover problem.

Return type

set of two element tuples.

property V

A copy of the vertex set of the graph. Updating the copy will not update the instance set.

Returns

V – A copy of the set of vertices corresponding to the edge set for the Vertex Cover problem.

Return type

set.

convert_solution(solution, spin=False)

convert_solution.

Convert the solution to the QUBO or QUSO to the solution to the Vertex Cover problem.

Parameters
  • solution (iterable or dict.) – The QUBO or QUSO solution output. The QUBO solution output is either a list or tuple where indices specify the label of the variable and the element specifies whether it’s 0 or 1 for QUBO (or 1 or -1 for QUSO), or it can be a dictionary that maps the label of the variable to is value.

  • spin (bool (optional, defaults to False)) – spin indicates whether solution is the solution to the boolean {0, 1} formulation of the problem or the spin {1, -1} formulation of the problem. This parameter usually does not matter, and it will be ignored if possible. The only time it is used is if solution contains all 1’s. In this case, it is unclear whether solution came from a spin or boolean formulation of the problem, and we will figure it out based on the spin parameter.

Returns

res – A set of which verticies need to be colored. Thus, if this function returns {0, 2}, then this means that vertex 0 and 2 should be colored.

Return type

set.

is_solution_valid(solution, spin=False)

is_solution_valid.

Returns whether or not the proposed solution satisfies the constraint that every edge has at least one colored vertex.

Parameters
  • solution (iterable or dict.) – solution can be the output of VertexCover.convert_solution, or the QUBO or QUSO solver output. The QUBO solution output is either a list or tuple where indices specify the label of the variable and the element specifies whether it’s 0 or 1 for QUBO (or 1 or -1 for QUSO), or it can be a dictionary that maps the label of the variable to is value.

  • spin (bool (optional, defaults to False)) – spin indicates whether solution is the solution to the boolean {0, 1} formulation of the problem or the spin {1, -1} formulation of the problem. This parameter usually does not matter, and it will be ignored if possible. The only time it is used is if solution contains all 1’s. In this case, it is unclear whether solution came from a spin or boolean formulation of the problem, and we will figure it out based on the spin parameter.

Returns

valid – True if the proposed solution is valid, else False.

Return type

boolean.

property num_binary_variables

num_binary_variables.

The number of binary variables that the QUBO and QUSO use.

Returns

num – The number of variables in the QUBO/QUSO formulation.

Return type

integer.

solve_bruteforce(*args, **kwargs)

solve_bruteforce.

Solve the problem bruteforce. THIS SHOULD NOT BE USED FOR LARGE PROBLEMS! This is implemented in the parent qubovert.utils.Problem class. Some problems use a lot of slack binary variables for their QUBO/QUSO formulations. If this is the case, then the child class for this problem should override this method with a better bruteforce solver. But, for problems that do not use slack variables, this method will suffice. It converts the problem to QUBO, solves it with qubovert.utils.solve_qubo_bruteforce, and then calls and returns convert_solution.

Parameters

aruments (arguments and keyword arguments.) – Contains args and kwargs for the to_qubo method. Also contains a all_solutions boolean flag, which indicates whether or not to return all the solutions, or just the best one found. all_solutions defaults to False.

Returns

res – If all_solutions is False, then res is just the output of the convert_solution method. If all_solutions is True, then res is a list of outputs of the convert_solution method, e.g. a converted solution for each solution that the bruteforce solver returns.

Return type

the output or outputs of the convert_solution method.

to_pubo(*args, **kwargs)

to_pubo.

Since the model is already degree two, self.to_pubo will simply return qubovert.utils.PUBOMatrix(self.to_qubo(*args, **kwargs)).

Returns

P – The upper triangular PUBO matrix, a PUBOMatrix object. For most practical purposes, you can use PUBOMatrix in the same way as an ordinary dictionary. For more information, see help(qubovert.utils.PUBOMatrix).

Return type

qubovert.utils.PUBOMatrix object.

to_puso(*args, **kwargs)

to_puso.

Create and return PUSO model representing the problem. Should be implemented in child classes. If this method is not implemented in the child class, then it simply calls to_pubo or to_quso and converts to a PUSO formulation.

Parameters

arguments (Defined in the child class.) – They should be parameters that define lagrange multipliers or factors in the QUSO model.

Returns

H – For most practical purposes, you can use PUSOMatrix in the same way as an ordinary dictionary. For more information, see help(qubovert.utils.PUSOMatrix).

Return type

qubovert.utils.PUSOMatrix object.

Raises
  • RecursionError` if neither to_pubo nor to_puso are define

  • in the subclass.

to_qubo(A=2, B=1)

to_qubo.

Create and return the vertex cover problem in QUBO form following section 4.3 of [Lucas]. The Q matrix for the QUBO will be returned as an uppertriangular dictionary. Thus, the problem becomes minimizing \(\sum_{i \leq j} x_i x_j Q_{ij}\). A and B are parameters to enforce constraints.

It is formatted such that if all the constraints are satisfied, then the objective function will be equal to the total number of colored verticies.

Parameters
  • A (positive float (optional, defaults to 2)) – A enforces the constraints. See section 4.3 of [Lucas].

  • B (positive float that is less than A (optional, defaults to 1)) – See section 4.3 of [Lucas].

Returns

Q – The upper triangular QUBO matrix, a QUBOMatrix object. For most practical purposes, you can use QUBOMatrix in the same way as an ordinary dictionary. For more information, see help(qubovert.utils.QUBOMatrix).

Return type

qubovert.utils.QUBOMatrix object.

Example

>>> problem = VertexCover({(0, 1), (0, 2)})
>>> Q = problem.to_qubo()
to_quso(*args, **kwargs)

to_quso.

Create and return QUSO model representing the problem. Should be implemented in child classes. If this method is not implemented in the child class, then it simply calls to_qubo and converts the QUBO formulation to an QUSO formulation.

Parameters

arguments (Defined in the child class.) – They should be parameters that define lagrange multipliers or factors in the QUSO model.

Returns

L – The upper triangular coupling matrix, where two element tuples represent couplings and one element tuples represent fields. For most practical purposes, you can use IsingCoupling in the same way as an ordinary dictionary. For more information, see help(qubovert.utils.QUSOMatrix).

Return type

qubovert.utils.QUSOMatrix object.

Raises
  • RecursionError` if neither to_qubo nor to_quso are define

  • in the subclass.