qiskit_addon_opt_mapper.applications.Maxcut

class Maxcut(graph)[source]

Bases: GraphOptimizationApplication

Optimization application for the “max-cut” [1] problem based on a NetworkX graph.

References

[1]: “Maximum cut”, https://en.wikipedia.org/wiki/Maximum_cut

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.

draw([result, pos])

Draw a graph with the result.

get_gset_result(x)

Get graph solution in Gset format from binary string.

interpret(result)

Interpret a result as two lists of node indices.

parse_gset_format(filename)

Read graph in Gset format from file.

sample_most_likely(state_vector)

Compute the most likely binary string from state vector.

to_optimization_problem()

Represent as an optimization problem.

Attributes

graph

Getter of the graph.

nx_graph

Getter of the graph in Networkx format.

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

static get_gset_result(x)[source]

Get graph solution in Gset format from binary string.

Parameters:

x (ndarray) – binary string as numpy array.

Returns:

A graph solution in Gset format.

Return type:

dict[int, int]

property graph: PyGraph

Getter of the graph.

Returns:

A graph for a problem

interpret(result)[source]

Interpret a result as two lists of node indices.

Parameters:

result (ndarray) – The calculated result of the problem

Returns:

Two lists of node indices correspond to two node sets for the Max-cut

Return type:

list[list[int]]

property nx_graph: Graph

Getter of the graph in Networkx format.

Returns:

A graph for a problem

static parse_gset_format(filename)[source]

Read graph in Gset format from file.

Parameters:

filename (str) – the name of the file.

Returns:

An adjacency matrix as a 2D numpy array.

Return type:

ndarray

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 Max-cut problem instance into a OptimizationProblem

Returns:

The OptimizationProblem created from the Max-cut problem instance.

Return type:

OptimizationProblem