Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ported DQM code to C++ #781

Open
wants to merge 36 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
c5872e0
Initial commit, added header file for dqm
Jan 12, 2021
b916187
Added the energy function
Jan 13, 2021
3dc630d
Added more functions
Jan 13, 2021
e98a393
Refinements non-specific
Jan 13, 2021
2497a30
Finished dqm basic functions except to-from conversion
Jan 14, 2021
d66cb30
Changed names to be compatible with current cython code
Jan 14, 2021
743bcac
NFC
Jan 14, 2021
e87373f
fixes
Jan 15, 2021
4cf6687
Fix asserts & parallelize quadratic operations
Jan 15, 2021
fcebf05
Fix parallelization bug
Jan 15, 2021
b4a983e
Added functions for converting from numpy vectors
Jan 16, 2021
bfea932
Added declaration for adjvectordqm.h in cython
Jan 18, 2021
ce9ed8b
Update the pxd file for dqm for type names
Jan 19, 2021
a51c270
Initial cython changes
Jan 19, 2021
9e160c0
Templated from numpy function
Jan 19, 2021
6d76a93
Templated the functions.
Jan 19, 2021
89fef86
Manual rebase to master
Jan 19, 2021
55809b0
Updated cydiscrete pyx file
Jan 20, 2021
aa12593
Fix pxd file
Jan 20, 2021
42d03fe
Completed initial to_numpy_vectors
Jan 20, 2021
3ceafa9
NFC & type fix
Jan 20, 2021
6fb1510
Fix bug missing arg
Jan 20, 2021
946d508
NFC and add new function to test connection
Jan 21, 2021
622117a
Reflect changes in cpp file in pxd file
Jan 21, 2021
194d4e8
Fixed most cython compilation errors
Jan 21, 2021
7cd854a
Fixed a bunch of errors
Jan 21, 2021
62bbd13
Fixed bug
Jan 21, 2021
49e476c
Fix auto related errors
Jan 21, 2021
86f8056
Remove auto as they are not safe for immediate assignments
Jan 22, 2021
6ad650d
Fix bugs
Jan 22, 2021
9372616
Fix bugs
Jan 23, 2021
36ee4cf
Fix bug
Jan 24, 2021
34769c1
Fix bugs
Jan 24, 2021
5e324b3
Addressed some review comments
Jan 25, 2021
2e5e4c8
Switched from get/set linear case to linear_case
Jan 25, 2021
0e75ca9
Made copy constructor templated
Jan 25, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions dimod/discrete/cppdqm.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# distutils: language = c++
# cython: language_level=3
#
# Copyright 2019 D-Wave Systems 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.
#
# =============================================================================

from dimod.bqm.cppbqm cimport AdjVectorBQM as cppAdjVectorBQM
from dimod.bqm.common cimport VarIndex, Bias
from libcpp cimport bool
from libcpp.pair cimport pair
from libcpp.vector cimport vector

cimport numpy as np


cdef extern from "dimod/adjvectordqm.h" namespace "dimod" nogil:

cdef cppclass AdjVectorDQM[V, B]:
ctypedef V variable_type
ctypedef B bias_type
ctypedef size_t size_type

cppAdjVectorBQM[variable_type, bias_type] bqm_
vector[variable_type] case_starts_
vector[vector[variable_type]] adj_

# constructors
# cython cannot handle templated constructors, so we call out the types
# explicitly

AdjVectorDQM() except +
AdjVectorDQM(const AdjVectorDQM & ) except +

