Type Conversions

Q# is a strongly-typed language. In particular, Q# does not implicitly cast between distinct types. For instance, 1 + 2.0 is not a valid Q# expression. Rather, Q# provides a variety of type conversion functions for constructing new values of a given type.

For example, Length function has an output type of Int, so its output must first be converted to a Double before it can be used as a part of a floating-point expression. This can be done using the IntAsDouble function function:

open Microsoft.Quantum.Convert as Convert;

function HalfLength<'T>(arr : 'T[]) : Double {
    return Convert.IntAsDouble(Length(arr)) / 2.0;
}

The Microsoft.Quantum.Convert namespace namespace provides common type conversion functions for working with the basic built-in types, such as Int, Double, BigInt, Result, and Bool:

let bool = Convert.ResultAsBool(One);        // true
let big = Convert.IntAsBigInt(271);          // 271L
let indices = Convert.RangeAsIntArray(0..4); // [0, 1, 2, 3, 4]

The Microsoft.Quantum.Convert namespace namespace also provides some more exotic conversions, such as FunctionAsOperation, which converts functions 'T -> 'U into new operations 'T => 'U.

Finally, the Q# standard library provides a number of user-defined types such as Complex user defined type and LittleEndian user defined type. Along with these types, the standard library provides functions such as BigEndianAsLittleEndian function:

open Microsoft.Quantum.Arithmetic as Arithmetic;

let register = Arithmetic.BigEndian(qubits);
IncrementByInteger(Arithmetic.BigEndianAsLittleEndian(register));