Skip to content

Error when the objective function includes a linear term #28

@eegzheng

Description

@eegzheng

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions