Code Monkey home page Code Monkey logo

qubotools.jl's Introduction

QUBOTools.jl

QUBOTools.jl
arXiv CI JuliaCon 2022 Docs DOI
Tools for Quadratic Unconstrained Binary Optimization models in Julia

Introduction

The QUBOTools.jl package implements codecs for QUBO (Quadratic Unconstrained Binary Optimization) instances. Its purpose is to provide fast and reliable conversion between common formats used to represent such problems. This allows for rapid leverage of many emergent computing architectures whose job is to solve this kind of optimization problem.

The term QUBO is widely used when referring to boolean problems of the form

$$\begin{array}{rl} \min & \mathbf{x}'\ Q\ \mathbf{x} \\ \text{s.t.} & \mathbf{x} \in \mathbb{B}^{n} \end{array}$$

with symmetric $Q \in \mathbb{R}^{n \times n}$. Nevertheless, this package also fully supports Ising Models, given by

$$\begin{array}{rl} \min & \mathbf{s}'\ J\ \mathbf{s} + \mathbf{h}'\ \mathbf{s} \\ \text{s.t.} & \mathbf{s} \in \left\lbrace-1, 1\right\rbrace^{n} \end{array}$$

where $J \in \mathbb{R}^{n \times n}$ is triangular and $\mathbf{h} \in \mathbb{R}^{n}$.

Getting Started

Installation

import Pkg

Pkg.add("QUBOTools")

Basic Usage

using QUBOTools

model = QUBOTools.read_model("problem.json")

QUBOTools.write_model("problem.qubo", model)

Supported Formats

The r and w marks indicate that reading and writing modes are available for the corresponding file format, respectively.

The BQPJSON format was designed at LANL-ANSI to represent Binary Quadratic Programs in a platform-independet fashion. This is accomplished by using .json files validated using a well-defined JSON Schema.

QUBO rw

The QUBO specification appears as the input format in many of D-Wave's applications. A brief explanation about it can be found in qbsolv's repository README.

This is the simplest of all current supported formats.

MiniZinc is a constraint modelling language that can be used as input for many solvers.

HFS w

HFS is a very low-level mapping of weights to D-Wave's chimera graph.

PSR Quantum Optimization Toolchain

ToQUBO.jl QUBODrivers.jl QUBOTools.jl

qubotools.jl's People

Contributors

pedromxavier avatar pedroripper avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

qubotools.jl's Issues

Naming Choices

As @ccoffrin pointed out, we need to chose a proper standard for naming the binary quadratic models under our attention.

Current cadidates are:

BQP (Binary Quadratic Program)

Pros Cons
Used in BQPJSON Clashes with the complexity class
Clashes with Boolean Quadratic Polytopes Cutting Planes method

BQM (Binary Quadratic Model)

Pros Cons
Used almost exclusively by D-Wave

QUBO (Quadratic Unconstrained Binary Optimization) ✔️

Pros Cons
The standard books and papers are using bias towards $\lbrace{0,1}\rbrace$ models and not Ising models
Nice to say out loud as a word

UBQP (Unconstrained Binary Quadratic Program)

Pros Cons
Already in use in some places somewhat lengthy to say (needs spelling)
Mentioned as an alias for QUBO in Wikipedia
Shortest distance to BQP (current name)

QP (Quadratic Programs)

Pros Cons
Already in use by IBM General term to identify specific class of problems
Ambiguous (as a LP generalization)

New suggestions and additions to pros and cons are welcome!

Should we provide `MOI` support?

Should we depend on and implement features directed to the MOI/JuMP ecossystem in this package?

Pros:

  1. This avoids duplicate code across packages when integrating QUBOTools and JuMP
  2. We could provide advanced, homogeneous support for spin variables, i.e., $s \in \lbrace{\pm 1}\rbrace$
  3. Also, there could be efficient MOI models for QUBO problems
  4. We could better negotiate with the MOI file format system

Cons:

  1. The package would be made less lightweight, which is a cool feature to deliver
  2. It would also become less platform-agnostic

@pedroripper, @AndradeTiago, @joaquimg and @bernalde, feel free to add your toughts on this discussion!

Implement fixed-size SampleSet

The ExactSampler searches $2^n$ states but it is interesting to hold only a few. This could be implemented using a Heap.

Add specialized wrappers for visualization recipes

Instead of implementing recipes using direct type dispatch, create wrappers that, for example, allow for different visualization schemes to be created from the same data.

Instead of doing

using QUBOTools
using Plots
sol = SampleSet(...)

plot(sol)

it should be

using QUBOTools
using Plots
sol = SampleSet(...)

plot(EnergyFrequencyPlot(sol))

Error writing problem to another format

write() command doesn't work.
image
image

Without any problems QUBOTools can read created model and give information about it. Yet it has problems with writing it.
It doesn't work even with the "problem.json" file mentioned in the markdown.

QUBOTools + MathOptInterface Wrapper for ToQUBO.jl and QUBODrivers.jl

As pointed out in #34, both ToQUBO.jl and QUBODrivers.jl will import MathOptInterface.jl and implement a couple methods to integrate MOI with QUBOTools.jl.

function QUBOTools.varlt(x::MOI.VariableIndex, y::MOI.VariableIndex)
    ...
end

function QUBOTools.sense(s::MOI.OptimizationSense)
    ...
end

Currently, both packages contain the same wrapper code.

%%{init: {'theme':'neutral'}}%%
flowchart LR;
    subgraph QUBO ["QUBO.jl"]
        direction LR;
        
        MOI(["MathOptInterface.jl"]);
        
        subgraph TOQUBO["ToQUBO.jl"]
            direction LR;
            TOQUBOWRAPPER["wrapper"];
        end
        
        MOI -..-> TOQUBOWRAPPER;
        
        subgraph QUBODRIVERS["QUBODrivers.jl"]
            direction LR;
            
            QUBODRIVERSWRAPPER["wrapper"];
        end
        
        PBO(["PBO.jl"]);
        QUBOTOOLS(["QUBOTools.jl"]);
        
        MOI -..-> QUBODRIVERSWRAPPER;
        
        QUBOTOOLS -.-> QUBODRIVERS;
        QUBOTOOLS -.-> TOQUBO;
        PBO -.-> QUBOTOOLS;
    end
Loading

When both are loaded in the same session, as done by QUBO.jl, the Julia compiler will warn about method redefinition and its impact in incremental compilation. Checking for definitions using hasmethod fails since the wrapped functions provide dispatch options on more abstract types, including Any. The ideal solution would be put the wrapper inside a module that would be imported by both packages. This could be done by creating an extra package or by using Package Extensions, which impose the drawback of requiring Julia 1.9 or greater.

%%{init: {'theme':'neutral'}}%%
flowchart LR;
    subgraph QUBO ["QUBO.jl"]
        direction LR;
        
        MOI(["MathOptInterface.jl"]);
        
        subgraph QUBOTOOLS["QUBOTools.jl"]
            direction LR;
            
            WRAPPER["wrapper"];
        end
        
        PBO(["PBO.jl"]);
        QUBODRIVERS(["QUBODrivers.jl"]);
        TOQUBO(["ToQUBO.jl"]);

        MOI -.-> WRAPPER;

        WRAPPER -.-> QUBODRIVERS;
        WRAPPER -.-> TOQUBO;
        PBO -.-> QUBOTOOLS;
    end
Loading

Another option would be to simulate C/C++'s #ifndef behavior using a hook defined by QUBOTools.jl or even environment variables.

TagBot trigger issue

This issue is used to trigger TagBot; feel free to unsubscribe.

If you haven't already, you should update your TagBot.yml to include issue comment triggers.
Please see this post on Discourse for instructions and more details.

If you'd like for me to do this for you, comment TagBot fix on this issue.
I'll open a PR within a few hours, please be patient!

Add function for computing Hamming distance between solution state vectors

Rationale

This measurement is useful to determine how far, in terms of state flips, a solution is from another, e.g., the groud state.

Suggestion

function hamming(x::Vector{U}, y::Vector{U}) where {U<:Integer}
    return count(x .!= y)
end

Example

julia> x = [0, 1, 0];

julia> y = [1, 1, 0];

julia> hamming(x, y)
1

How to measure time?

We currently let the sampler tell us the time it took during optimization, as in:

function Anneal.sample(annealer::Optimizer{T}) where {T}
    sampler = neal.SimulatedAnnealingSampler()

    t₀ = time()
    records = sampler.sample_qubo(
        annealer.Q;
        num_reads=MOI.get(annealer, MOI.RawOptimizerAttribute("num_reads")),
        num_sweeps=MOI.get(annealer, MOI.RawOptimizerAttribute("num_sweeps")),
    ).record
    samples = [(pyconvert.(Int, s), pyconvert(Int, n), pyconvert(Float64, e + annealer.c)) for (s, e, n) in records]
    t₁ = time()

    δt = t₁ - t₀

    return (samples, δt)
end

Shouldn't we care about the sampling only and measure time automatically from outside of the Anneal.sample call, taking initialization and data formatting into account?

HDF5 format

I am working on adding a new format to QUBOTools.jl - HDF5.
In mentioned format in dataset I would like to have two-dimensional matrix. Model allows only dictionaries for linear and quadratic terms.
Yet, rewriting an array to dictionary takes plenty of time. I would like to skip this step and write it as matrix.
I was thinking about adding reading and writing terms for matrix to every other format, but I don't think it's the best way. Do you have any idea how to do it better?

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.