Skip to content

flag to run simulation in reverse #231

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
2 changes: 1 addition & 1 deletion include/algorithms/simulation/simple_simulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
#include "core/circuit.hpp"
#include "core/properties.hpp"

#include <boost/dynamic_bitset.hpp>

Check warning on line 16 in include/algorithms/simulation/simple_simulation.hpp

View workflow job for this annotation

GitHub Actions / 🚨 Lint

include/algorithms/simulation/simple_simulation.hpp:16:1 [misc-include-cleaner]

included header dynamic_bitset.hpp is not used directly
#include <functional>

Check warning on line 17 in include/algorithms/simulation/simple_simulation.hpp

View workflow job for this annotation

GitHub Actions / 🚨 Lint

include/algorithms/simulation/simple_simulation.hpp:17:1 [misc-include-cleaner]

included header functional is not used directly
#include <memory>

Check warning on line 18 in include/algorithms/simulation/simple_simulation.hpp

View workflow job for this annotation

GitHub Actions / 🚨 Lint

include/algorithms/simulation/simple_simulation.hpp:18:1 [misc-include-cleaner]

included header memory is not used directly

namespace syrec {

Expand All @@ -30,7 +30,7 @@
* @param g The gate to be simulated
* @param input An input pattern
*/
void coreGateSimulation(const Gate& g, boost::dynamic_bitset<>& input);

Check warning on line 33 in include/algorithms/simulation/simple_simulation.hpp

View workflow job for this annotation

GitHub Actions / 🚨 Lint

include/algorithms/simulation/simple_simulation.hpp:33:51 [misc-include-cleaner]

no header providing "boost::dynamic_bitset" is directly included

Check warning on line 33 in include/algorithms/simulation/simple_simulation.hpp

View workflow job for this annotation

GitHub Actions / 🚨 Lint

include/algorithms/simulation/simple_simulation.hpp:33:35 [misc-include-cleaner]

no header providing "syrec::Gate" is directly included

/**
* @brief Simple Simulation function for a circuit
Expand Down Expand Up @@ -60,6 +60,6 @@
* </table>
*/
void simpleSimulation(boost::dynamic_bitset<>& output, const Circuit& circ, const boost::dynamic_bitset<>& input,
const Properties::ptr& statistics = Properties::ptr());
const Properties::ptr& statistics = Properties::ptr(), const bool reverse = false);

Check warning on line 63 in include/algorithms/simulation/simple_simulation.hpp

View workflow job for this annotation

GitHub Actions / 🚨 Lint

include/algorithms/simulation/simple_simulation.hpp:63:82 [readability-avoid-const-params-in-decls]

parameter 'reverse' is const-qualified in the function declaration; const-qualification of parameters only has an effect in function definitions

} // namespace syrec
13 changes: 10 additions & 3 deletions src/algorithms/simulation/simple_simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <boost/dynamic_bitset/dynamic_bitset.hpp>
#include <cstddef>
#include <iostream>
#include <ranges>

Check warning on line 21 in src/algorithms/simulation/simple_simulation.cpp

View workflow job for this annotation

GitHub Actions / 🚨 Lint

src/algorithms/simulation/simple_simulation.cpp:21:1 [misc-include-cleaner]

included header ranges is not used directly

namespace syrec {

Expand Down Expand Up @@ -58,7 +59,7 @@
}

