Skip to main content

PyQUBO allows you to create QUBOs or Ising models from mathematical expressions.

Project description

https://img.shields.io/pypi/v/pyqubo.svg https://codecov.io/gh/recruit-communications/pyqubo/branch/master/graph/badge.svg https://readthedocs.org/projects/pyqubo/badge/?version=latest https://pepy.tech/badge/pyqubo https://github.com/recruit-communications/pyqubo/actions/workflows/build_and_upolad.yaml/badge.svg

PyQUBO

PyQUBO allows you to create QUBOs or Ising models from flexible mathematical expressions easily. Some of the features of PyQUBO are

  • Python based (C++ backend).

  • Fully integrated with Ocean SDK. (details)

  • Automatic validation of constraints. (details)

  • Placeholder for parameter tuning. (details)

For more details, see PyQUBO Documentation.

Example Usage

Creating QUBO

This example constructs a simple expression and compile it to model. By calling model.to_qubo(), we get the resulting QUBO. (This example solves Number Partitioning Problem with a set S = {4, 2, 7, 1})

>>> from pyqubo import Spin
>>> s1, s2, s3, s4 = Spin("s1"), Spin("s2"), Spin("s3"), Spin("s4")
>>> H = (4*s1 + 2*s2 + 7*s3 + s4)**2
>>> model = H.compile()
>>> qubo, offset = model.to_qubo()
>>> pprint(qubo)
{('s1', 's1'): -160.0,
('s1', 's2'): 64.0,
('s1', 's3'): 224.0,
('s1', 's4'): 32.0,
('s2', 's2'): -96.0,
('s2', 's3'): 112.0,
('s2', 's4'): 16.0,
('s3', 's3'): -196.0,
('s3', 's4'): 56.0,
('s4', 's4'): -52.0}

Integration with D-Wave Ocean

PyQUBO can output the BinaryQuadraticModel(BQM) which is compatible with Sampler class defined in D-Wave Ocean SDK. In the example below, we solve the problem with SimulatedAnnealingSampler.

>>> import neal
>>> sampler = neal.SimulatedAnnealingSampler()
>>> bqm = model.to_bqm()
>>> sampleset = sampler.sample(bqm, num_reads=10)
>>> decoded_samples = model.decode_sampleset(sampleset)
>>> best_sample = min(decoded_samples, key=lambda x: x.energy)
>>> best_sample.sample # doctest: +SKIP
{'s1': 0, 's2': 0, 's3': 1, 's4': 0}

If you want to solve the problem by actual D-Wave machines, just replace the sampler by a DWaveCliqueSampler instance, for example.

For more examples, see example notebooks.

Benchmarking

Since the core logic of the new PyQUBO (>=1.0.0) is written in C++ and the logic itself is also optimized, the execution time to produce QUBO has become shorter. We benchmarked the execution time to produce QUBOs of TSP with the new PyQUBO (1.0.0) and the previous PyQUBO (0.4.0). The result shows the new PyQUBO runs 1000 times faster as the problem size increases.

https://raw.githubusercontent.com/recruit-communications/pyqubo/master/images/benchmark.svg

Execution time includes building Hamiltonian, compilation, and producing QUBOs. The code to produce the above result is found in here.

Installation

pip install pyqubo

or

python -m pip install .

Supported Python Versions

Python 3.6, 3.7, 3.8, 3.9 and 3.10 are supported.

Supported Operating Systems

  • Linux (32/64bit)

  • OSX (x86_64(Intel)/arm64(Apple Silicon), 64bit, >=10.9)

  • Win (64bit)

Test

Run all tests.

export USE_TEST=1
python -m unittest discover test

Show coverage report.

export USE_TEST=1
coverage run -m unittest discover
coverage html

Run test with circleci CLI.

circleci build --job $JOBNAME

Run doctest.

make doctest

Dependency

This repository contains the source code of cimod which is licensed under the Apache License 2.0. cimod is the C++ header-only library for a binary quadratic model, developed by OpenJij.

Citation

If you use PyQUBO in your research, please cite the following papers ([M. Zaman, et al., 2021], [K. Tanahashi, et al., 2019]).

@article{zaman2021pyqubo,
    title={PyQUBO: Python Library for QUBO Creation},
    author={Zaman, Mashiyat and Tanahashi, Kotaro and Tanaka, Shu},
    journal={IEEE Transactions on Computers},
    year={2021},
    publisher={IEEE}
}

@article{tanahashi2019application,
  title={Application of Ising Machines and a Software Development for Ising Machines},
  author={Tanahashi, Kotaro and Takayanagi, Shinichi and Motohashi, Tomomitsu and Tanaka, Shu},
  journal={Journal of the Physical Society of Japan},
  volume={88},
  number={6},
  pages={061010},
  year={2019},
  publisher={The Physical Society of Japan}
}

Organization

Recruit Communications Co., Ltd.

Licence

Released under the Apache License 2.0.

Contribution

We welcome contributions to this project. See CONTRIBUTING.

We thank all contributors, especially @tail-island and @29rou.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyqubo-1.2.0.tar.gz (40.3 kB view hashes)

Uploaded Source

Built Distributions

pyqubo-1.2.0-cp310-cp310-win_amd64.whl (369.0 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

pyqubo-1.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (223.1 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

pyqubo-1.2.0-cp310-cp310-manylinux_2_17_i686.manylinux2014_i686.whl (241.3 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ i686

pyqubo-1.2.0-cp310-cp310-macosx_11_0_arm64.whl (183.8 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

pyqubo-1.2.0-cp310-cp310-macosx_10_9_x86_64.whl (206.2 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

pyqubo-1.2.0-cp39-cp39-win_amd64.whl (367.1 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

pyqubo-1.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (223.5 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

pyqubo-1.2.0-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl (240.1 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686

pyqubo-1.2.0-cp39-cp39-macosx_11_0_arm64.whl (183.9 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

pyqubo-1.2.0-cp39-cp39-macosx_10_9_x86_64.whl (206.4 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

pyqubo-1.2.0-cp38-cp38-win_amd64.whl (368.9 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

pyqubo-1.2.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (222.9 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

pyqubo-1.2.0-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl (240.9 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ i686

pyqubo-1.2.0-cp38-cp38-macosx_11_0_arm64.whl (184.0 kB view hashes)

Uploaded CPython 3.8 macOS 11.0+ ARM64

pyqubo-1.2.0-cp38-cp38-macosx_10_9_x86_64.whl (206.3 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

pyqubo-1.2.0-cp37-cp37m-win_amd64.whl (389.9 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

pyqubo-1.2.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (229.8 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

pyqubo-1.2.0-cp37-cp37m-manylinux_2_17_i686.manylinux2014_i686.whl (244.9 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ i686

pyqubo-1.2.0-cp37-cp37m-macosx_10_9_x86_64.whl (203.6 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

pyqubo-1.2.0-cp36-cp36m-win_amd64.whl (389.8 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

pyqubo-1.2.0-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (229.8 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ x86-64

pyqubo-1.2.0-cp36-cp36m-manylinux_2_17_i686.manylinux2014_i686.whl (244.8 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ i686

pyqubo-1.2.0-cp36-cp36m-macosx_10_9_x86_64.whl (203.7 kB view hashes)

Uploaded CPython 3.6m macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page