Width Counter

The Width Counter counts the number of qubits allocated and borrowed by each operation. All operations from the Microsoft.Quantum.Primitive namespace are expressed in terms of single qubit rotations, T gates, single qubit Clifford gates, CNOT gates and measurements of multi-qubit Pauli observables. Some of the primitive operations can allocate extra qubits. For example, multiply controlled X gates or controlled T gates. Let us compute the number of extra qubits allocated by the implementation of a multiply controlled X gate:

open Microsoft.Quantum.Primitive;
operation MultiControlledXDriver( numberOfQubits : Int ) : () {
    body {
        using( qubits = Qubit[numberOfQubits] ) {
            (Controlled X)(qubits[ 1 .. numberOfQubits - 1] ,qubits[0]);
        } 
    }
}

Using Width Counter within a C# Program

Multiply controlled X acting on a total of 5 qubits will allocate 2 ancillary qubits and its input width will be 5. To check that this is the case, we can use the following C# program:

var config = new QCTraceSimulatorConfiguration();
config.useWidthCounter = true;
var sim = new QCTraceSimulator(config);
int totalNumberOfQubits = 5;
var res = MultiControlledXDriver.Run(sim, totalNumberOfQubits).Result;

double allocatedQubits = 
    sim.GetMetric<Primitive.X, MultiControlledXDriver>(
        WidthCounter.Metrics.ExtraWidth,
        functor: OperationFunctor.Controlled); 

double inputWidth =
    sim.GetMetric<Primitive.X, MultiControlledXDriver>(
        WidthCounter.Metrics.InputWidth,
        functor: OperationFunctor.Controlled);

The first part of the program executes MultiControlledXDriver. In the second part we use the method QCTraceSimulator.GetMetric to get the number of allocated qubits as well as the number of qubits that Controlled X received as input.

Finally, to output all the statistics collected by width counter in CSV format we can use the following:

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

See also