-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeneralSynthesis.cpp
200 lines (160 loc) · 6.78 KB
/
generalSynthesis.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// ReversibleLogicGenerator - generator of reversible logic circuits, based on permutation group theory.
// Copyright (C) 2015 <Dmitry Zakablukov>
// E-mail: [email protected]
// Web: https://github.com/dmitry-zakablukov/ReversibleLogicGenerator
//
// This file is part of ReversibleLogicGenerator.
//
// ReversibleLogicGenerator is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ReversibleLogicGenerator is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ReversibleLogicGenerator. If not, see <http://www.gnu.org/licenses/>.
#include "std.h"
TruthTable getHwb(uint n)
{
word count = 1 << n;
word mask = count - 1;
TruthTable table;
table.resize(count);
for (word x = 0; x < count; ++x)
{
uint weight = countNonZeroBits(x);
word y = ((x << weight) & mask) |
((x >> (n - weight)) & mask);
table[x] = y;
}
return table;
}
void synthesizeScheme(const TruthTable& table, ostream& resultsOutput, const string& tfcOutputFileName,
ReversibleLogic::TfcFormatter* formatter)
{
using namespace ReversibleLogic;
assertd(formatter, string("synthesizeScheme(): null ptr"));
try
{
CompositeGenerator generator;
Scheme scheme = generator.generate(table, resultsOutput);
ofstream tfcOutput(tfcOutputFileName);
assert(tfcOutput.is_open(),
string("Failed to open tfc file \"") + tfcOutputFileName + "\" for writing");
resultsOutput << "Scheme file: " << tfcOutputFileName << endl;
formatter->format(tfcOutput, scheme);
tfcOutput.close();
}
catch (exception& ex)
{
resultsOutput << ex.what() << endl;
}
catch (...)
{
resultsOutput << "Unknown exception" << endl;
}
resultsOutput << "\n===============================================================" << endl;
}
void synthesizeScheme(const TruthTable& table, ostream& resultsOutput, const string& tfcOutputFileName)
{
ReversibleLogic::TfcFormatter formatter;
synthesizeScheme(table, resultsOutput, tfcOutputFileName, &formatter);
}
void processTruthTables(ostream& resultsOutput, const string& schemesFolder)
{
using namespace ReversibleLogic;
const char* strTruthTableInput = "truth-table-input";
const ProgramOptions& options = ProgramOptions::get();
if (options.options.has(strTruthTableInput))
{
auto truthTableInputFiles = options.options[strTruthTableInput];
for (auto& truthTableInputFileName : truthTableInputFiles)
{
try
{
ifstream inputFile(truthTableInputFileName);
assert(inputFile.is_open(),
string("Failed to open input file \"") + truthTableInputFileName + "\" for reading");
resultsOutput << "Truth table: " << truthTableInputFileName << endl;
TruthTableParser parser;
TruthTable table = parser.parse(inputFile);
uint inputCount = parser.getInputCount();
uint outputCount = parser.getOutputCount();
unordered_map<uint, uint> outputVariablesOrder;
if (inputCount == outputCount)
{
for (uint index = 0; index < inputCount; ++index)
outputVariablesOrder[index] = index;
}
if (inputCount != outputCount || !options.isTuningEnabled ||
!options.options.getBool("do-not-alter-output-variables-order", false))
{
table = TruthTableUtils::optimizeHammingDistance(table,
inputCount, outputCount, &outputVariablesOrder);
}
string tfcOutputFileName = appendPath(schemesFolder,
getFileName(truthTableInputFileName) + "-out.tfc");
TfcFormatter formatter(inputCount, outputCount, outputVariablesOrder);
synthesizeScheme(table, resultsOutput, tfcOutputFileName, &formatter);
}
catch (exception& ex)
{
resultsOutput << ex.what() << endl;
resultsOutput << "\n===============================================================" << endl;
}
}
}
}
void processTfcFiles(ostream& resultsOutput, const string& schemesFolder)
{
using namespace ReversibleLogic;
const char* strTfcInput = "tfc-input";
const ProgramOptions& options = ProgramOptions::get();
if (options.options.has(strTfcInput))
{
auto tfcInputFiles = options.options[strTfcInput];
for (auto& tfcInputFileName : tfcInputFiles)
{
try
{
TfcFormatter formatter;
ifstream inputFile(tfcInputFileName);
assert(inputFile.is_open(),
string("Failed to open input file \"") + tfcInputFileName + "\" for reading");
resultsOutput << "TFC file: " << tfcInputFileName << endl;
Scheme scheme = formatter.parse(inputFile);
resultsOutput << "Original quantum cost: ";
resultsOutput << SchemeUtils::calculateQuantumCost(scheme) << endl;
TruthTable table = makePermutationFromScheme(scheme, formatter.getVariablesCount());
string tfcOutputFileName = appendPath(schemesFolder,
getFileName(tfcInputFileName) + "-out.tfc");
synthesizeScheme(table, resultsOutput, tfcOutputFileName, &formatter);
}
catch (exception& ex)
{
resultsOutput << ex.what() << endl;
resultsOutput << "\n===============================================================" << endl;
}
}
}
}
void generalSynthesis()
{
using namespace ReversibleLogic;
const ProgramOptions& options = ProgramOptions::get();
// open results output
ofstream resultsOutput(options.resultsFile);
assert(resultsOutput.is_open(),
string("Failed to open results file \"") + options.resultsFile + "\" for writing");
// check schemes folder existence, create if not exist
string schemesFolder = options.schemesFolder;
if (_access(schemesFolder.c_str(), 0))
_mkdir(schemesFolder.c_str());
processTruthTables(resultsOutput, schemesFolder);
processTfcFiles(resultsOutput, schemesFolder);
resultsOutput.close();
}