|
| 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. |
0 commit comments