Skip to content

Circuit Diagrams from Q# Code

Mine Starks edited this page Mar 28, 2024 · 9 revisions

Circuit Diagrams from Q# Code

The QDK VS Code extension and qsharp Python package both provide the ability to synthesize circuit diagrams from Q# code.

These diagrams capture the quantum operations (gates) that have been applied during the execution of the program.

VS Code

Circuit diagram for a Q# program

You can use the "Circuit" code lens to synthesize a circuit diagram for a Q# program.

image

Clicking on this code lens will cause a panel titled "Q# Circuit" to open up.

image

Circuit diagrams for operations

In addition to showing the circuit for the whole program, you can also synthesize circuits for individual operations that are declared in your code.

As long as the operation only takes parameters of type Qubit or Qubit[], a circuit can be generated for it.

In the editor, look for a Circuit code lens above the operation.

image

Clicking on this code lens will show the circuit diagram for only that operation.

image

Note that this operation takes an array of qubits (Qubit[]). Circuit synthesis will assume a length of 2 for qubit array arguments.

Circuit diagrams while debugging

You can also view circuit diagrams during step-by-step debugging of Q# programs.

Click the "Debug" code lens (or invoke the "Debug Q# File" command) to start stepping through the Q# source code.

image

In the Run and Debug view, expand "Quantum Circuit" under the Variables pane.

image

The current quantum circuit will be shown in the Q# Circuit pane.

image

This circuit diagram represents the current state of the simulator, i.e. the gates that have been applied up until the current point of execution.

Python

Refer to circuits.ipynb for a walkthrough of circuit diagrams in Python. (Note that the GitHub web viewer does not display the circuit images in this notebook. Open the notebook in VS Code or Jupyter to display the circuits).

Conditions that affect circuit synthesis

Measurement result comparisons

Target profile

The currently selected target profile (set using qsharp.init() in Python, and the status bar icon in VS Code) influences how the circuit is synthesized.

Specifically, gate decompositions are applied that would make the resulting circuit compatible with the capabilities of the target hardware. These are the same decompositions that would get applied during code (QIR) generation and submission to Azure Quantum.

As an example, take the Measurement.qs sample. When target profile is set to "Unrestricted", the gates displayed on the circuit correspond exactly to the quantum operations that are invoked in the Q# program.

image

When the target profile is "QIR: Base", the circuit looks different.

image

Since Base Profile targets do not allow qubit reuse after measurement, the measurement is now performed on an entangled qubit instead. Since Reset is not a supported gate in Base Profile, it's dropped. The resulting circuit matches what would be run on hardware if this program was submitted to Azure Quantum with this target profile.

Troubleshooting

I'm getting different circuit diagrams for the same program.

  1. Target profile
  2. Measurement results, simulator

I do not see the Circuit code lens for an operation in my code.

  1. Needs to take qubit arguments
  2. Cannot be internal

An operation circuit fails because of the register width

Register width is 2 and this cannot be changed.

Clone this wiki locally