Skip to content

Q#

The in-app starter (Bell state — note the DumpMachine() call, which is what lights up the statevector panels; see the contract):

import Std.Diagnostics.DumpMachine;
// Create a Bell State
operation Main() : Result[] {
use qs = Qubit[2];
H(qs[0]);
CNOT(qs[0], qs[1]);
DumpMachine(); // shows the live quantum state in Nuclei's panels
let results = [M(qs[0]), M(qs[1])];
ResetAll(qs);
return results;
}

Q# is the one framework whose code never reaches Python exec(). Its AdapterSpec is marked source_mode, so the executor hands the raw text to parse_source / execute_source, which drive the qdk interpreter (qsharp.initqsharp.evalqsharp.circuit / qsharp.run).

Q# compile errors come back as compile_error with the qdk diagnostic as the traceback — there is no Python traceback for Q# code.

Only zero-parameter operations are runnable. Resolution order:

  1. An operation named MainMain() wins.
  2. Otherwise the last zero-parameter operation defined in the file (the newest one wins).
  3. None defined → parse quietly returns an empty circuit (no error while live-typing); execute returns no_circuit with a hint to define operation Main() : Result[].

Q# runs are sampled shot-by-shot (qsharp.run(entry, shots=...)); the exact quantum state is only recoverable when the program itself calls DumpMachine():

With DumpMachine()Without
state_vectorExact dense state — the last dump of the first shot, if its qubit count matches the circuit[] (empty)
probabilitiesExact (replaces sampled frequencies)Sampled count / shots
bloch_coordsPartial trace of the dumped stateMeasurement marginals — only z is observable from counts, so x = y = 0
measurementsSampled counts either way: flattened Result values in entry-point return order same

Dump text and Message() output from the first shot appear as terminal output. Key conventions (qubit 0 leftmost — big-endian, like DumpMachine’s own display) are on the schemas page.

The snapshot is built from qsharp.circuit(entry)’s JSON, flattened and re-layered with the same greedy algorithm as Qiskit — so Q# circuits lay out identically in the renderer.

Q# circuit-JSONCanonical
X with 1 controlCNOT
X with 2 controlsToffoli
Z with 1 controlCZ
Rx / Ry / RzRX / RY / RZ (adjoint is already folded into the emitted angle — not re-applied)
S / T with isAdjointSdg / Tdg
H X Y Z S T SWAP (uncontrolled)unchanged
measurement componentsMeasure
ket (mid-circuit |0⟩ preparation)Reset — Q# is the only framework that emits it
anything elseuppercase passthrough, controls preserved

Two distinct paths:

  • Editor runs use the unrestricted local interpreter, per-shot, as above.
  • Hardware submission to Azure Quantum compiles the same source to QIR via compile_qir(code, profile); the profile is selected per target — see submitting Q#. The interpreter profile is restored to Unrestricted afterwards.

The qdk interpreter is process-global and pinned to one dedicated thread — all Q# work (every connection’s) serializes through it.

Desktop-only: the web build rejects Q# execution outright (editor diagnostics/completions still work there) — see desktop vs web.