qrisp.qaoa.QAOAProblem.train_function#

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

This function allows for training of a circuit with a given QAOAProblem instance. It returns a function that can be applied to a QuantumVariable, such that it represents a solution to the problem instance. When applied to a QuantumVariable, 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 or QuantumArray or callable

The argument to which the QAOA circuit is applied, or a function returning a QuantumVariable or QuantumArray 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 and tqa. The default is random: The parameters are initialized uniformly at random in the interval \([0,\pi/2]\). For tqa, the parameters are chosen based on the Trotterized Quantum Annealing protocol. If tqa is chosen, and no init_function for the QAOAProblem is specified, the \(\ket{-}^n\) state is prepared (the ground state for the X mixer).

init_pointndarray, shape (n,), optional

Specifies the initial optimization parameters.

optimizerstr, optional

Specifies the SciPy optimization routine. Available are, e.g., COBYLA, COBYQA, Nelder-Mead. The Default is COBYLA. In tracing mode (i.e. Jasp) Jax-traceable optimization routines must be utilized. Available are COBYLA, SPSA.

optionsdict

A dictionary of solver options.

Returns:
circuit_generatorfunction

A function that can be applied to a QuantumVariable , with optimized parameters for the problem instance. The QuantumVariable 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 = QuantumVariable(G.number_of_nodes())
training_func = qaoa_instance.train_function(qarg, 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}))