Quantum trace simulator: width counter
The width counter is a part of the Quantum Development Kit Quantum trace simulator. You can use it to count the number of qubits allocated and borrowed by each operation in a Q# program. Some primitive operations can allocate extra qubits, for example, multiply controlled X
operations or controlled T
operations.
Invoking the width counter
To run the quantum trace simulator with the width counter, you must create a QCTraceSimulatorConfiguration instance, set the UseWidthCounter
property to true, and then create a new QCTraceSimulator instance with the QCTraceSimulatorConfiguration
as the parameter.
var config = new QCTraceSimulatorConfiguration();
config.UseWidthCounter = true;
var sim = new QCTraceSimulator(config);
Using the width counter in a C# host program
The C# example that follows in this section computes the number of extra qubits allocated by the implementation of a multiply controlled X operation operation, based on the following Q# sample code:
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Arrays;
operation ApplyMultiControlledX( numberOfQubits : Int ) : Unit {
use qubits = Qubit[numberOfQubits];
Controlled X (Rest(qubits), Head(qubits));
}
The multiply controlled X operation operation acts on a total of five qubits, allocates two auxiliary qubits, and has an input width of 5. Use the following C# program to verify the counts:
var config = new QCTraceSimulatorConfiguration();
config.UseWidthCounter = true;
var sim = new QCTraceSimulator(config);
int totalNumberOfQubits = 5;
var res = ApplyMultiControlledX.Run(sim, totalNumberOfQubits).Result;
double allocatedQubits =
sim.GetMetric<Primitive.X, ApplyMultiControlledX>(
WidthCounter.Metrics.ExtraWidth,
functor: OperationFunctor.Controlled);
double inputWidth =
sim.GetMetric<Primitive.X, ApplyMultiControlledX>(
WidthCounter.Metrics.InputWidth,
functor: OperationFunctor.Controlled);
The first part of the program runs the ApplyMultiControlledX
operation. The second part uses the QCTraceSimulator.GetMetric
method to retrieve the number of allocated qubits as well as the number of qubits that the Controlled X
operation received as input.
Finally, you can output all the statistics collected by the width counter in CSV format using the following:
string csvSummary = sim.ToCSV()[MetricsCountersNames.widthCounter];
See also
- The Quantum Development Kit Quantum trace simulator overview.
- The QCTraceSimulator API reference.
- The QCTraceSimulatorConfiguration API reference.
- The MetricsNames.WidthCounter API reference.
Feedback
Submit and view feedback for