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

🐍 Minor fixes and change of template parameter of BDL pairs. #442

Merged
merged 10 commits into from
May 20, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,47 @@ void design_sidb_gates(pybind11::module& m)
namespace py = pybind11;
using namespace py::literals;

m.def("design_sidb_gates", &fiction::design_sidb_gates<Lyt, py_tt>, "skeleton"_a, "spec"_a,
"params"_a = fiction::design_sidb_gates_params<fiction::cell<Lyt>>{}, DOC(fiction_design_sidb_gates));
}

} // namespace detail

inline void design_sidb_gates(pybind11::module& m)
{
namespace py = pybind11;
/**
* Design approach selector type.
*/
pybind11::enum_<typename fiction::design_sidb_gates_params<Lyt>::design_sidb_gates_mode>(
pybind11::enum_<typename fiction::design_sidb_gates_params<fiction::offset::ucoord_t>::design_sidb_gates_mode>(
m, "design_sidb_gates_mode", DOC(fiction_design_sidb_gates_params_design_sidb_gates_mode))
.value("EXHAUSTIVE", fiction::design_sidb_gates_params<Lyt>::design_sidb_gates_mode::EXHAUSTIVE,
.value("EXHAUSTIVE",
fiction::design_sidb_gates_params<fiction::offset::ucoord_t>::design_sidb_gates_mode::EXHAUSTIVE,
DOC(fiction_design_sidb_gates_params_design_sidb_gates_mode_EXHAUSTIVE))
.value("RANDOM", fiction::design_sidb_gates_params<Lyt>::design_sidb_gates_mode::RANDOM,
.value("RANDOM", fiction::design_sidb_gates_params<fiction::offset::ucoord_t>::design_sidb_gates_mode::RANDOM,
DOC(fiction_design_sidb_gates_params_design_sidb_gates_mode_RANDOM));

/**
* Parameters.
*/
py::class_<fiction::design_sidb_gates_params<Lyt>>(m, "design_sidb_gates_params",
DOC(fiction_design_sidb_gates_params))
py::class_<fiction::design_sidb_gates_params<fiction::offset::ucoord_t>>(m, "design_sidb_gates_params",
DOC(fiction_design_sidb_gates_params))
.def(py::init<>())
.def_readwrite("simulation_parameters", &fiction::design_sidb_gates_params<Lyt>::simulation_parameters,
.def_readwrite("simulation_parameters",
&fiction::design_sidb_gates_params<fiction::offset::ucoord_t>::simulation_parameters,
DOC(fiction_design_sidb_gates_params))
.def_readwrite("design_mode", &fiction::design_sidb_gates_params<Lyt>::design_mode,
.def_readwrite("design_mode", &fiction::design_sidb_gates_params<fiction::offset::ucoord_t>::design_mode,
DOC(fiction_design_sidb_gates_params_design_mode))
.def_readwrite("canvas", &fiction::design_sidb_gates_params<Lyt>::canvas,
.def_readwrite("canvas", &fiction::design_sidb_gates_params<fiction::offset::ucoord_t>::canvas,
DOC(fiction_design_sidb_gates_params_canvas))
.def_readwrite("number_of_sidbs", &fiction::design_sidb_gates_params<Lyt>::number_of_sidbs,
.def_readwrite("number_of_sidbs",
&fiction::design_sidb_gates_params<fiction::offset::ucoord_t>::number_of_sidbs,
DOC(fiction_design_sidb_gates_params_number_of_sidbs))
.def_readwrite("sim_engine", &fiction::design_sidb_gates_params<Lyt>::sim_engine,
.def_readwrite("sim_engine", &fiction::design_sidb_gates_params<fiction::offset::ucoord_t>::sim_engine,
DOC(fiction_design_sidb_gates_params_sim_engine));

m.def("design_sidb_gates", &fiction::design_sidb_gates<Lyt, py_tt>, "skeleton"_a, "spec"_a,
"params"_a = fiction::design_sidb_gates_params<Lyt>{}, DOC(fiction_design_sidb_gates));
}

} // namespace detail

inline void design_sidb_gates(pybind11::module& m)
{
detail::design_sidb_gates<py_sidb_layout>(m);
detail::design_sidb_gates<py_sidb_100_lattice>(m);
detail::design_sidb_gates<py_sidb_111_lattice>(m);
}

} // namespace pyfiction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ inline void critical_temperature(pybind11::module& m)
// NOTE be careful with the order of the following calls! Python will resolve the first matching overload!

detail::critical_temperature<py_sidb_100_lattice>(m);
detail::critical_temperature<py_sidb_100_lattice>(m);
detail::critical_temperature<py_sidb_111_lattice>(m);
}

} // namespace pyfiction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,12 @@ namespace detail
{

template <typename Lyt>
void detect_bdl_pairs(pybind11::module& m, const std::string& lattice = "")
void detect_bdl_pairs(pybind11::module& m)
{
namespace py = pybind11;
using namespace pybind11::literals;

py::class_<fiction::bdl_pair<Lyt>>(m, fmt::format("bdl_pair{}", lattice).c_str(), DOC(fiction_bdl_pair))
.def(py::init<>(), DOC(fiction_bdl_pair_bdl_pair))
.def(py::init<fiction::sidb_technology::cell_type, fiction::cell<Lyt>, fiction::cell<Lyt>>(), "t"_a, "u"_a,
"l"_a, DOC(fiction_bdl_pair_bdl_pair_2))
.def_readonly("type", &fiction::bdl_pair<Lyt>::type, DOC(fiction_bdl_pair_type))
.def_readonly("upper", &fiction::bdl_pair<Lyt>::upper, DOC(fiction_bdl_pair_upper))
.def_readonly("lower", &fiction::bdl_pair<Lyt>::lower, DOC(fiction_bdl_pair_lower))

;

m.def(fmt::format("detect_bdl_pairs{}", lattice).c_str(), &fiction::detect_bdl_pairs<Lyt>, "lyt"_a, "type"_a,
m.def("detect_bdl_pairs", &fiction::detect_bdl_pairs<Lyt>, "lyt"_a, "type"_a,
"params"_a = fiction::detect_bdl_pairs_params{}, DOC(fiction_detect_bdl_pairs));
}

Expand All @@ -44,6 +34,15 @@ void detect_bdl_pairs(pybind11::module& m, const std::string& lattice = "")
inline void detect_bdl_pairs(pybind11::module& m)
{
namespace py = pybind11;
using namespace pybind11::literals;

py::class_<fiction::bdl_pair<fiction::offset::ucoord_t>>(m, "bdl_pair", DOC(fiction_bdl_pair))
.def(py::init<>(), DOC(fiction_bdl_pair_bdl_pair))
.def(py::init<fiction::sidb_technology::cell_type, fiction::offset::ucoord_t, fiction::offset::ucoord_t>(),
"t"_a, "u"_a, "l"_a, DOC(fiction_bdl_pair_bdl_pair_2))
.def_readonly("type", &fiction::bdl_pair<fiction::offset::ucoord_t>::type, DOC(fiction_bdl_pair_type))
.def_readonly("upper", &fiction::bdl_pair<fiction::offset::ucoord_t>::upper, DOC(fiction_bdl_pair_upper))
.def_readonly("lower", &fiction::bdl_pair<fiction::offset::ucoord_t>::lower, DOC(fiction_bdl_pair_lower));

py::class_<fiction::detect_bdl_pairs_params>(m, "detect_bdl_pairs_params", DOC(fiction_detect_bdl_pairs_params))
.def(py::init<>())
Expand All @@ -54,8 +53,8 @@ inline void detect_bdl_pairs(pybind11::module& m)

// NOTE be careful with the order of the following calls! Python will resolve the first matching overload!

detail::detect_bdl_pairs<py_sidb_100_lattice>(m, "_100");
detail::detect_bdl_pairs<py_sidb_111_lattice>(m, "_111");
detail::detect_bdl_pairs<py_sidb_100_lattice>(m);
detail::detect_bdl_pairs<py_sidb_111_lattice>(m);
}

} // namespace pyfiction
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import unittest

from mnt.pyfiction import *


class TestDesignSiDBGates(unittest.TestCase):

def test_siqad_and_gate_skeleton(self):
layout = sidb_layout((20, 20))
def test_siqad_and_gate_skeleton_100(self):
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
layout = sidb_100_lattice((20, 20))

layout.assign_cell_type((0, 1), sidb_technology.cell_type.INPUT)
layout.assign_cell_type((2, 3), sidb_technology.cell_type.INPUT)
Expand Down Expand Up @@ -40,6 +42,52 @@ def test_siqad_and_gate_skeleton(self):

self.assertEqual(len(designed_gates), 23)

def test_nor_gate_111(self):
layout = sidb_111_lattice((20, 20))

layout.assign_cell_type((0, 0), sidb_technology.cell_type.INPUT)
layout.assign_cell_type((25, 0), sidb_technology.cell_type.INPUT)

layout.assign_cell_type((23, 3), sidb_technology.cell_type.INPUT)
layout.assign_cell_type((1, 3), sidb_technology.cell_type.INPUT)

layout.assign_cell_type((4, 8), sidb_technology.cell_type.NORMAL)
layout.assign_cell_type((21, 4), sidb_technology.cell_type.NORMAL)

layout.assign_cell_type((5, 11), sidb_technology.cell_type.NORMAL)
layout.assign_cell_type((19, 11), sidb_technology.cell_type.NORMAL)

layout.assign_cell_type((8, 16), sidb_technology.cell_type.NORMAL)
layout.assign_cell_type((17, 16), sidb_technology.cell_type.NORMAL)

layout.assign_cell_type((9, 19), sidb_technology.cell_type.NORMAL)
layout.assign_cell_type((15, 19), sidb_technology.cell_type.NORMAL)

layout.assign_cell_type((15, 43), sidb_technology.cell_type.NORMAL)
layout.assign_cell_type((17, 46), sidb_technology.cell_type.NORMAL)

layout.assign_cell_type((19, 51), sidb_technology.cell_type.OUTPUT)
layout.assign_cell_type((21, 54), sidb_technology.cell_type.OUTPUT)

layout.assign_cell_type((23, 59), sidb_technology.cell_type.NORMAL)

params = design_sidb_gates_params()
params.simulation_parameters.base = 2
params.simulation_parameters.mu_minus = -0.32
params.design_mode = design_sidb_gates_mode.EXHAUSTIVE
params.canvas = [(10, 22), (14, 34)]
params.number_of_sidbs = 3
params.sim_engine = sidb_simulation_engine.QUICKEXACT

self.assertEqual(params.simulation_parameters.mu_minus, -0.32)
self.assertEqual(params.number_of_sidbs, 3)
self.assertEqual(params.canvas[0], (10, 22, 0))
self.assertEqual(params.canvas[1], (14, 34))

designed_gates = design_sidb_gates(layout, [create_nor_tt()], params)

self.assertEqual(len(designed_gates), 206)


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class TestCriticalTemperature(unittest.TestCase):

def test_perturber_and_DB_pair(self):
def test_perturber_and_DB_pair_100(self):
layout = sidb_100_lattice((10, 10))
layout.assign_cell_type((0, 1), sidb_technology.cell_type.NORMAL)
layout.assign_cell_type((4, 1), sidb_technology.cell_type.NORMAL)
Expand All @@ -26,6 +26,24 @@ def test_perturber_and_DB_pair(self):
self.assertEqual(stats.algorithm_name, "QuickExact")
self.assertEqual(stats.num_valid_lyt, 1)

def test_perturber_and_DB_pair_111(self):
layout = sidb_111_lattice((10, 10))
layout.assign_cell_type((0, 1), sidb_technology.cell_type.NORMAL)
layout.assign_cell_type((4, 1), sidb_technology.cell_type.NORMAL)
layout.assign_cell_type((6, 1), sidb_technology.cell_type.NORMAL)

params = critical_temperature_params()
params.engine = simulation_engine.EXACT

stats = critical_temperature_stats()

cds = charge_distribution_surface_111(layout)

self.assertEqual(critical_temperature_non_gate_based(cds, params, stats), 400)

self.assertEqual(stats.algorithm_name, "QuickExact")
self.assertEqual(stats.num_valid_lyt, 1)

def test_gate_based_simulation(self):
layout = read_sqd_layout_100(dir_path + "/../../../resources/hex_21_inputsdbp_xor_v1.sqd", "xor_gate")
params = critical_temperature_params()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ def test_detect_bdl_pairs_100_lattice(self):

params = detect_bdl_pairs_params()

input_bdl_pairs = detect_bdl_pairs_100(lyt, sidb_technology.cell_type.INPUT, params)
output_bdl_pairs = detect_bdl_pairs_100(lyt, sidb_technology.cell_type.OUTPUT, params)
normal_bdl_pairs = detect_bdl_pairs_100(lyt, sidb_technology.cell_type.NORMAL, params)
input_bdl_pairs = detect_bdl_pairs(lyt, sidb_technology.cell_type.INPUT, params)
output_bdl_pairs = detect_bdl_pairs(lyt, sidb_technology.cell_type.OUTPUT, params)
normal_bdl_pairs = detect_bdl_pairs(lyt, sidb_technology.cell_type.NORMAL, params)

self.assertEqual(len(input_bdl_pairs), 0)
self.assertEqual(len(output_bdl_pairs), 0)
self.assertEqual(len(normal_bdl_pairs), 2)

def test_detect_bdl_pairs_100_lattice(self):
def test_detect_bdl_pairs_111_lattice(self):
lyt = sidb_111_lattice((7, 0))

lyt = charge_distribution_surface_111(lyt)
Expand All @@ -49,9 +49,9 @@ def test_detect_bdl_pairs_100_lattice(self):

params = detect_bdl_pairs_params()

input_bdl_pairs = detect_bdl_pairs_111(lyt, sidb_technology.cell_type.INPUT, params)
output_bdl_pairs = detect_bdl_pairs_111(lyt, sidb_technology.cell_type.OUTPUT, params)
normal_bdl_pairs = detect_bdl_pairs_111(lyt, sidb_technology.cell_type.NORMAL, params)
input_bdl_pairs = detect_bdl_pairs(lyt, sidb_technology.cell_type.INPUT, params)
output_bdl_pairs = detect_bdl_pairs(lyt, sidb_technology.cell_type.OUTPUT, params)
normal_bdl_pairs = detect_bdl_pairs(lyt, sidb_technology.cell_type.NORMAL, params)

self.assertEqual(len(input_bdl_pairs), 0)
self.assertEqual(len(output_bdl_pairs), 0)
Expand Down
7 changes: 2 additions & 5 deletions docs/algorithms/sidb_simulation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -327,14 +327,11 @@ Binary-dot Logic (BDL) Pair Detection
.. doxygenfunction:: fiction::detect_bdl_pairs

.. tab:: Python
.. autoclass:: mnt.pyfiction.bdl_pair_100
:members:
.. autoclass:: mnt.pyfiction.bdl_pair_111
.. autoclass:: mnt.pyfiction.bdl_pair
:members:
.. autoclass:: mnt.pyfiction.detect_bdl_pairs_params
:members:
.. autofunction:: mnt.pyfiction.detect_bdl_pairs_100
.. autofunction:: mnt.pyfiction.detect_bdl_pairs_111
.. autofunction:: mnt.pyfiction.detect_bdl_pairs


Assess Population Stability
Expand Down
3 changes: 2 additions & 1 deletion docs/io/physical_simulation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ SiQAD
.. autofunction:: mnt.pyfiction.write_sqd_layout
.. autofunction:: mnt.pyfiction.write_sqd_sim_result
.. autofunction:: mnt.pyfiction.write_location_and_ground_state
.. autofunction:: mnt.pyfiction.read_sqd_layout
.. autofunction:: mnt.pyfiction.read_sqd_layout_100
.. autofunction:: mnt.pyfiction.read_sqd_layout_111

.. autoclass:: mnt.pyfiction.sqd_parsing_error
:members:
Expand Down
2 changes: 1 addition & 1 deletion include/fiction/algorithms/iter/bdl_input_iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ class bdl_input_iterator
/**
* The detected input BDL pairs.
*/
const std::vector<bdl_pair<Lyt>> input_pairs;
const std::vector<bdl_pair<cell<Lyt>>> input_pairs;
/**
* The amount of input BDL pairs.
*/
Expand Down
15 changes: 8 additions & 7 deletions include/fiction/algorithms/physical_design/design_sidb_gates.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ namespace fiction
/**
* This struct contains parameters and settings to design SiDB gates.
*
* @tparam Cell-level layout type.
* @tparam CellType Cell type.
*
*/
template <typename Lyt>
template <typename CellType>
struct design_sidb_gates_params
{
/**
Expand Down Expand Up @@ -64,7 +64,7 @@ struct design_sidb_gates_params
/**
* Canvas spanned by the northwest and southeast cell.
*/
std::pair<typename Lyt::cell, typename Lyt::cell> canvas{};
std::pair<CellType, CellType> canvas{};
/**
* Number of SiDBs placed in the canvas to create a working gate.
*/
Expand All @@ -90,7 +90,8 @@ class design_sidb_gates_impl
* @param tt Expected Boolean function of the layout given as a multi-output truth table.
* @param ps Parameters and settings for the gate designer.
*/
design_sidb_gates_impl(const Lyt& skeleton, const std::vector<TT>& tt, const design_sidb_gates_params<Lyt>& ps) :
design_sidb_gates_impl(const Lyt& skeleton, const std::vector<TT>& tt,
const design_sidb_gates_params<cell<Lyt>>& ps) :
skeleton_layout{skeleton},
truth_table{tt},
params{ps},
Expand Down Expand Up @@ -219,7 +220,7 @@ class design_sidb_gates_impl
/**
* Parameters for the *SiDB Gate Designer*.
*/
const design_sidb_gates_params<Lyt>& params;
const design_sidb_gates_params<cell<Lyt>>& params;
/**
* All cells within the canvas.
*/
Expand Down Expand Up @@ -305,7 +306,7 @@ class design_sidb_gates_impl
*/
template <typename Lyt, typename TT>
[[nodiscard]] std::vector<Lyt> design_sidb_gates(const Lyt& skeleton, const std::vector<TT>& spec,
const design_sidb_gates_params<Lyt>& params = {}) noexcept
const design_sidb_gates_params<cell<Lyt>>& params = {}) noexcept
{
static_assert(is_cell_level_layout_v<Lyt>, "Lyt is not a cell-level layout");
static_assert(has_sidb_technology_v<Lyt>, "Lyt is not an SiDB layout");
Expand All @@ -322,7 +323,7 @@ template <typename Lyt, typename TT>

detail::design_sidb_gates_impl<Lyt, TT> p{skeleton, spec, params};

if (params.design_mode == design_sidb_gates_params<Lyt>::design_sidb_gates_mode::EXHAUSTIVE)
if (params.design_mode == design_sidb_gates_params<cell<Lyt>>::design_sidb_gates_mode::EXHAUSTIVE)
{
return p.run_exhaustive_design();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
#ifndef FICTION_CALCULATE_ENERGY_AND_STATE_TYPE_HPP
#define FICTION_CALCULATE_ENERGY_AND_STATE_TYPE_HPP

#include "fiction/algorithms/iter/bdl_input_iterator.hpp"
#include "fiction/algorithms/simulation/sidb/detect_bdl_pairs.hpp"
#include "fiction/algorithms/simulation/sidb/energy_distribution.hpp"
#include "fiction/technology/charge_distribution_surface.hpp"
#include "fiction/technology/physical_constants.hpp"
#include "fiction/traits.hpp"
#include "fiction/utils/math_utils.hpp"

#include <kitty/bit_operations.hpp>
#include <kitty/traits.hpp>

#include <cassert>
#include <cmath>
#include <cstdint>
#include <map>
#include <string>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -44,7 +47,7 @@ template <typename Lyt, typename TT>
[[nodiscard]] sidb_energy_and_state_type
calculate_energy_and_state_type(const sidb_energy_distribution& energy_distribution,
const std::vector<charge_distribution_surface<Lyt>>& valid_charge_distributions,
const std::vector<bdl_pair<Lyt>>& output_bdl_pairs, const std::vector<TT>& spec,
const std::vector<bdl_pair<cell<Lyt>>>& output_bdl_pairs, const std::vector<TT>& spec,
const uint64_t input_index) noexcept

{
Expand Down
Loading
Loading