Database Utilities

Utilities for access to the sqlite cache.

cache_connect(database=None)[source]

Returns a connection object to a sqlite database.

Parameters:database (str, optional) – The path to the database the user wishes to connect to. If not specified, a default is chosen using cache_file(). If the special database name ‘:memory:’ is given, then a temporary database is created in memory.
Returns:sqlite3.Connection
insert_graph(cur, nodelist, edgelist, encoded_data=None)[source]

Insert a graph into the cache.

A graph is stored by number of nodes, number of edges and a json-encoded list of edges.

Parameters:
  • cur (sqlite3.Cursor) – An sqlite3 cursor. This function is meant to be run within a with statement.
  • nodelist (list) – The nodes in the graph.
  • edgelist (list) – The edges in the graph.
  • encoded_data (dict, optional) – If a dictionary is provided, it will be populated with the serialized data. This is useful for preventing encoding the same information many times.

Notes

This function assumes that the nodes are index-labeled and range from 0 to num_nodes - 1.

In order to minimize the total size of the cache, it is a good idea to sort the nodelist and edgelist before inserting.

Examples

>>> nodelist = [0, 1, 2]
>>> edgelist = [(0, 1), (1, 2)]
>>> with pmc.cache_connect(':memory:') as cur:
...     pmc.insert_graph(cur, nodelist, edgelist)
>>> nodelist = [0, 1, 2]
>>> edgelist = [(0, 1), (1, 2)]
>>> encoded_data = {}
>>> with pmc.cache_connect(':memory:') as cur:
...     pmc.insert_graph(cur, nodelist, edgelist, encoded_data)
>>> encoded_data['num_nodes']
3
>>> encoded_data['num_edges']
2
>>> encoded_data['edges']
'[[0,1],[1,2]]'
iter_graph(cur)[source]

Iterate over all graphs in the cache.

Parameters:

cur (sqlite3.Cursor) – An sqlite3 cursor. This function is meant to be run within a with statement.

Yields:

tuple

A 2-tuple containing:

list: The nodelist for a graph in the cache.

list: the edgelist for a graph in the cache.

Examples

>>> nodelist = [0, 1, 2]
>>> edgelist = [(0, 1), (1, 2)]
>>> with pmc.cache_connect(':memory:') as cur:
...     pmc.insert_graph(cur, nodelist, edgelist)
...     list(pmc.iter_graph(cur))
[([0, 1, 2], [[0, 1], [1, 2]])]
insert_feasible_configurations(cur, feasible_configurations, encoded_data=None)[source]

Insert a group of feasible configurations into the cache.

Parameters:
  • cur (sqlite3.Cursor) – An sqlite3 cursor. This function is meant to be run within a with statement.
  • feasible_configurations (dict[tuple[int]) – The set of feasible configurations. Each key should be a tuple of variable assignments. The values are the relative energies.
  • encoded_data (dict, optional) – If a dictionary is provided, it will be populated with the serialized data. This is useful for preventing encoding the same information many times.

Examples

>>> feasible_configurations = {(-1, -1): 0.0, (+1, +1): 0.0}
>>> with pmc.cache_connect(':memory:') as cur:
...     pmc.insert_feasible_configurations(cur, feasible_configurations)
iter_feasible_configurations(cur)[source]

Iterate over all of the sets of feasible configurations in the cache.

Parameters:cur (sqlite3.Cursor) – An sqlite3 cursor. This function is meant to be run within a with statement.
Yields:dict[tuple(int) – number]: The feasible_configurations.
insert_ising_model(cur, nodelist, edgelist, linear, quadratic, offset, encoded_data=None)[source]

Insert an Ising model into the cache.

Parameters:
  • cur (sqlite3.Cursor) – An sqlite3 cursor. This function is meant to be run within a with statement.
  • nodelist (list) – The nodes in the graph.
  • edgelist (list) – The edges in the graph.
  • linear (dict) – The linear bias associated with each node in nodelist.
  • quadratic (dict) – The quadratic bias associated with teach edge in edgelist.
  • offset (float) – The constant offset applied to the ising problem.
  • encoded_data (dict, optional) – If a dictionary is provided, it will be populated with the serialized data. This is useful for preventing encoding the same information many times.
iter_ising_model(cur)[source]

Iterate over all of the Ising models in the cache.

Parameters:

cur (sqlite3.Cursor) – An sqlite3 cursor. This function is meant to be run within a with statement.

Yields:

tuple

A 5-tuple consisting of:

list: The nodelist for a graph in the cache.

list: the edgelist for a graph in the cache.

dict: The linear biases of an Ising Model in the cache.

dict: The quadratic biases of an Ising Model in the cache.

float: The constant offset of an Ising Model in the cache.

insert_penalty_model(cur, penalty_model)[source]

Insert a penalty model into the database.

Parameters:
  • cur (sqlite3.Cursor) – An sqlite3 cursor. This function is meant to be run within a with statement.
  • penalty_model (penaltymodel.PenaltyModel) – A penalty model to be stored in the database.

Examples

>>> import networkx as nx
>>> import penaltymodel.core as pm
>>> import dimod
>>> graph = nx.path_graph(3)
>>> decision_variables = (0, 2)
>>> feasible_configurations = {(-1, -1): 0., (+1, +1): 0.}
>>> spec = pm.Specification(graph, decision_variables, feasible_configurations, dimod.SPIN)
>>> linear = {v: 0 for v in graph}
>>> quadratic = {edge: -1 for edge in graph.edges}
>>> model = dimod.BinaryQuadraticModel(linear, quadratic, 0.0, vartype=dimod.SPIN)
>>> widget = pm.PenaltyModel.from_specification(spec, model, 2., -2)
>>> with pmc.cache_connect(':memory:') as cur:
...     pmc.insert_penalty_model(cur, widget)
iter_penalty_model_from_specification(cur, specification)[source]

Iterate through all penalty models in the cache matching the given specification.

Parameters:
  • cur (sqlite3.Cursor) – An sqlite3 cursor. This function is meant to be run within a with statement.
  • specification (penaltymodel.Specification) – A specification for a penalty model.
Yields:

penaltymodel.PenaltyModel