Skip to main content

C++ library for a binary (and polynomial) quadratic model.

Project description

cimod : C++ header-only library for a binary quadratic model

PyPI version shields.io PyPI pyversions PyPI implementation PyPI format PyPI license PyPI download month Downloads

Test Build&Upload CodeQL Build Documentation pages-build-deployment Codacy Badge Maintainability codecov

Coverage Graph

Sunburst Grid Icicle

How to use

You should only include a header src/binary_quadratic_model.hpp in your project.

Example

C++

#include "src/binary_quadratic_model.hpp"

using namespace cimod;
int main()
{
// Set linear biases and quadratic biases
Linear<uint32_t, double> linear{ {1, 1.0}, {2, 2.0}, {3, 3.0}, {4, 4.0} };
Quadratic<uint32_t, double> quadratic
{
     {std::make_pair(1, 2), 12.0}, {std::make_pair(1, 3), 13.0}, {std::make_pair(1, 4), 14.0},
     {std::make_pair(2, 3), 23.0}, {std::make_pair(2, 4), 24.0},
     {std::make_pair(3, 4), 34.0}
 };

// Set offset
double offset = 0.0;

// Set variable type
Vartype vartype = Vartype::BINARY;
// Create a BinaryQuadraticModel instance
BinaryQuadraticModel<uint32_t, double, cimod::Dense> bqm(linear, quadratic, offset, vartype);

//linear terms -> bqm.get_linear()
//quadratic terms -> bqm.get_quadratic()

return 0;
}

Python

import cimod
import dimod

# Set linear biases and quadratic biases
linear = {1:1.0, 2:2.0, 3:3.0, 4:4.0}
quadratic = {(1,2):12.0, (1,3):13.0, (1,4):14.0, (2,3):23.0, (2,4):24.0, (3,4):34.0}

# Set offset
offset = 0.0

# Set variable type
vartype = dimod.BINARY

# Create a BinaryQuadraticModel instance
bqm = cimod.BinaryQuadraticModel(linear, quadratic, offset, vartype)

print(bqm.linear)
print(bqm.quadratic)

For Contributor

Use pre-commit for auto chech before git commit. .pre-commit-config.yaml

# pipx install pre-commit 
# or 
# pip install pre-commit
pre-commit install

Install

via this directory

$ python -m pip install -vvv .

via pip

# Binary
$ pip install jij-cimod
# From Source 
$ pip install --no-binary=jij-cimod jij-cimod 

Test

Python

$ python -m venv .venv
$ pip install pip-tools 
$ pip-compile setup.cfg
$ pip-compile dev-requirements.in
$ pip-sync requirements.txt dev-requirements.txt
$ source .venv/bin/activate
$ export CMAKE_BUILD_TYPE=Debug
$ python setup.py --force-cmake install --build-type Debug -G Ninja
$ python setup.py --build-type Debug test 
$ python -m coverage html

C++

$ mkdir build 
$ cmake -DCMAKE_BUILD_TYPE=Debug -S . -B build
$ cmake --build build --parallel
$ cd build
$ ./tests/cimod_test
# Alternatively Use CTest 
$ ctest --extra-verbose --parallel --schedule-random

Needs: CMake > 3.22, C++17

  • Format
$ pip-compile format-requirements.in
$ pip-sync format-requirements.txt
$ python -m isort 
$ python -m black 
  • Aggressive Format
$ python -m isort --force-single-line-imports --verbose ./cimod
$ python -m autoflake --in-place --recursive --remove-all-unused-imports --ignore-init-module-imports --remove-unused-variables ./cimod
$ python -m autopep8 --in-place --aggressive --aggressive  --recursive ./cimod
$ python -m isort ./cimod
$ python -m black ./cimod
  • Lint
$ pip-compile setup.cfg
$ pip-compile dev-requirements.in
$ pip-compile lint-requirements.in
$ pip-sync requirements.txt dev-requirements.txt lint-requirements.txt
$ python -m flake8
$ python -m mypy
$ python -m pyright

Benchmark

Benchmark code

import dimod
import cimod
import time

fil = open("benchmark", "w")
fil.write("N t_dimod t_cimod\n")

def benchmark(N, test_fw):
    linear = {}
    quadratic = {}

    spin = {}

    # interactions

    for i in range(N):
        spin[i] = 1

    for elem in range(N):
        linear[elem] = 2.0*elem;

    for i in range(N):
        for j in range(i+1, N):
            if i != j:
                quadratic[(i,j)] = (i+j)/(N)

    t1 = time.time()

    # initialize
    a = test_fw.BinaryQuadraticModel(linear, quadratic, 0, test_fw.BINARY)
    a.change_vartype(test_fw.SPIN)

    # calculate energy for 50 times.
    for _ in range(50):
        print(a.energy(spin))

    t2 = time.time()

    return t2-t1

d_arr = []
c_arr = []

for N in [25, 50, 100, 200, 300, 400, 600, 800,1000, 1600, 2000, 3200, 5000]:
    print("N {}".format(N))
    d = benchmark(N, dimod)
    c = benchmark(N, cimod)
    print("{} {} {}".format(N, d, c))
    fil.write("{} {} {}\n".format(N, d, c))

Software versions

Package Version
cimod 1.0.3
dimod 0.9.2

Result

benchmark

Licences

Copyright 2022 Jij Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0  

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

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

jij_cimod-1.6.2.tar.gz (84.0 kB view hashes)

Uploaded Source

Built Distributions

jij_cimod-1.6.2-cp312-cp312-win_amd64.whl (1.0 MB view hashes)

Uploaded CPython 3.12 Windows x86-64

jij_cimod-1.6.2-cp312-cp312-manylinux_2_28_x86_64.whl (11.6 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.28+ x86-64

jij_cimod-1.6.2-cp312-cp312-manylinux_2_28_aarch64.whl (1.0 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.28+ ARM64

jij_cimod-1.6.2-cp312-cp312-macosx_13_0_universal2.whl (2.1 MB view hashes)

Uploaded CPython 3.12 macOS 13.0+ universal2 (ARM64, x86-64)

jij_cimod-1.6.2-cp312-cp312-macosx_10_14_universal2.whl (2.1 MB view hashes)

Uploaded CPython 3.12 macOS 10.14+ universal2 (ARM64, x86-64)

jij_cimod-1.6.2-cp311-cp311-win_amd64.whl (1.0 MB view hashes)

Uploaded CPython 3.11 Windows x86-64

jij_cimod-1.6.2-cp311-cp311-manylinux_2_28_x86_64.whl (11.6 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.28+ x86-64

jij_cimod-1.6.2-cp311-cp311-manylinux_2_28_aarch64.whl (1.0 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.28+ ARM64

jij_cimod-1.6.2-cp311-cp311-macosx_13_0_universal2.whl (2.1 MB view hashes)

Uploaded CPython 3.11 macOS 13.0+ universal2 (ARM64, x86-64)

jij_cimod-1.6.2-cp311-cp311-macosx_10_14_universal2.whl (2.1 MB view hashes)

Uploaded CPython 3.11 macOS 10.14+ universal2 (ARM64, x86-64)

jij_cimod-1.6.2-cp310-cp310-win_amd64.whl (1.0 MB view hashes)

Uploaded CPython 3.10 Windows x86-64

jij_cimod-1.6.2-cp310-cp310-manylinux_2_28_x86_64.whl (11.6 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.28+ x86-64

jij_cimod-1.6.2-cp310-cp310-manylinux_2_28_aarch64.whl (999.6 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.28+ ARM64

jij_cimod-1.6.2-cp310-cp310-macosx_13_0_universal2.whl (2.0 MB view hashes)

Uploaded CPython 3.10 macOS 13.0+ universal2 (ARM64, x86-64)

jij_cimod-1.6.2-cp310-cp310-macosx_10_14_universal2.whl (2.0 MB view hashes)

Uploaded CPython 3.10 macOS 10.14+ universal2 (ARM64, x86-64)

jij_cimod-1.6.2-cp39-cp39-win_amd64.whl (1.1 MB view hashes)

Uploaded CPython 3.9 Windows x86-64

jij_cimod-1.6.2-cp39-cp39-manylinux_2_28_x86_64.whl (11.6 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.28+ x86-64

jij_cimod-1.6.2-cp39-cp39-manylinux_2_28_aarch64.whl (1.0 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.28+ ARM64

jij_cimod-1.6.2-cp39-cp39-macosx_13_0_universal2.whl (2.0 MB view hashes)

Uploaded CPython 3.9 macOS 13.0+ universal2 (ARM64, x86-64)

jij_cimod-1.6.2-cp39-cp39-macosx_10_14_universal2.whl (2.0 MB view hashes)

Uploaded CPython 3.9 macOS 10.14+ universal2 (ARM64, x86-64)

jij_cimod-1.6.2-cp38-cp38-win_amd64.whl (1.0 MB view hashes)

Uploaded CPython 3.8 Windows x86-64

jij_cimod-1.6.2-cp38-cp38-manylinux_2_28_x86_64.whl (11.6 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.28+ x86-64

jij_cimod-1.6.2-cp38-cp38-manylinux_2_28_aarch64.whl (999.6 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.28+ ARM64

jij_cimod-1.6.2-cp38-cp38-macosx_13_0_universal2.whl (2.0 MB view hashes)

Uploaded CPython 3.8 macOS 13.0+ universal2 (ARM64, x86-64)

jij_cimod-1.6.2-cp38-cp38-macosx_10_14_universal2.whl (2.0 MB view hashes)

Uploaded CPython 3.8 macOS 10.14+ universal2 (ARM64, 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