qrisp.qaoa.QAOAProblem.train_function#

QAOAProblem.train_function(qarg, depth, mes_kwargs={}, max_iter=50, init_type='random')[source]#

This function allows for training of a circuit with a given instance of a QAOAProblem. It will then return a function that can be applied to a QuantumVariable, s.t. that it is a solution to the problem instance. The function therefore acts as a circuit for the problem instance with optimized parameters.

Parameters:
qargQuantumVariable

The quantum variable to which the QAOA circuit is applied.

depthint

The depth of the QAOA circuit.

mes_kwargsdict, optional

The keyword arguments for the measurement function. Default is an empty dictionary.

max_iterint, optional

The maximum number of iterations for the optimization method. Default is 50.

Returns:
circuit_generatorfunction

A function that can be applied to a `QuantumVariable , with optimized parameters for the problem instance. The QuantumVariable then represent a solution of the problem.

Examples

We create a MaxClique instance and train the QAOAProblem instance

from qrisp.qaoa import QAOAProblem
from qrisp.qaoa.problems.maxCliqueInfrastr import maxCliqueCostfct,maxCliqueCostOp,init_state
from qrisp.qaoa.mixers import RX_mixer
from qrisp import QuantumVariable
import networkx as nx

    #create QAOAinstance
G = nx.erdos_renyi_graph(9,0.7, seed =  133)
    QAOAinstance = QAOAProblem(maxCliqueCostOp(G), RX_mixer, maxCliqueCostfct(G))
    QAOAinstance.set_init_function(init_function=init_state)

# create a blueprint-qv to train the problem instance on and train it
qarg_new = QuantumVariable(G.number_of_nodes())
training_func = QAOAinstance.train_circuit( qarg=qarg_new, depth=5 )

# apply the trained function to a new qv 
qarg_trained = QuantumVariable(G.number_of_nodes())
training_func(qarg_trained)

# get the results in a nice format
opt_res = qarg_trained.get_measurement()
aClCostFct = maxCliqueCostfct(G)

print("5 most likely Solutions") 
maxfive = sorted(opt_res, key=opt_res.get, reverse=True)[:5]
for res, val in opt_res.items():  
    if res in maxfive:

        print((res, val))
        print(aClCostFct({res : 1}))