From b730f01d536b4979fa9a90df18b2855f0897321b Mon Sep 17 00:00:00 2001 From: Aditya Giridharan Date: Thu, 19 Nov 2020 22:35:59 +0530 Subject: [PATCH 1/3] 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'); From d3fc8b68f350807373257e1029f32bc5ad9bfdc0 Mon Sep 17 00:00:00 2001 From: Aditya Giridharan Date: Fri, 20 Nov 2020 23:53:38 +0530 Subject: [PATCH 2/3] complete qasm export --- src/ui/exports.js | 175 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 154 insertions(+), 21 deletions(-) diff --git a/src/ui/exports.js b/src/ui/exports.js index a22ce021..0845a1b6 100644 --- a/src/ui/exports.js +++ b/src/ui/exports.js @@ -78,39 +78,173 @@ function initExports(revision, mostRecentStats, obsIsAnyOverlayShowing) { }); const convertJsonToQasm = (jsonText) => { + // X, Y, Z, H, S, T, Sdg, Tdg, Swap, CX, CCX, RX, RY, RZ, SX, SXdg, Measure, CRX, CRY, CRZ const map = { X: "x", Y: "y", Z: "z", - H: "h" + H: "h", + "Z^½": "s", + "Z^¼": "t", + "Z^-½": "sdg", + "Z^-¼": "tdg", + "Swap": "swap", + "•": "c", + "Rxft": "rx", + "Ryft": "ry", + "Rzft": "rz", + "X^½": "sx", + "X^-½": "sxdg", + "Measure": "measure" } let qasmString = 'OPENQASM 2.0;\ninclude "qelib1.inc"\n'; //noinspection UnusedCatchParameterJS var json = "" + + const handleControlGates = (arr) => { + var qasmStr = ""; + const controlGate = "•"; + const acceptedGates = { + 1: ['H', 'X', 'Y', 'Z', 'Rxft', 'Ryft', 'Rzft', 'X^½', 'Swap'], + 2: ['X'] + } + // the way quirk works, if you have a control in any column, all gates in the column are controlled + var numCtrls = arr.filter(elem => elem == controlGate).length; + //console.log(arr.filter(elem => Object.keys(map).includes(elem))) + if (numCtrls > 2) throw new Error("Too many controls (max 2)"); + + //check for unsupported gates + // if (arr.filter(elem => !acceptedGates[numCtrls].includes(elem) && elem != 1 && elem != controlGate).length !== 0) { + // // check for invalid control ops + // //TODO - better logging? + // throw new Error("Invalid circuit - some controlled operations are not supported!"); + // } + + //check for at least 1 supported gate + if (arr.filter(elem => elem !== controlGate && elem !== 1).length === 0) { + throw new Error("Invalid circuit - controlled operation not specified!") + } + + + const ctrlString = "c".repeat(numCtrls); + const ctrlQubits = ` q[${arr.indexOf("•")}],` + (numCtrls == 2 ? `q[${arr.lastIndexOf("•")}],` : ``); + + arr.forEach((gate, idx) => { + //console.log(gate) + if (gate == controlGate || gate == 1) return; + //if (!acceptedGates[numCtrls].includes(gate)) throw new Error(`Unsupported control gate (${ctrlString+map[gate]})`) + if (typeof gate == "string") { + if (!acceptedGates[numCtrls].includes(gate)) throw new Error(`Unsupported control gate (${ctrlString+gate})`) + var targetQubits; + if (gate == "Swap") { + if (arr.filter(elem => elem == 'Swap').length != 2) throw new Error('Wrong number of swaps!'); + targetQubits = `q[${arr.indexOf('Swap')}],q[${arr.lastIndexOf('Swap')}];\n`; + arr[arr.lastIndexOf('Swap')] = arr[arr.indexOf('Swap')] = 1; + } + else targetQubits = `q[${idx}];\n`; + qasmStr = qasmStr + ctrlString + map[gate] + ctrlQubits + targetQubits; + //console.log(qasmStr); + } + else { //parametrized gate + if(!acceptedGates[numCtrls].includes(gate["id"])) throw new Error(`Unsupported control gate (${ctrlString+gate["id"]})`) + qasmStr = qasmStr + ctrlString + `${map[gate["id"]]}(${gate["arg"]})${ctrlQubits}q[${idx}];\n`; + //console.log(qasmStr); + } + + }); + return qasmStr; + + } + + try { json = JSON.parse(jsonText) + const cols = json["cols"]; + const numQubits = Math.max(...(cols.map((arr) => arr.length))); + const numCbits = cols.filter(arr => arr.includes("Measure")).length; + + console.log(numCbits); + qasmString += `qreg q[${numQubits}];\n` + if (numCbits > 0) qasmString +=`creg c[${numCbits}];\n`; + + var measurements = 0; + + cols.forEach((col) => { + //var measureStr = ""; + if (col.includes('Rxft') || col.includes('Ryft') || col.includes('Rzft')) + throw new Error("R*ft gates not supported, please provide a time-independent parameter") + + // if col contains controls, parse it fully and move on + if (col.includes("•")) { + qasmString += handleControlGates(col); + return; + } + + + // no controls left! + col.forEach((gate, idx) => { + if (gate == 1) return; + if (typeof gate == "string") { + if (!Object.keys(map).includes(gate)) throw new Error("Unsupported gate!"); + if (gate == "Measure") { + qasmString += `measure q[${idx}]->c[${measurements}];\n`; + measurements = measurements + 1; + return; + } + if (gate == "Swap") { + if (col.filter(elem => elem == 'Swap').length != 2) throw new Error('Wrong number of swaps!'); + var targetQubits = ` q[${col.indexOf('Swap')}],q[${col.lastIndexOf('Swap')}];\n`; + col[col.lastIndexOf('Swap')] = col[col.indexOf('Swap')] = 1; + qasmString += map[gate] + targetQubits; + return; + } + qasmString += map[gate] + ` q[${idx}];\n` + } + else if(typeof gate == "object") { + if (!Object.keys(map).includes(gate["id"])) throw new Error("Unsupported gate!"); + qasmString += `${map[gate["id"]]}(${gate["arg"]}) q[${idx}];\n` + } + }); + + + //check for + // var measurements = 0; + // //console.log(col) + // var thisQasm = ``; + // var controls = 0; + // col.forEach((gate, idx) => { + // if (gate == 1) return; + // if (typeof gate == "string") { + // //console.log(gate); + // if (gate == "Measure") { + // thisQasm += `measure q[${idx}] -> c[${measurements}];\n`; + // measurements = measurements + 1; + // return; + // } + // if (gate == "•") { + // thisQasm += `c`; + // controls = controls + 1; + // if (controls > 2) throw new Error("Too many controls"); + // return; + // } + + // if (controls > 0 && !(["X", "Z"].includes(gate))) throw new Error("Unsupported control gate"); + // controls = 0; + // thisQasm += map[gate] + ` q[${idx}];\n` + // qasmString += thisQasm; + // } + // else if(typeof gate == "object") { + // //console.log(gate, "OBJ!") + // thisQasm = `${map[gate[id]]}(${gate[arg]}) q[${idx}];\n` + // } + // }); + }); } - catch(_) { + catch(e) { + console.error(e) 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; - }); - }); - + console.log(qasmString) return qasmString; } @@ -136,7 +270,6 @@ 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, ' '); From 977d606b6c806ce7a9e7a04c67402f7ddc68bc26 Mon Sep 17 00:00:00 2001 From: Aditya Giridharan Date: Thu, 17 Dec 2020 20:24:07 +0530 Subject: [PATCH 3/3] clean up some code --- package-lock.json | 112 +++++++++++++++++++++++++++++++++++----------- src/ui/exports.js | 59 +++++------------------- 2 files changed, 96 insertions(+), 75 deletions(-) diff --git a/package-lock.json b/package-lock.json index 992dd18b..b20085c3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,12 +14,14 @@ "version": "14.10.1", "resolved": "https://registry.npmjs.org/@types/node/-/node-14.10.1.tgz", "integrity": "sha512-aYNbO+FZ/3KGeQCEkNhHFRIzBOUgc7QvcVNKXbfnhDkSfwUv91JsQQa10rDgKSTSLkXZ1UIyPe4FJJNVgw1xWQ==", + "dev": true, "optional": true }, "@types/yauzl": { "version": "2.9.1", "resolved": "https://registry.npmjs.org/@types/yauzl/-/yauzl-2.9.1.tgz", "integrity": "sha512-A1b8SU4D10uoPjwb0lnHmmu8wZhR9d+9o2PKBQT2jU5YPTKsxac6M2qGAdY7VcL+dHHhARVUDmeg0rOrcd9EjA==", + "dev": true, "optional": true, "requires": { "@types/node": "*" @@ -50,7 +52,8 @@ "agent-base": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-5.1.1.tgz", - "integrity": "sha512-TMeqbNl2fMW0nMjTEPOwe3J/PRFP4vqeoNuQMG0HlMrtm5QxKqdvAkZ1pRBQ/ulIyDD5Yq0nJ7YbdD8ey0TO3g==" + "integrity": "sha512-TMeqbNl2fMW0nMjTEPOwe3J/PRFP4vqeoNuQMG0HlMrtm5QxKqdvAkZ1pRBQ/ulIyDD5Yq0nJ7YbdD8ey0TO3g==", + "dev": true }, "amdefine": { "version": "1.0.1", @@ -176,7 +179,8 @@ "balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", - "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", + "dev": true }, "base": { "version": "0.11.2", @@ -242,7 +246,8 @@ "base64-js": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.3.1.tgz", - "integrity": "sha512-mLQ4i2QO1ytvGWFWmcngKO//JXAQueZvwEKtjgQFM4jIK0kU+ytMfplL8j+n5mspOfjHwoAg+9yhb7BwAHm36g==" + "integrity": "sha512-mLQ4i2QO1ytvGWFWmcngKO//JXAQueZvwEKtjgQFM4jIK0kU+ytMfplL8j+n5mspOfjHwoAg+9yhb7BwAHm36g==", + "dev": true }, "base64id": { "version": "2.0.0", @@ -269,6 +274,7 @@ "version": "4.0.3", "resolved": "https://registry.npmjs.org/bl/-/bl-4.0.3.tgz", "integrity": "sha512-fs4G6/Hu4/EE+F75J8DuN/0IpQqNjAdC7aEQv7Qt8MHGUH7Ckv2MwTEEeN9QehD0pfIDkMI1bkHYkKy7xHyKIg==", + "dev": true, "requires": { "buffer": "^5.5.0", "inherits": "^2.0.4", @@ -303,6 +309,7 @@ "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -341,6 +348,7 @@ "version": "5.6.0", "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.6.0.tgz", "integrity": "sha512-/gDYp/UtU0eA1ys8bOs9J6a+E/KWIY+DZ+Q2WESNUA0jFRsJOc0SNUO6xJ5SGA1xueg3NL65W6s+NY5l9cunuw==", + "dev": true, "requires": { "base64-js": "^1.0.2", "ieee754": "^1.1.4" @@ -349,7 +357,8 @@ "buffer-crc32": { "version": "0.2.13", "resolved": "https://registry.npmjs.org/buffer-crc32/-/buffer-crc32-0.2.13.tgz", - "integrity": "sha1-DTM+PwDqxQqhRUq9MO+MKl2ackI=" + "integrity": "sha1-DTM+PwDqxQqhRUq9MO+MKl2ackI=", + "dev": true }, "bytes": { "version": "3.1.0", @@ -459,7 +468,8 @@ "chownr": { "version": "1.1.4", "resolved": "https://registry.npmjs.org/chownr/-/chownr-1.1.4.tgz", - "integrity": "sha512-jJ0bqzaylmJtVnNgzTeSOs8DPavpbYgEr/b0YL8/2GO3xJEhInFmhKMUnEJQjZumK7KXGFhUy89PrsJWlakBVg==" + "integrity": "sha512-jJ0bqzaylmJtVnNgzTeSOs8DPavpbYgEr/b0YL8/2GO3xJEhInFmhKMUnEJQjZumK7KXGFhUy89PrsJWlakBVg==", + "dev": true }, "class-utils": { "version": "0.3.6", @@ -573,7 +583,8 @@ "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", - "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", + "dev": true }, "connect": { "version": "3.7.0", @@ -700,7 +711,8 @@ "devtools-protocol": { "version": "0.0.799653", "resolved": "https://registry.npmjs.org/devtools-protocol/-/devtools-protocol-0.0.799653.tgz", - "integrity": "sha512-t1CcaZbvm8pOlikqrsIM9GOa7Ipp07+4h/q9u0JXBWjPCjHdBl9KkddX87Vv9vBHoBGtwV79sYQNGnQM6iS5gg==" + "integrity": "sha512-t1CcaZbvm8pOlikqrsIM9GOa7Ipp07+4h/q9u0JXBWjPCjHdBl9KkddX87Vv9vBHoBGtwV79sYQNGnQM6iS5gg==", + "dev": true }, "di": { "version": "0.0.1", @@ -748,6 +760,7 @@ "version": "1.4.4", "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.4.tgz", "integrity": "sha512-+uw1inIHVPQoaVuHzRyXd21icM+cnt4CzD5rW+NC1wjOUSTOs+Te7FOv7AhN7vS9x/oIyhLP5PR1H+phQAHu5Q==", + "dev": true, "requires": { "once": "^1.4.0" } @@ -1029,6 +1042,7 @@ "version": "2.0.1", "resolved": "https://registry.npmjs.org/extract-zip/-/extract-zip-2.0.1.tgz", "integrity": "sha512-GDhU9ntwuKyGXdZBUgTIe+vXnWj0fppUEtMDL0+idd5Sta8TGpHssn/eusA9mrPr9qNDym6SxAYZjNvCn/9RBg==", + "dev": true, "requires": { "@types/yauzl": "^2.9.1", "debug": "^4.1.1", @@ -1040,6 +1054,7 @@ "version": "4.1.1", "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==", + "dev": true, "requires": { "ms": "^2.1.1" } @@ -1047,7 +1062,8 @@ "ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", + "dev": true } } }, @@ -1055,6 +1071,7 @@ "version": "1.1.0", "resolved": "https://registry.npmjs.org/fd-slicer/-/fd-slicer-1.1.0.tgz", "integrity": "sha1-JcfInLH5B3+IkbvmHY85Dq4lbx4=", + "dev": true, "requires": { "pend": "~1.2.0" } @@ -1117,6 +1134,7 @@ "version": "4.1.0", "resolved": "https://registry.npmjs.org/find-up/-/find-up-4.1.0.tgz", "integrity": "sha512-PpOwAdQ/YlXQ2vj8a3h8IipDuYRi3wceVQQGYWxNINccq40Anw7BlsEXCMbt1Zt+OLA6Fq9suIpIWD0OsnISlw==", + "dev": true, "requires": { "locate-path": "^5.0.0", "path-exists": "^4.0.0" @@ -1204,7 +1222,8 @@ "fs-constants": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs-constants/-/fs-constants-1.0.0.tgz", - "integrity": "sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==" + "integrity": "sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==", + "dev": true }, "fs-extra": { "version": "8.1.0", @@ -1220,7 +1239,8 @@ "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", - "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", + "dev": true }, "fsevents": { "version": "2.1.3", @@ -1239,6 +1259,7 @@ "version": "5.2.0", "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-5.2.0.tgz", "integrity": "sha512-nBF+F1rAZVCu/p7rjzgA+Yb4lfYXrpl7a6VmJrU8wF9I1CKvP/QwPNZHnOlwbTkY6dvtFIzFMSyQXbLoTQPRpA==", + "dev": true, "requires": { "pump": "^3.0.0" } @@ -1259,6 +1280,7 @@ "version": "7.1.6", "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.6.tgz", "integrity": "sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==", + "dev": true, "requires": { "fs.realpath": "^1.0.0", "inflight": "^1.0.4", @@ -1740,6 +1762,7 @@ "version": "4.0.0", "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-4.0.0.tgz", "integrity": "sha512-zoDhWrkR3of1l9QAL8/scJZyLu8j/gBkcwcaQOZh7Gyh/+uJQzGVETdgT30akuwkpL8HTRfssqI3BZuV18teDg==", + "dev": true, "requires": { "agent-base": "5", "debug": "4" @@ -1749,6 +1772,7 @@ "version": "4.1.1", "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==", + "dev": true, "requires": { "ms": "^2.1.1" } @@ -1756,7 +1780,8 @@ "ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", + "dev": true } } }, @@ -1772,7 +1797,8 @@ "ieee754": { "version": "1.1.13", "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.1.13.tgz", - "integrity": "sha512-4vf7I2LYV/HaWerSo3XmlMkp5eZ83i+/CDluXi/IGTs/O1sejBNhTtnxzmRZfvOUqj7lZjqHkeTvpgSFDlWZTg==" + "integrity": "sha512-4vf7I2LYV/HaWerSo3XmlMkp5eZ83i+/CDluXi/IGTs/O1sejBNhTtnxzmRZfvOUqj7lZjqHkeTvpgSFDlWZTg==", + "dev": true }, "indexof": { "version": "0.0.1", @@ -1784,6 +1810,7 @@ "version": "1.0.6", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", + "dev": true, "requires": { "once": "^1.3.0", "wrappy": "1" @@ -1792,7 +1819,8 @@ "inherits": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", - "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", + "dev": true }, "ini": { "version": "1.3.5", @@ -2164,6 +2192,7 @@ "version": "5.0.0", "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-5.0.0.tgz", "integrity": "sha512-t7hw9pI+WvuwNJXwk5zVHpyhIqzg2qTlklJOf0mVxGSbe3Fp2VieZcduNYjaLDoy6p9uGpQEGWG87WpMKlNq8g==", + "dev": true, "requires": { "p-locate": "^4.1.0" } @@ -2297,7 +2326,8 @@ "mime": { "version": "2.4.6", "resolved": "https://registry.npmjs.org/mime/-/mime-2.4.6.tgz", - "integrity": "sha512-RZKhC3EmpBchfTGBVb8fb+RL2cWyw/32lshnsETttkBAyAUXSGHxbEJWWRXc751DrIxG1q04b8QwMbAwkRPpUA==" + "integrity": "sha512-RZKhC3EmpBchfTGBVb8fb+RL2cWyw/32lshnsETttkBAyAUXSGHxbEJWWRXc751DrIxG1q04b8QwMbAwkRPpUA==", + "dev": true }, "mime-db": { "version": "1.44.0", @@ -2318,6 +2348,7 @@ "version": "3.0.4", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "dev": true, "requires": { "brace-expansion": "^1.1.7" } @@ -2352,7 +2383,8 @@ "mkdirp-classic": { "version": "0.5.3", "resolved": "https://registry.npmjs.org/mkdirp-classic/-/mkdirp-classic-0.5.3.tgz", - "integrity": "sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A==" + "integrity": "sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A==", + "dev": true }, "ms": { "version": "2.0.0", @@ -2502,6 +2534,7 @@ "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", + "dev": true, "requires": { "wrappy": "1" } @@ -2532,6 +2565,7 @@ "version": "2.3.0", "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.3.0.tgz", "integrity": "sha512-//88mFWSJx8lxCzwdAABTJL2MyWB12+eIY7MDL2SqLmAkeKU9qxRvWuSyTjm3FUmpBEMuFfckAIqEaVGUDxb6w==", + "dev": true, "requires": { "p-try": "^2.0.0" } @@ -2540,6 +2574,7 @@ "version": "4.1.0", "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-4.1.0.tgz", "integrity": "sha512-R79ZZ/0wAxKGu3oYMlz8jy/kbhsNrS7SKZ7PxEHBgJ5+F2mtFW2fK2cOtBh1cHYkQsbzFV7I+EoRKe6Yt0oK7A==", + "dev": true, "requires": { "p-limit": "^2.2.0" } @@ -2547,7 +2582,8 @@ "p-try": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz", - "integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==" + "integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==", + "dev": true }, "parse-filepath": { "version": "1.0.2", @@ -2599,12 +2635,14 @@ "path-exists": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz", - "integrity": "sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==" + "integrity": "sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==", + "dev": true }, "path-is-absolute": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", - "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=" + "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", + "dev": true }, "path-parse": { "version": "1.0.6", @@ -2630,7 +2668,8 @@ "pend": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/pend/-/pend-1.2.0.tgz", - "integrity": "sha1-elfrVQpng/kRUzH89GY9XI4AelA=" + "integrity": "sha1-elfrVQpng/kRUzH89GY9XI4AelA=", + "dev": true }, "picomatch": { "version": "2.2.2", @@ -2642,6 +2681,7 @@ "version": "4.2.0", "resolved": "https://registry.npmjs.org/pkg-dir/-/pkg-dir-4.2.0.tgz", "integrity": "sha512-HRDzbaKjC+AOWVXxAU/x54COGeIv9eb+6CkDSQoNTt4XyWoIJvuPsXizxu/Fr23EiekbtZwmh1IcIG/l/a10GQ==", + "dev": true, "requires": { "find-up": "^4.0.0" } @@ -2664,17 +2704,20 @@ "progress": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/progress/-/progress-2.0.3.tgz", - "integrity": "sha512-7PiHtLll5LdnKIMw100I+8xJXR5gW2QwWYkT6iJva0bXitZKa/XMrSbdmg3r2Xnaidz9Qumd0VPaMrZlF9V9sA==" + "integrity": "sha512-7PiHtLll5LdnKIMw100I+8xJXR5gW2QwWYkT6iJva0bXitZKa/XMrSbdmg3r2Xnaidz9Qumd0VPaMrZlF9V9sA==", + "dev": true }, "proxy-from-env": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", - "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==", + "dev": true }, "pump": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz", "integrity": "sha512-LwZy+p3SFs1Pytd/jYct4wpv49HiYCqd9Rlc5ZVdk0V+8Yzv6jR5Blk3TRmPL1ft69TxP0IMZGJ+WPFU2BFhww==", + "dev": true, "requires": { "end-of-stream": "^1.1.0", "once": "^1.3.1" @@ -2684,6 +2727,7 @@ "version": "5.3.0", "resolved": "https://registry.npmjs.org/puppeteer/-/puppeteer-5.3.0.tgz", "integrity": "sha512-GjqMk5GRro3TO0sw3QMsF1H7n+/jaK2OW45qMvqjYUyJ7y4oA//9auy969HHhTG3HZXaMxY/NWXF/NXlAFIvtw==", + "dev": true, "requires": { "debug": "^4.1.0", "devtools-protocol": "0.0.799653", @@ -2703,6 +2747,7 @@ "version": "4.1.1", "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==", + "dev": true, "requires": { "ms": "^2.1.1" } @@ -2710,7 +2755,8 @@ "ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", + "dev": true } } }, @@ -2748,6 +2794,7 @@ "version": "3.6.0", "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.0.tgz", "integrity": "sha512-BViHy7LKeTz4oNnkcLJ+lVSL6vpiFeX6/d3oSH8zCW7UxP2onchk+vTGB143xuFjHS3deTgkKoXXymXqymiIdA==", + "dev": true, "requires": { "inherits": "^2.0.3", "string_decoder": "^1.1.1", @@ -2853,6 +2900,7 @@ "version": "3.0.2", "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz", "integrity": "sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA==", + "dev": true, "requires": { "glob": "^7.1.3" } @@ -2866,7 +2914,8 @@ "safe-buffer": { "version": "5.2.1", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", - "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==" + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", + "dev": true }, "safe-regex": { "version": "1.1.0", @@ -3340,6 +3389,7 @@ "version": "1.3.0", "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", + "dev": true, "requires": { "safe-buffer": "~5.2.0" } @@ -3366,6 +3416,7 @@ "version": "2.1.0", "resolved": "https://registry.npmjs.org/tar-fs/-/tar-fs-2.1.0.tgz", "integrity": "sha512-9uW5iDvrIMCVpvasdFHW0wJPez0K4JnMZtsuIeDI7HyMGJNxmDZDOCQROr7lXyS+iL/QMpj07qcjGYTSdRFXUg==", + "dev": true, "requires": { "chownr": "^1.1.1", "mkdirp-classic": "^0.5.2", @@ -3377,6 +3428,7 @@ "version": "2.1.4", "resolved": "https://registry.npmjs.org/tar-stream/-/tar-stream-2.1.4.tgz", "integrity": "sha512-o3pS2zlG4gxr67GmFYBLlq+dM8gyRGUOvsrHclSkvtVtQbjV0s/+ZE8OpICbaj8clrX3tjeHngYGP7rweaBnuw==", + "dev": true, "requires": { "bl": "^4.0.3", "end-of-stream": "^1.4.1", @@ -3388,7 +3440,8 @@ "through": { "version": "2.3.8", "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", - "integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=" + "integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=", + "dev": true }, "tmp": { "version": "0.2.1", @@ -3507,6 +3560,7 @@ "version": "1.4.3", "resolved": "https://registry.npmjs.org/unbzip2-stream/-/unbzip2-stream-1.4.3.tgz", "integrity": "sha512-mlExGW4w71ebDJviH16lQLtZS32VKqsSfk80GCfUlwT/4/hNRFsoscrF/c++9xinkMzECL1uL9DDwXqFWkruPg==", + "dev": true, "requires": { "buffer": "^5.2.1", "through": "^2.3.8" @@ -3613,7 +3667,8 @@ "util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", - "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" + "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=", + "dev": true }, "utils-merge": { "version": "1.0.1", @@ -3682,12 +3737,14 @@ "wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", - "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", + "dev": true }, "ws": { "version": "7.3.1", "resolved": "https://registry.npmjs.org/ws/-/ws-7.3.1.tgz", - "integrity": "sha512-D3RuNkynyHmEJIpD2qrgVkc9DQ23OrN/moAwZX4L8DfvszsJxpjQuUq3LMx6HoYji9fbIOBY18XWBsAux1ZZUA==" + "integrity": "sha512-D3RuNkynyHmEJIpD2qrgVkc9DQ23OrN/moAwZX4L8DfvszsJxpjQuUq3LMx6HoYji9fbIOBY18XWBsAux1ZZUA==", + "dev": true }, "xmlhttprequest-ssl": { "version": "1.5.5", @@ -3734,6 +3791,7 @@ "version": "2.10.0", "resolved": "https://registry.npmjs.org/yauzl/-/yauzl-2.10.0.tgz", "integrity": "sha1-x+sXyT4RLLEIb6bY5R+wZnt5pfk=", + "dev": true, "requires": { "buffer-crc32": "~0.2.3", "fd-slicer": "~1.1.0" diff --git a/src/ui/exports.js b/src/ui/exports.js index 0845a1b6..92a24e29 100644 --- a/src/ui/exports.js +++ b/src/ui/exports.js @@ -97,7 +97,7 @@ function initExports(revision, mostRecentStats, obsIsAnyOverlayShowing) { "X^-½": "sxdg", "Measure": "measure" } - let qasmString = 'OPENQASM 2.0;\ninclude "qelib1.inc"\n'; + let qasmString = 'OPENQASM 2.0;include "qelib1.inc";';//here //noinspection UnusedCatchParameterJS var json = "" @@ -110,7 +110,6 @@ function initExports(revision, mostRecentStats, obsIsAnyOverlayShowing) { } // the way quirk works, if you have a control in any column, all gates in the column are controlled var numCtrls = arr.filter(elem => elem == controlGate).length; - //console.log(arr.filter(elem => Object.keys(map).includes(elem))) if (numCtrls > 2) throw new Error("Too many controls (max 2)"); //check for unsupported gates @@ -130,7 +129,6 @@ function initExports(revision, mostRecentStats, obsIsAnyOverlayShowing) { const ctrlQubits = ` q[${arr.indexOf("•")}],` + (numCtrls == 2 ? `q[${arr.lastIndexOf("•")}],` : ``); arr.forEach((gate, idx) => { - //console.log(gate) if (gate == controlGate || gate == 1) return; //if (!acceptedGates[numCtrls].includes(gate)) throw new Error(`Unsupported control gate (${ctrlString+map[gate]})`) if (typeof gate == "string") { @@ -138,16 +136,16 @@ function initExports(revision, mostRecentStats, obsIsAnyOverlayShowing) { var targetQubits; if (gate == "Swap") { if (arr.filter(elem => elem == 'Swap').length != 2) throw new Error('Wrong number of swaps!'); - targetQubits = `q[${arr.indexOf('Swap')}],q[${arr.lastIndexOf('Swap')}];\n`; + targetQubits = `q[${arr.indexOf('Swap')}],q[${arr.lastIndexOf('Swap')}];`;//here arr[arr.lastIndexOf('Swap')] = arr[arr.indexOf('Swap')] = 1; } - else targetQubits = `q[${idx}];\n`; + else targetQubits = `q[${idx}];`;//here qasmStr = qasmStr + ctrlString + map[gate] + ctrlQubits + targetQubits; //console.log(qasmStr); } else { //parametrized gate if(!acceptedGates[numCtrls].includes(gate["id"])) throw new Error(`Unsupported control gate (${ctrlString+gate["id"]})`) - qasmStr = qasmStr + ctrlString + `${map[gate["id"]]}(${gate["arg"]})${ctrlQubits}q[${idx}];\n`; + qasmStr = qasmStr + ctrlString + `${map[gate["id"]]}(${gate["arg"]})${ctrlQubits}q[${idx}];`;//here //console.log(qasmStr); } @@ -160,12 +158,12 @@ function initExports(revision, mostRecentStats, obsIsAnyOverlayShowing) { try { json = JSON.parse(jsonText) const cols = json["cols"]; + if (cols.length === 0) return "Empty circuit"; const numQubits = Math.max(...(cols.map((arr) => arr.length))); const numCbits = cols.filter(arr => arr.includes("Measure")).length; - console.log(numCbits); - qasmString += `qreg q[${numQubits}];\n` - if (numCbits > 0) qasmString +=`creg c[${numCbits}];\n`; + qasmString += `qreg q[${numQubits}];`;//here + if (numCbits > 0) qasmString +=`creg c[${numCbits}];`;//here var measurements = 0; @@ -187,66 +185,31 @@ function initExports(revision, mostRecentStats, obsIsAnyOverlayShowing) { if (typeof gate == "string") { if (!Object.keys(map).includes(gate)) throw new Error("Unsupported gate!"); if (gate == "Measure") { - qasmString += `measure q[${idx}]->c[${measurements}];\n`; + qasmString += `measure q[${idx}]->c[${measurements}];`;//here measurements = measurements + 1; return; } if (gate == "Swap") { if (col.filter(elem => elem == 'Swap').length != 2) throw new Error('Wrong number of swaps!'); - var targetQubits = ` q[${col.indexOf('Swap')}],q[${col.lastIndexOf('Swap')}];\n`; + var targetQubits = ` q[${col.indexOf('Swap')}],q[${col.lastIndexOf('Swap')}];`;//here col[col.lastIndexOf('Swap')] = col[col.indexOf('Swap')] = 1; qasmString += map[gate] + targetQubits; return; } - qasmString += map[gate] + ` q[${idx}];\n` + qasmString += map[gate] + ` q[${idx}];`;//here } else if(typeof gate == "object") { if (!Object.keys(map).includes(gate["id"])) throw new Error("Unsupported gate!"); - qasmString += `${map[gate["id"]]}(${gate["arg"]}) q[${idx}];\n` + qasmString += `${map[gate["id"]]}(${gate["arg"]}) q[${idx}];`;//here } }); - - - //check for - // var measurements = 0; - // //console.log(col) - // var thisQasm = ``; - // var controls = 0; - // col.forEach((gate, idx) => { - // if (gate == 1) return; - // if (typeof gate == "string") { - // //console.log(gate); - // if (gate == "Measure") { - // thisQasm += `measure q[${idx}] -> c[${measurements}];\n`; - // measurements = measurements + 1; - // return; - // } - // if (gate == "•") { - // thisQasm += `c`; - // controls = controls + 1; - // if (controls > 2) throw new Error("Too many controls"); - // return; - // } - - // if (controls > 0 && !(["X", "Z"].includes(gate))) throw new Error("Unsupported control gate"); - // controls = 0; - // thisQasm += map[gate] + ` q[${idx}];\n` - // qasmString += thisQasm; - // } - // else if(typeof gate == "object") { - // //console.log(gate, "OBJ!") - // thisQasm = `${map[gate[id]]}(${gate[arg]}) q[${idx}];\n` - // } - // }); }); } catch(e) { console.error(e) return "Invalid Circuit JSON." } - console.log(qasmString) return qasmString; - } // Export escaped link.