If I implement an adder operation in Q#, I'd like to see a quantum circuit diagram of what that adder is doing in order to check that it looks right. Is there a built-in way to do this?

  • $\begingroup$ Seems not. I tried to look everywhere but could not find something. $\endgroup$ – cnada Dec 29 '18 at 3:33

Unfortunately, there is indeed currently no way to generate circuit diagrams from a Q# program. Since this is a feature request, consider making it here: https://quantum.uservoice.com/forums/906940-debugging-and-simulation.

To give a little bit of context, Q# makes a conscious effort to encourage reasoning about quantum algorithms in terms of control flow and transformations rather than circuits, allowing e.g. qubit aliasing. Correspondingly, the kit focuses on tools that are commonly used in software engineering like unit testing instead of circuit diagrams. I can understand that a diagram may come in handy for publications, though it is certainly not unheard of to give pseudo code instead.

  • $\begingroup$ What about a print out of the sequence of gates being applied? Then I could feed that into an external tool. $\endgroup$ – Craig Gidney Dec 31 '18 at 17:03
  • 1
    $\begingroup$ The way to go for that is to have a simulator that generates the desired sequence when Q# code is executed on it. Would you mind taking a look here, and let me know it this one meets your needs? $\endgroup$ – Bettina Heim Dec 31 '18 at 19:55

Q#'s full state simulator has an OnOperationStart and OnOperationEnd events that, as the names imply, are triggered every time a quantum operation starts/ends. Thus the easiest way to get and print the sequence of gates would be to attach a handler to the OnOperationStart, for example:

        using (var qsim = new QuantumSimulator())
            qsim.OnOperationStart += (op, arg) => Console.WriteLine($"{op.Name}:{op.Variant}");


The event sends two parameters:

  1. the operation itself, exposed via the ICallable interface; from it, you can get the Name (e.g. X), the FullName (e.g. Microsoft.Quantum.Primitive.X) or the Variant (i.e. Body, Adjoint, Controlled or ControlledAdjoint).
  2. the argument tuple, exposed via the IApplyData interface; from it you can get the actual values the operation received or just the list of Qubits.

This doesn't produce the circuit diagram, but as you point out, could allow you to print and feed the list of operations to other tools.

We're still actively working on this part of the Quantum Development Kit, which is why it still needs much more documentation. However, we're active monitoring stackoverflow so please let us know if you need any more help.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.