Q# Pocket Guide

Book description

Ready to build quantum computing applications using Q# and the Microsoft Quantum Development Kit? This is the book for you. Q# is a domain-specific language for expressing quantum algorithms that combines familiar "classical" language constructs with quantum-specific ones. Ideal for any developer familiar with (or willing to learn) the basics of quantum computing and looking to get started with quantum programming, this pocket guide quickly helps you find syntax and usage information for unfamiliar aspects of Q#.

You'll explore the quantum software development lifecycle from implementing the program to running it on quantum simulators to testing and debugging it. You'll learn to use the tools provided by Microsoft's Quantum Development Kit for each step of the process.

You'll explore:

  • Q# language details, including data types, statements, and operators
  • Guidelines for organizing Q# code and invoking it from different environments
  • Information on simulators and tools in the Microsoft Quantum Development Kit
  • Advice on testing and debugging tools and techniques for quantum programs

Publisher resources

View/Submit Errata

Table of contents

  1. Foreword
  2. Preface
    1. Why I Wrote This Book
    2. Who This Book Is For
    3. Navigating This Book
    4. Conventions Used in This Book
    5. Using Code Examples
    6. O’Reilly Online Learning
    7. How to Contact Us
    8. Acknowledgments
  3. I. Q# Programming Language
  4. 1. Program Structure
    1. Your First Q# Program
    2. Namespaces
    3. Operations and Functions
    4. Type Declarations
    5. Comments
    6. Conclusion
  5. 2. Data Types
    1. The Q# Type System
    2. Primitive Classical Data Types
    3. Qubits
      1. Physics-Imposed Constraints
      2. Abstracting Away Physical Implementation
      3. The Qubit Data Type
    4. Other Primitive Quantum Data Types
      1. Measurement Results
      2. Pauli Matrices
    5. Data Structures
      1. Arrays
      2. Tuples
      3. User-Defined Types
    6. Operations and Functions
    7. Conclusion
  6. 3. Expressions
    1. Equality Operators
    2. Comparison Operators
    3. Logical Operators
    4. Bitwise Operators
    5. Arithmetic Operators
    6. Conditional Expression
    7. Range Operator
    8. String Expressions
    9. Array Expressions
    10. User-Defined Type Expressions
    11. Call Expressions
    12. Lambda Expressions
    13. Adjoint and Controlled Functors
    14. Conclusion
  7. 4. Statements
    1. Example: Calculate Euler’s Totient Function
    2. Working with Variables
      1. Variable Scope
      2. Mutable and Immutable Variables
      3. Declaring Immutable Variables: let Statements
      4. Declaring Mutable Variables: mutable Statements
      5. Reassigning Mutable Variables: set Statements
      6. Assigning Tuples: Tuple Deconstruction
    3. Conditional Execution: if Statements
    4. Loops
      1. Iterate Over a Sequence: for Loops
      2. Classical Conditional Loop: while Loops
    5. Call an Operation or a Function: Call Statements
    6. Stop Execution: return and fail Statements
      1. Finish Execution: return Statements
      2. Throw an Exception: fail Statements
    7. Example: Prepare a Quantum State
    8. Allocate Qubits: use and borrow Statements
    9. Quantum Conditional Loops: repeat-until Loops
    10. Conjugation: within-apply Statements
    11. Conclusion
  8. 5. Operations and Functions
    1. Defining and Using Operations and Functions: The Basics
    2. Signatures of Callables
    3. Quantum Gates and Measurements
    4. Defining and Using Adjoint and Controlled Specializations of Operations
      1. Using Operation Specializations
      2. Generating Operation Specializations Automatically
      3. Defining Operation Specializations Manually
    5. Functional Elements of Q#
      1. Callable-Typed Variables
      2. Using Callables as Arguments
      3. Partial Application
      4. Lambda Expressions
    6. Defining and Using Type-Parameterized Callables
    7. Conclusion
  9. II. Using the Microsoft Quantum Development Kit
  10. 6. Running Q# Programs
    1. Quantum Applications
    2. Quantum Software Development
    3. Quantum Simulators
      1. Full State Simulator
      2. Resources Estimator
      3. Trace Simulator
      4. Toffoli Simulator
      5. Sparse Simulator
      6. Noise Simulator
    4. Running Q# Programs
      1. Standalone Q#
      2. Q# with a Classical Host
      3. Q# with .NET Host
      4. Q# with Python Host
      5. Q# Jupyter Notebooks
    5. Conclusion
  11. 7. Microsoft Quantum Development Kit Libraries
    1. Getting and Using the Libraries
    2. Standard Libraries
      1. Microsoft.Quantum.Core: Built-In Functions and Attributes
      2. Microsoft.Quantum.Convert: Data Type Conversions
      3. Microsoft.Quantum.Logical: Logical and Comparison Functions
      4. Microsoft.Quantum.Bitwise: Bitwise Functions
      5. Microsoft.Quantum.Math: Classical Math and Arithmetic
      6. Microsoft.Quantum.Random: Random Numbers and Probability Distributions
      7. Microsoft.Quantum.Arrays: Generic Array Manipulation
      8. Microsoft.Quantum.Intrinsic: Basic Quantum Gates and Measurements
      9. Microsoft.Quantum.Diagnostics: Testing and Troubleshooting Quantum Programs
      10. Microsoft.Quantum.Measurement: Additional Measurement Routines
      11. Microsoft.Quantum.Preparation: Quantum State Preparation
      12. Microsoft.Quantum.Arithmetic: Quantum Arithmetic
      13. Microsoft.Quantum.Canon: Catchall
    3. Advanced Libraries
    4. Conclusion
  12. 8. Testing and Debugging
    1. Inspecting Elements of Q# Programs
      1. Classical Variables
      2. Quantum States
      3. Quantum Operations
      4. Program Structure and Its Evolution
    2. Testing Q# Programs
      1. Two Ways to Write a Test
      2. Standalone Q# Test Projects
      3. Testing Classical Conditions
      4. Testing Conditions on the Quantum State
      5. Comparing the Unitaries Implemented by Operations
    3. Conclusion
  13. Index
  14. About the Author

Product information

  • Title: Q# Pocket Guide
  • Author(s): Mariia Mykhailova
  • Release date: June 2022
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098108861