qiskit-community / qiskit-optimization

Quantum Optimization
https://qiskit-community.github.io/qiskit-optimization/
Apache License 2.0
227 stars 140 forks source link

Inaccurate Results in TSP Problem Solution with tsp.py and QAOA #511

Closed Syed-Affan-Hussain closed 1 year ago

Syed-Affan-Hussain commented 1 year ago

Environment

What is happening?

I encountered difficulties while attempting to implement the Traveling Salesman Problem (TSP) using the QAOA function from Qiskit Optimization, as demonstrated in [1]. Unfortunately, the results were not satisfactory compared to the VQE approach. I also utilized the tsp.py function from [3] as provided in [1]. While I managed to obtain the correct energy value, I encountered issues with the sampling and evaluation of the values, leading to inaccurate results. This situation has made it challenging to solve the remaining aspects of the problem.

Here are the relevant resources for reference:

[1] TSP Implementation: Link [2] Qiskit QAOA Function: Link [3] TSP.py Function: Link

QAOA Code: image QAOA Output: image

VQE Code and Output: image

How can we reproduce the issue?

Revisiting the source code for both QAOA and the TSP implementation would help identify any incompatibilities or discrepancies between them.

What should happen?

It is expected that the energies obtained from VQE and QAOA should match for small graphs in the context of the Traveling Salesman Problem (TSP). Additionally, the TSP functions utilized in QAOA should function similarly to those used in VQE. However, in my implementation, I have observed discrepancies between the results of VQE and QAOA for the energy values.

Any suggestions?

If there are any mistakes or areas where I may have gone wrong, I kindly request your guidance and assistance. I am open to any suggestions or advice to help resolve the issues I encountered during the implementation. Your expertise and insights would be greatly appreciated.

t-imamichi commented 1 year ago

You need to try larger maxiter and reps for QAOA. VQE and QAOA has no guarantee that the final solution converges to the optimal solution. So, your situation can happen, especially with small maxiter and reps.

Syed-Affan-Hussain commented 1 year ago

Thank you so much I set maxiter to 3000 and rep to 1, surprisingly it worked

woodsp-ibm commented 1 year ago

I will also note that QAOA is simply a subclass of SamplingVQE where QAOA fixes the ansatz to use the QAOAAnsatz. To compare more adequately choosing the same optimizer and algorithm_globals seed would minimize other differences. In QAOA you use COBYLA and in VQE the SPSA. The seeds are also different which affects the start point, which in the absence of being set explicitly its chosen randomly. Of course the start point will mean different things to the ansatzes (different starting state result) which may affect the outcome and make the direct comparison more tricky.

With Sampler() from qiskit.primitives, by default, as you created it above it gives the ideal distribution - created thus it has no sampling/shot noise. SPSA was designed to work in noisy enviroments and works rather differently that say the COBYLA. Given the above has an ideal outcome you could use SLSQP, a classical gradient based optimizer. EIther way choosing the same optimizer would reduce the differences for the comparison.

In the end the real difference would/should come down to just the ansatz.