Functions

Sum over indices

class Sum(start_index, end_index, func)[source]

Define sum of the expressions over sequent indices.

Note

Indices run from start_index to end_index-1.

Parameters:
  • start_index (int) – index to start with.
  • end_index (int) – index ends with end_index-1.
  • func (function) – function which takes integer as an argument and returns Express.

Example

>>> from pyqubo import Sum, Array
>>> x = Array.create('x', 3, 'BINARY')
>>> exp = (Sum(0, 3, lambda i: x[i]) - 1.0)**2
>>> pprint(exp.compile().to_qubo())
({('x[0]', 'x[0]'): -1.0,
  ('x[0]', 'x[1]'): 2.0,
  ('x[0]', 'x[2]'): 2.0,
  ('x[1]', 'x[1]'): -1.0,
  ('x[1]', 'x[2]'): 2.0,
  ('x[2]', 'x[2]'): -1.0},
 1.0)