Variables

A class and utilities for encoding variable objects.

The Variables class is intended to be used as an attribute of other classes, such as DiscreteQuadraticModel and SampleSet.

The requirements for the class are:
  • Have a minimal memory footprint when the variables are labeled [0, n)

  • Behave like a list for iteration and getting items

  • Behave like a set for determining if it contains an item

  • Constant time for finding the index of an item

Variables Class

class Variables[source]

Set-like and list-like variables tracking.

Parameters

iterable (iterable) – An iterable of labels. Duplicate labels are ignored. All labels must be hashable.

Properties

is_range

Return True if the variables are labeled [0,n).

Methods

index

Return the index of v.

count

Return the number of times v appears in the variables.

to_serializable()

Return an object that is json-serializable.

Mutation Methods

The Variables object comes with a number of semi-private methods that allow other classes to manipulate its contents. These are intended to by used by parent classes, not by the user. Modifying a Variables object that is an attribute of a class results in undefined behaviour.

_append

Append a new variable.

_extend

Add new variables.

_pop

Remove the last variable.

_relabel

Relabel the variables in-place.

_relabel_as_integers

Relabel the variables as integers in-place.