# the actual signature is more general, but we already have a large
# number of these so we'll add them as needed
# template <class io_variable_type, class io_bias_type>
# AdjVectorDQM(io_variable_type *case_starts, size_type num_variables,
# io_bias_type *linear_biases, size_type num_cases,
# io_variable_type *irow, io_variable_type *icol,
# io_bias_type *quadratic_biases, size_type num_interactions)
AdjVectorDQM(np.uint32_t*, size_type, np.uint32_t*, size_type, np.uint32_t*, np.uint32_t*, np.uint32_t*, size_type)
AdjVectorDQM(np.uint32_t*, size_type, np.uint64_t*, size_type, np.uint32_t*, np.uint32_t*, np.uint64_t*, size_type)
AdjVectorDQM(np.uint32_t*, size_type, np.int32_t*, size_type, np.uint32_t*, np.uint32_t*, np.int32_t*, size_type)
AdjVectorDQM(np.uint32_t*, size_type, np.int64_t*, size_type, np.uint32_t*, np.uint32_t*, np.int64_t*, size_type)
AdjVectorDQM(np.uint32_t*, size_type, np.float32_t*, size_type, np.uint32_t*, np.uint32_t*, np.float32_t*, size_type)
AdjVectorDQM(np.uint32_t*, size_type, np.float64_t*, size_type, np.uint32_t*, np.uint32_t*, np.float64_t*, size_type)
AdjVectorDQM(np.uint64_t*, size_type, np.uint32_t*, size_type, np.uint64_t*, np.uint64_t*, np.uint32_t*, size_type)
AdjVectorDQM(np.uint64_t*, size_type, np.uint64_t*, size_type, np.uint64_t*, np.uint64_t*, np.uint64_t*, size_type)
AdjVectorDQM(np.uint64_t*, size_type, np.int32_t*, size_type, np.uint64_t*, np.uint64_t*, np.int32_t*, size_type)
AdjVectorDQM(np.uint64_t*, size_type, np.int64_t*, size_type, np.uint64_t*, np.uint64_t*, np.int64_t*, size_type)
AdjVectorDQM(np.uint64_t*, size_type, np.float32_t*, size_type, np.uint64_t*, np.uint64_t*, np.float32_t*, size_type)
AdjVectorDQM(np.uint64_t*, size_type, np.float64_t*, size_type, np.uint64_t*, np.uint64_t*, np.float64_t*, size_type)
AdjVectorDQM(np.int32_t*, size_type, np.uint32_t*, size_type, np.int32_t*, np.int32_t*, np.uint32_t*, size_type)
AdjVectorDQM(np.int32_t*, size_type, np.uint64_t*, size_type, np.int32_t*, np.int32_t*, np.uint64_t*, size_type)
AdjVectorDQM(np.int32_t*, size_type, np.int32_t*, size_type, np.int32_t*, np.int32_t*, np.int32_t*, size_type)
AdjVectorDQM(np.int32_t*, size_type, np.int64_t*, size_type, np.int32_t*, np.int32_t*, np.int64_t*, size_type)
AdjVectorDQM(np.int32_t*, size_type, np.float32_t*, size_type, np.int32_t*, np.int32_t*, np.float32_t*, size_type)
AdjVectorDQM(np.int32_t*, size_type, np.float64_t*, size_type, np.int32_t*, np.int32_t*, np.float64_t*, size_type)
AdjVectorDQM(np.int64_t*, size_type, np.uint32_t*, size_type, np.int64_t*, np.int64_t*, np.uint32_t*, size_type)
AdjVectorDQM(np.int64_t*, size_type, np.uint64_t*, size_type, np.int64_t*, np.int64_t*, np.uint64_t*, size_type)
AdjVectorDQM(np.int64_t*, size_type, np.int32_t*, size_type, np.int64_t*, np.int64_t*, np.int32_t*, size_type)
AdjVectorDQM(np.int64_t*, size_type, np.int64_t*, size_type, np.int64_t*, np.int64_t*, np.int64_t*, size_type)
AdjVectorDQM(np.int64_t*, size_type, np.float32_t*, size_type, np.int64_t*, np.int64_t*, np.float32_t*, size_type)
AdjVectorDQM(np.int64_t*, size_type, np.float64_t*, size_type, np.int64_t*, np.int64_t*, np.float64_t*, size_type)

# methods

bool self_loop_present() except +
bool connection_present(variable_type, variable_type) except +
void connect_variables(variable_type, variable_type) except +
size_type num_variables() except +
size_type num_variable_interactions() except +
size_type num_cases(variable_type) except +
size_type num_case_interactions() except +
bias_type & linear_case(variable_type, variable_type) except +
void get_linear[io_bias_type](variable_type, io_bias_type*) except +
void set_linear[io_bias_type](variable_type, io_bias_type*) except +
pair[bias_type, bool] get_quadratic_case(variable_type, variable_type, variable_type, variable_type) except +
bool set_quadratic_case(variable_type, variable_type, variable_type, variable_type, bias_type) except +
bool get_quadratic[io_bias_type](variable_type, variable_type, io_bias_type*) except +
bool set_quadratic[io_bias_type](variable_type, variable_type, io_bias_type*) except +
double get_energy[io_variable_type](io_variable_type*) except +
void to_coo[io_variable_type, io_bias_type](io_variable_type*, io_bias_type*, io_variable_type*, io_variable_type*, io_bias_type*) except +

# shapeable methods

variable_type add_variable(variable_type) except +
6 changes: 2 additions & 4 deletions dimod/discrete/cydiscrete_quadratic_model.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ from libcpp.vector cimport vector

cimport numpy as np

from dimod.bqm.cppbqm cimport AdjVectorBQM as cppAdjVectorBQM
from dimod.discrete.cppdqm cimport AdjVectorDQM as cppAdjVectorDQM
from dimod.bqm.common cimport Integral32plus, Numeric, Numeric32plus


Expand All @@ -36,9 +36,7 @@ ctypedef fused Unsigned:


cdef class cyDiscreteQuadraticModel:
cdef cppAdjVectorBQM[CaseIndex, Bias] bqm_
cdef vector[CaseIndex] case_starts_ # len(adj_) + 1
cdef vector[vector[VarIndex]] adj_
cdef cppAdjVectorDQM[VarIndex, Bias] dqm_

cdef readonly object dtype
cdef readonly object case_dtype
Expand Down
Loading