Skip to main content
Introducing The Quantum Infrastructure Cloud Learn more

DevelopersHiQ / Language

HiQ Objects

Classes and interfaces with exactly one main block. Every object is resolved at compile time: objects flatten to named qubits and variables, every call inlines and dispatch is static.

  • .qcos3
  • qcos-l3/0
  • Classes, interfaces and one main block
  • Lowers to HiQ Script

A HiQ Objects program is classes, interfaces and exactly one main block. There is no program line. Comments start with // or #. Every object is resolved at compile time: objects flatten to named qubits and variables, every call inlines, and dispatch is static.

HiQ Objectsqcos-l3/0
class Bell {  public qubit a  public qubit b  public bit ma  public bit mb  public def prepare() {    h a    cx a, b  }  public def read() {    measure a -> ma    measure b -> mb  }}main {  Bell pair  pair.prepare()  pair.read()}

Classes and interfaces

FormMeaning
interface Oracle { def mark(target: qubit) }Method signatures
class Name(params) extends Base(args) implements A, B { ... }A class. All the clauses are optional
abstract class Name { abstract def f() }Cannot be instantiated
qubit x, qubit x[n], bit c, uint8 n = 3, int8 nFields
Class y(args), Class ys[n](args)Object fields and arrays of objects
public, override, abstract before defMethod modifiers, in any order
def init() { ... }Runs once, after the object is built

Fields and methods are visible to the class and its heirs unless public. A local object is declared the same way inside main or a method.

Parameters

int and float parameters are compile-time values. qubit, bit, uintN, intN, their arrays, classes and interfaces bind by reference. A classical expression binds by value into a new variable.

Statements

FormMeaning
h a, rz(pi / 2) a, cx a, bA gate
measure q -> c, reset qMeasure and reset
x = y + 1Assignment
qubit t, uint8 n = 0A local declaration. A local qubit lives until the end of its scope
if c { } else { }, match x { 0 => { } _ => { } }, while c budget 8 { }Control flow
for i in 0..n step 1 { }Always unrolled, stop excluded
within { } do { }, inverse { }Compute and uncompute, and inversion of a gate block
obj.method(args), self.method(args), super.method(args)Calls

There are no return values: values travel through fields and arguments. There is no controlled block. Recursion is refused (l3.recursion, l3.recursive_class). A class or interface cannot take a gate's name.

Idioms

Polymorphism through an interface:

HiQ Objectsqcos-l3/0
interface Oracle { def mark(target: qubit) }​class Flip implements Oracle {  public def mark(target: qubit) { x target }}​class Runner(oracle: Oracle) {  public qubit t  public bit r  public def run() {    oracle.mark(t)    measure t -> r  }}​main {  Flip flip  Runner runner(flip)  runner.run()}

An array of objects, a parameterised rotation, and feedback:

HiQ Objectsqcos-l3/0
class Cell(angle: float) {  public qubit q  public bit m  public def spin() {    ry(angle) q  }  public def read() {    measure q -> m  }}​class Chain(n: int) {  public Cell cells[n](pi / 4)  public def run() {    for i in 0..n {      cells[i].spin()    }    cells[0].read()    if cells[0].m {      x cells[1].q    }  }}​main {  Chain chain(2)  chain.run()}

Refused, with the code

Refused by the checkerl3.main
class Empty {  public qubit q}
Refused by the checkergate.unknown
class Bad {  public qubit q  public def go() {    foo q  }}main {  Bad b  b.go()}

Examples in this layer