# Syntax highlighting for quantum programming languages

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. :)

• An alternative valid answer would be if someone could clarify that prettify works well for the quantum languages we have so far
– Mithrandir24601 Mod
Mar 12 '19 at 12:07
• 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 Mar 13 '19 at 22:16
• @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. Mar 13 '19 at 22:27
• 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? Mar 14 '19 at 16:05

## 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']);  • Thanks a lot, Mariia! We're trying to converse with SE and get this enabled as soon as possible. Mar 22 '19 at 18:00 • Any idea what flavor of regex Prettify uses in/^\"(?:[^\"\$|\$\s\S])*(?:\"|)/, null, '"' is? I'm trying to put it together for Qiskit. Mar 29 '19 at 16:21 • JavaScript flavor, I believe. Mar 29 '19 at 16:49 • Thanks. By the way, would you happen to have any idea about this issue? Mar 31 '19 at 20:17 • 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. Apr 2 '19 at 22:32 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']);