Skip to content

Commit 6d1f045

Browse files
authored
show copilot instructions prompt at startup (#2361)
Directly invoked by the user via "create Q# project" or "update Copilot instructions for Q#": ![image](https://github.com/user-attachments/assets/7b8c4a42-c244-4a77-ab06-2d4025a142a6) One-time prompt at startup, or indirectly invoked whenever our copilot tools get called: ![image](https://github.com/user-attachments/assets/94dd16ba-8cfa-4f62-9643-40e997ffa936)
1 parent 0871448 commit 6d1f045

File tree

7 files changed

+455
-244
lines changed

7 files changed

+455
-244
lines changed

vscode/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@
457457
{
458458
"command": "qsharp-vscode.updateCopilotInstructions",
459459
"category": "QDK",
460-
"title": "Update Copilot instructions file for Q#"
460+
"title": "Add Copilot instructions file for Q#"
461461
}
462462
],
463463
"breakpoints": [
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
---
2+
applyTo: "**/*.{qs,qsc,ipynb}"
3+
---
4+
5+
# Q# coding instructions
6+
7+
Follow these instructions when generating Q# code in .qs files,
8+
and Q# project folders that include a `qsharp.json` file.
9+
10+
## Syntax
11+
12+
When writing Q#, the `for (i in 1..100)` syntax is invalid, use `for i in 1..100` or
13+
`for element in array[2...]`.
14+
15+
While many Q# operators are C-like, it uses `or` instead of `||` and `and` instead of `&&`.
16+
17+
To extract values from a tuple, use destructuring via the `let (item0, item1) = tupleValue;` syntax.
18+
19+
## Project structure
20+
21+
### Single-file projects
22+
23+
Q# files don't always need to exist in a project. A single `.qs` file can be compiled and
24+
run without a `qsharp.json` file. Prefer a single `.qs` file for simple programs.
25+
26+
### Multi-file projects
27+
28+
When Q# source files need to to reference each other, a `qsharp.json` file must be
29+
created. Source files must exist under the `src` folder.
30+
31+
Example layout:
32+
33+
```
34+
project_root
35+
|--qsharp.json
36+
|--src
37+
|--|--Main.qs
38+
|--|--Tests.qs
39+
```
40+
41+
A typical `qsharp.json` will be a JSON file with an empty JSON object in it.
42+
43+
```json
44+
{}
45+
```
46+
47+
Modern Q# does not use `namespace` blocks to enclose code.
48+
Each function or operation is in a namespace which is the name of the containing file.
49+
For example, if `Main.qs` has an operation `Foo`, then `Tests.qs` could reference the
50+
operation as `Main.Foo`, or bring `Foo` into scope by adding `import Main.Foo;` in the file.
51+
52+
## Testing
53+
54+
The Q# language supports unit testing in VS Code. To write a test, use the `@Test()`
55+
attribute on an operation, and `fail` with a message on test failure, e.g.,
56+
57+
```qsharp
58+
@Test()
59+
operation MyTestCase() : Unit {
60+
let result = DoOp();
61+
if (result != Expected) {
62+
fail $"DoOp returned {result}"
63+
}
64+
}
65+
```
66+
67+
Note: Prefer using a conditional `fail` statement to `Fact` calls, as `fail` gives a better error location.
68+
69+
## Libraries
70+
71+
A Q# project can reference a library from GitHub by updating the `dependencies` entry of
72+
the `qsharp.json` file. For example, to reference the `chemistry` library, the `qsharp.json`
73+
file might appear as:
74+
75+
```json
76+
{
77+
"dependencies": {
78+
"Chemistry": {
79+
"github": {
80+
"ref": "v1.15.0",
81+
"owner": "microsoft",
82+
"repo": "qsharp",
83+
"path": "library/chemistry"
84+
}
85+
}
86+
}
87+
}
88+
```
89+
90+
## Jupyter Notebooks
91+
92+
Q# has first-class support for Jupyter notebooks. Typically the first cell will contain `import qsharp`.
93+
94+
Jupyter cells can contain Q# code directly by using the `%%qsharp` magic command at the beginning of the cell. For example:
95+
96+
```python
97+
%%qsharp
98+
99+
operation GHZSample(n: Int) : Result[] {
100+
use qs = Qubit[n];
101+
102+
H(qs[0]);
103+
ApplyToEach(CNOT(qs[0], _), qs[1...]);
104+
105+
let results = MeasureEachZ(qs);
106+
ResetAll(qs);
107+
return results;
108+
}
109+
```
110+
111+
The `qsharp_widgets` package provides viewers for circuits and histograms, e.g.
112+
113+
```python
114+
from qsharp_widgets import Circuit, Histogram
115+
Circuit(qsharp.circuit("GHZSample(3)"))
116+
```
117+
118+
Note that the latest Q# and QDK releases don't require or use the old IQ# kernel. It just needs to the `qsharp` PyPI package,
119+
and maybe `qsharp_widgets` for visuals.
120+
121+
## Setup and tools
122+
123+
The Quantum Development Kit (QDK) was re-written at the start of 2024 and no longer uses
124+
the IQ# Jupyter kernel, or the `dotnet` command line tools. Job management is best handled
125+
now via tool calls integration into GitHub Copilot, or via Python code using the `qsharp`
126+
and `azure-quantum` packages.
127+
128+
To execute Q# code, use the provided tools.
129+
130+
## Response formatting
131+
132+
Avoid using LaTeX in your responses to the user.

vscode/src/createProject.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import { log, samples } from "qsharp-lang";
55
import * as vscode from "vscode";
66
import { qsharpExtensionId } from "./common";
7-
import { updateCopilotInstructions } from "./gh-copilot/instructions";
87
import registryJson from "./registry.json";
98
import { EventType, sendTelemetryEvent } from "./telemetry";
9+
import { updateCopilotInstructions } from "./gh-copilot/instructions";
1010

1111
export async function initProjectCreator(context: vscode.ExtensionContext) {
1212
context.subscriptions.push(
@@ -65,7 +65,7 @@ export async function initProjectCreator(context: vscode.ExtensionContext) {
6565
}
6666

6767
// Call updateCopilotInstructions to update the Copilot instructions file
68-
await updateCopilotInstructions(folderUri);
68+
await updateCopilotInstructions("Project", context);
6969
},
7070
),
7171
);

vscode/src/extension.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export async function activate(
8888
await initProjectCreator(context);
8989
registerCopilotPanel(context);
9090
registerLanguageModelTools(context);
91+
// fire-and-forget
9192
registerGhCopilotInstructionsCommand(context);
9293

9394
log.info("Q# extension activated.");

0 commit comments

Comments
 (0)