dimod.utilities.qubo_energy#
- qubo_energy(sample, Q, offset=0.0)[source]#
Calculate the energy for the specified sample of a QUBO model.
Energy of a sample for a binary quadratic model is defined as a sum, offset by the constant energy offset associated with the model, of the sample multipled by the linear bias of the variable and all its interactions. For a quadratic unconstrained binary optimization (QUBO) model,
\[E(\mathbf{x}) = \sum_{u,v} Q_{u,v} x_u x_v + c\]where \(x_v\) is the sample, \(Q_{u,v}\) a matrix of biases, and \(c\) the energy offset.
- Parameters:
sample (dict[variable, spin]) – Sample for a binary quadratic model as a dict of form {v: bin, …}, where keys are variables of the model and values are binary (either 0 or 1).
Q (dict[(variable, variable), coefficient]) – QUBO coefficients in a dict of form {(u, v): coefficient, …}, where keys are 2-tuples of variables of the model and values are biases associated with the pair of variables. Tuples (u, v) represent interactions and (v, v) linear biases.
offset (numeric, optional, default=0) – Constant offset to be applied to the energy. Default 0.
- Returns:
The induced energy.
- Return type:
Notes
No input checking is performed.
Examples
This example calculates the energy of a sample representing two zeros for a QUBO model of two variables that have positive biases of value 1 and are positively coupled with an interaction of value 1.
>>> sample = {1: 0, 2: 0} >>> Q = {(1, 1): 1, (2, 2): 1, (1, 2): 1} >>> dimod.qubo_energy(sample, Q, 0.5) 0.5
References