Chapter 1. Program Structure
This chapter offers an overview of Q# program structure and the main elements it comprises. This will help you orient yourself in the following chapters, in which we’ll dive deeper into writing Q# code.
Your First Q# Program
Example 1-1 shows a Q# program that prints a message to the console. While very simple, it shows the main elements of Q# code: a namespace, an open directive, an operation that contains the actual Q# statements, and comments.
Note
The code samples in Part I of this book are written as Q# standalone applications. You can follow the installation instructions to set up the Microsoft Quantum Development Kit for your environment and run these samples. In Chapter 6, we’ll discuss different ways to run Q# programs and the nuances of writing Q# code for each of them.
Example 1-1. “Hello World” program in Q#
namespaceHelloQuantumWorld{openMicrosoft.Quantum.Intrinsic;// To use Message/// # Summary/// Prints a message to the console.@EntryPoint()operationSayHello():Unit{Message("Hello quantum world!");}}
Note
Once you’ve installed the QDK for your preferred development environment, you can create a Q# project, copy-paste Example 1-1 to the .qs file in the project, and run it following these instructions. You should see a console window with this text message in it:
Hello quantum world!
Congratulations, you just ran your first Q# program!
Let’s take a closer look at the code elements that are shown in Example 1-1.
Namespaces ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access