Skip to content

Commit

Permalink
update qiskit version
Browse files Browse the repository at this point in the history
  • Loading branch information
DeborahVolpe committed May 31, 2024
1 parent 2dfe8e3 commit 07e1f26
Showing 1 changed file with 55 additions and 35 deletions.
90 changes: 55 additions & 35 deletions src/mqt/qao/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -2140,7 +2140,8 @@ def _convert_expression_prec(
sign = field
return func, min_precision

def _convert_expression_logic(self, expr: Expr, binary_variables_name_weight: dict[str, Any]) -> boolean_var | bool:
@staticmethod
def _convert_expression_logic(expr: Expr, binary_variables_name_weight: dict[str, Any]) -> boolean_var | bool:
"""function for translating an expression in the problem variable in case of logic constraints
Keyword arguments:
Expand Down Expand Up @@ -2188,7 +2189,52 @@ def _min_max_const_estimation(exp: PUBO) -> tuple[float, float, float]:
return min_val, max_val, const

@staticmethod
def _expression_to_hamiltonian(exp: Expr, binary_variables_name_weight_val: list[Any]) -> PUBO:
def _expression_to_hamiltonian_power(
powers: list[str], binary_variables_name_weight_val: list[Any], to_add: float
) -> float | bool:
try:
power = int(powers[1])
except TypeError:
print("Expression not supported\n")
return False
key = powers[0]
for elm in binary_variables_name_weight_val:
if isinstance(elm, list):
for el in elm:
if not isinstance(el, str) and key == (next(iter(el[0].variables))):
to_add *= el[0] ** power
break
elif not isinstance(elm, str) and key == next(iter(elm[0].variables)):
to_add *= elm[0] ** power
break
return to_add

@staticmethod
def _expression_to_hamiltonian_no_power(
poly_field: str, binary_variables_name_weight_val: list[Any], to_add: float
) -> float:
key = poly_field
is_float = False
try:
temp = float(key)
except ValueError:
pass
else:
to_add *= temp
is_float = True

if not is_float:
for elm in binary_variables_name_weight_val:
if isinstance(elm, list):
for el in elm:
if not isinstance(el, str) and key == next(iter(el[0].variables)):
to_add *= el[0]
elif not isinstance(elm, str) and key == next(iter(elm[0].variables)):
to_add *= elm[0]

return to_add

def _expression_to_hamiltonian(self, exp: Expr, binary_variables_name_weight_val: list[Any]) -> PUBO:
"""function for translating an expression in the problem variable into an Hamiltonian when is directly written with the inside binary variables
Keyword arguments:
Expand All @@ -2208,40 +2254,14 @@ def _expression_to_hamiltonian(exp: Expr, binary_variables_name_weight_val: list
for poly_field in poly_fields:
powers = poly_field.split("^")
if len(powers) == 2:
try:
power = int(powers[1])
except TypeError:
print("Expression not supported\n")
return False
key = powers[0]
for elm in binary_variables_name_weight_val:
if isinstance(elm, list):
for el in elm:
if not isinstance(el, str) and key == (next(iter(el[0].variables))):
to_add *= el[0] ** power
break
elif not isinstance(elm, str) and key == next(iter(elm[0].variables)):
to_add *= elm[0] ** power
break
t = self._expression_to_hamiltonian_power(powers, binary_variables_name_weight_val, to_add)
if not isinstance(t, bool):
to_add = t
else:
key = poly_field
is_float = False
try:
temp = float(key)
except ValueError:
pass
else:
to_add *= temp
is_float = True

if not is_float:
for elm in binary_variables_name_weight_val:
if isinstance(elm, list):
for el in elm:
if not isinstance(el, str) and key == next(iter(el[0].variables)):
to_add *= el[0]
elif not isinstance(elm, str) and key == next(iter(elm[0].variables)):
to_add *= elm[0]
to_add = self._expression_to_hamiltonian_no_power(
poly_field, binary_variables_name_weight_val, to_add
)

if sign == "+":
hamiltonian += to_add
else:
Expand Down

0 comments on commit 07e1f26

Please sign in to comment.