Skip to content

show copilot instructions prompt at startup #2361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@
{
"command": "qsharp-vscode.updateCopilotInstructions",
"category": "QDK",
"title": "Update Copilot instructions file for Q#"
"title": "Add Copilot instructions file for Q#"
}
],
"breakpoints": [
Expand Down
132 changes: 132 additions & 0 deletions vscode/resources/chat-instructions/qsharp.instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
---
applyTo: "**/*.{qs,qsc,ipynb}"
---

# Q# coding instructions

Follow these instructions when generating Q# code in .qs files,
and Q# project folders that include a `qsharp.json` file.

## Syntax

When writing Q#, the `for (i in 1..100)` syntax is invalid, use `for i in 1..100` or
`for element in array[2...]`.

While many Q# operators are C-like, it uses `or` instead of `||` and `and` instead of `&&`.

To extract values from a tuple, use destructuring via the `let (item0, item1) = tupleValue;` syntax.

## Project structure

### Single-file projects

Q# files don't always need to exist in a project. A single `.qs` file can be compiled and
run without a `qsharp.json` file. Prefer a single `.qs` file for simple programs.

### Multi-file projects

When Q# source files need to to reference each other, a `qsharp.json` file must be
created. Source files must exist under the `src` folder.

Example layout:

```
project_root
|--qsharp.json
|--src
|--|--Main.qs
|--|--Tests.qs
```

A typical `qsharp.json` will be a JSON file with an empty JSON object in it.

```json
{}
```

Modern Q# does not use `namespace` blocks to enclose code.
Each function or operation is in a namespace which is the name of the containing file.
For example, if `Main.qs` has an operation `Foo`, then `Tests.qs` could reference the
operation as `Main.Foo`, or bring `Foo` into scope by adding `import Main.Foo;` in the file.

## Testing

The Q# language supports unit testing in VS Code. To write a test, use the `@Test()`
attribute on an operation, and `fail` with a message on test failure, e.g.,

```qsharp
@Test()
operation MyTestCase() : Unit {
let result = DoOp();
if (result != Expected) {
fail $"DoOp returned {result}"
}
}
```

Note: Prefer using a conditional `fail` statement to `Fact` calls, as `fail` gives a better error location.

## Libraries

A Q# project can reference a library from GitHub by updating the `dependencies` entry of
the `qsharp.json` file. For example, to reference the `chemistry` library, the `qsharp.json`
file might appear as:

```json
{
"dependencies": {
"Chemistry": {
"github": {
"ref": "v1.15.0",
"owner": "microsoft",
"repo": "qsharp",
"path": "library/chemistry"
}
}
}
}
```

## Jupyter Notebooks

Q# has first-class support for Jupyter notebooks. Typically the first cell will contain `import qsharp`.

Jupyter cells can contain Q# code directly by using the `%%qsharp` magic command at the beginning of the cell. For example:

```python
%%qsharp

operation GHZSample(n: Int) : Result[] {
use qs = Qubit[n];

H(qs[0]);
ApplyToEach(CNOT(qs[0], _), qs[1...]);

let results = MeasureEachZ(qs);
ResetAll(qs);
return results;
}
```

The `qsharp_widgets` package provides viewers for circuits and histograms, e.g.

```python
from qsharp_widgets import Circuit, Histogram
Circuit(qsharp.circuit("GHZSample(3)"))
```

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,
and maybe `qsharp_widgets` for visuals.

## Setup and tools

The Quantum Development Kit (QDK) was re-written at the start of 2024 and no longer uses
the IQ# Jupyter kernel, or the `dotnet` command line tools. Job management is best handled
now via tool calls integration into GitHub Copilot, or via Python code using the `qsharp`
and `azure-quantum` packages.

To execute Q# code, use the provided tools.

## Response formatting

Avoid using LaTeX in your responses to the user.
4 changes: 2 additions & 2 deletions vscode/src/createProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import { log, samples } from "qsharp-lang";
import * as vscode from "vscode";
import { qsharpExtensionId } from "./common";
import { updateCopilotInstructions } from "./gh-copilot/instructions";
import registryJson from "./registry.json";
import { EventType, sendTelemetryEvent } from "./telemetry";
import { updateCopilotInstructions } from "./gh-copilot/instructions";

export async function initProjectCreator(context: vscode.ExtensionContext) {
context.subscriptions.push(
Expand Down Expand Up @@ -65,7 +65,7 @@ export async function initProjectCreator(context: vscode.ExtensionContext) {
}

// Call updateCopilotInstructions to update the Copilot instructions file
await updateCopilotInstructions(folderUri);
await updateCopilotInstructions("Project", context);
},
),
);
Expand Down
1 change: 1 addition & 0 deletions vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export async function activate(
await initProjectCreator(context);
registerCopilotPanel(context);
registerLanguageModelTools(context);
// fire-and-forget
registerGhCopilotInstructionsCommand(context);

log.info("Q# extension activated.");
Expand Down
Loading