Skip to content

Commit

Permalink
Added support for OpenQasm (#2797)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Mar 21, 2021
1 parent f9b6952 commit 1a2347a
Show file tree
Hide file tree
Showing 16 changed files with 296 additions and 3 deletions.
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions components.json
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,11 @@
],
"owner": "Milania1"
},
"openqasm": {
"title": "OpenQasm",
"alias": "qasm",
"owner": "RunDevelopment"
},
"oz": {
"title": "Oz",
"owner": "Golmote"
Expand Down
23 changes: 23 additions & 0 deletions components/prism-openqasm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// https://qiskit.github.io/openqasm/grammar/index.html

Prism.languages.openqasm = {
'comment': /\/\*[\s\S]*?\*\/|\/\/.*/,
'string': {
pattern: /"[^"\r\n\t]*"|'[^'\r\n\t]*'/,
greedy: true
},

'keyword': /\b(?:barrier|boxas|boxto|break|const|continue|ctrl|def|defcal|defcalgrammar|delay|else|end|for|gate|gphase|if|in|include|inv|kernel|lengthof|let|measure|pow|reset|return|rotary|stretchinf|while|CX|OPENQASM|U)\b|#pragma\b/,
'class-name': /\b(?:angle|bit|bool|creg|fixed|float|int|length|qreg|qubit|stretch|uint)\b/,
'function': /\b(?:sin|cos|tan|exp|ln|sqrt|rotl|rotr|popcount)\b(?=\s*\()/,

'constant': /\b(?:pi|tau|euler)\b|[π𝜏ℇ]/,
'number': {
pattern: /(^|[^.\w$])(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?(?:dt|ns|us|µs|ms|s)?/i,
lookbehind: true
},
'operator': /->|>>=?|<<=?|&&|\|\||\+\+|--|[!=<>&|~^+\-*/%]=?|@/,
'punctuation': /[(){}\[\];,:.]/
};

Prism.languages.qasm = Prism.languages.openqasm;
1 change: 1 addition & 0 deletions components/prism-openqasm.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions examples/prism-openqasm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<h2>Full example</h2>
<pre class="language-cpp"><code>// https://github.com/Qiskit/openqasm
/*
* Repeat-until-success circuit for Rz(theta),
* cos(theta-pi)=3/5, from Nielsen and Chuang, Chapter 4.
*/
OPENQASM 3;
include "stdgates.inc";

/*
* Applies identity if out is 01, 10, or 11 and a Z-rotation by
* theta + pi where cos(theta)=3/5 if out is 00.
* The 00 outcome occurs with probability 5/8.
*/
def segment qubit[2]:anc, qubit:psi -> bit[2] {
bit[2] b;
reset anc;
h anc;
ccx anc[0], anc[1], psi;
s psi;
ccx anc[0], anc[1], psi;
z psi;
h anc;
measure anc -> b;
return b;
}

qubit input;
qubit ancilla[2];
bit flags[2] = "11";
bit output;

reset input;
h input;

// braces are optional in this case
while(int(flags) != 0) {
flags = segment ancilla, input;
}
rz(pi - arccos(3 / 5)) input;
h input;
output = measure input; // should get zero
</code></pre>
1 change: 1 addition & 0 deletions plugins/autoloader/prism-autoloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@
"n4jsd": "n4js",
"nani": "naniscript",
"objc": "objectivec",
"qasm": "openqasm",
"objectpascal": "pascal",
"px": "pcaxis",
"pcode": "peoplecode",
Expand Down
2 changes: 1 addition & 1 deletion plugins/autoloader/prism-autoloader.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions plugins/show-language/prism-show-language.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@
"objc": "Objective-C",
"ocaml": "OCaml",
"opencl": "OpenCL",
"openqasm": "OpenQasm",
"qasm": "OpenQasm",
"parigp": "PARI/GP",
"objectpascal": "Object Pascal",
"psl": "PATROL Scripting Language",
Expand Down
Loading

0 comments on commit 1a2347a

Please sign in to comment.