Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when the objective function includes a linear term #28

Open
eegzheng opened this issue Jul 22, 2024 · 0 comments
Open

Error when the objective function includes a linear term #28

eegzheng opened this issue Jul 22, 2024 · 0 comments

Comments

@eegzheng
Copy link

eegzheng commented Jul 22, 2024

Hello,

I am trying a toy example by adapting the code from your max-cut example and is shown below, but I found as long as there is a linear term in the objective function, it will shows error below:
... \SimCIM.py", line 86, in update
(self.J.dot(self.x) + self.h) * self.dt
ValueError: operands could not be broadcast together with shapes (8,100) (8,)

Without h or without s.update(), it works fine.

##########################################################
import numpy as np
from mindquantum.algorithm.qaia import ASB, BSB, DSB, SimCIM, NMFA

N = 8 # number of spins

QUBO problem formulation

min x'Jx + h'*x, x is a spin vector variable

J = np.array([[ 0, 50, 0, 0, -50, -100, 0, 0],
[ 50, 0, 0, 0, -50, -100, 0, 0],
[ 0, 0, 0, 50, 0, 0, -50, -100],
[ 0, 0, 50, 0, 0, 0, -50, -100],
[ -50, -50, 0, 0, 0, 100, -2, 0],
[-100, -100, 0, 0, 100, 0, 0, 0],
[ 0, 0, -50, -50, -2, 0, 0, 100],
[ 0, 0, -100, -100, 0, 0, 100, 0]])
h = np.array([ 103, 100, 100, 100, -100, -200, -100, -200])

sample_size=100
n_iter_list = [10, 25, 50]
cut_value_list = []
for n_iter in n_iter_list:
s = SimCIM(-2*J,-h,batch_size=sample_size, n_iter=n_iter)
s.update()
cut_value = s.calc_energy()
cut_value_list.append(cut_value)

I also wonder how to extra the desired solution and the associated objective value. Is x = np.sign(s.x) the way to find all sample solutions, and the associated solutions are stored in s.calc_energy()?

Many thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant