GithubHelp home page GithubHelp logo

ashhchika / fmbqm Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tsudalab/fmbqm

0.0 1.0 0.0 12 KB

The repository is moved to https://github.com/tsudalab/fmqa

License: MIT License

Python 100.00%

fmbqm's Introduction

CAUTION!!!

The fmbqm library is renamed to fmqa and has been moved to https://github.com/tsudalab/fmqa . This repository will be no longer updated.

fmbqm

The fmbqm package provides a trainable binary quadratic model FMBQM. In combination with annealing solvers, it enables optimization of a black-box function in a data-driven way. This could expand the application of annealing solvers.

A common way of solving a combinatorial optimization problem is to encode the objective function into a binary quadratic model (BQM), where the user has to set parameters of the BQM beforehand. However, our fmbqm.FMBQM class can automatically learn the parameters based on a dataset provided by users. This is an ideal approach when the user can evaluate the objective function on any given input, but has no knowledge about the analytical form of it.

The FMBQM class inherits from dimod.BQM of D-Wave Ocean Tools, so the basic usage of FMBQM has many in common with that of BQM. For the functions specific to FMBQM, such as how to train the model, please refer to the example code below.

Install

On the root of the project, run

$ python setup.py install

Example

For an example use of the package, we try to minimize this function:

def two_complement(x, scaling=True):
    '''
    Evaluation function for binary array
    of two's complement representation.

    example (when scaling=False):
    [0,0,0,1] => 1
    [0,0,1,0] => 2
    [0,1,0,0] => 4
    [1,0,0,0] => -8
    [1,1,1,1] => -1
    '''
    val, n = 0, len(x)
    for i in range(n):
        val += (1<<(n-i-1)) * x[i] * (1 if (i>0) else -1)
    return val * (2**(1-n) if scaling else 1)

This is an evaluator of two's complement representation, while its output is scaled to [-1,1].

We fix the input length to 16, and generate initial dataset of size 5 for training.

import numpy as np

xs = np.random.randint(2, size=(5,16))
ys = np.array([two_complement(x) for x in xs])

Based on the dataset, train a FMBQM model.

import fmbqm
model = fmbqm.FMBQM.from_data(xs, ys)

We use simulated annealing from dimod package here to solve the trained model.

import dimod
sa_sampler = dimod.samplers.SimulatedAnnealingSampler()

We repeat taking 3 samples at once and updating the model for 15 times (45 samples taken in total).

for _ in range(15):
    res = sa_sampler.sample(model, num_reads=3)
    xs = np.r_[xs, res.record['sample']]
    ys = np.r_[ys, [two_complement(x) for x in res.record['sample']]]
    model.train(xs, ys)

Then, the history of the sampling looks like this.

import matplotlib.pyplot as plt
plt.plot(ys, 'o')
plt.xlabel('Selection ID')
plt.ylabel('value (scaled)')
plt.ylim([-1.0,1.0])

image

We can see that the sampling go down to near optimal as the dataset grows.

License

The fmbqm package is licensed under the MIT "Expat" License.

Citation

If you use this package in your work, please cite:

@article{kitai2019expanding,
  title={Expanding the horizon of automated metamaterials discovery via quantum annealing},
  author={Koki Kitai and Jiang Guo and Shenghong Ju and Shu Tanaka and Koji Tsuda and Junichiro Shiomi and Ryo Tamura},
  journal={arXiv preprint arXiv:1902.06573},
  year={2019}
}

fmbqm's People

Contributors

k-kitai avatar

Watchers

James Cloos avatar

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.