GithubHelp home page GithubHelp logo

m3ller / penaltymodel Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dwavesystems/penaltymodel

0.0 1.0 0.0 674 KB

Utilities and interfaces for using penalty models.

Home Page: https://docs.ocean.dwavesys.com/projects/penaltymodel

License: Apache License 2.0

Python 100.00%

penaltymodel's Introduction

https://readthedocs.com/projects/d-wave-systems-penaltymodel/badge/?version=latest https://ci.appveyor.com/api/projects/status/cqfk8il1e4hgg7ih?svg=true https://circleci.com/gh/dwavesystems/penaltymodel.svg?style=svg

Included Packages

penaltymodel core
penaltymodel-cache cache
penaltymodel-maxgap maxgap
penaltymodel-mip mip
penaltymodel-lp lp

penaltymodel

One approach to solve a constraint satisfaction problem (CSP) using an Ising model or a QUBO, is to map each individual constraint in the CSP to a 'small' Ising model or QUBO. This mapping is called a penalty model.

Imagine that we want to map an AND clause to a QUBO. In other words, we want the solutions to the QUBO (the solutions that minimize the energy) to be exactly the valid configurations of an AND gate. Let z = AND(x_1, x_2).

Before anything else, let's import that package we will need.

import penaltymodel.core as pm
import dimod
import networkx as nx

Next, we need to determine the feasible configurations that we wish to target (by making the energy of these configuration in the binary quadratic low). Below is the truth table representing an AND clause.

AND Gate
x_1 x_2 z
0 0 0
0 1 0
1 0 0
1 1 1

The rows of the truth table are exactly the feasible configurations.

feasible_configurations = {(0, 0, 0), (0, 1, 0), (1, 0, 0), (1, 1, 1)}

We also need a target graph and to label the decision variables. We create a node in the graph for each variable in the problem, and we add an edge between each node, represnting the interactions between the variables. In this case we allow an interaction between every variable, but more sparse interactions are possible. The labels of the nodes and the decision variables match.

graph = nx.Graph()
graph.add_edges_from([('x1', 'x2'), ('x1', 'z'), ('x2', 'z')])
decision_variables = ['x1', 'x2', 'z']

We now have everything needed to build our Specification. We have binary variables so we select the appropriate variable type.

spec = pm.Specification(graph, decision_variables, feasible_configurations, dimod.BINARY)

At this point, if we have any factories installed, we could use the factory interface to get an appropriate penalty model for our specification.

p_model = pm.get_penalty_model(spec)

However, if we know the QUBO, we can build the penalty model ourselves. We observe that for the equation:

E(x_1, x_2, z) = x_1 x_2 - 2(x_1 + x_2) z + 3 z + 0

We get the following energies for each row in our truth table.

https://user-images.githubusercontent.com/8395238/34234533-8da5a364-e5a0-11e7-9d9f-068b4ab3a0fd.png

We can see that the energy is minimized on exactly the desired feasible configurations. So we encode this energy function as a QUBO. We make the offset 0.0 because there is no constant energy offset.

qubo = dimod.BinaryQuadraticModel({'x1': 0., 'x2': 0., 'z': 3.},
                               {('x1', 'x2'): 1., ('x1', 'z'): 2., ('x2', 'z'): 2.},
                               0.0,
                               dimod.BINARY)

We know from the table that our ground energy is 0, but we can calculate it using the qubo to check that this is true for the feasible configuration (0, 1, 0).

ground_energy = qubo.energy({'x1': 0, 'x2': 1, 'z': 0})

The last value that we need is the classical gap. This is the difference in energy between the lowest infeasible state and the ground state.

https://user-images.githubusercontent.com/8395238/34234545-9c93e5f2-e5a0-11e7-8792-5777a5c4303e.png

With all of the pieces, we can now build the penalty model.

classical_gap = 1
p_model = pm.PenaltyModel.from_specification(spec, qubo, classical_gap, ground_energy)

This project is part of the D-Wave Ocean software stack.

Installation

To install the core package:

pip install penaltymodel

License

Released under the Apache License 2.0

penaltymodel's People

Contributors

arcondello avatar joelpasvolsky avatar m3ller avatar randomir avatar uxvrob avatar

Watchers

 avatar

penaltymodel's Issues

Small Gap from MaxGap

Description
NOTE: this issue is for branch, feature/67-gap-wrt-highest-valid-state

For the particular example below, MaxGap is not returning the maximum possible gap. Instead, it is returning a smaller gap. If min_classical_gap is set to a value that is slightly larger than this smaller gap, the solver immediately fails at the first solver.solve(..) in generation.py. (i.e. The gap size is not even searched as the min_classical_gap is considered to be too large.)

To Reproduce

min_classical_gap = 3
decision_variables = ['a', 'b']
configurations = {(1, -1): -2.5,
                  (-1, 1): -2.5,
                  (-1, -1): 0.5}
graph = nx.complete_graph(decision_variables + ['c'])

linear_energy_ranges = {v: (-2, 2) for v in graph}
quadratic_energy_ranges = {(u, v): (-1, 1) for u, v in graph.edges}

self.generate_and_check(graph, configurations, decision_variables,
                        linear_energy_ranges,
                        quadratic_energy_ranges,
                        min_classical_gap)

Expected behavior
Expected at least a gap of 4 because ..

objective = 2*a + 2*b -2*c + 0.5*a*b + a*c + b*c

 a   b   c   objective
+1  +1  +1   4.5
+1  +1  -1   4.5
+1  -1  +1  -2.5    <--
+1  -1  -1   1.5
-1  +1  +1  -2.5    <--
-1  +1  -1   1.5
-1  -1  +1   -7.5
-1  -1  -1   0.5    <--

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.