Skip to content

Commit

Permalink
add export to qasm (UI + some code)
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya-giri committed Nov 19, 2020
1 parent 1f54b8e commit b730f01
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
15 changes: 12 additions & 3 deletions html/export.partial.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,23 @@
<pre tabindex="104" id="export-circuit-json-pre" style="overflow:auto; max-width:600px; max-height:60px; border: 1px solid black; padding:5px; margin:2px;"></pre>
</div>

<br>
<strong>OpenQASM</strong>
<div style="margin: 10px 0 0 20px;">
<button tabindex="105" id="export-qasm-copy-button">Copy to Clipboard</button>&nbsp;&nbsp;<span id="export-qasm-copy-result"></span>
<br>
<pre tabindex="106" id="export-qasm-pre" style="overflow:auto; max-width:600px; max-height:200px; border: 1px solid black; padding:5px; margin:2px;"></pre>
</div>


<br>
<strong>Simulation Data JSON</strong> - Output amplitudes, detector results, display data, etc.
<div style="margin: 10px 0 0 20px;">
<button tabindex="105" id="export-amplitudes-button">Generate and Copy to Clipboard</button>&nbsp;<span id="export-amplitudes-result"></span>
<input type="checkbox" tabindex="106" id="export-amplitudes-use-amps" style="float: right; margin-right: 10px;">
<button tabindex="107" id="export-amplitudes-button">Generate and Copy to Clipboard</button>&nbsp;<span id="export-amplitudes-result"></span>
<input type="checkbox" tabindex="108" id="export-amplitudes-use-amps" style="float: right; margin-right: 10px;">
<label style="float: right" for="export-amplitudes-use-amps">Skip output amplitudes</label>
<br>
<pre tabindex="107" id="export-amplitudes-pre" style="overflow:auto; max-width:600px; max-height:60px; border: 1px solid black; padding:5px; margin:2px;"></pre>
<pre tabindex="109" id="export-amplitudes-pre" style="overflow:auto; max-width:600px; max-height:60px; border: 1px solid black; padding:5px; margin:2px;"></pre>
</div>
</div>
</div>
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
},
"scripts": {
"build": "grunt build-src",
"build-debug": "grunt build-debug && cp ./out/quirk.html /mnt/x -f",
"test": "grunt test",
"test-chrome": "grunt test-chrome",
"test-firefox": "grunt test-firefox",
Expand Down
58 changes: 58 additions & 0 deletions src/ui/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,44 @@ function initExports(revision, mostRecentStats, obsIsAnyOverlayShowing) {
}, 1000);
});

const convertJsonToQasm = (jsonText) => {
const map = {
X: "x",
Y: "y",
Z: "z",
H: "h"
}
let qasmString = 'OPENQASM 2.0;\ninclude "qelib1.inc"\n';
//noinspection UnusedCatchParameterJS
var json = ""
try {
json = JSON.parse(jsonText)
}
catch(_) {
return "Invalid Circuit JSON."
}
const cols = json["cols"];
const numQubits = Math.max(...(cols.map((arr) => arr.length)));
const numCbits = cols.filter(arr => arr.includes("Measure"));
// HANDLE CASE WHEN No MEASUREMENTS
console.log(numCbits);
qasmString += `qreg q[${numQubits}];\n`
if (numCbits > 0) qasmString +=`creg c[${numCbits.length}];\n`;

cols.forEach((col) => {
console.log(col)
col.forEach((gate, idx) => {
if (gate == '1') return;
console.log(gate);
var thisQasm = map[gate] + ` q[${idx}];\n`
qasmString += thisQasm;
});
});

return qasmString;

}

// Export escaped link.
(() => {
const linkElement = /** @type {HTMLAnchorElement} */ document.getElementById('export-escaped-anchor');
Expand All @@ -98,6 +136,7 @@ function initExports(revision, mostRecentStats, obsIsAnyOverlayShowing) {
setupButtonElementCopyToClipboard(copyButton, jsonTextElement, copyResultElement);
revision.latestActiveCommit().subscribe(jsonText => {
//noinspection UnusedCatchParameterJS
debugger;
try {
let val = JSON.parse(jsonText);
jsonTextElement.innerText = JSON.stringify(val, null, ' ');
Expand All @@ -107,6 +146,25 @@ function initExports(revision, mostRecentStats, obsIsAnyOverlayShowing) {
});
})();

// Export QASM
(() => {
const qasmTextElement = /** @type {HTMLPreElement} */ document.getElementById('export-qasm-pre');
const copyButton = /** @type {HTMLButtonElement} */ document.getElementById('export-qasm-copy-button');
const copyResultElement = /** @type {HTMLElement} */ document.getElementById('export-qasm-copy-result');
setupButtonElementCopyToClipboard(copyButton, qasmTextElement, copyResultElement);
revision.latestActiveCommit().subscribe(jsonText => {
//noinspection UnusedCatchParameterJS
//debugger;
try {
let val = convertJsonToQasm(jsonText);
qasmTextElement.innerText = val;
} catch (_) {
console.error("ERROR")
qasmTextElement.innerText = jsonText;
}
});
})();

// Export final output.
(() => {
const outputTextElement = /** @type {HTMLPreElement} */ document.getElementById('export-amplitudes-pre');
Expand Down

0 comments on commit b730f01

Please sign in to comment.