Skip to content

Commit ba9c24c

Browse files
authored
Add Q# language support (github-linguist#4901)
* Add Q# language support * Add a color for Q# * Make Q# colour closer to the VSCode extension logo * Add support for Qt Script * Remove Q# syntax repo * Properly remove the submodule * Add heuristics for Q# and Qt Script * Fine-tune the '.qs' heuristics * Change Q# colour to "fridge gold" * Add the official grammar for Q#
1 parent 99a06ba commit ba9c24c

File tree

11 files changed

+2575
-0
lines changed

11 files changed

+2575
-0
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,9 @@
818818
[submodule "vendor/grammars/python-django.tmbundle"]
819819
path = vendor/grammars/python-django.tmbundle
820820
url = https://github.com/textmate/python-django.tmbundle
821+
[submodule "vendor/grammars/qsharp-compiler"]
822+
path = vendor/grammars/qsharp-compiler
823+
url = https://github.com/microsoft/qsharp-compiler
821824
[submodule "vendor/grammars/quake"]
822825
path = vendor/grammars/quake
823826
url = https://github.com/newgrammars/quake

grammars.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,8 @@ vendor/grammars/protobuf-tmbundle:
722722
vendor/grammars/python-django.tmbundle:
723723
- source.python.django
724724
- text.html.django
725+
vendor/grammars/qsharp-compiler:
726+
- source.qsharp
725727
vendor/grammars/quake:
726728
- source.quake
727729
vendor/grammars/r.tmbundle:

lib/linguist/heuristics.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,12 @@ disambiguations:
407407
pattern: '((?i:[A-Z.][\w.]*:{)|(^|\n)\\(cd?|d|l|p|ts?) )'
408408
- language: HiveQL
409409
pattern: '(?i:SELECT\s+[\w*,]+\s+FROM|(CREATE|ALTER|DROP)\s(DATABASE|SCHEMA|TABLE))'
410+
- extensions: ['.qs']
411+
rules:
412+
- language: Q#
413+
pattern: '^((\/{2,3})?\s*(namespace|operation)\b)'
414+
- language: Qt Script
415+
pattern: '(\w+\.prototype\.\w+|===|\bvar\b)'
410416
- extensions: ['.r']
411417
rules:
412418
- language: Rebol

lib/linguist/languages.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4304,6 +4304,16 @@ Python traceback:
43044304
tm_scope: text.python.traceback
43054305
ace_mode: text
43064306
language_id: 304
4307+
Q#:
4308+
type: programming
4309+
extensions:
4310+
- ".qs"
4311+
aliases:
4312+
- qsharp
4313+
color: "#fed659"
4314+
ace_mode: text
4315+
tm_scope: source.qsharp
4316+
language_id: 697448245
43074317
QML:
43084318
type: programming
43094319
color: "#44a51c"
@@ -4323,6 +4333,19 @@ QMake:
43234333
tm_scope: source.qmake
43244334
ace_mode: text
43254335
language_id: 306
4336+
Qt Script:
4337+
type: programming
4338+
ace_mode: javascript
4339+
codemirror_mode: javascript
4340+
codemirror_mime_type: text/javascript
4341+
extensions:
4342+
- ".qs"
4343+
filenames:
4344+
- "installscript.qs"
4345+
- "toolchain_installscript.qs"
4346+
color: "#00b841"
4347+
tm_scope: source.js
4348+
language_id: 558193693
43264349
Quake:
43274350
type: programming
43284351
filenames:

samples/Q#/CustomModAdd.qs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
namespace Microsoft.Quantum.Numerics.Samples {
5+
open Microsoft.Quantum.Intrinsic;
6+
open Microsoft.Quantum.Arithmetic;
7+
open Microsoft.Quantum.Diagnostics;
8+
open Microsoft.Quantum.Arrays;
9+
10+
/// # Summary
11+
/// Tests a modular addition similar to the one in Fig. 4
12+
/// of https://arxiv.org/pdf/quant-ph/9511018v1.pdf.
13+
///
14+
/// # Input
15+
/// ## inputs1
16+
/// List of integers to use for the first number
17+
/// ## inputs2
18+
/// List of integers to use for the second number
19+
/// ## modulus
20+
/// Modulus used when adding each pair of numbers
21+
/// ## numBits
22+
/// Number of bits to use to represent each number
23+
operation CustomModAdd(inputs1 : Int[], inputs2 : Int[], modulus : Int,
24+
numBits : Int) : Int[]
25+
{
26+
EqualityFactI(Length(inputs1), Length(inputs2),
27+
"Input arrays need to be of equal length.");
28+
mutable results = new Int[Length(inputs1)];
29+
for (i in IndexRange(inputs1)) {
30+
let input1 = inputs1[i];
31+
let input2 = inputs2[i];
32+
using ((xQubits, yQubits, mQubits, tmp, ctrl) = (Qubit[numBits], Qubit[numBits], Qubit[numBits], Qubit(), Qubit())) {
33+
let x = LittleEndian(xQubits);
34+
let y = LittleEndian(yQubits);
35+
let m = LittleEndian(mQubits);
36+
// initialize inputs
37+
ApplyXorInPlace(input1, x);
38+
ApplyXorInPlace(input2, y);
39+
ApplyXorInPlace(modulus, m);
40+
41+
// first, add the two inputs
42+
AddI(x, y);
43+
// if the result is greater than or equal to the modulus,
44+
// we have to subtract `modulus` from the result:
45+
let yc = LittleEndian(yQubits + [tmp]);
46+
(Adjoint AddI)(m, yc);
47+
within {
48+
CNOT(tmp, ctrl);
49+
} apply {
50+
// we should not have subtracted m if there is overflow:
51+
Controlled AddI([ctrl], (m, yc));
52+
// now, uncompute temporary qubits:
53+
Adjoint AddI(x, yc);
54+
}
55+
AddI(x, yc);
56+
X(ctrl);
57+
58+
set results w/= i <- MeasureInteger(y);
59+
ResetAll(xQubits + yQubits + mQubits);
60+
}
61+
}
62+
return results;
63+
}
64+
}

0 commit comments

Comments
 (0)