qiboteam / qibo

A framework for quantum computing
https://qibo.science
Apache License 2.0
288 stars 56 forks source link

How do we run QAOA efficiently #682

Closed shangtai closed 10 months ago

shangtai commented 1 year ago

Hello, I notice that when we run QAOA.

We tend to get the following error message:

Calculating the dense form of a symbolic Hamiltonian. This operation is memory inefficient.

Is it due to we are performing exponential and if we examine the code, we have the following

def exp(self, a):
    return self.dense.exp(a)

Is there any advice on how can we perform QAOA efficiently so that we can do a benchmark that can scale to a larger problem size?

Thanks.

stavros11 commented 1 year ago

Thanks for opening this. Could you please post some code that gives this warning?

I tried the following:

import numpy as np
from qibo.models import QAOA
from qibo.hamiltonians import XXZ

ham = XXZ(15, dense=False)

qaoa = QAOA(ham)
qaoa.set_parameters(np.random.random(8))
result = qaoa()

and I do not get the warning. Also, if I use dense=True in this example, which forces constructing the full matrix, my laptop runs out of memory, while with dense=False it works and is quite fast. So it seems that it is working as expected. When using symbolic Hamiltonian the exponantiation is done using the Trotter decomposition which does not require calculating the full matrix.

shangtai commented 1 year ago

Hi Stavros,

I think if we do the following, we get the warning.

 best_energy, final_parameters, extra = qaoa.minimize(initial_p=[0.1] * 2,
                                                 initial_state=initial_state, method='BFGS')

Does it mean that I should just call QAOA as illustrated without calling minimization method?

stavros11 commented 1 year ago

No, it is fine to use minimize if you want to train the model. I tried it but it is still fast and does not raise the warning. If you check the QAOA.minimize method in the code it just uses QAOA.__call__ to calculate the loss, so I would expect to have the same behavior.

Maybe the warning is raised from some other part of your code not related to the QAOA?

shangtai commented 1 year ago

Hi Stavros,

I tried the following code:

  import numpy as np
  from qibo.models import QAOA
  from qibo.hamiltonians import XXZ

  ham = XXZ(15, dense=False)
  qaoa = QAOA(ham)
  qaoa.set_parameters(np.random.random(8))
  print('hi Stavros')
  qaoa.minimize([0.1,0.2])
  print('hi Siong Thye')

and I obtain the following output:

[Qibo 0.1.8|INFO|2022-11-04 10:23:08]: Using qibojit (numba) backend on /CPU:0
hi Stavros
[Qibo 0.1.8|WARNING|2022-11-04 10:23:08]: Calculating the dense form of a symbolic Hamiltonian. This operation is memory inefficient.

not sure what is going on.... let me investigate more . :)

stavros11 commented 1 year ago

Thank you for posting the code. I believe the issue you are having is due to #639. This was fixed in #640 but is not included in the latest release. Could you please retry with the latest qibo and qibojit master? You can install using

pip install git+https://github.com/qiboteam/qibo
pip install git+https://github.com/qiboteam/qibojit

The qibojit part is optional. With this the warning should disappear and execution takes about 7.5sec on my laptop using the numpy backend. Sorry for the confusion.

@scarrazza maybe we should consider making a release particularly because of the incompatibility between latest qibo master and older qibojit. If someone installs qibo master and does not update qibojit they may run into troubles.

shangtai commented 1 year ago

Thanks. It works when I try it.