Closed Wenlong-Y closed 1 year ago
Hi @Wenlong-Y,
You're correct but this is not a problem particular to the diet-planning example. It is noted in the concepts/CQM documentation that, "Real-valued variables are currently not supported in quadratic interactions."
Outside of the documentation example, do you have a problem you're looking to optimize that has interactions between real variables?
Hi Joel, Thanks for the quick response. I don't have other problems outside of this example. I was just trying to go through the examples provided to learn how to use dwave system. I saw this problem, which doesn't seem to cause any trouble running the rest of the example and want to alert you about this thing. It could scare people when they see this kind of errors.
Description I am using the dimod package to try the Diet Planning example on my computer. I got this error when I am running: quantities[0] * quantities[1]
It seems the package doesn't allow real numbers to have interactions.
To Reproduce import dimod
foods = { 'rice': {'Calories': 100, 'Protein': 3, 'Fat': 1, 'Carbs': 22, 'Fiber': 2, 'Taste': 7, 'Cost': 2.5, 'Units': 'continuous'}, 'tofu': {'Calories': 140, 'Protein': 17, 'Fat': 9, 'Carbs': 3, 'Fiber': 2, 'Taste': 2, 'Cost': 4.0, 'Units': 'continuous'}, 'banana': {'Calories': 90, 'Protein': 1, 'Fat': 0, 'Carbs': 23, 'Fiber': 3, 'Taste': 10, 'Cost': 1.0, 'Units': 'discrete'}}
quantities = [dimod.Real(f"{food}") if foods[food]["Units"] == "continuous" else dimod.Integer(f"{food}") for food in foods.keys()]
quantities[0] * quantities[1]
The program shows this:
ValueError Traceback (most recent call last) Cell In[20], line 1 ----> 1 quantities[0] * quantities[1]
File ~\Anaconda3\envs\dwave\lib\site-packages\dimod\quadratic\quadratic_model.py:252, in QuadraticModel.mul(self, other) 250 raise RuntimeError("unexpected vartype") 251 else: --> 252 new.add_quadratic(u, v, ubias vbias) 254 new.add_linear(u, ubias other_offset) 256 for v, bias in other.linear.items():
File ~\Anaconda3\envs\dwave\lib\site-packages\dimod\decorators.py:404, in forwarding_method..wrapper(obj, *args, *kwargs)
399 except TypeError:
400 raise TypeError(
401 f"the 'dict' attribute of {type(obj).name!r} "
402 "instance does not support item assignment.") from None
--> 404 return method(args, **kwargs)
File ~\Anaconda3\envs\dwave\lib\site-packages\dimod\quadratic\cyqm\cyqm_template.pyx.pxi:184, in dimod.quadratic.cyqm.cyqm_float64.cyQM_template.add_quadratic()
File ~\Anaconda3\envs\dwave\lib\site-packages\dimod\quadratic\cyqm\cyqm_template.pyx.pxi:169, in dimod.quadratic.cyqm.cyqm_float64.cyQM_template._add_quadratic()
ValueError: REAL variables (e.g. 'rice') cannot have interactions
Expected behavior QuadraticModel({'rice': 0.0, 'tofu': 0.0}, {('tofu', 'rice'): 1.0}, 0.0, {'rice': 'REAL', 'tofu': 'REAL'}, dtype='float64')
Environment:
Additional context Add any other context about the problem here.