entropicalabs / openqaoa

Multi-backend SDK for quantum optimisation
MIT License
113 stars 59 forks source link

QAOA().compile appears to 'lock-in' values after first run? #256

Closed robliou closed 4 months ago

robliou commented 1 year ago

Prerequisites

Before raising this issue, I have already checked that I am:

Describe the bug

Hello, when performing testing using the QAOA package, I find that the initial run of the program will seem to execute correctly. However, if subsequent runs are executed, the program will re-execute as if no variables had changed.

To Reproduce

In a new folder, install the necessary dependencies, and then execute this code:

import numpy as np
import networkx as nx
from openqaoa.utilities import plot_graph

# Generating a graph of 100 nodes
n = 100  # Number of nodes in graph

G = nx.generators.fast_gnp_random_graph(n, p=0.4, seed=42)
plot_graph(G)
from openqaoa.problems import MaximumCut

maxcut_prob = MaximumCut(G)
maxcut_qubo = maxcut_prob.qubo

print(maxcut_qubo.hamiltonian.expression)
maxcut_qubo.asdict()
print('this is maxcut_qubo', maxcut_qubo.asdict())

plot_graph(G)

from openqaoa import QAOA, create_device  

q = QAOA()
analytical_device = create_device(location='local', name='analytical_simulator')
q.set_device(analytical_device)

q.compile(maxcut_qubo)
q.optimize()

print('this is answer',q.result.most_probable_states)

You will get an error saying 'ValueError: Maximum allowed dimension exceeded'

Then make a slight modification to the code, setting n to 6.

The code will run as if n still equals to 100, and you will get the same error.

Expected behavior

The code should run as id n=6 on the second try.

Additional context

Please note that I tested this on two separate PC laptops, in both virtual and non-virtual environments, and got the same error each time. Also, there is a follow-up question as to why the analytical machine cannot solve the initial max-cut problem at 100 nodes...?

Thank you,

KilianPoirier commented 1 year ago

Hi @robliou , thank you for raising this issue.

I have tried to reproduce your error on a clean install of openqaoa through pip install openqaoa as well as using the current version of dev without success. What version of openqaoa are you using and how did you install the package?

I first execute your code with n=6 and then modify the value of n and as soon as I use q.compile(<MODIFIED QUBO>) the internal number of qubit is updated and the rest follows as expected. Maybe I misunderstood your question but I still want to add that you need to use q.compile(maxcut_qubo) every time you modify a parameter for the changes to take action. In your case, when you modify n, you need to recreate the graph, then the problem, generate qubo form, and finally compile.

I am quite surprised to see that the analytical simulator produces such an error, a quick search through the code base indicates that it does not appear in our tests anymore. The closest similar ValueError I could find is: ValueError('number of qubits exceeds maximum of 20') but it should not be raised in that case. My first guess concerning the ValueError is that you tried to compile on another device/simulator where the number of qubits is limited for computational reasons (matrix multiplication in a $2^n$-dimensional vector space is not scalable).

To answer your follow-up question, the analytical simulator should be able to support larger problem sizes since it only computes the analytical expectation value. Naturally, the larger the problem, the longer it takes to compute that value because of the number of terms. The number of parameters/angles does not vary since p=1. However this can only allow you to find the optimal angles. In order to find the most probable state as showed in your code, you would still need to execute/simulate the large circuit with these parameters you found.

Let us know if that helps!