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, float, number, string, list, list-int (and other list-… element types), and boolean. 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).

A parameter can accept more than one classical type. Write a type list in parentheses before the name: (bitstring,list) target or (string or int) kind. Commas and or both work, including mixes. Inside the body, branch with typeof(target) (and listtype when you allowed list). C-style casts such as (float)1 or (int)x convert at the call site; they are not the same as a union header, which must list two or more types.

function Oracle((bitstring,list) target, int ... = 0..(len(target)-1)) {
  if (typeof(target) == "bitstring") {
    // one marked basis state
  } else {
    // list of marked states
  }
}

Oracle(0b101)
Oracle((0b101, 0b111))

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++
    }
  }
}

Scope blackbox (anonymous wrap)

The same display properties work outside a named function. Put them at the start of a bare { … } block, or at the top of the file (after #include / definitions), to wrap the following statements as one opaque tile - identical chrome to blackbox: true on a function.

{
  blackbox: true
  label: BF
  color: green
  bfec()
}

Without braces, a leading header applies to the rest of the file body:

blackbox: true
label: BF
bfec()

Allowed scope keys: blackbox / encapsulate, label, name, color, sidelabel, font, fontsize, size, fontcolor, desc. blackbox: true (or encapsulate: true) is required to form the tile; if you omit label, the tile shows BOX.

Same name from two includes

If two #include / #import files both define Oracle, call them with a file-stem prefix: teama-Oracle(...) and teamb-Oracle(...). The first import also keeps the bare name. Details and autocomplete behavior are under #import / #include.

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.
StatePreparation(target, prob, ...)Prepares target basis states or presets ("bell", "ghz", "superposition") with specific probabilities (e.g. StatePreparation(0b101, 0.4) or StatePreparation((0b101..0b111), (0.2, 0.3, 0.4))).
QubitPreparation(prob, ...)Prepares individual qubits with specified P(|1⟩) probabilities using single-qubit rotations (e.g. QubitPreparation((0.2, 0.5, 0.8)) or stepped QubitPreparation((0.2.0.1.0.5))).
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(N, a, ...)Shor period finding with a real controlled modular multiplier. The library is generated for the (N, a) chosen in Examples; counting wires first, work register last.
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(s, theta, eve, a, b)Quantum steganography. Secret bit s, cover key theta, Eve mode none / measure_z / wrong_key, then Alice/Bob wires.

Interactive templates with parameters live on Quantum Algorithms. 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.