-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaig2qasm.cxx
195 lines (142 loc) · 5.38 KB
/
aig2qasm.cxx
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
/*
Copyright (c) 2022 Guilhem Ribeill, Raytheon BBN
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.
*/
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <iterator>
#include <string>
#include <filesystem>
#include <fmt/format.h>
#include "caterpillar.hpp"
#include "mockturtle/mockturtle.hpp"
#include "lorina/lorina.hpp"
#include "tweedledum/tweedledum.hpp"
#include "cxxopts.hpp"
namespace fs = std::filesystem;
//https://stackoverflow.com/questions/8581832/converting-a-vectorint-to-string
template<typename T>
std::string vec_to_str(std::vector<T> &vec){
std::ostringstream oss;
if (!vec.empty()) {
std::copy(vec.begin(), vec.end()-1,
std::ostream_iterator<T>(oss, ", "));
oss << vec.back();
}
return oss.str();
}
int main(int argc, char* argv[])
{
using namespace caterpillar;
using namespace mockturtle;
using namespace tweedledum;
// Input Options ///////////////////////////////////////////////////////////////////////////////
cxxopts::Options options("aig2qasm", "Convert AIG to QASM circuit.");
options.add_options()
("h,help", "Help me!")
("p,pebble", "Use pebbling strategy for network synthesis",
cxxopts::value<bool>()->default_value("true") )
("b,bestfit", "Use best-fit strategy for network synthesis",
cxxopts::value<bool>()->default_value("false") )
("v,verbose", "Verbose output", cxxopts::value<bool>()->default_value("false"))
("L,limit", "Pebble limit.", cxxopts::value<int>()->default_value("100"))
("O,to-stdout", "Output to standard output, instead of file.",
cxxopts::value<bool>()->default_value("false"))
("file", "The aig file to convert.", cxxopts::value<std::string>());
options.parse_positional({"file"});
auto result = options.parse(argc, argv);
if (result.count("help")) {
std::cout << options.help() << std::endl;
exit(0);
}
std::string file;
if (result.count("file")) {
file = result["file"].as<std::string>();
} else {
std::cout << "Must supply an input file!" << std::endl;
exit(1);
}
if (fs::path(file).extension() != fs::path(".aig")) {
std::cout << "Must supply an AIG file as input!\nGot: " << file << std::endl;
exit(1);
}
bool pebble = result["pebble"].as<bool>();
int pebble_limit = result["limit"].as<int>();
bool bestfit = result["bestfit"].as<bool>();
bool verbose = result["verbose"].as<bool>();
bool stdout = result["to-stdout"].as<bool>();
if (bestfit){
pebble = false; //dumb way to do this
}
//Network Synthesis ////////////////////////////////////////////////////////////////////////////
//read in AIGER file
aig_network aig;
auto const retcode = lorina::read_aiger(file, aiger_reader(aig));
if (retcode != lorina::return_code::success)
return 0;
netlist<stg_gate> circ;
logic_network_synthesis_stats stats;
if (pebble) {
pebbling_mapping_strategy_params ps;
ps.pebble_limit = pebble_limit;
ps.progress = true;
#ifdef USE_Z3
pebbling_mapping_strategy< aig_network, z3_pebble_solver<aig_network> > strategy( ps );
#else
pebbling_mapping_strategy< aig_network, bsat_pebble_solver<aig_network> > strategy( ps );
#endif
logic_network_synthesis(circ, aig, strategy, stg_from_pprm(), {}, &stats);
} else if (bestfit) {
best_fit_mapping_strategy<aig_network> strategy;
logic_network_synthesis(circ, aig, strategy, stg_from_pprm(), {}, &stats);
} else {
std::cout << "Did nothing!" << std::endl;
return 0;
}
std::string output_idx = vec_to_str(stats.o_indexes);
std::string input_idx = vec_to_str(stats.i_indexes);
std::string elapsed_time = fmt::format( "//Total time = {:>5.4f} secs", to_seconds(stats.time_total));
auto [CNOT, Tcount, Tdepth] = caterpillar::detail::qc_stats(circ);
if (verbose){
std::cout << "//Network synthesis statistics:\n" << std::endl;
std::cout << elapsed_time << std::endl;
std::cout << "//CNOT# = " << CNOT << " T# = " << Tcount << " Td = " << Tdepth << std::endl;
std::cout << "//Required ancillae: " << stats.required_ancillae << std::endl;
std::cout << "//Input indices: \n\t" << input_idx << std::endl;
std::cout << "//Output indices: \n\t" << output_idx << std::endl;
}
std::ostringstream qasm_oss;
write_qasm(circ, qasm_oss);
if (stdout) {
std::cout << qasm_oss.str() << std::endl;
} else {
fs::path ofile = fs::path(file).replace_extension(fs::path(".qasm"));
//add some info for downstream tools
std::ofstream ofs (ofile);
if (ofs.is_open())
{
ofs << "//Synthesized with aig2qasm from " << file << std::endl;
ofs << elapsed_time << std::endl;
ofs << "//CNOT# = " << CNOT << " T# = " << Tcount << " Td = " << Tdepth << std::endl;
ofs << "//Required ancillae: " << stats.required_ancillae << std::endl;
ofs << "//Input indices: " << input_idx << std::endl;
ofs << "//Output indices: " << output_idx << std::endl;
ofs << qasm_oss.str();
} else {
std::cout << "Could not open output file!" << std::endl;
exit(1);
}
}
return 0;
}