recruit-communications / pyqubo

Python DSL for constructing QUBOs from mathematical expressions.
https://pyqubo.readthedocs.io/
Apache License 2.0
177 stars 46 forks source link

Error in parsing specific type of Hamiltonian expression #227

Open deepakpathania-py opened 1 year ago

deepakpathania-py commented 1 year ago

Pyqubo gives the error for the following Hamiltonian:

n=3 from pyqubo import solve_qubo,Array,Placeholder x = Array.create('x', shape=(5), vartype='BINARY') dic={"0":12,"1":6,"2":21,"3":32,"4":7}

H_1 = (sum(x)-n)*2 H_2 = sum(x[i]dic["{}".format(i)] for i in range(5)) H = sum(dic.values())3/5H_1 + H_2

Error Reported:

TypeError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_12292/2264980511.py in 1 H_1 = (sum(x)-n)*2 2 H_2 = sum(x[i]dic["{}".format(i)] for i in range(5)) ----> 3 H = sum(dic.values())3/5H_1 + H_2

TypeError: radd(): incompatible function arguments. The following argument types are supported:

  1. (self: cpp_pyqubo.Add, arg0: float) -> cpp_pyqubo.Add

However, this error can be worked around if we re-write the above expression as follows:

H_1 = (sum(x)-n)*2 H_2 = sum(x[i]dic["{}".format(i)] for i in range(5)) H = sum(dic.values())3/5H_1 - (- H_2) # Negative sign is a work-around