Classical Types
Classical types in Qmod are not very different from classical types in conventional programming languages. There are scalar types likeint and bool, and aggregate
types, such as arrays and structs.
Classical types are used to declare classical function arguments, and global constants.
Variables and literal values of classical types can be used in expressions, and support
the conventional set of operators commonly available in conventional programming languages.
Scalar Types
In Qmod, scalar types represent numeric values, Boolean values, and Pauli base elements.Syntax
- Python
- Native
Python Classes are used to represent scalar types
CIntrepresents integersCRealrepresents real numbers (using floating point encoding)CBoolrepresents the Boolean valuesFalseandTruePaulirepresents the Pauli base elements using the symbolsPauli.I,Pauli.X,Pauli.Y, andPauli.Z(with the integer values 0, 1, 2, 3 respectively)
Arrays
Arrays are homogenous collections of scalars or structs with random access.Syntax
- Python
- Native
Array types are represented with the generic class
CArray. Arguments are declared with the type hint in the form:name : CArray [ [ element-type [ , length_expression ] ] ]Semantics
element-type is any scalar, array, or struct type. length_expression is optional, determining the length of the array. When left out, the length is determined upon variable initialization. Expressions of array type support the following operations:- Subscript: array-expression [ index-expression ]
- Slice: array-expression [ from-index-expression : to-index-expression ]
- Length: array-expression . len
Example
The following example demonstrates the use of classical arrays in Qmod. Functionfoo
takes an array of reals, and uses the .len attribute and array subscripting to access
the elements of the array. Note that index -1 signifies the last element in an array
(similar to Python).
- Python
- Native
Structs
Structs are aggregates of variables, called fields, each with its own name and type.- Python
- Native
A Qmod classical struct is defined with a Python data class: A class decorated with
@dataclasses.dataclass.Fields need to be declared with type-hints like classical arguments of functions.
Fields are initialized and accessed like attributes of Python object.Example
In the following example a struct type calledMyStruct is defined. Function foo
takes an argument of this type and accesses its fields. Function main instantiates
and populates MyStruct in its call to foo.
- Python
- Native
Hamiltonians
Qmod’s Python embedding offers a specialized syntax for creatingsparse Hamiltonian
Pauli
Pauli.X) with an index (e.g., Pauli.X(3)) creates a
single-qubit Pauli operator.
The multiplication of single-qubit Pauli operators (e.g.,
Pauli.X(1) * Pauli.Y(2)) constructs the tensor product of these operators on
the respective qubits.
These can be linearly combined in a sum, each optionally
with a scalar coefficient (e.g., 0.5 * Pauli.X(2) + Pauli.Y(0)*Pauli.Z(2)).
Example
The Hamiltonian specified by the Pauli stringsXYZ and IXI with coefficients
0.5 and 0.8 respectively is specified in the standard struct literal syntax
as follows: