Chapter 8. Testing and Debugging

This chapter describes the visualization, testing, and debugging tools and techniques available to you as a Q# developer. As with previous chapters, some of these tools will be similar to their equivalents in classical software development; the others will be unique to quantum software development, reflecting the fundamental differences between the two.

The first part of this chapter covers the tools used for inspecting elements of Q# programs, both classical and quantum. In it, I show:

  • How to print the quantum state of your program during its execution

  • How to inspect the matrix of the transformation implemented by a quantum operation

  • How to observe the quantum circuit that represents the structure of the program

  • How to follow the program state through its execution

The second part of this chapter covers the tools used to test quantum programs. I’ll start by showing two ways to write tests for quantum programs: using standalone Q# programs or combining them with classical host code. After that, I’ll dig deeper into the library functions and operations used for unit testing quantum programs, and the various scenarios in which they can be useful.

The tools and techniques I describe in this chapter rely on running quantum code on the simulators you saw in Chapter 6. These tools take advantage of direct access to the quantum state of the program as it is executed and allow you to test the programs without using quantum devices, which makes ...

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.