8
$\begingroup$

We're thinking of writing up a syntax highlighter for our site and enabling it on our tag (either using a user script or by asking the SE devs). Unfortunately, Google Prettify, which SE uses, doesn't support quantum programming languages. Yes, Qiskit is similar to Python and Q# is similar to F# and C#, but that would lead to non-uniform highlighting when writing quantum programs.

Questions:

Are there already open source syntax highlighters for quantum languages like Qiskit, Q#, PyQuil, etc.? Or do there exist sources having complete lists of syntax for the quantum programming languages? Basically, I'm looking for something like this. Please write one answer per language!

Alternatively, if you wish, you could prepare a list of syntax for one or more languages, in the form of answers. We'll collaboratively try to improve on them.

Note: Oh, and today is Quantum Computing's birthday. :)

$\endgroup$
4
  • $\begingroup$ An alternative valid answer would be if someone could clarify that prettify works well for the quantum languages we have so far $\endgroup$
    – Mithrandir24601 Mod
    Mar 12 '19 at 12:07
  • $\begingroup$ I've written a Q# syntax highlighter and I can't find it anymore >.< I know there's one in the VSC extension but IDK how to get at it $\endgroup$
    – Pavel
    Mar 13 '19 at 22:16
  • $\begingroup$ @Pavel There's a precedent on Tex SE. I think if we manage to put together a Prettify plugin (say for Q#), SE can add it on our behalf. $\endgroup$ Mar 13 '19 at 22:27
  • $\begingroup$ Q# syntax highlighter uses TextMate grammars (I think you can get it be downloading VS extension and extracting files from it treating it as a zip archive). I could probably convert it to prettify-style grammar without too much pain. We don't need it accepted to the main google/code-prettify distribution, right? $\endgroup$ Mar 14 '19 at 16:05
5
$\begingroup$

Q#

Here is a draft of Q# syntax highlight that will work with code-prettify.

PR['registerLangHandler'](
    PR['createSimpleLexer'](
        [
            [PR['PR_PLAIN'],       /^[\t\n\r \xA0]+/, null, '\t\n\r \xA0'],
            // string literals
            [PR['PR_STRING'],      /^\"(?:[^\"\\]|\\[\s\S])*(?:\"|$)/, null, '"'],
        ],
        [
            // comments (includes documentation comments)
            [PR['PR_COMMENT'],     /^\/\/.*/],
            // control keywords
            [PR['PR_KEYWORD'],     /\b(?:if|elif|else|repeat|until|fixup|for|in|return|fail)\b/],
            // operation-related keywords
            [PR['PR_KEYWORD'],     /\b(?:namespace|open|operation|function|body|(a|A)djoint|(c|C)ontrolled|self|auto|none|distribute|invert|intrinsic)\b/],
            // "other" keywords
            [PR['PR_KEYWORD'],     /\b(?:let|set|new|not|using|borrowing|newtype|mutable)\b/],
            // data types
            [PR['PR_TYPE'],        /\b(?:Int|BigInt|Double|Bool|Qubit|Pauli|Result|Range|String|Unit)\b/],
            // builtin symbols
            [PR['PR_LITERAL'],     /\b(?:true|false|Pauli(I|X|Y|Z)|One|Zero)\b/],
            // decimal numbers
            [PR['PR_LITERAL'],     /^[+-]?([0-9]+(\.[0-9]+)?|\.[0-9]+)([eE][+-]?[0-9]+)?[Li]?/],
            // valid variable names
            [PR['PR_PLAIN'],       /^(?:[A-Za-z]+[A-Za-z0-9_.]*|\.[a-zA-Z_][0-9a-zA-Z\._]*)(?![A-Za-z0-9_.])/],
        ]),
    ['qsharp']);
$\endgroup$
5
  • 2
    $\begingroup$ Thanks a lot, Mariia! We're trying to converse with SE and get this enabled as soon as possible. $\endgroup$ Mar 22 '19 at 18:00
  • $\begingroup$ Any idea what flavor of regex Prettify uses in/^\"(?:[^\"\\]|\\[\s\S])*(?:\"|$)/, null, '"' is? I'm trying to put it together for Qiskit. $\endgroup$ Mar 29 '19 at 16:21
  • $\begingroup$ JavaScript flavor, I believe. $\endgroup$ Mar 29 '19 at 16:49
  • $\begingroup$ Thanks. By the way, would you happen to have any idea about this issue? $\endgroup$ Mar 31 '19 at 20:17
  • $\begingroup$ No idea, sorry :-( I tested my syntax highlighter locally (as opposed to pulling the updated script from GitHub), and that required importing lang-qsharp.js as a separate script. $\endgroup$ Apr 2 '19 at 22:32
2
$\begingroup$

Note: This isn't complete yet. I've tried to put together an extremely preliminary version so that people can start working on it. Please feel free to edit to improve or add to the code. This is the documentation for Qiskit.


Qiskit

PR['registerLangHandler'](
    PR['createSimpleLexer'](
        [   
            // plain text
            [PR['PR_PLAIN'],       /^\s+/, null, ' \r\n\t\xA0'],
            // string literals
            [PR['PR_STRING'],      /^(?:\'\'\'(?:[^\'\\]|\\[\s\S]|\'{1,2}(?=[^\']))*(?:\'\'\'|$)|\"\"\"(?:[^\"\\]|\\[\s\S]|\"{1,2}(?=[^\"]))*(?:\"\"\"|$)|\'(?:[^\\\']|\\[\s\S])*(?:\'|$)|\"(?:[^\\\"]|\\[\s\S])*(?:\"|$))/, null],
        ],
        [   // hash comments 
             [PR['PR_COMMENT'],    /^#(?:##(?:[^#]|#(?!##))*(?:###|$)|.*)/, null, '#'],
            // Python v3.7.3 keywords
            [PR['PR_KEYWORD'],     /\b(?:False|None|True|and|as|assert|async|await|break|class|continue|def|del|elif|else|except|finally|for|from|global|if|import|in|is|lambda|nonlocal|not|or|pass|raise|return|try|while|with|yield)\b/],
            // valid class names
            [PR['PR_TYPE'],        /^(?:[@_]?[a-zA-Z]+[a-zA-Z0-9][A-Za-z_$@0-9]*|\w+_t\b)/, null],
            // valid variable names
            [PR['PR_PLAIN'],       /^[a-z_$][a-z_$0-9]*/],
            // valid literals
            [PR['PR_LITERAL'],     /^(?:0x[a-f0-9]+|(?:\d(?:_\d+)*\d*(?:\.\d*)?|\.\d\+)(?:e[+\-]?\d+)?)[a-z]*/],
    ]),
    ['qiskit']);
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .