Flame Graphs for Q#

Profiling is an important technique in software engineering as it allows you to evaluate the performance of your software along with its constituent parts.

Turns out, this is also true for quantum software! Any quantum program or circuit consists of a sequence of quantum gates like CNOT or T. Profiling such programs is very useful in determining the resource-intensive parts and better targeting our optimization efforts.

Flame graphs are a visualization of profiled software, allowing the most frequent code-paths to be identified quickly and accurately. — Brendan Gregg

The idea behind this mini-project, however, was to examine the resource consumption of each function rather than the frequency of different code-paths.

We used the Q# Resources Estimator which provides statistics on the number of gates and qubits used by a function across all its calls during the execution of a program.

Our goal was to modify the resources estimator such that, given some Q# code, it generates a profile that can be used as an input to the open-source flame graph library. To achieve this, we keep track of the stack trace as a string. When we encounter an unseen stack trace, we create a new entry with its resource consumption and for a previously seen one, we simply update the entry.

We tested our implementation to measure the number of CNOT gates used in the Q# implementation of Shor’s algorithm, a polynomial-time quantum algorithm for integer factorization. Here is the result!

Flame Graph for Shor’s Algorithm; “samples” correspond to the number of CNOT gates

You can find the project in this pull request to Microsoft’s Quantum repository. I would like to thank Mathias Soeken for his mentorship in this project!

I hope that you found this post interesting and that this extension comes in handy for your future projects with Q#.

Happy holidays!




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aman Bansal

Aman Bansal

Computer Science @ EPFL, Switzerland