49int main(
int argc,
char** argv) {
51 std::ios_base::sync_with_stdio(
false);
52 std::cin.tie(
nullptr);
54 boost::program_options::options_description desc(
"Allowed options");
55 desc.add_options()(
"help,h",
"Print a help description")(
56 "version,v",
"Print version information")(
57 "nrqubits,n", boost::program_options::value<int>(),
58 "Specify the number of qubits")(
59 "shots,s", boost::program_options::value<int>(),
60 "Specify the number of shots for execution")(
61 "mbd,m", boost::program_options::value<int>(),
62 "Specify the max bond dimension for the MPS simulator")(
63 "simulator,r", boost::program_options::value<std::string>(),
64 "Simulator type, either aer, qcsim, composite_aer, composite_qcsim, "
66 "or quest")(
"type,t", boost::program_options::value<std::string>(),
67 "Simulation type, either statevector, mps, stabilizer, "
68 "tensor or pauli_propagation")(
69 "file,f", boost::program_options::value<std::string>(),
70 "Provide a qasm file for execution")(
71 "output,o", boost::program_options::value<std::string>(),
72 "Specify the json output file")(
73 "expectations,e",
"Compute expectation values of observables");
75 boost::program_options::positional_options_description pos_desc;
76 pos_desc.add(
"file", 1);
77 pos_desc.add(
"output", 1);
79 boost::program_options::variables_map vars;
81 boost::program_options::basic_command_line_parser<char> parser(argc,
83 parser.positional(pos_desc);
86 boost::program_options::store(parser.run(), vars);
87 boost::program_options::notify(vars);
88 }
catch (boost::program_options::error& e) {
89 std::cerr <<
"ERROR: " << e.what() <<
"\n";
93 if (vars.count(
"version")) std::cout <<
"Version 1.0\n";
95 if (vars.count(
"help")) std::cout << desc <<
"\n";
100 int simulatorType = 0;
101 int simulationType = 0;
103 if (vars.count(
"nrqubits")) {
104 nrQubits = vars[
"nrqubits"].as<
int>();
106 const std::string qstr =
_get_env_var(
"maestro_nrqubits");
108 const int nrQubitsMax = std::stoi(qstr);
109 if (nrQubits > nrQubitsMax) nrQubits = nrQubitsMax;
112 const std::string qstr =
_get_env_var(
"maestro_nrqubits");
113 if (!qstr.empty()) nrQubits = std::stoi(qstr);
117 std::cerr <<
"Invalid number of qubits" << std::endl;
121 if (vars.count(
"shots")) {
122 nrShots = vars[
"shots"].as<
int>();
126 const int nrShotsMax = std::stoi(sstr);
127 if (nrShots > nrShotsMax) nrShots = nrShotsMax;
131 if (!sstr.empty()) nrShots = std::stoi(sstr);
134 if (nrShots <= 0) nrShots = 1;
136 if (vars.count(
"mbd")) {
137 maxBondDim = vars[
"mbd"].as<
int>();
139 const std::string mbds =
_get_env_var(
"maestro_max_bond_dim");
141 const int mbdMax = std::stoi(mbds);
142 if (maxBondDim > mbdMax || (maxBondDim <= 0 && mbdMax > 0))
146 const std::string mbds =
_get_env_var(
"maestro_max_bond_dim");
147 if (!mbds.empty()) maxBondDim = std::stoi(mbds);
150 if (maxBondDim < 0) maxBondDim = 0;
153 if (vars.count(
"simulator"))
154 stype = vars[
"simulator"].as<std::string>();
158 if (!stype.empty()) {
159 std::transform(stype.begin(), stype.end(), stype.begin(),
160 [](
unsigned char chr) { return std::tolower(chr); });
164 else if (stype ==
"qcsim")
166 else if (stype ==
"composite_aer" || stype ==
"pblocks_aer")
168 else if (stype ==
"composite_qcsim" || stype ==
"pblocks_qcsim")
170 else if (stype ==
"gpu")
172 else if (stype ==
"quest")
175 simulatorType = 1000;
178 if (simulatorType != 2 && simulatorType != 3) {
180 if (vars.count(
"type"))
181 stype = vars[
"type"].as<std::string>();
185 if (!stype.empty()) {
186 std::transform(stype.begin(), stype.end(), stype.begin(),
187 [](
unsigned char chr) { return std::tolower(chr); });
189 if (stype ==
"statevector" || stype ==
"sv")
191 else if (stype ==
"mps" || stype ==
"matrix_product_state")
193 else if (stype ==
"stabilizer" || stype ==
"clifford")
195 else if (stype ==
"tensor" || stype ==
"tensor_network" ||
198 else if (stype ==
"pauli_propagation" || stype ==
"pauli" ||
202 simulationType = 1000;
207 if (vars.count(
"file") == 0) {
208 std::cerr <<
"No qasm file provided" << std::endl;
212 const std::string qasmFileName = vars[
"file"].as<std::string>();
213 if (qasmFileName.empty()) {
214 std::cerr <<
"Invalid qasm file" << std::endl;
218 std::ifstream file(qasmFileName);
219 if (!file.is_open()) {
220 std::cerr <<
"Couldn't read the qasm file" << std::endl;
224 std::string qasmStr((std::istreambuf_iterator<char>(file)),
225 std::istreambuf_iterator<char>());
226 if (qasmStr.empty()) {
227 std::cerr <<
"Empty qasm" << std::endl;
231 bool computeExpectations =
false;
232 if (vars.count(
"expectations"))
233 computeExpectations =
true;
235 const std::string estr =
_get_env_var(
"maestro_expectations");
236 if (!estr.empty()) computeExpectations = (std::stoi(estr) != 0);
250 std::cerr <<
"Couldn't load maestro library" << std::endl;
256 if (simulatorType < 2)
258 if (simulationType < 4 ||
259 (simulatorType == 1 &&
260 simulationType == 4))
262 static_cast<int>(simulatorType),
static_cast<int>(simulationType));
265 static_cast<int>(simulatorType), 0);
269 }
else if (simulatorType <
273 static_cast<int>(simulatorType), 0);
274 }
else if (simulatorType == 4)
276 if (simulationType < 2 || simulationType == 3 ||
280 static_cast<int>(simulatorType),
static_cast<int>(simulationType));
283 static_cast<int>(simulatorType), 0);
284 }
else if (simulatorType == 5)
287 static_cast<int>(simulatorType), 0);
290 static std::string configStr =
GetConfigJson(nrShots, maxBondDim);
293 if (!qasmStr.empty()) {
294 if (computeExpectations) {
295 std::string obsFileName = qasmFileName;
296 size_t lastDot = obsFileName.find_last_of(
".");
297 if (lastDot != std::string::npos) {
298 obsFileName = obsFileName.substr(0, lastDot);
300 obsFileName +=
".obs";
302 std::ifstream obsFile(obsFileName);
303 if (obsFile.is_open()) {
304 std::string obsStr((std::istreambuf_iterator<char>(obsFile)),
305 std::istreambuf_iterator<char>());
306 obsStr.erase(std::remove(obsStr.begin(), obsStr.end(),
'\n'),
308 obsStr.erase(std::remove(obsStr.begin(), obsStr.end(),
'\r'),
310 if (!obsStr.empty()) {
312 qasmStr.c_str(), obsStr.c_str(), configStr.c_str());
318 std::cerr <<
"Empty .obs file" << std::endl;
321 std::cerr <<
"Couldn't read the .obs file: " << obsFileName
325 char* res = simulator.
SimpleExecute(qasmStr.c_str(), configStr.c_str());
335 if (vars.count(
"output")) {
336 const std::string fileName = vars[
"output"].as<std::string>();
337 if (!fileName.empty()) {
338 std::ofstream outFile(fileName);
342 std::cout << result << std::endl;
343 }
catch (std::exception& e) {
344 std::cerr <<
"ERROR: " << e.what() << std::endl;
347 std::cerr <<
"Exception of unknown type!" << std::endl;