I would like to know if there is a easy way or native solution to retrieve the number of qubits that is required when using QAOA from qiskit_algorithms
package to solve an optimization problem.
I understand that QuantumCircuit.num_qubits
can do the work if I have a circuit objects. However, I am using QAOA algorithms from qiskit_algorithms
and I couldn't have a circuit object to call even though I think the package construct the circuit under the hood for me.
The only way that I could know the required number of qubit for target problem right now is deliberately setting up a small number of qubit for the local simulator, and then I will get the error message qiskit.transpiler.exceptions.CircuitTooWideForTarget: 'Number of qubits (5) in QAOA is greater than maximum (1) in the coupling_map'
, and I know the problem require 5 qubits to solve, but I would like to know the number of qubit required before running it.
The code is shown below:
from qiskit_optimization.algorithms import MinimumEigenOptimizer
from qiskit_algorithms import QAOA
from qiskit_algorithms.optimizers import COBYLA
from qiskit_optimization.translators import from_docplex_mp
from qiskit.providers.fake_provider import GenericBackendV2
mod = from_docplex_mp(mdl) #the optimization model
backend = GenericBackendV2(num_qubits=1) #fake backend
sampler = BackendSampler(backend=backend)
qaoa_mes = QAOA(sampler=sampler,reps=1,optimizer=COBYLA(), initial_point=[0.0, 0.0])
qaoa = MinimumEigenOptimizer(qaoa_mes)
exact_result = qaoa.solve(mod) #solve the problem