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.
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.
- 103
- 3
- 81
- 1
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
ICallableinterface; from it, you can get theName(e.g.X), theFullName(e.g.Microsoft.Quantum.Primitive.X) or theVariant(i.e.Body,Adjoint,ControlledorControlledAdjoint). - the argument tuple, exposed via the
IApplyDatainterface; 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.
- 406
- 2
- 3