qiskit_addon_opt_mapper.applications.Tsp

class Tsp(graph)[source]

Bases: GraphOptimizationApplication

Optimization application for the “traveling salesman problem” [1] based on a NetworkX graph.

References

[1]: “Travelling salesman problem”, https://en.wikipedia.org/wiki/Travelling_salesman_problem

Init method.

Parameters:
  • graph (Graph | ndarray | list | PyGraph) –

    A graph representing a problem. It can be specified in the following formats:

    • A Rustworkx undirected graph (rx.PyGraph)

    • A NetworkX undirected graph (nx.Graph)

    • A NumPy adjacency matrix (np.ndarray)

    • A list of edges or adjacency list (list)

  • rx.PyGraph. (The input graph will be internally normalized to a)

__init__(graph)[source]

Init method.

Parameters:
  • graph (Graph | ndarray | list | PyGraph) –

    A graph representing a problem. It can be specified in the following formats:

    • A Rustworkx undirected graph (rx.PyGraph)

    • A NetworkX undirected graph (nx.Graph)

    • A NumPy adjacency matrix (np.ndarray)

    • A list of edges or adjacency list (list)

  • rx.PyGraph. (The input graph will be internally normalized to a)

Return type:

None

Methods

__init__(graph)

Init method.

create_random_instance(n[, low, high, seed])

Create a random instance of the traveling salesman problem.

draw([result, pos])

Draw a graph with the result.

interpret(result)

Interpret a result as a list of node indices.

parse_tsplib_format(filename)

Read a graph in TSPLIB format from file and return a Tsp instance.

sample_most_likely(state_vector)

Compute the most likely binary string from state vector.

to_optimization_problem()

Represent as an optimization problem.

tsp_value(z, adj_matrix)

Compute the TSP value of a solution.

Attributes

graph

Getter of the graph.

nx_graph

Getter of the graph in Networkx format.

static create_random_instance(n, low=0, high=100, seed=None)[source]

Create a random instance of the traveling salesman problem.

Parameters:
  • n (int) – the number of nodes.

  • low (int) – The minimum value for the coordinate of a node.

  • high (int) – The maximum value for the coordinate of a node.

  • seed (int | None) – the seed for the random coordinates.

Returns:

A Tsp instance created from the input information

Return type:

Tsp

draw(result=None, pos=None)[source]

Draw a graph with the result.

When the result is None, draw an original graph without colors.

Parameters:
  • result (ndarray | None) – The calculated result for the problem

  • pos (dict[int, ndarray] | None) – The positions of nodes

Return type:

None

property graph: PyGraph

Getter of the graph.

Returns:

A graph for a problem

interpret(result)[source]

Interpret a result as a list of node indices.

Parameters:

result (ndarray) – The calculated result of the problem

Returns:

A list of nodes whose indices correspond to its order in a prospective cycle.

Return type:

list[int | list[int]]

property nx_graph: Graph

Getter of the graph in Networkx format.

Returns:

A graph for a problem

static parse_tsplib_format(filename)[source]

Read a graph in TSPLIB format from file and return a Tsp instance.

Only the EUC_2D edge weight format is supported.

Parameters:

filename (str) – the name of the file.

Raises:
  • OptimizationError – If the type is not “TSP”

  • OptimizationError – If the edge weight type is not “EUC_2D”

Returns:

A Tsp instance data.

Return type:

Tsp

static sample_most_likely(state_vector)[source]

Compute the most likely binary string from state vector.

Parameters:

state_vector (QuasiDistribution | Statevector | ndarray | dict) – state vector or counts or quasi-probabilities.

Returns:

binary string as numpy.ndarray of ints.

Raises:

ValueError – if state_vector is not QuasiDistribution, Statevector, np.ndarray, or dict.

Return type:

ndarray

to_optimization_problem()[source]

Represent as an optimization problem.

Convert a traveling salesman problem instance into a OptimizationProblem

Returns:

The OptimizationProblem created from the traveling salesman problem instance.

Return type:

OptimizationProblem

static tsp_value(z, adj_matrix)[source]

Compute the TSP value of a solution.

Parameters:
  • z (list[int]) – list of cities.

  • adj_matrix (ndarray) – adjacency matrix.

Returns:

value of the total length

Return type:

float