17#include <pybind11/pybind11.h>
18#include <pybind11/stl.h>
19#include <pybind11/functional.h>
20#include <pybind11/eigen.h>
22#include <pybind11_json/pybind11_json.hpp>
24#include <nlohmann/json.hpp>
34namespace py = pybind11;
36using namespace py::literals;
44 py::class_<graph::Graph>(m,
"Graph", py::module_local())
45 .def(py::init<std::size_t>(),
"num_spins"_a)
74template <
typename FloatType>
77 using json = nlohmann::json;
79 auto str = std::string(
"Dense") + suffix;
80 py::class_<graph::Dense<FloatType>,
graph::Graph>(m, str.c_str(),
82 .def(py::init<std::size_t>(),
"num_spins"_a)
83 .def(py::init([](py::object obj) {
84 return std::unique_ptr<graph::Dense<FloatType>>(
89 .def(
"set_interaction_matrix",
94 const Eigen::Matrix<FloatType, Eigen::Dynamic, 1, Eigen::ColMajor>
106 const std::pair<std::size_t, std::size_t> &key,
107 FloatType val) { self.
J(key.first, key.second) = val; },
112 const std::pair<std::size_t, std::size_t> &key) {
113 return self.
J(key.first, key.second);
132template <
typename FloatType>
135 using json = nlohmann::json;
137 auto str = std::string(
"Sparse") + suffix;
138 py::class_<graph::Sparse<FloatType>,
graph::Graph>(m, str.c_str(),
140 .def(py::init<std::size_t, std::size_t>(),
"num_spins"_a,
"num_edges"_a)
141 .def(py::init<std::size_t>(),
"num_spins"_a)
142 .def(py::init([](py::object obj, std::size_t num_edges) {
143 return std::unique_ptr<graph::Sparse<FloatType>>(
147 "obj"_a,
"num_edges"_a)
148 .def(py::init([](py::object obj) {
149 return std::unique_ptr<graph::Sparse<FloatType>>(
159 const Eigen::Matrix<FloatType, Eigen::Dynamic, 1, Eigen::ColMajor>
171 const std::pair<std::size_t, std::size_t> &key,
172 FloatType val) { self.
J(key.first, key.second) = val; },
177 const std::pair<std::size_t, std::size_t> &key) {
178 return self.
J(key.first, key.second);
196template <
typename FloatType>
199 auto str = std::string(
"CSRSparse") + suffix;
200 py::class_<graph::CSRSparse<FloatType>,
graph::Graph>(m, str.c_str(),
202 .def(py::init<
const Eigen::SparseMatrix<FloatType, Eigen::RowMajor> &>(),
"interaction"_a)
207 const Eigen::Matrix<FloatType, Eigen::Dynamic, 1, Eigen::ColMajor>
220template <
typename FloatType>
223 using json = nlohmann::json;
225 auto str = std::string(
"Polynomial") + suffix;
227 py::class_<Poly, graph::Graph>(m, str.c_str(), py::module_local())
228 .def(py::init<const std::size_t>(),
"num_variables"_a)
229 .def(py::init([](
const py::object &obj) {
230 return std::unique_ptr<graph::Polynomial<FloatType>>(
234 .def(
"get_num_interactions", &Poly::get_num_interactions)
235 .def(
"calc_energy", &Poly::calc_energy,
"spins"_a,
"omp_flag"_a =
true)
236 .def(
"energy", &Poly::energy,
"spins"_a,
"omp_flag"_a =
true)
245 [](Poly &self, std::vector<graph::Index> &key,
FloatType val) {
251 [](
const Poly &self, std::vector<graph::Index> &key) {
257 [](
const Poly &self,
graph::Index key) {
return self.J(key); },
259 .def(
"get_polynomial", [](
const Poly &self) {
260 py::dict py_polynomial;
261 for (std::size_t i = 0; i < self.get_keys().size(); ++i) {
263 for (
const auto &it : self.get_keys()[i]) {
264 temp = temp + py::make_tuple(it);
266 py_polynomial[temp] = self.get_values()[i];
268 return py_polynomial;
274 py::enum_<graph::Dir>(m,
"Dir", py::module_local())
282template <
typename FloatType>
285 using json = nlohmann::json;
287 auto str = std::string(
"Square") + suffix;
289 m, str.c_str(), py::module_local())
290 .def(py::init<std::size_t, std::size_t, FloatType>(),
"num_row"_a,
291 "num_column"_a,
"init_val"_a = 0)
293 .def(py::init([](py::object obj, std::size_t num_row,
294 std::size_t num_column,
FloatType init_val) {
295 return std::unique_ptr<graph::Square<FloatType>>(
297 num_column, init_val));
299 "obj"_a,
"num_row"_a,
"num_column"_a,
"init_val"_a = 0)
307 const std::tuple<std::size_t, std::size_t, graph::Dir> &key,
309 self.
J(std::get<0>(key), std::get<1>(key), std::get<2>(key)) = val;
315 const std::tuple<std::size_t, std::size_t, graph::Dir> &key) {
316 return self.
J(std::get<0>(key), std::get<1>(key), std::get<2>(key));
322 const std::pair<std::size_t, std::size_t> &key,
323 FloatType val) { self.
h(key.first, key.second) = val; },
328 const std::pair<std::size_t, std::size_t> &key) {
329 return self.
h(key.first, key.second);
336 py::enum_<graph::ChimeraDir>(m,
"ChimeraDir")
348template <
typename FloatType>
351 using json = nlohmann::json;
353 auto str = std::string(
"Chimera") + suffix;
355 m, str.c_str(), py::module_local())
356 .def(py::init<std::size_t, std::size_t, FloatType>(),
"num_row"_a,
357 "num_column"_a,
"init_val"_a = 0)
359 .def(py::init([](py::object obj, std::size_t num_row,
360 std::size_t num_column,
FloatType init_val) {
361 return std::unique_ptr<graph::Chimera<FloatType>>(
363 num_column, init_val));
365 "obj"_a,
"num_row"_a,
"num_column"_a,
"init_val"_a = 0)
374 const std::tuple<std::size_t, std::size_t, std::size_t,
377 self.
J(std::get<0>(key), std::get<1>(key), std::get<2>(key),
378 std::get<3>(key)) = val;
384 const std::tuple<std::size_t, std::size_t, std::size_t,
386 return self.
J(std::get<0>(key), std::get<1>(key), std::get<2>(key),
393 const std::tuple<std::size_t, std::size_t, std::size_t> &key,
395 self.
h(std::get<0>(key), std::get<1>(key), std::get<2>(key)) = val;
401 const std::tuple<std::size_t, std::size_t, std::size_t> &key) {
402 return self.
h(std::get<0>(key), std::get<1>(key), std::get<2>(key));
410template <
typename GraphType>
412 const std::string >ype_str) {
416 auto str = std::string(
"ClassicalIsing") + gtype_str;
417 py::class_<ClassicalIsing>(m, str.c_str(), py::module_local())
418 .def(py::init<const graph::Spins &, const GraphType &>(),
"init_spin"_a,
419 "init_interaction"_a)
422 [](ClassicalIsing &self,
const graph::Spins &init_spin) {
423 self.reset_spins(init_spin);
426 .def_readwrite(
"spin", &ClassicalIsing::spin)
427 .def_readonly(
"interaction", &ClassicalIsing::interaction)
428 .def_readonly(
"num_spins", &ClassicalIsing::num_spins);
431 auto mkci_str = std::string(
"make_classical_ising");
434 [](
const graph::Spins &init_spin,
const GraphType &init_interaction) {
435 return system::make_classical_ising(init_spin, init_interaction);
437 "init_spin"_a,
"init_interaction"_a);
441template <
typename GraphType>
443 const std::string >ype_str) {
446 auto str = std::string(
"ClassicalIsing") + gtype_str;
448 py::class_<CIP>(m, str.c_str(), py::module_local())
450 const cimod::Vartype>(),
451 "init_variables"_a,
"init_interaction"_a,
"vartype"_a)
453 const std::string>(),
454 "init_variables"_a,
"init_interaction"_a,
"vartype"_a)
455 .def(py::init([](
const graph::Spins &init_spins,
const py::object &obj) {
456 return std::unique_ptr<CIP>(
457 new CIP(init_spins,
static_cast<nlohmann::json
>(obj)));
459 "init_spin"_a,
"obj"_a)
460 .def_readonly(
"variables", &CIP::variables)
461 .def_readonly(
"num_variables", &CIP::num_variables)
462 .def(
"reset_variables", &CIP::reset_variables,
"init_variables"_a)
463 .def(
"reset_spins", &CIP::reset_variables,
"init_spins"_a)
464 .def(
"get_values", &CIP::get_values)
465 .def(
"get_keys", &CIP::get_keys)
466 .def(
"get_adj", &CIP::get_adj)
467 .def(
"get_vartype_to_string", &CIP::get_vartype_string)
468 .def(
"get_max_effective_dE", &CIP::get_max_effective_dE)
469 .def(
"get_min_effective_dE", &CIP::get_min_effective_dE);
472 auto mkcip_str = std::string(
"make_classical_ising_polynomial");
475 [](
const graph::Spins &init_spin,
const GraphType &init_interaction,
476 const cimod::Vartype vartype) {
477 return system::make_classical_ising_polynomial(
478 init_spin, init_interaction, vartype);
480 "init_spin"_a,
"init_interaction"_a,
"vartype"_a);
485 [](
const graph::Spins &init_spin,
const GraphType &init_interaction,
486 const std::string vartype) {
487 return system::make_classical_ising_polynomial(
488 init_spin, init_interaction, vartype);
490 "init_spin"_a,
"init_interaction"_a,
"vartype"_a);
495 [](
const graph::Spins &init_spin,
const py::object &obj) {
496 return system::make_classical_ising_polynomial(
497 init_spin, static_cast<nlohmann::json>(obj));
499 "init_spin"_a,
"obj"_a);
502template <
typename GraphType>
504 const std::string >ype_str) {
507 auto str = std::string(
"KLocal") + gtype_str;
509 py::class_<KLP>(m, str.c_str(), py::module_local())
510 .def(py::init<const graph::Binaries &, const GraphType &>(),
511 "init_spin"_a,
"init_interaction"_a)
514 return std::unique_ptr<KLP>(
515 new KLP(init_binaries,
static_cast<nlohmann::json
>(obj)));
517 "init_binaries"_a,
"obj"_a)
518 .def_readonly(
"binaries", &KLP::binaries)
519 .def_readonly(
"num_binaries", &KLP::num_binaries)
520 .def_readonly(
"count_call_updater", &KLP::count_call_updater)
521 .def_property_readonly(
"num_interactions", &KLP::GetNumInteractions)
522 .def_readwrite(
"rate_call_k_local", &KLP::rate_call_k_local)
523 .def(
"reset_binaries", &KLP::reset_binaries,
"init_binaries"_a)
524 .def(
"reset_spins", &KLP::reset_binaries,
"init_spins"_a)
525 .def(
"reset_dE", &KLP::reset_dE)
526 .def(
"get_active_binaries", &KLP::get_active_binaries)
527 .def(
"get_max_effective_dE", &KLP::get_max_effective_dE)
528 .def(
"get_min_effective_dE", &KLP::get_min_effective_dE)
529 .def(
"get_keys", &KLP::get_keys)
530 .def(
"get_values", &KLP::get_values)
531 .def(
"get_vartype_to_string", &KLP::get_vartype_string)
532 .def(
"get_polynomial",
533 [](
const KLP &self) {
534 py::dict py_polynomial;
535 const auto &poly_key_list = self.get_keys();
536 const auto &poly_value_list = self.get_values();
537 for (std::size_t i = 0; i < poly_key_list.size(); ++i) {
539 for (
const auto &index : poly_key_list[i]) {
540 tuple = tuple + py::make_tuple(index);
542 py_polynomial[tuple] = poly_value_list[i];
544 return py_polynomial;
546 .def(
"get_adj", [](
const KLP &self) {
547 const auto &adj = self.get_adj();
548 const auto &poly_key_list = self.get_keys();
549 const auto &poly_value_list = self.get_values();
551 for (int64_t i = 0; i < self.num_binaries; ++i) {
553 for (
const auto &index_key : adj[i]) {
555 for (
const auto &index_binary : poly_key_list[index_key]) {
556 tuple = tuple + py::make_tuple(index_binary);
558 dict[tuple] = poly_value_list[index_key];
560 py_adj[py::int_(i)] = dict;
566 auto mkcip_str = std::string(
"make_k_local_polynomial");
569 [](
const graph::Spins &init_spin,
const GraphType &init_interaction) {
570 return system::make_k_local_polynomial(init_spin, init_interaction);
572 "init_spin"_a,
"init_interaction"_a);
575 auto mkcip_json_str = std::string(
"make_k_local_polynomial");
577 mkcip_json_str.c_str(),
578 [](
const graph::Spins &init_spin,
const py::object &obj) {
579 return system::make_k_local_polynomial(
580 init_spin, static_cast<nlohmann::json>(obj));
582 "init_spin"_a,
"obj"_a);
586template <
typename GraphType>
588 const std::string >ype_str) {
591 using FloatType =
typename GraphType::value_type;
593 auto str = std::string(
"TransverseIsing") + gtype_str;
594 py::class_<TransverseIsing>(m, str.c_str(), py::module_local())
597 "init_spin"_a,
"init_interaction"_a,
"gamma"_a)
600 "init_classical_spins"_a,
"init_interaction"_a,
"gamma"_a,
601 "num_trotter_slices"_a)
604 [](TransverseIsing &self,
606 self.reset_spins(init_trotter_spins);
608 "init_trotter_spins"_a)
611 [](TransverseIsing &self,
const graph::Spins &classical_spins) {
612 self.reset_spins(classical_spins);
615 .def_readwrite(
"trotter_spins", &TransverseIsing::trotter_spins)
616 .def_readonly(
"interaction", &TransverseIsing::interaction)
617 .def_readonly(
"num_classical_spins",
618 &TransverseIsing::num_classical_spins)
619 .def_readwrite(
"gamma", &TransverseIsing::gamma);
622 auto mkci_str = std::string(
"make_transverse_ising");
626 const GraphType &init_interaction,
double gamma) {
627 return system::make_transverse_ising(init_trotter_spins,
628 init_interaction, gamma);
630 "init_trotter_spins"_a,
"init_interaction"_a,
"gamma"_a);
634 [](
const graph::Spins &classical_spins,
const GraphType &init_interaction,
635 double gamma, std::size_t num_trotter_slices) {
636 return system::make_transverse_ising(classical_spins, init_interaction,
637 gamma, num_trotter_slices);
639 "classical_spins"_a,
"init_interaction"_a,
"gamma"_a,
640 "num_trotter_slices"_a);
644template <
typename GraphType>
646 const std::string >ype_str) {
649 using FloatType =
typename GraphType::value_type;
650 using SpinConfiguration =
typename TransverseIsing::SpinConfiguration;
652 auto str = std::string(
"ContinuousTimeIsing") + gtype_str;
653 py::class_<TransverseIsing>(m, str.c_str(), py::module_local())
654 .def(py::init<const SpinConfiguration &, const GraphType &, FloatType>(),
655 "init_spin_config"_a,
"init_interaction"_a,
"gamma"_a)
656 .def(py::init<const graph::Spins &, const GraphType &, FloatType>(),
657 "init_spins"_a,
"init_interaction"_a,
"gamma"_a)
660 [](TransverseIsing &self,
const SpinConfiguration &init_spin_config) {
661 self.reset_spins(init_spin_config);
663 "init_spin_config"_a)
666 [](TransverseIsing &self,
const graph::Spins &classical_spins) {
667 self.reset_spins(classical_spins);
670 .def_readwrite(
"spin_config", &TransverseIsing::spin_config)
671 .def_readonly(
"interaction", &TransverseIsing::interaction)
672 .def_readonly(
"num_spins", &TransverseIsing::num_spins)
673 .def_readonly(
"gamma", &TransverseIsing::gamma);
676 auto mkci_str = std::string(
"make_continuous_time_ising");
679 [](
const graph::Spins &classical_spins,
const GraphType &init_interaction,
681 return system::make_continuous_time_ising(classical_spins,
682 init_interaction, gamma);
684 "classical_spins"_a,
"init_interaction"_a,
"gamma"_a);
688template <
template <
typename>
class Updater,
typename System,
689 typename RandomNumberEngine>
691 const std::string &updater_str) {
692 auto str = std::string(
"Algorithm_") + updater_str + std::string(
"_run");
697 [](System &system, std::size_t seed,
699 const std::function<
void(
703 py::gil_scoped_release release;
705 using Callback = std::function<void(
706 const System &, const utility::UpdaterParameter<SystemType> &)>;
707 RandomNumberEngine rng(seed);
708 algorithm::Algorithm<Updater>::run(
709 system, rng, schedule_list,
710 callback ? [=](const System &system,
711 const utility::UpdaterParameter<SystemType>
712 ¶m) { callback(system, param.get_tuple()); }
713 : Callback(
nullptr));
715 py::gil_scoped_acquire acquire;
717 "system"_a,
"seed"_a,
"schedule_list"_a,
"callback"_a =
nullptr);
722 [](System &system,
const utility::ScheduleList<SystemType> &schedule_list,
723 const std::function<void(
725 const typename utility::UpdaterParameter<SystemType>::Tuple &)>
727 py::gil_scoped_release release;
729 using Callback = std::function<void(
730 const System &,
const utility::UpdaterParameter<SystemType> &)>;
731 RandomNumberEngine rng(std::random_device{}());
733 system, rng, schedule_list,
734 callback ? [=](
const System &system,
735 const utility::UpdaterParameter<SystemType>
736 ¶m) { callback(system, param.get_tuple()); }
737 : Callback(
nullptr));
739 py::gil_scoped_acquire acquire;
741 "system"_a,
"schedule_list"_a,
"callback"_a =
nullptr);
744 using TupleList = std::vector<std::pair<
745 typename utility::UpdaterParameter<SystemType>::Tuple, std::size_t>>;
750 [](System &system, std::size_t seed,
const TupleList &tuplelist,
751 const std::function<void(
753 const typename utility::UpdaterParameter<SystemType>::Tuple &)>
755 py::gil_scoped_release release;
757 using Callback = std::function<void(
758 const System &,
const utility::UpdaterParameter<SystemType> &)>;
761 system, rng, utility::make_schedule_list<SystemType>(tuplelist),
762 callback ? [=](
const System &system,
763 const utility::UpdaterParameter<SystemType>
764 ¶m) { callback(system, param.get_tuple()); }
765 : Callback(
nullptr));
767 py::gil_scoped_acquire acquire;
769 "system"_a,
"seed"_a,
"tuplelist"_a,
"callback"_a =
nullptr);
774 [](System &system,
const TupleList &tuplelist,
775 const std::function<void(
777 const typename utility::UpdaterParameter<SystemType>::Tuple &)>
779 py::gil_scoped_release release;
780 using Callback = std::function<void(
781 const System &,
const utility::UpdaterParameter<SystemType> &)>;
784 system, rng, utility::make_schedule_list<SystemType>(tuplelist),
785 callback ? [=](
const System &system,
786 const utility::UpdaterParameter<SystemType>
787 ¶m) { callback(system, param.get_tuple()); }
788 : Callback(
nullptr));
790 py::gil_scoped_acquire acquire;
792 "system"_a,
"tuplelist"_a,
"callback"_a =
nullptr);
796template <
typename SystemType>
802 return "(beta: " + std::to_string(obj.beta) +
")";
808 return "(beta: " + std::to_string(obj.beta) +
809 ", lambda: " + std::to_string(obj.lambda) +
")";
815 return "(beta: " + std::to_string(obj.beta) +
816 ", s: " + std::to_string(obj.s) +
")";
820 py::class_<utility::ClassicalUpdaterParameter>(m,
"ClassicalUpdaterParameter",
823 .def(py::init<double>(),
"beta"_a)
824 .def_readwrite(
"beta", &utility::ClassicalUpdaterParameter::beta)
831 py::class_<utility::ClassicalConstraintUpdaterParameter>(
832 m,
"ClassicalConstraintUpdaterParameter", py::module_local())
834 .def(py::init<double, double>(),
"beta"_a,
"lambda"_a)
835 .def(py::init<
const std::pair<double, double> &>(),
"obj"_a)
836 .def_readwrite(
"beta",
837 &utility::ClassicalConstraintUpdaterParameter::beta)
838 .def_readwrite(
"lambda",
839 &utility::ClassicalConstraintUpdaterParameter::lambda)
847 py::class_<utility::TransverseFieldUpdaterParameter>(
848 m,
"TransverseFieldUpdaterParameter", py::module_local())
850 .def(py::init<double, double>(),
"beta"_a,
"s"_a)
851 .def(py::init<
const std::pair<double, double> &>(),
"obj"_a)
852 .def_readwrite(
"beta", &utility::TransverseFieldUpdaterParameter::beta)
853 .def_readwrite(
"s", &utility::TransverseFieldUpdaterParameter::s)
860template <
typename SystemType>
862 auto str = systemtype_str +
"Schedule";
863 py::class_<utility::Schedule<SystemType>>(m, str.c_str(), py::module_local())
865 .def(py::init<
const std::pair<
869 .def_readwrite(
"updater_parameter",
877 m.def(
"make_schedule_list", &utility::make_schedule_list<SystemType>,
886 [](
const System &system) {
return result::get_solution(system); },
891template<
typename FloatType>
895 std::string name = std::string(
"BinaryPolynomialModel");
896 auto py_class = py::class_<BPM>(m, name.c_str(), py::module_local());
898 py_class.def(py::init<
const std::vector<std::vector<typename BPM::IndexType>>&,
899 const std::vector<FloatType>&>(),
900 "key_list"_a,
"value_list"_a);
902 py_class.def(
"get_degree", &BPM::GetDegree);
903 py_class.def(
"get_system_size", &BPM::GetSystemSize);
904 py_class.def(
"get_index_list", &BPM::GetIndexList);
905 py_class.def(
"get_index_map", &BPM::GetIndexMap);
906 py_class.def(
"get_key_value_list", &BPM::GetKeyValueList);
907 py_class.def(
"get_adjacency_list", &BPM::GetAdjacencyList);
908 py_class.def(
"get_estimated_min_energy_difference", &BPM::GetEstimatedMinEnergyDifference);
909 py_class.def(
"get_estimated_max_energy_difference", &BPM::GetEstimatedMaxEnergyDifference);
910 py_class.def(
"calculate_energy", &BPM::CalculateEnergy);
913template<
typename FloatType>
917 std::string name = std::string(
"IsingPolynomialModel");
918 auto py_class = py::class_<IPM>(m, name.c_str(), py::module_local());
920 py_class.def(py::init<std::vector<std::vector<typename IPM::IndexType>>&,
921 std::vector<FloatType>&>(),
922 "key_list"_a,
"value_list"_a);
924 py_class.def(
"get_degree", &IPM::GetDegree);
925 py_class.def(
"get_system_size", &IPM::GetSystemSize);
926 py_class.def(
"get_index_list", &IPM::GetIndexList);
927 py_class.def(
"get_index_map", &IPM::GetIndexMap);
928 py_class.def(
"get_key_value_list", &IPM::GetKeyValueList);
929 py_class.def(
"get_adjacency_list", &IPM::GetAdjacencyList);
930 py_class.def(
"get_estimated_min_energy_difference", &IPM::GetEstimatedMinEnergyDifference);
931 py_class.def(
"get_estimated_max_energy_difference", &IPM::GetEstimatedMaxEnergyDifference);
932 py_class.def(
"calculate_energy", &IPM::CalculateEnergy);
938 std::string name = std::string(
"IntegerQuadraticModel");
939 auto py_class = py::class_<IQM>(m, name.c_str(), py::module_local());
941 py_class.def(py::init<std::vector<std::vector<std::int64_t>>&,
942 std::vector<double>&,
943 std::vector<std::pair<std::int64_t, std::int64_t>>&>(),
944 "key_list"_a,
"value_list"_a,
"bound_list"_a);
946 py_class.def(
"get_max_min_terms", &IQM::GetMaxMinTerms);
947 py_class.def(
"get_index_list", &IQM::GetIndexList);
948 py_class.def(
"get_num_variables", &IQM::GetNumVariables);
949 py_class.def(
"get_quadratic", &IQM::GetQuadratic);
950 py_class.def(
"get_linear", &IQM::GetLinear);
951 py_class.def(
"get_squared", &IQM::GetSquared);
952 py_class.def(
"get_constant", &IQM::GetConstant);
953 py_class.def(
"get_bound_list", &IQM::GetBounds);
954 py_class.def(
"get_only_bilinear_index_set", &IQM::GetOnlyBilinearIndexSet);
960 std::string name = std::string(
"IntegerPolynomialModel");
961 auto py_class = py::class_<IPM>(m, name.c_str(), py::module_local());
963 py_class.def(py::init<std::vector<std::vector<std::int64_t>>&,
964 std::vector<double>&,
965 std::vector<std::pair<std::int64_t, std::int64_t>>&>(),
966 "key_list"_a,
"value_list"_a,
"bound_list"_a);
968 py_class.def(
"get_max_min_terms", &IPM::GetMaxMinTerms);
969 py_class.def(
"get_index_list", &IPM::GetIndexList);
970 py_class.def(
"get_num_variables", &IPM::GetNumVariables);
971 py_class.def(
"get_bound_list", &IPM::GetBounds);
972 py_class.def(
"get_constant", &IPM::GetConstant);
973 py_class.def(
"get_key_value_list", &IPM::GetKeyValueList);
974 py_class.def(
"get_index_to_interactions", &IPM::GetIndexToInteractions);
975 py_class.def(
"get_each_variable_degree", &IPM::GetEachVariableDegree);
979 auto py_result = py::class_<sampler::IntegerSAResult>(m,
"IntegerSAResult", py::module_local());
980 py_result.def_readonly(
"energy", &sampler::IntegerSAResult::energy);
981 py_result.def_readonly(
"solution", &sampler::IntegerSAResult::solution);
982 py_result.def_readonly(
"energy_history", &sampler::IntegerSAResult::energy_history);
983 py_result.def_readonly(
"temperature_history", &sampler::IntegerSAResult::temperature_history);
988 m.def(
"sample_by_integer_sa_quadratic",
989 &sampler::SampleByIntegerSA<graph::IntegerQuadraticModel>,
990 "model"_a,
"num_sweeps"_a,
"update_method"_a,
"rand_type"_a,
991 "schedule"_a,
"num_reads"_a,
"seed"_a,
"num_threads"_a,
992 "min_T"_a,
"max_T"_a,
"log_history"_a);
995 m.def(
"sample_by_integer_sa_polynomial",
996 &sampler::SampleByIntegerSA<graph::IntegerPolynomialModel>,
997 "model"_a,
"num_sweeps"_a,
"update_method"_a,
"rand_type"_a,
998 "schedule"_a,
"num_reads"_a,
"seed"_a,
"num_threads"_a,
999 "min_T"_a,
"max_T"_a,
"log_history"_a);
1003template<
class ModelType>
1007 std::string name = std::string(
"SASampler") + post_name;
1009 auto py_class = py::class_<SAS>(m, name.c_str(), py::module_local());
1011 py_class.def(py::init<const ModelType&>(),
"model"_a);
1013 py_class.def(
"set_num_sweeps", &SAS::SetNumSweeps,
"num_sweeps"_a);
1014 py_class.def(
"set_num_reads", &SAS::SetNumReads,
"num_reads"_a);
1015 py_class.def(
"set_num_threads", &SAS::SetNumThreads,
"num_threads"_a);
1016 py_class.def(
"set_beta_min", &SAS::SetBetaMin,
"beta_min"_a);
1017 py_class.def(
"set_beta_max", &SAS::SetBetaMax,
"beta_max"_a);
1018 py_class.def(
"set_beta_min_auto", &SAS::SetBetaMinAuto);
1019 py_class.def(
"set_beta_max_auto", &SAS::SetBetaMaxAuto);
1020 py_class.def(
"set_update_method", &SAS::SetUpdateMethod,
"update_method"_a);
1021 py_class.def(
"set_random_number_engine", &SAS::SetRandomNumberEngine,
"random_number_engine"_a);
1022 py_class.def(
"set_temperature_schedule", &SAS::SetTemperatureSchedule,
"temperature_schedule"_a);
1023 py_class.def(
"get_model", &SAS::GetModel);
1024 py_class.def(
"get_num_sweeps", &SAS::GetNumSweeps);
1025 py_class.def(
"get_num_reads", &SAS::GetNumReads);
1026 py_class.def(
"get_num_threads", &SAS::GetNumThreads);
1027 py_class.def(
"get_beta_min", &SAS::GetBetaMin);
1028 py_class.def(
"get_beta_max", &SAS::GetBetaMax);
1029 py_class.def(
"get_update_method", &SAS::GetUpdateMethod);
1030 py_class.def(
"get_random_number_engine", &SAS::GetRandomNumberEngine);
1031 py_class.def(
"get_temperature_schedule", &SAS::GetTemperatureSchedule);
1032 py_class.def(
"get_seed", &SAS::GetSeed);
1033 py_class.def(
"get_index_list", &SAS::GetIndexList);
1034 py_class.def(
"get_samples", &SAS::GetSamples);
1035 py_class.def(
"calculate_energies", &SAS::CalculateEnergies);
1036 py_class.def(
"sample", py::overload_cast<>(&SAS::Sample));
1037 py_class.def(
"sample", py::overload_cast<const std::uint64_t>(&SAS::Sample),
"seed"_a);
1039 m.def(
"make_sa_sampler", [](
const ModelType &model) {
1040 return sampler::make_sa_sampler(model);
1046 py::enum_<algorithm::UpdateMethod>(m,
"UpdateMethod")
1047 .value(
"METROPOLIS", algorithm::UpdateMethod::METROPOLIS)
1048 .value(
"HEAT_BATH", algorithm::UpdateMethod::HEAT_BATH)
1049 .value(
"SUWA_TODO", algorithm::UpdateMethod::SUWA_TODO)
1050 .value(
"OPT_METROPOLIS", algorithm::UpdateMethod::OPT_METROPOLIS);
1054 py::enum_<algorithm::RandomNumberEngine>(m,
"RandomNumberEngine")
1055 .value(
"MT", algorithm::RandomNumberEngine::MT)
1056 .value(
"MT_64", algorithm::RandomNumberEngine::MT_64)
1057 .value(
"XORSHIFT", algorithm::RandomNumberEngine::XORSHIFT);
1061 py::enum_<utility::TemperatureSchedule>(m,
"TemperatureSchedule")
1062 .value(
"LINEAR", utility::TemperatureSchedule::LINEAR)
1063 .value(
"GEOMETRIC", utility::TemperatureSchedule::GEOMETRIC);
Definition binary_polynomial_model.hpp:27
CSRSparse graph: just store CSR Sparse Matrix (Eigen::Sparse) The Hamiltonian is like.
Definition csr_sparse.hpp:42
FloatType calc_energy(const Spins &spins) const
calculate total energy
Definition csr_sparse.hpp:123
chimera lattice graph
Definition chimera.hpp:95
FloatType & h(std::size_t r, std::size_t c, std::size_t i)
access h(row, colum, in-chimera) (local field)
Definition chimera.hpp:458
FloatType & J(std::size_t r, std::size_t c, std::size_t i, ChimeraDir dir)
access J(row, colum, in-chimera, direction)
Definition chimera.hpp:354
two-body all-to-all interactions The Hamiltonian is like
Definition dense.hpp:44
FloatType & J(Index i, Index j)
access J_{ij}
Definition dense.hpp:211
FloatType calc_energy(const Spins &spins) const
calculate total energy
Definition dense.hpp:160
FloatType & h(Index i)
access h_{i} (local field)
Definition dense.hpp:246
Abstract graph class.
Definition graph.hpp:37
const Spins gen_spin(RandomNumberEngine &random_numder_engine) const
generate spins randomly.
Definition graph.hpp:55
const Binaries gen_binary(RandomNumberEngine &random_numder_engine) const
generate spins randomly.
Definition graph.hpp:73
std::size_t size() const noexcept
get number of spins
Definition graph.hpp:96
Definition integer_polynomial_model.hpp:20
Definition integer_quadratic_model.hpp:20
Definition ising_polynomial_model.hpp:23
Polynomial graph class, which can treat many-body interactions.
Definition polynomial.hpp:47
Sparse graph: two-body intereactions with O(1) connectivity The Hamiltonian is like.
Definition sparse.hpp:40
FloatType calc_energy(const Spins &spins) const
calculate total energy
Definition sparse.hpp:218
FloatType & h(Index i)
access h_{i} (local field)
Definition sparse.hpp:300
FloatType & J(Index i, Index j)
access J_{ij}
Definition sparse.hpp:269
square lattice graph
Definition square.hpp:64
FloatType & h(std::size_t r, std::size_t c)
access h(row, colum) (local field)
Definition square.hpp:337
FloatType & J(std::size_t r, std::size_t c, Dir dir)
access J(row, colum, direction)
Definition square.hpp:276
Class for executing simulated annealing.
Definition sa_sampler.hpp:27
ClassicalIsingPolynomial class, which is a system to solve higher order unconstrained binary optimiza...
Definition classical_ising_polynomial.hpp:36
KLocalPolynomial class, which is a system to solve higher order unconstrained binary optimization (HU...
Definition k_local_polynomial.hpp:32
xorshift random generator for c++11 random
Definition random.hpp:39
RandomNumberEngine
Definition algorithm.hpp:78
std::vector< Binary > Binaries
Definition graph.hpp:29
auto json_parse(const json &obj, bool relabel=true)
parse json object from bqm.to_serializable
Definition parse.hpp:50
ChimeraDir
direction in chimera graph
Definition chimera.hpp:46
@ MINUS_C
minus-column direction: (r, c, ind) -> (r, c-1, ind)
@ IN_0or4
inside-chimera 0or4 direction: (r, c, ind) -> (r, c, 0or4)
@ IN_2or6
inside-chimera 2or6 direction: (r, c, ind) -> (r, c, 2or6)
@ MINUS_R
minus-row direction: (r, c, ind) -> (r-1, c, ind)
@ IN_1or5
inside-chimera 1or5 direction: (r, c, ind) -> (r, c, 1or5)
@ IN_3or7
inside-chimera 3or7 direction: (r, c, ind) -> (r, c, 3or7)
@ PLUS_C
plus-column direction: (r, c, ind) -> (r, c+1, ind)
@ PLUS_R
plus-row direction: (r, c, ind) -> (r+1, c, ind)
std::vector< Spin > Spins
Definition graph.hpp:27
@ MINUS_C
minux-column direction: (r, c) -> (r, c-1)
@ MINUS_R
minus-row direction: (r, c) -> (r-1, c)
@ PLUS_C
plus-column direction: (r, c) -> (r, c+1)
@ PLUS_R
plus-row direction: (r, c) -> (r+1, c)
std::size_t Index
Definition graph.hpp:30
std::vector< graph::Spins > TrotterSpins
trotterized spin (std::vector<Spins>) trotter_spins[i][j] -> jth spin in ith trotter slice.
Definition transverse_ising.hpp:32
std::vector< Schedule< SystemType > > ScheduleList
schedule list alias
Definition schedule_list.hpp:135
Definition algorithm.hpp:24
void declare_Dir(py::module &m)
Definition declare.hpp:273
void declare_Chimera(py::module &m, const std::string &suffix)
Definition declare.hpp:349
void declare_IsingPolynomialModel(py::module &m)
Definition declare.hpp:914
void declare_ClassicalConstraintUpdaterParameter(py::module &m)
Definition declare.hpp:830
void declare_ClassicalIsingPolynomial(py::module &m, const std::string >ype_str)
Definition declare.hpp:442
void declare_TransverseFieldUpdaterParameter(py::module &m)
Definition declare.hpp:846
void declare_KLocalPolynomial(py::module &m, const std::string >ype_str)
Definition declare.hpp:503
void declare_IntegerSAResult(py::module &m)
Definition declare.hpp:978
void declare_Schedule(py::module &m, const std::string &systemtype_str)
Definition declare.hpp:861
void declare_Dense(py::module &m, const std::string &suffix)
Definition declare.hpp:75
void declare_CSRSparse(py::module &m, const std::string &suffix)
Definition declare.hpp:197
void declare_TemperatureSchedule(py::module &m)
Definition declare.hpp:1060
void declare_Algorithm_run(py::module &m, const std::string &updater_str)
Definition declare.hpp:690
void declare_ContinuousTimeIsing(py::module &m, const std::string >ype_str)
Definition declare.hpp:645
void declare_ClassicalIsing(py::module &m, const std::string >ype_str)
Definition declare.hpp:411
void declare_TransverseIsing(py::module &m, const std::string >ype_str)
Definition declare.hpp:587
void declare_RandomNumberEngine(py::module &m)
Definition declare.hpp:1053
void declare_BinaryPolynomialModel(py::module &m)
Definition declare.hpp:892
void declare_get_solution(py::module &m)
Definition declare.hpp:883
void declare_ClassicalUpdaterParameter(py::module &m)
Definition declare.hpp:819
void declare_ChimeraDir(py::module &m)
Definition declare.hpp:335
void declare_IntegerQuadraticModel(py::module &m)
Definition declare.hpp:935
void declare_Square(py::module &m, const std::string &suffix)
Definition declare.hpp:283
void declare_Graph(py::module &m)
Definition declare.hpp:43
void declare_UpdateMethod(py::module &m)
Definition declare.hpp:1045
void declare_SampleByIntegerSA(py::module &m)
Definition declare.hpp:986
double FloatType
Note:
Definition compile_config.hpp:37
void declare_Polynomial(py::module &m, const std::string &suffix)
Definition declare.hpp:221
void declare_Sparse(py::module &m, const std::string &suffix)
Definition declare.hpp:133
std::string repr_impl(const utility::UpdaterParameter< SystemType > &)
void declare_SASampler(py::module &m, const std::string &post_name="")
Definition declare.hpp:1004
void declare_IntegerPolynomialModel(py::module &m)
Definition declare.hpp:957
static void run(System &system, RandomNumberEngine &random_number_engine, const utility::ScheduleList< typename system::get_system_type< System >::type > &schedule_list, const std::function< void(const System &, const utility::UpdaterParameter< typename system::get_system_type< System >::type > &)> &callback=nullptr)
Definition algorithm.hpp:29
ClassicalIsing structure (system for classical Ising model)
Definition classical_ising.hpp:38
Continuous Time Quantum Ising system.
Definition continuous_time_ising.hpp:34
TransverseIsing structure with discrete-time trotter spins.
Definition transverse_ising.hpp:39
typename System::system_type type
Definition system.hpp:77
schedule struct
Definition schedule_list.hpp:121
std::size_t one_mc_step
Definition schedule_list.hpp:126
UpdaterParameter< SystemType > updater_parameter
Definition schedule_list.hpp:125
updater paramter for classical ising model with a single constraint
Definition schedule_list.hpp:52
updater parameter for classical ising system
Definition schedule_list.hpp:37
updater paramter for transverse ising model
Definition schedule_list.hpp:74
updater parameter for monte carlo simulation
Definition schedule_list.hpp:32