I solved "01 KnapSack problem" in Qiskit optimization module. Though the iterations doesn't gets converged but I got an answer (may be infeasible). But my question is that how to interpret the given result because my problem has 10 variable but the result it shows has only 4 variable and an improper vaule of objective function.
This is the code - https://github.com/imAshish28/Quantum_OR/blob/main/Untitled24.ipynb
filename = 'binary_knapsack_10.lp'
qp = QuadraticProgram()
qp.read_from_lp_file(filename)
print(qp.prettyprint())
''' Problem name: binary_knapsack_10
Minimize -135x_0 - 42x_1 - 677x_2 - 773x_3 - 333x_4 - 24x_5 - 819x_6 - 184x_7
- 683x_8 - 895x_9
Subject to Linear constraints (1) 291x_0 + 80x_1 + 76x_2 + 111x_3 + 687x_4 + 87x_5 + 567x_6 + 969x_7 + 967x_8 + 392x_9 <= 2114 'capacity'
Binary variables (10) x_0 x_1 x_2 x_3 x_4 x_5 x_6 x_7 x_8 x_9 '''
from qiskit_optimization.converters import QuadraticProgramToQubo
qp2qubo = QuadraticProgramToQubo()
qubo = qp2qubo.convert(qp)
qubitOp, offset = qubo.to_ising()
print("Offset:", offset)
print("Ising Hamiltonian:")
print(str(qubitOp))
from qiskit_ibm_runtime import QiskitRuntimeService, Sampler, Estimator, Options, Session
# loading the IBM Acoount with the Backend
QiskitRuntimeService.save_account(channel="ibm_quantum", token="API TOKEN",overwrite=True)
service = QiskitRuntimeService(channel='ibm_quantum')
backend = service.least_busy(operational=True, simulator=False)
from qiskit.circuit.library import QAOAAnsatz
hamiltonian = qubitOp
# QAOA ansatz circuit
ansatz = QAOAAnsatz(hamiltonian, reps=2)
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
target = backend.target
pm = generate_preset_pass_manager(target=target, optimization_level=3)
ansatz_isa = pm.run(ansatz)
hamiltonian_isa = hamiltonian.apply_layout(ansatz_isa.layout)
hamiltonian_isa
def cost_func(params, ansatz, hamiltonian, estimator):
"""Return estimate of energy from estimator
Parameters:
params (ndarray): Array of ansatz parameters
ansatz (QuantumCircuit): Parameterized ansatz circuit
hamiltonian (SparsePauliOp): Operator representation of Hamiltonian
estimator (Estimator): Estimator primitive instance
Returns:
float: Energy estimate
"""
energy = estimator.run(ansatz, hamiltonian, parameter_values=params).result().values[0]
return energy
session = Session(backend=backend)
# Configure estimator
estimator = Estimator(session=session)
estimator.options.default_shots = 100
# Configure sampler
sampler = Sampler(session=session)
sampler.options.default_shots = 100
x0 = 2 * np.pi * np.random.rand(ansatz_isa.num_parameters)
from scipy.optimize import minimize
res = minimize(cost_func, x0, args=(ansatz_isa, hamiltonian_isa, estimator), method="COBYLA" , options= {'maxiter' : 5})
res
''' And this is the result which I get: message: Maximum number of function evaluations has been exceeded. success: False status: 2 fun: -464074414.1873996 x: [ 1.743e+00 3.937e+00 3.707e+00 4.516e+00] nfev: 5 maxcv: 0.0 '''
convert
method but also aninterpret
to go the other way. See this tutorial for examples qiskit-community.github.io/qiskit-optimization/tutorials/… $\endgroup$