function { } Blocks

Named, callable quantum routines with a gate-like header and a braced body. The simulator prepends #include standard/all.qubi, which registers built-in gates plus algorithm functions (Grover, QFT, Deutsch, and others). You can author your own function blocks the same way.

Syntax

function and fn are equivalent. The header lists typed classical parameters, optional variadic wire parameters (...), and defaults. The body mixes metadata properties (name:, desc:, …) with executable Qubi lines, nested function definitions, if branches, and gates.

function Grover(bitstring target=0b101, int ... = 0..(len(target)-1)) {
  name: Grover Search
  desc: Amplitude amplification for marked basis state(s)
  label: Grvr
  category: Functions
  examples: Grover(), Grover(0b101), Grover((0b101, 0b111))

  if (typeof(target) == "bitstring") {
    wires = 0..(len(target)-1)
    H (wires)
    // oracle + diffusion …
  } else {
    error("Grover: target must be a bitstring or bitstring list")
  }
}

Grover(0b1011)

Parameter types include bitstring, int, string, and float. Wire parameters use int names or int ... for a variadic wire list. Omit trailing arguments to pick up defaults; use empty commas to skip slots (Deutsch(,,5) keeps default inputs but sets ancilla wire 5).

Opaque vs expandable

Functions expand on the circuit by default (blackbox: false / encapsulate: false). Set blackbox: true (or encapsulate: true) to show one tile on the diagram instead of inlining every gate. Only expandable functions support partial and full decomposition.

function Oracle(string kind, int ..., int anc) {
  blackbox: false    // expand so if/elseif branches are visible
  label: ORC
  if (kind == "balanced") {
    i = 1
    LOOP (i < argmax) {
      CX [arg[i], anc]
      i++
    }
  }
}

Standard library calls

These functions are available once the prepended layer includes standard/all.qubi (default in every session). Drag them from the palette under Functions, load an example from the sidebar, or call by name in your main file.

FunctionSummary
Bell(kind, a, b)Bell states (phi_plus, phi_minus, psi_plus, psi_minus).
GHZ(...)Greenberger-Horne-Zeilinger state on a wire range (default 0..2).
W(...)W state on a wire range.
Superdense(msg, a, b)Superdense coding with a 2-bit classical message.
Teleport(state, msg, alice, bob)Quantum teleportation (plus, zero, one input states).
Deutsch(kind, ..., anc)Deutsch-Jozsa (const0, const1, balanced).
BV(secret, ..., anc)Bernstein-Vazirani with a secret bitstring.
Grover(target, ...)Grover search. Target is one bitstring or a parenthesized list of marked states.
QFT(...) / IQFT(...)Quantum Fourier transform and inverse (wire range defaults to 0..2).
Shor(...)Shor period-finding skeleton for teaching.
PhaseKickback(kind, a, b)Controlled-phase kickback demo (CX, CZ, etc.).
PhaseOracle(target, ...)Phase oracle that marks one computational basis state.
SwapTest(kind, anc, a, b)Swap test for equal vs orthogonal states.
BitFlip(error, ...)Three-qubit bit-flip code encode / error / correct demo.
QPE(phi, ...)Quantum phase estimation to estimate angle phi.
Stego(a, b)Quantum steganography toy protocol.

Interactive templates with parameters live on Live Examples. Each example can emit either inline gate soup or a library file plus a one-line call.

Relation to gate blocks

gate { } defines a unitary or fixed sequence for the palette. function { } is the same block machinery plus classical parameters, control flow, and optional nesting. Matrix-only custom gates still belong in gate blocks; algorithms with branches and variables belong in functions.