void simpleSimulation(boost::dynamic_bitset<>& output, const Circuit& circ, const boost::dynamic_bitset<>& input,
const Properties::ptr& statistics) {
const Properties::ptr& statistics, const bool reverse) {
Timer<PropertiesTimer> t;

if (statistics) {
Expand All @@ -67,8 +68,14 @@
}

output = input;
for (const auto& g: circ) {
coreGateSimulation(*g, output);
if (reverse) {
for (const auto& g: std::ranges::reverse_view(circ)) {

Check failure on line 72 in src/algorithms/simulation/simple_simulation.cpp

View workflow job for this annotation

GitHub Actions / 🚨 Lint

src/algorithms/simulation/simple_simulation.cpp:72:38 [clang-diagnostic-error]

no member named 'ranges' in namespace 'std'
coreGateSimulation(*g, output);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to use ranges here and raise the minimum standard to C++20.
Just use circ.crbegin() and circ.crend() to do the reverse iteration.

} else {
for (const auto& g: circ) {
coreGateSimulation(*g, output);
}
}

if (statistics) {
Expand Down
2 changes: 1 addition & 1 deletion src/python/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,5 @@ PYBIND11_MODULE(pysyrec, m) {

m.def("cost_aware_synthesis", &CostAwareSynthesis::synthesize, "circ"_a, "program"_a, "settings"_a = Properties::ptr(), "statistics"_a = Properties::ptr(), "Cost-aware synthesis of the SyReC program.");
m.def("line_aware_synthesis", &LineAwareSynthesis::synthesize, "circ"_a, "program"_a, "settings"_a = Properties::ptr(), "statistics"_a = Properties::ptr(), "Line-aware synthesis of the SyReC program.");
m.def("simple_simulation", &simpleSimulation, "output"_a, "circ"_a, "input"_a, "statistics"_a = Properties::ptr(), "Simulation of the synthesized circuit circ.");
m.def("simple_simulation", &simpleSimulation, "output"_a, "circ"_a, "input"_a, "statistics"_a = Properties::ptr(), "reverse"_a = false, "Simulation of the synthesized circuit circ.");
}
10 changes: 10 additions & 0 deletions test/python/test_syrec.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def test_simulation_no_lines(data_line_aware_simulation: dict[str, Any]) -> None

my_inp_bitset = syrec.bitset(circ.lines)
my_out_bitset = syrec.bitset(circ.lines)
my_out_bitset2 = syrec.bitset(circ.lines)
set_list = data_line_aware_simulation[file_name]["set_lines"]

for set_index in set_list:
Expand All @@ -100,6 +101,10 @@ def test_simulation_no_lines(data_line_aware_simulation: dict[str, Any]) -> None
syrec.simple_simulation(my_out_bitset, circ, my_inp_bitset)
assert data_line_aware_simulation[file_name]["sim_out"] == str(my_out_bitset)

# Check reversed simulation
syrec.simple_simulation(my_out_bitset2, circ, my_out_bitset, reverse=True)
assert str(my_out_bitset2) == str(my_inp_bitset)


def test_simulation_add_lines(data_cost_aware_simulation: dict[str, Any]) -> None:
for file_name in data_cost_aware_simulation:
Expand All @@ -112,6 +117,7 @@ def test_simulation_add_lines(data_cost_aware_simulation: dict[str, Any]) -> Non

my_inp_bitset = syrec.bitset(circ.lines)
my_out_bitset = syrec.bitset(circ.lines)
my_out_bitset2 = syrec.bitset(circ.lines)
set_list = data_cost_aware_simulation[file_name]["set_lines"]

for set_index in set_list:
Expand All @@ -120,6 +126,10 @@ def test_simulation_add_lines(data_cost_aware_simulation: dict[str, Any]) -> Non
syrec.simple_simulation(my_out_bitset, circ, my_inp_bitset)
assert data_cost_aware_simulation[file_name]["sim_out"] == str(my_out_bitset)

# Check reversed simulation
syrec.simple_simulation(my_out_bitset2, circ, my_out_bitset, reverse=True)
assert str(my_out_bitset2) == str(my_inp_bitset)


def test_no_lines_to_qasm(data_line_aware_synthesis: dict[str, Any]) -> None:
for file_name in data_line_aware_synthesis:
Expand Down
11 changes: 9 additions & 2 deletions test/unittests/test_cost_aware_simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

#include "gtest/gtest.h"
#include <algorithm>
#include <boost/dynamic_bitset.hpp>

Check warning on line 19 in test/unittests/test_cost_aware_simulation.cpp

View workflow job for this annotation

GitHub Actions / 🚨 Lint

test/unittests/test_cost_aware_simulation.cpp:19:1 [misc-include-cleaner]

included header dynamic_bitset.hpp is not used directly
#include <nlohmann/json.hpp>
#include <string>
#include <vector>

using json = nlohmann::json;

Check warning on line 24 in test/unittests/test_cost_aware_simulation.cpp

View workflow job for this annotation

GitHub Actions / 🚨 Lint

test/unittests/test_cost_aware_simulation.cpp:24:24 [misc-include-cleaner]

no header providing "nlohmann::json" is directly included

using namespace syrec;

Expand All @@ -30,10 +30,10 @@
std::string testConfigsDir = "./configs/";
std::string testCircuitsDir = "./circuits/";
std::string fileName;
boost::dynamic_bitset<> input;

Check warning on line 33 in test/unittests/test_cost_aware_simulation.cpp

View workflow job for this annotation

GitHub Actions / 🚨 Lint

test/unittests/test_cost_aware_simulation.cpp:33:12 [misc-include-cleaner]

no header providing "boost::dynamic_bitset" is directly included
boost::dynamic_bitset<> output;
boost::dynamic_bitset<> output, output2;
std::vector<int> setLines;
std::string expectedSimOut;
std::string expectedSimOut, expectedSimIn;
std::string outputString;

void SetUp() override {
Expand Down Expand Up @@ -78,11 +78,18 @@
}

output.resize(circ.getLines());
output2.resize(circ.getLines());

simpleSimulation(output, circ, input, statistics);

boost::to_string(output, outputString);
std::reverse(outputString.begin(), outputString.end());

EXPECT_EQ(expectedSimOut, outputString);

// Check reversed simulation
simpleSimulation(output2, circ, output, statistics, true);
boost::to_string(output2, outputString);
boost::to_string(input, expectedSimIn);
EXPECT_EQ(expectedSimIn, outputString);
}
11 changes: 9 additions & 2 deletions test/unittests/test_line_aware_simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class SyrecSimulationTest: public testing::TestWithParam<std::string> {
std::string testCircuitsDir = "./circuits/";
std::string fileName;
boost::dynamic_bitset<> input;
boost::dynamic_bitset<> output;
boost::dynamic_bitset<> output, output2;
std::vector<int> setLines;
std::string expectedSimOut;
std::string expectedSimOut, expectedSimIn;
std::string outputString;

void SetUp() override {
Expand Down Expand Up @@ -78,11 +78,18 @@ TEST_P(SyrecSimulationTest, GenericSimulationTest) {
}

output.resize(circ.getLines());
output2.resize(circ.getLines());

simpleSimulation(output, circ, input, statistics);

boost::to_string(output, outputString);
std::reverse(outputString.begin(), outputString.end());

EXPECT_EQ(expectedSimOut, outputString);

// Check reversed simulation
simpleSimulation(output2, circ, output, statistics, true);
boost::to_string(output2, outputString);
boost::to_string(input, expectedSimIn);
EXPECT_EQ(expectedSimIn, outputString);
}
Loading