dimod.BinaryQuadraticModel.from_pandas_dataframe

classmethod BinaryQuadraticModel.from_pandas_dataframe(bqm_df, offset=0.0, interactions=None)[source]

Create a binary quadratic model from a QUBO model formatted as a pandas DataFrame.

Parameters:
  • bqm_df (pandas.DataFrame) – Quadratic unconstrained binary optimization (QUBO) model formatted as a pandas DataFrame. Row and column indices label the QUBO variables; values are QUBO coefficients.
  • offset (optional, default=0.0) – Constant offset for the binary quadratic model.
  • interactions (iterable, optional, default=[]) – Any additional 0.0-bias interactions to be added to the binary quadratic model.
Returns:

Binary quadratic model with vartype set to vartype.BINARY.

Return type:

BinaryQuadraticModel

Examples

This example creates a binary quadratic model from a QUBO in pandas DataFrame format while adding an interaction and setting a constant offset.

>>> import dimod
>>> import pandas as pd
>>> pd_qubo = pd.DataFrame(data={0: [-1, 0], 1: [2, -1]})
>>> pd_qubo
   0  1
0 -1  2
1  0 -1
>>> model = dimod.BinaryQuadraticModel.from_pandas_dataframe(pd_qubo,
...         offset = 2.5,
...         interactions = {(0,2), (1,2)})
>>> model.linear        # doctest: +SKIP
{0: -1, 1: -1.0, 2: 0.0}
>>> model.quadratic     # doctest: +SKIP
{(0, 1): 2, (0, 2): 0.0, (1, 2): 0.0}
>>> model.offset
2.5
>>> model.vartype
<Vartype.BINARY: frozenset({0, 1})>