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?
2 Answers
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$ Dec 31, 2018 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$ Dec 31, 2018 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}");
HelloQ.Run(qsim).Wait();
}
The event sends two parameters:
- the operation itself, exposed via the
ICallable
interface; from it, you can get theName
(e.g.X
), theFullName
(e.g.Microsoft.Quantum.Primitive.X
) or theVariant
(i.e.Body
,Adjoint
,Controlled
orControlledAdjoint
). - 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.