qrisp.qaoa.QAOAProblem.train_function#
- QAOAProblem.train_function(qarg, depth, mes_kwargs={}, max_iter=50, init_type='random', optimizer='COBYLA')[source]#
This function allows for training of a circuit with a given
QAOAProblem
instance. It returns a function that can be applied to aQuantumVariable
, such that it represents a solution to the problem instance. When applied to aQuantumVariable
, the function therefore prepares the state\[\ket{\psi_p}=U_M(B,\beta_p)U_P(C,\gamma_p)\dotsb U_M(B,\beta_1)U_P(C,\gamma_1)\ket{\psi_0}\]with optimized parameters \(\gamma, \beta\).
- Parameters:
- qargQuantumVariable
The quantum argument to which the QAOA circuit is applied.
- depthint
The amount of QAOA layers.
- 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.
- init_typestring, optional
Specifies the way the initial optimization parameters are chosen. Available are
random
andtqa
. The default israndom
: The parameters are initialized uniformly at random in the interval \([0,\pi/2]\). Fortqa
, the parameters are chosen based on the Trotterized Quantum Annealing protocol. Iftqa
is chosen, and noinit_function
for the QAOAProblem is specified, the \(\ket{-}^n\) state is prepared (the ground state for the X mixer).- optimizerstr, optional
Specifies the optimization routine. Available are, e.g.,
COBYLA
,COBYQA
,Nelder-Mead
. The Default isCOBYLA
.
- Returns:
- circuit_generatorfunction
A function that can be applied to a
QuantumVariable
, with optimized parameters for the problem instance. TheQuantumVariable
then represents a solution of the problem.
Examples
We create a MaxIndepSet instance and train a ciruit with the QAOAProblem instance.
from qrisp import QuantumVariable from qrisp.qaoa import QAOAProblem, RZ_mixer, create_max_indep_set_cl_cost_function, create_max_indep_set_mixer, max_indep_set_init_function import networkx as nx import matplotlib.pyplot as plt G = nx.erdos_renyi_graph(9, 0.5, seed = 133) qaoa_instance = QAOAProblem(cost_operator=RZ_mixer, mixer=create_max_indep_set_mixer(G), cl_cost_function=create_max_indep_set_cl_cost_function(G), init_function=max_indep_set_init_function) # create a blueprint-qv to train the circuit with the problem instance qarg_new = QuantumVariable(G.number_of_nodes()) training_func = qaoa_instance.train_function(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 measurement results opt_res = qarg_trained.get_measurement() cl_cost = create_max_indep_set_cl_cost_function(G) print("5 most likely solutions") max_five = sorted(opt_res.items(), key=lambda item: item[1], reverse=True)[:5] for res, prob in max_five: print([index for index, value in enumerate(res) if value == '1'], prob, cl_cost({res : 1}))