From b730f01d536b4979fa9a90df18b2855f0897321b Mon Sep 17 00:00:00 2001 From: Aditya Giridharan Date: Thu, 19 Nov 2020 22:35:59 +0530 Subject: [PATCH] add export to qasm (UI + some code) --- html/export.partial.html | 15 ++++++++--- package.json | 1 + src/ui/exports.js | 58 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 3 deletions(-) diff --git a/html/export.partial.html b/html/export.partial.html index af7d65a9..1054cbde 100644 --- a/html/export.partial.html +++ b/html/export.partial.html @@ -20,14 +20,23 @@

         
 
+        
+ OpenQASM +
+    +
+

+        
+ +
Simulation Data JSON - Output amplitudes, detector results, display data, etc.
-   - +   +
-

+            

         
diff --git a/package.json b/package.json index 07580244..01ca317a 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/ui/exports.js b/src/ui/exports.js index 14b7bd78..a22ce021 100644 --- a/src/ui/exports.js +++ b/src/ui/exports.js @@ -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'); @@ -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, ' '); @@ -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');