-
Notifications
You must be signed in to change notification settings - Fork 130
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.
You can use the "Circuit" code lens to synthesize a circuit diagram for a Q# program.
Clicking on this code lens will cause a panel titled "Q# Circuit" to open up.
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.
Clicking on this code lens will show the circuit diagram for only that operation.
Note that this operation takes an array of qubits (Qubit[]
). Circuit synthesis will assume a length of 2 for qubit array arguments.
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.
In the Run and Debug view, expand "Quantum Circuit" under the Variables pane.
The current quantum circuit will be shown in the Q# Circuit pane.
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.
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).
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.
When the target profile is "QIR: Base", the circuit looks different.
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.
- Target profile
- Measurement results, simulator
- Needs to take qubit arguments
- Cannot be internal
Register width is 2 and this cannot be changed.
Q# Wiki
Overview
Q# language & features
- Q# Structs
- Q# External Dependencies (Libraries)
- Differences from the previous QDK
- V1.3 features
- Curated list of Q# libraries
- Advanced Topics and Configuration
OpenQASM support
VS Code
Python
Circuit diagrams
Azure Quantum
For contributors