Introduction to the Q# Standard Libraries

Q# is supported by a range of different useful operations, functions, and user-defined types that comprise the Q# standard libraries. The Microsoft.Quantum.Sdk NuGet package installed during installation and validation automatically provides the Q# standard library.

The symbols defined by the Q# standard libraries are defined in much greater and more exhaustive detail in the API documentation.

This section describes the most salient features of each part of the standard library and how each feature might be used in practice.


The prelude provides a set of essential and very useful functions and operations that can be used when writing quantum programs in Q#.

For example, the Microsoft.Quantum.Intrinsic namespace includes the Pauli operators (X operation, Y operation and Z operation), the Hadamard operation, rotations such as S operation, T operation and the general R operation. You can also find two-qubit operations such as CNOT operation and SWAP operation. This section it also defines the Measure operation which performs a joint measurement of one or more qubits.

Classical mathematics

The standard libraries offer the posibility of doing classical mathematics. The Microsoft.Quantum.Math namespace contains classical mathematical functions and data types, which are primarily used to work with the Q# built-in data types Int, Double, and Range.

Type conversions

Q# is a strongly-typed language, which implies that Q# does not implicitly cast between distinct types. You can use the standard libraries to perform type conversions. The Microsoft.Quantum.Convert namespace contains functions for converting between various Q# data types, for example the BoolAsResult function converts a Bool type to a Result type, where true is mapped to One and false is mapped to Zero.

Higher-order control flow

You can use the standard libraries to manage higher-order control flow. Fpr example, the Microsoft.Quantum.Canon namespace provides a variety of different flow control constructs to express high-level algorithmic as quantum programs easier.

Data structures and modeling

The standard libraries allow working with data structures and modeling. The Microsoft.Quantum.Canon namespace provides operations, functions, and types for working with classical data, such as pairs and arrays.

This section also includes two quantum oracles, Amplitude Amplification and Phase Estimation. The Microsoft.Quantum.Oracles namespace provides user-defined types that are used to label the different oracle representations in a type-safe way, making it difficult to accidentally conflate different kinds of black box operations.

Quantum algorithms

With the standard libraries you can write some of the common and very useful quantum algorithms. For example, the Microsoft.Quantum.Canon namespace provides the ApproximateQFT operation, which is and approximate generalization of the Quantum Fourier Transform.


The standard libraries allow different ways to diagnose mistakes and errors in quantum programs. For example, the Microsoft.Quantum.Diagnostics namespace provides many operators for testing and debugging.


You can use the standard libraries to characterize the effects of operations in order to develop useful quantum algorithms. Since every measurement of a quantum system yields at most one bit of information, the aim is less to learn classical information about the system, rather than to perform a unitary transformation on a state vector.

Error correction

One of the most known applications of quantum computing is quantum error correction. The Microsoft.Quantum.Canon namespace provides several distinct user-defined types for building error correcting codes.


Applications describes some other applications of quantum computing that can be performed using Q#, such as the simulation of algorithms and Hamiltonians, and Shor's algorithm.