Quantum trace simulator: primitive operations counter

The primitive operation counter is a part of the Quantum Development Kit Quantum trace simulator. It counts the number of primitive processes used by every operation invoked in a quantum program.

All Microsoft.Quantum.Intrinsic namespace operations are expressed in terms of single-qubit rotations, T operations, single-qubit Clifford operations, CNOT operations, and measurements of multi-qubit Pauli observables. The Primitive Operations Counter aggregates and collects statistics over all the edges of the operation's call graph.

Invoking the primitive operation counter

To run the quantum trace simulator with the primitive operation counter, you must create a QCTraceSimulatorConfiguration instance, set the UsePrimitiveOperationsCounter property to true, and then create a new QCTraceSimulator instance with the QCTraceSimulatorConfiguration as the parameter.

var config = new QCTraceSimulatorConfiguration();
config.UsePrimitiveOperationsCounter = true;
var sim = new QCTraceSimulator(config);

Using the primitive operation counter in a C# host program

The C# example that follows in this section counts how many T operation operations are needed to implement the <xref:Microsoft.Quantum.Intrinsic.ccnot> operation, based on the following Q# sample code:

open Microsoft.Quantum.Intrinsic;
operation ApplySampleWithCCNOT() : Unit {

    using (qubits = Qubit[3]) {
        CCNOT(qubits[0], qubits[1], qubits[2]);

To check that CCNOT requires seven T operations and that ApplySampleWithCCNOT runs eight T operations, use the following C# code:

// using Microsoft.Quantum.Simulation.Simulators.QCTraceSimulators;
// using System.Diagnostics;
var config = new QCTraceSimulatorConfiguration();
config.UsePrimitiveOperationsCounter = true;
var sim = new QCTraceSimulator(config);
var res = ApplySampleWithCCNOT.Run(sim).Result;

double tCountAll = sim.GetMetric<ApplySampleWithCCNOT>(PrimitiveOperationsGroupsNames.T);
double tCount = sim.GetMetric<Primitive.CCNOT, ApplySampleWithCCNOT>(PrimitiveOperationsGroupsNames.T);

The first part of the program runs ApplySampleWithCCNOT. The second part uses the QCTraceSimulator.GetMetric method to retrieve the number of T operations run by ApplySampleWithCCNOT:

When you call GetMetric with two type parameters, it returns the value of the metric associated with a given call graph edge. In the preceding example, the program calls the Primitive.CCNOT operation within ApplySampleWithCCNOT and therefore the call graph contains the edge <Primitive.CCNOT, ApplySampleWithCCNOT>.

To retrieve the number of CNOT operations used, add the following line:

double cxCount = sim.GetMetric<Primitive.CCNOT, ApplySampleWithCCNOT>(PrimitiveOperationsGroupsNames.CX);

Finally, you can output all the statistics collected by the Primitive Operations Counter in CSV format using the following:

string csvSummary = sim.ToCSV()[MetricsCountersNames.primitiveOperationsCounter];

See also