-
Notifications
You must be signed in to change notification settings - Fork 5
/
cli.js
executable file
·253 lines (207 loc) · 6.57 KB
/
cli.js
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#! /usr/bin/env node
const commandLineArgs = require("command-line-args");
const fs = require("fs");
const path = require("path");
const replaceExt = require("replace-ext");
const QuantumCircuit = require("quantum-circuit");
const optionDefinitions = [
{ name: "input", alias: "i", type: String },
{ name: "source", alias: "s", type: String },
{ name: "output", alias: "o", type: String },
{ name: "dest", alias: "d", type: String },
{ name: "jupyter", alias: "j", type: Boolean},
{ name: "overwrite", alias: "w", type: Boolean},
{ name: "help", alias: "h", type: Boolean}
];
const args = commandLineArgs(optionDefinitions);
var printUsage = function() {
console.log("Q-Convert - Quantum Language Converter");
console.log("");
console.log("Usage:");
console.log(" q-convert -i input_file -s source_format -o output_file -d destination_format [-j] [-w]");
console.log(" -i, --input\tInput file");
console.log(" -s, --source\tSource format: qasm, quil, qobj, ionq, quantum-circuit, toaster");
console.log(" -o, --output\tOutput file");
console.log(" -d, --dest\tDestination format: qiskit, qasm, qobj, quil, pyquil, braket, cirq, tfq, qsharp, quest, js, quantum-circuit, toaster, svg, svg-inline");
console.log(" -j, --jupyter\tOutput jupyter notebook (for qiskit, pyquil, braket, cirq, tfq, qsharp, and js only)");
console.log(" -w, --overwrite\tOverwrite output file if it already exists");
console.log(" -h, --help\tPrint this help text");
console.log("");
console.log("Enjoy! :)");
console.log("");
};
if(args.help) {
printUsage();
if(!args.input) {
process.exit(1);
}
}
if(!args.input) {
console.log("Error: please specify input file.");
process.exit(1);
}
if(!fs.existsSync(args.input)) {
console.log("Error: input file \"" + args.input + "\" not found.");
process.exit(1);
}
var inputStat = fs.lstatSync(args.input);
if(!inputStat.isFile()) {
console.log("Error: input \"" + args.input + "\" is not a file.");
process.exit(1);
}
if(!args.output) {
console.log("Error: please specify output file.");
process.exit(1);
}
if(fs.existsSync(args.output)) {
if(!args.overwrite) {
console.log("Error: output file \"" + args.output + "\" already exists.");
console.log("Use -w switch to overwrite.");
process.exit(1);
}
var outputStat = fs.lstatSync(args.output);
if(outputStat.isDirectory()) {
console.log("Error: output \"" + args.output + "\" is not a file.");
process.exit(1);
}
}
if(!args.source) {
console.log("Error: please specify source file format.");
process.exit(1);
}
if(!args.dest) {
console.log("Error: please specify destination file format.");
process.exit(1);
}
// read input file
var inputFile = "";
try {
inputFile = fs.readFileSync(args.input, "utf8");
} catch(e) {
console.log("Error: cannot read input file \"" + args.input + "\". " + e.message);
process.exit(1);
}
var jupyter = !!args.jupyter;
var writeOutput = function(circuit) {
var outputStr = "";
switch(args.dest) {
case "qiskit": outputStr = circuit.exportQiskit("", false, null, null, null, null, jupyter); break;
case "qasm": outputStr = circuit.exportQASM(); break;
case "qasm-ext": outputStr = circuit.exportQASM(null, false, null, false, true); break;
case "qobj": outputStr = JSON.stringify(circuit.exportQobj()); break;
case "quil": outputStr = circuit.exportQuil(); break;
case "pyquil": outputStr = circuit.exportPyquil("", false, null, null, null, null, jupyter); break;
case "braket": outputStr = circuit.exportBraket("", false, null, null, jupyter); break;
case "cirq": outputStr = circuit.exportCirq("", false, null, null, jupyter); break;
case "tfq": outputStr = circuit.exportTFQ("", false, null, null, jupyter); break;
case "qsharp": outputStr = circuit.exportQSharp("", false, null, null, jupyter); break;
case "js": outputStr = circuit.exportJavaScript("", false, null, jupyter); break;
case "quest": outputStr = circuit.exportQuEST("", false, null, null); break;
case "quantum-circuit": outputStr = JSON.stringify(circuit.save()); break;
case "toaster": outputStr = JSON.stringify(circuit.exportRaw()); break;
case "svg": outputStr = circuit.exportSVG(false); break;
case "svg-inline": outputStr = circuit.exportSVG(true); break;
default: {
console.log("Error: unknown destination format \"" + args.dest + "\".");
process.exit(1);
}
}
try {
fs.writeFileSync(args.output, outputStr, "utf8");
} catch(e) {
console.log("Error: cannot write output \"" + args.output + "\". " + e.message);
process.exit(1);
}
};
var convert = function() {
var circuit = new QuantumCircuit();
switch(args.source) {
case "qasm": {
circuit.importQASM(inputFile, function(errors) {
if(errors && errors.length) {
console.log(errors);
process.exit(1);
}
writeOutput(circuit);
});
}; break;
case "quil": {
circuit.importQuil(inputFile, function(errors) {
if(errors && errors.length) {
console.log(errors);
process.exit(1);
}
});
writeOutput(circuit);
}; break;
case "qobj": {
var inputJson = null;
try {
inputJson = JSON.parse(inputFile);
} catch(err) {
console.log("Error parsing input file as JSON. " + err.message);
process.exit(1);
}
circuit.importQobj(inputJson, function(errors) {
if(errors && errors.length) {
console.log(errors);
process.exit(1);
}
writeOutput(circuit);
});
}; break;
case "ionq": {
var inputJson = null;
try {
inputJson = JSON.parse(inputFile);
} catch(err) {
console.log("Error parsing input file as JSON. " + err.message);
process.exit(1);
}
try {
circuit.importIonq(inputJson);
} catch(err) {
console.log(err);
process.exit(1);
}
writeOutput(circuit);
}; break;
case "quantum-circuit": {
var inputJson = null;
try {
inputJson = JSON.parse(inputFile);
} catch(err) {
console.log("Error parsing input file as JSON. " + err.message);
process.exit(1);
}
try {
circuit.load(inputJson);
} catch(err) {
console.log(err);
process.exit(1);
}
writeOutput(circuit);
}; break;
case "toaster": {
var inputJson = null;
try {
inputJson = JSON.parse(inputFile);
} catch(err) {
console.log("Error parsing input file as JSON. " + err.message);
process.exit(1);
}
circuit.importRaw(inputJson, function(errors) {
if(errors && errors.length) {
console.log(errors);
process.exit(1);
}
writeOutput(circuit);
});
}; break;
default: {
console.log("Error: unknown input file format \"" + args.source + "\".");
process.exit(1);
}
};
};
convert();