Skip to content

Commit 1b63cd0

Browse files
authoredMar 23, 2021
Αdded support for Q# (#2804)
1 parent ebbbfd4 commit 1b63cd0

14 files changed

+318
-3
lines changed
 

‎components.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎components.json

+6
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,12 @@
10091009
"alias": "py",
10101010
"owner": "multipetros"
10111011
},
1012+
"qsharp": {
1013+
"title": "Q#",
1014+
"require": "clike",
1015+
"alias": "qs",
1016+
"owner": "fedonman"
1017+
},
10121018
"q": {
10131019
"title": "Q (kdb+ database)",
10141020
"owner": "Golmote"

‎components/prism-qsharp.js

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
(function (Prism) {
2+
3+
/**
4+
* Replaces all placeholders "<<n>>" of given pattern with the n-th replacement (zero based).
5+
*
6+
* Note: This is a simple text based replacement. Be careful when using backreferences!
7+
*
8+
* @param {string} pattern the given pattern.
9+
* @param {string[]} replacements a list of replacement which can be inserted into the given pattern.
10+
* @returns {string} the pattern with all placeholders replaced with their corresponding replacements.
11+
* @example replace(/a<<0>>a/.source, [/b+/.source]) === /a(?:b+)a/.source
12+
*/
13+
function replace(pattern, replacements) {
14+
return pattern.replace(/<<(\d+)>>/g, function (m, index) {
15+
return '(?:' + replacements[+index] + ')';
16+
});
17+
}
18+
/**
19+
* @param {string} pattern
20+
* @param {string[]} replacements
21+
* @param {string} [flags]
22+
* @returns {RegExp}
23+
*/
24+
function re(pattern, replacements, flags) {
25+
return RegExp(replace(pattern, replacements), flags || '');
26+
}
27+
28+
/**
29+
* Creates a nested pattern where all occurrences of the string `<<self>>` are replaced with the pattern itself.
30+
*
31+
* @param {string} pattern
32+
* @param {number} depthLog2
33+
* @returns {string}
34+
*/
35+
function nested(pattern, depthLog2) {
36+
for (var i = 0; i < depthLog2; i++) {
37+
pattern = pattern.replace(/<<self>>/g, function () { return '(?:' + pattern + ')'; });
38+
}
39+
return pattern.replace(/<<self>>/g, '[^\\s\\S]');
40+
}
41+
42+
// https://docs.microsoft.com/en-us/azure/quantum/user-guide/language/typesystem/
43+
// https://github.com/microsoft/qsharp-language/tree/main/Specifications/Language/5_Grammar
44+
var keywordKinds = {
45+
// keywords which represent a return or variable type
46+
type: 'Adj BigInt Bool Ctl Double false Int One Pauli PauliI PauliX PauliY PauliZ Qubit Range Result String true Unit Zero',
47+
// all other keywords
48+
other: 'Adjoint adjoint apply as auto body borrow borrowing Controlled controlled distribute elif else fail fixup for function if in internal intrinsic invert is let mutable namespace new newtype open operation repeat return self set until use using while within'
49+
}
50+
// keywords
51+
function keywordsToPattern(words) {
52+
return '\\b(?:' + words.trim().replace(/ /g, '|') + ')\\b';
53+
}
54+
var keywords = RegExp(keywordsToPattern(keywordKinds.type + ' ' + keywordKinds.other));
55+
56+
// types
57+
var identifier = /\b[A-Za-z_]\w*\b/.source;
58+
var qualifiedName = replace(/<<0>>(?:\s*\.\s*<<0>>)*/.source, [identifier]);
59+
60+
var typeInside = {
61+
'keyword': keywords,
62+
'punctuation': /[<>()?,.:[\]]/
63+
};
64+
65+
// strings
66+
var regularString = /"(?:\\.|[^\\"])*"/.source;
67+
68+
Prism.languages.qsharp = Prism.languages.extend('clike', {
69+
'comment': /\/\/.*/,
70+
'string': [
71+
{
72+
pattern: re(/(^|[^$\\])<<0>>/.source, [regularString]),
73+
lookbehind: true,
74+
greedy: true
75+
}
76+
],
77+
'class-name': [
78+
{
79+
// open Microsoft.Quantum.Canon;
80+
// open Microsoft.Quantum.Canon as CN;
81+
pattern: re(/(\b(?:as|open)\s+)<<0>>(?=\s*(?:;|as\b))/.source, [qualifiedName]),
82+
lookbehind: true,
83+
inside: typeInside
84+
},
85+
{
86+
// namespace Quantum.App1;
87+
pattern: re(/(\bnamespace\s+)<<0>>(?=\s*{)/.source, [qualifiedName]),
88+
lookbehind: true,
89+
inside: typeInside
90+
},
91+
],
92+
'keyword': keywords,
93+
'number': /(?:\b0(?:x[\da-f]+|b[01]+|o[0-7]+)|(?:\B\.\d+|\b\d+(?:\.\d*)?)(?:e[-+]?\d+)?)l?\b/i,
94+
'operator': /\band=|\bor=|\band\b|\bor\b|\bnot\b|<[-=]|[-=]>|>>>=?|<<<=?|\^\^\^=?|\|\|\|=?|&&&=?|w\/=?|~~~|[*\/+\-^=!%]=?/,
95+
'punctuation': /::|[{}[\];(),.:]/
96+
});
97+
98+
Prism.languages.insertBefore('qsharp', 'number', {
99+
'range': {
100+
pattern: /\.\./,
101+
alias: 'operator'
102+
}
103+
});
104+
105+
// single line
106+
var interpolationExpr = nested(replace(/\{(?:[^"{}]|<<0>>|<<self>>)*\}/.source, [regularString]), 2);
107+
108+
Prism.languages.insertBefore('qsharp', 'string', {
109+
'interpolation-string': {
110+
pattern: re(/\$"(?:\\.|<<0>>|[^\\"{])*"/.source, [interpolationExpr]),
111+
greedy: true,
112+
inside: {
113+
'interpolation': {
114+
pattern: re(/((?:^|[^\\])(?:\\\\)*)<<0>>/.source, [interpolationExpr]),
115+
lookbehind: true,
116+
inside: {
117+
'punctuation': /^\{|\}$/,
118+
'expression': {
119+
pattern: /[\s\S]+/,
120+
alias: 'language-qsharp',
121+
inside: Prism.languages.qsharp
122+
}
123+
}
124+
},
125+
'string': /[\s\S]+/
126+
}
127+
}
128+
});
129+
130+
}(Prism));
131+
132+
Prism.languages.qs = Prism.languages.qsharp;

‎components/prism-qsharp.min.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎examples/prism-qsharp.html

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<h2>Full example</h2>
2+
<pre><code>namespace Bell {
3+
open Microsoft.Quantum.Canon;
4+
open Microsoft.Quantum.Intrinsic;
5+
6+
operation SetQubitState(desired : Result, target : Qubit) : Unit {
7+
if desired != M(target) {
8+
X(target);
9+
}
10+
}
11+
12+
@EntryPoint()
13+
operation TestBellState(count : Int, initial : Result) : (Int, Int) {
14+
15+
mutable numOnes = 0;
16+
use qubit = Qubit();
17+
for test in 1..count {
18+
SetQubitState(initial, qubit);
19+
let res = M(qubit);
20+
21+
// Count the number of ones we saw:
22+
if res == One {
23+
set numOnes += 1;
24+
}
25+
}
26+
27+
SetQubitState(Zero, qubit);
28+
29+
// Return number of times we saw a |0> and number of times we saw a |1>
30+
Message("Test results (# of 0s, # of 1s): ");
31+
return (count - numOnes, numOnes);
32+
}
33+
}</code></pre>

‎plugins/autoloader/prism-autoloader.js

+2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
],
109109
"purebasic": "clike",
110110
"purescript": "haskell",
111+
"qsharp": "clike",
111112
"qml": "javascript",
112113
"qore": "clike",
113114
"racket": "scheme",
@@ -215,6 +216,7 @@
215216
"pbfasm": "purebasic",
216217
"purs": "purescript",
217218
"py": "python",
219+
"qs": "qsharp",
218220
"rkt": "racket",
219221
"rpy": "renpy",
220222
"robot": "robotframework",

‎plugins/autoloader/prism-autoloader.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎plugins/show-language/prism-show-language.js

+2
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@
173173
"pbfasm": "PureBasic",
174174
"purs": "PureScript",
175175
"py": "Python",
176+
"qsharp": "Q#",
177+
"qs": "Q#",
176178
"q": "Q (kdb+ database)",
177179
"qml": "QML",
178180
"rkt": "Racket",

‎plugins/show-language/prism-show-language.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// The following using block creates a fresh qubit and initializes it
2+
// in the |0> state.
3+
4+
use qubit = Qubit();
5+
6+
----------------------------------------------------
7+
8+
[
9+
["comment", "// The following using block creates a fresh qubit and initializes it"],
10+
["comment", "// in the |0> state."],
11+
["keyword", "use"],
12+
" qubit ",
13+
["operator", "="],
14+
["keyword", "Qubit"],
15+
["punctuation", "("],
16+
["punctuation", ")"],
17+
["punctuation", ";"]
18+
]
19+
20+
21+
----------------------------------------------------
22+
23+
Checks for comments
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
if result == One {
2+
X(qubit);
3+
}
4+
5+
----------------------------------------------------
6+
7+
[
8+
["keyword", "if"],
9+
" result ",
10+
["operator", "=="],
11+
["keyword", "One"],
12+
["punctuation", "{"],
13+
["function", "X"],
14+
["punctuation", "("],
15+
"qubit",
16+
["punctuation", ")"],
17+
["punctuation", ";"],
18+
["punctuation", "}"]
19+
]
20+
21+
22+
----------------------------------------------------
23+
24+
Checks for if, equality and function
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace Quantum.App1 {
2+
open Microsoft.Quantum.Canon;
3+
}
4+
5+
----------------------------------------------------
6+
7+
[
8+
["keyword", "namespace"],
9+
["class-name", [
10+
"Quantum",
11+
["punctuation", "."],
12+
"App1"
13+
]],
14+
["punctuation", "{"],
15+
["keyword", "open"],
16+
["class-name", [
17+
"Microsoft",
18+
["punctuation", "."],
19+
"Quantum",
20+
["punctuation", "."],
21+
"Canon"
22+
]],
23+
["punctuation", ";"],
24+
["punctuation", "}"]
25+
]
26+
27+
28+
----------------------------------------------------
29+
30+
Checks for namespace declaration and import
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
and or not
2+
and= or=
3+
<- <= -> =>
4+
* / + - = ^ ! %
5+
*= /= += -= == ^= != %=
6+
>>> >>>= <<< <<<=
7+
^^^ ^^^=
8+
||| |||=
9+
&&& &&&=
10+
w/ w/=
11+
~~~
12+
13+
----------------------------------------------------
14+
15+
[
16+
["operator", "and"], ["operator", "or"], ["operator", "not"],
17+
["operator", "and="], ["operator", "or="],
18+
["operator", "<-"], ["operator", "<="], ["operator", "->"], ["operator", "=>"],
19+
["operator", "*"], ["operator", "/"], ["operator", "+"], ["operator", "-"], ["operator", "="], ["operator", "^"], ["operator", "!"], ["operator", "%"],
20+
["operator", "*="], ["operator", "/="], ["operator", "+="], ["operator", "-="], ["operator", "=="], ["operator", "^="], ["operator", "!="], ["operator", "%="],
21+
["operator", ">>>"], ["operator", ">>>="], ["operator", "<<<"], ["operator", "<<<="],
22+
["operator", "^^^"], ["operator", "^^^="],
23+
["operator", "|||"], ["operator", "|||="],
24+
["operator", "&&&"], ["operator", "&&&="],
25+
["operator", "w/"], ["operator", "w/="],
26+
["operator", "~~~"]
27+
]
28+
29+
30+
----------------------------------------------------
31+
32+
Checks for opertors
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
let var1 = 3;
2+
mutable var2 = 5L;
3+
set var2 = var2 + 1.5;
4+
5+
----------------------------------------------------
6+
7+
[
8+
["keyword", "let"],
9+
" var1 ",
10+
["operator", "="],
11+
["number", "3"],
12+
["punctuation", ";"],
13+
["keyword", "mutable"],
14+
" var2 ",
15+
["operator", "="],
16+
["number", "5L"],
17+
["punctuation", ";"],
18+
["keyword", "set"],
19+
" var2 ",
20+
["operator", "="],
21+
" var2 ",
22+
["operator", "+"],
23+
["number", "1.5"],
24+
["punctuation", ";"]
25+
]
26+
27+
28+
----------------------------------------------------
29+
30+
Checks for variable assignment and numbers

0 commit comments

Comments
 (0)
Please sign in to comment.