Chapter 5. Operations and Functions

This chapter dives deeper into defining and using operations and functions—the main units of Q# code.

Operations are the most general type of Q# subroutine—sequences of statements that perform a certain task, packaged as a single logical unit. Functions are a special type of subroutine that perform only deterministic classical computation. Together, operations and functions are called callables.

Since Q# is a quantum programming language, operations are much more common in Q# code—you cannot write a quantum program without using an operation. However, functions are useful in their own right, as I’ll show you in this chapter.

The chapter starts with the basic ways to define functions and operations and use them, including call expressions and statements, and a description of signatures of callables.

After that I’ll show how quantum gates and measurements are represented as operations in Q#, thus finishing the discussion we started in Chapter 2 of how the language expresses central quantum computing concepts. Next, I’ll discuss defining and using adjoint and controlled variants of operations—powerful tools for expressing quantum algorithms more concisely.

I’ll wrap up the chapter with a discussion of the functional elements of Q#, such as passing callables as arguments to other callables and using partial application to construct new callables, and the type-parameterized callables.

Defining and Using Operations and Functions: The Basics

A basic ...

Get Q# Pocket Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.