Introduction to the Quantum Programming Language Q# - Azure Quantum | Microsoft Learn
Table of contents
Exit editor mode
Ask Learn
Ask Learn
Reading mode
Table of contents
Read in English
Add
Add to plan
Edit
Copy Markdown
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Introduction to the quantum programming language Q#
Feedback
Summarize this article for me
👉 Learn more about the Microsoft Quantum platform
Q# is a high-level, open-source programming language developed by Microsoft for writing quantum programs. Q# is included in the Microsoft Quantum Development Kit (QDK). For more information, see Set up the Microsoft Quantum Development Kit.
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 a specific quantum hardware or layout. The Q# compiler and runtime handle the mapping from program qubits to physical qubits, allowing the same code to run on different quantum processors.
Integration of quantum and classical computing: Q# allows for the integration of quantum and classical computations, 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#.
For more information about the origins of Q#, see the blog post Why do we need Q#?.
Structure of a Q# program
Before you start writing Q# programs, it's important to understand their structure and components. Consider the following Q# program, named Superposition, that creates a superposition state:
namespace Superposition {<br>@EntryPoint()<br>operation MeasureOneQubit() : Result {<br>// Allocate a qubit. By default, it's in the 0 state.<br>use q = Qubit();<br>// Apply the Hadamard operation, H, to the state.<br>// It now has a 50% chance of being measured as 0 or 1.<br>H(q);<br>// Measure the qubit in the Z-basis.<br>let result = M(q);<br>// Reset the qubit before releasing it.<br>Reset(q);<br>// Return the result of the measurement.<br>return result;
Based on the comments (//), the Q# program first allocates a qubit, applies an operation to put the qubit in superposition, measures the qubit state, resets the qubit, and finally returns the result.
Let's break this Q# program down into its components.
User namespaces
Q# programs can optionally start with a user-defined namespace, such as:
namespace Superposition {<br>// Your code goes here.
Namespaces can help you organize related functionality. Namespaces are optional in Q# programs, meaning that you can write a program without defining a namespace.
For example, the Superposition program from the example can also be written without a namespace as:
@EntryPoint()<br>operation MeasureOneQubit() : Result {<br>// Allocate a qubit. By default, it's in the 0 state.<br>use q = Qubit();<br>// Apply the Hadamard operation, H, to the state.<br>// It now has a 50% chance of being measured as 0 or 1.<br>H(q);<br>// Measure the qubit in the Z-basis.<br>let result = M(q);<br>// Reset the qubit before releasing it.<br>Reset(q);<br>// Return the result of the measurement.<br>return result;
Note
Each Q# program can have only one namespace. If you don't specify a namespace, the Q# compiler uses the filename as the namespace.
Entry points
Every Q# program must have an entry point, which is the starting point of the program. By default, the Q# compiler starts executing a program from the Main() operation, if available, which can be located anywhere in the program. Optionally, you can use the @EntryPoint() attribute to specify any operation in the program as the point of execution.
For example, in the Superposition program, the MeasureOneQubit() operation is the entry point of the program because it has the @EntryPoint() attribute before the operation definition:
@EntryPoint()<br>operation MeasureOneQubit() : Result {<br>...
However, the program could also be written without the @EntryPoint() attribute by renaming the MeasureOneQubit() operation to Main(), such as:
// The Q# compiler automatically detects the Main() operation as the entry point.
operation Main() : Result {<br>// Allocate a qubit. By default, it's in the 0 state.<br>use q = Qubit();<br>// Apply the Hadamard operation, H, to the state.<br>// It now has a 50% chance of being measured as 0 or 1.<br>H(q);<br>// Measure the qubit in the Z-basis.<br>let result = M(q);<br>// Reset the qubit before releasing it.<br>Reset(q);<br>// Return the result of the measurement.<br>return result;
Types
Types are essential in any programming language because they define the data that a program can work with. Q# provides built-in types that are common to most...