Introduction to the quantum programming language Q#
As a quantum programming language, Q# meets the following requirements for language, compiler, and runtime:
- Hardware agnostic. Qubits in quantum algorithms aren't tied to specific quantum hardware or layout — the Q# compiler and runtime handle mapping program qubits to physical qubits, so the same code runs on different quantum processors.
- Integration of quantum and classical computing. Q# allows quantum and classical computations to be combined, which is essential for universal quantum computing.
- Qubit management. Q# provides built-in operations and functions for managing qubits, including creating superposition states, entangling qubits, and performing quantum measurements.
- Respect the laws of physics. Q# and quantum algorithms must follow the rules of quantum physics — for example, you can't directly copy or access the qubit state in Q#.
Structure of a Q# program#
Before writing Q# programs, it helps to understand their structure and components. Consider the following program, named Superposition, that creates a superposition state:
namespace Superposition {
@EntryPoint()
operation MeasureOneQubit() : Result {
// Allocate a qubit. By default, it's in the 0 state.
use q = Qubit();
// Apply the Hadamard operation, H, to the state.
// It now has a 50% chance of being measured as 0 or 1.
H(q);
// Measure the qubit in the Z-basis.
let result = M(q);
// Reset the qubit before releasing it.
Reset(q);
// Return the result of the measurement.
return result;
}
}Based on the comments, the program allocates a qubit, applies an operation to put it in superposition, measures its state, resets it, and finally returns the result. This is the same shape of program you build step by step in Create your first Q# program, extended there to two entangled qubits.
Q# programs can optionally start with a user-defined namespace to organize related operations and functions — useful once a project grows beyond a single file. Inside it, an operation (like MeasureOneQubit above) is where you allocate qubits, apply gates, and take measurements.
Where to go next#
- Install the tooling: Set up the Microsoft Quantum Development Kit
- Write your first program: Create your first Q# program
- Understand what the gates in a Q# program actually do to a qubit's state: The qubit in quantum computing