QAOAProblem#

class QAOAProblem(cost_operator, mixer, cl_cost_function, init_function=None, init_type='random')[source]#

Central structure to facilitate treatment of QAOA problems.

This class encapsulates the cost operator, mixer operator, and classical cost function for a specific QAOA problem instance. It also provides methods to set the initial state preparation function, classical cost post-processing function, and optimizer for the problem.

For a quick demonstration, we import the relevant functions from already implemented problem instances:

from networkx import Graph

G = Graph()

G.add_edges_from([[0,3],[0,4],[1,3],[1,4],[2,3],[2,4]])

from qrisp.qaoa import (QAOAProblem, 
                        create_maxcut_cost_operator, 
                        create_maxcut_cl_cost_function,
                        RX_mixer)

maxcut_instance = QAOAProblem(cost_operator = create_maxcut_cost_operator(G),
                               mixer = RX_mixer,
                               cl_cost_function = create_maxcut_cl_cost_function(G))

from qrisp import QuantumVariable

res = maxcut_instance.run(qarg = QuantumVariable(5),
                          depth = 4, 
                          max_iter = 25)

print(res)
#Yields: {'11100': 0.2847, '00011': 0.2847, '10000': 0.0219, '01000': 0.0219, '00100': 0.0219, '11011': 0.0219, '10111': 0.0219, '01111': 0.0219, '00010': 0.02, '11110': 0.02, '00001': 0.02, '11101': 0.02, '00000': 0.0173, '11111': 0.0173, '10010': 0.0143, '01010': 0.0143, '11010': 0.0143, '00110': 0.0143, '10110': 0.0143, '01110': 0.0143, '10001': 0.0143, '01001': 0.0143, '11001': 0.0143, '00101': 0.0143, '10101': 0.0143, '01101': 0.0143, '11000': 0.0021, '10100': 0.0021, '01100': 0.0021, '10011': 0.0021, '01011': 0.0021, '00111': 0.0021}

For an in-depth tutorial, make sure to check out MaxCut QAOA Implementation!

Parameters
cost_operatorfunction

A function receiving a QuantumVariable or QuantumArray and parameter \(\gamma\). This function should perform the application of the cost operator.

mixerfunction

A function receiving a QuantumVariable or QuantumArray and parameter \(\beta\). This function should perform the application mixing operator.

cl_cost_functionfunction

The classical cost function for the specific QAOA problem instance.

Methods#

QAOAProblem.run(qarg, depth[, mes_kwargs, ...])

Run the specific QAOA problem instance with given quantum arguments, depth of QAOA circuit, measurement keyword arguments (mes_kwargs) and maximum iterations for optimization (max_iter).

QAOAProblem.train_function(qarg, depth[, ...])

This function allows for training of a circuit with a given instance of a QAOAProblem.

QAOAProblem.compile_circuit(qarg, depth)

Compiles the circuit that is evaluated by the run method.

QAOAProblem.benchmark(qarg, depth_range, ...)

This method enables convenient data collection regarding performance of the implementation.