基元操作计数器Primitive Operations Counter

Primitive Operations Counter 是 "量程计算机跟踪模拟器" 的一部分。The Primitive Operations Counter is a part of the quantum computer Trace Simulator. 它计算在量程程序中调用的每个操作所使用的原始执行数。It counts the number of primitive executions used by every operation invoked in a quantum program. Microsoft.Quantum.Intrinsic 中的所有操作都以单个 qubit 旋转、T 入口、单个 qubit Clifford 入口、CNOT-CONTAINS 入口和测量为多 qubit Pauli 可观察量表示。All operations from Microsoft.Quantum.Intrinsic are expressed in terms of single qubit rotations, T gates, single qubit Clifford gates, CNOT gates and measurements of multi-qubit Pauli observables. 收集的统计信息在操作调用关系图的边缘上聚合。Collected statistics are aggregated over the edges of the operations call graph. 现在,让我们计算实现 CCNOT 操作需要多少 T 入口。Let us now count how many T gates are needed to implement the CCNOT operation.

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

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

在C#程序中使用基元操作计数器Using the Primitive Operations Counter within a C# Program

若要检查 CCNOT 确实需要 7 T 入口,ApplySampleWithCCNOT 执行8个 T 入口,我们可以使用以下C#代码:To check that CCNOT indeed requires 7 T gates and that ApplySampleWithCCNOT executes 8 T gates we can 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);

程序的第一个部分执行 ApplySampleWithCCNOTThe first part of the program executes ApplySampleWithCCNOT. 在第二部分中,我们使用方法 QCTraceSimulator.GetMetric 获取 ApplySampleWithCCNOT所执行的 T 入口数:In the second part, we use the method QCTraceSimulator.GetMetric to get the number of T gates executed by ApplySampleWithCCNOT:

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

如果使用两个类型参数调用 GetMetric,它将返回与给定的调用图形边缘关联的度量值。When GetMetric is called with two type parameters it returns the value of the metric associated with a given call graph edge. 在我们的示例操作中 Primitive.CCNOTApplySampleWithCCNOT 中调用,因此调用关系图包含 <Primitive.CCNOT, ApplySampleWithCCNOT>的边缘。In our example operation Primitive.CCNOT is called within ApplySampleWithCCNOT and therefore the call graph contains the edge <Primitive.CCNOT, ApplySampleWithCCNOT>.

若要获取使用的 CNOT 入口数,可以添加以下行:To get the number of CNOT gates used, we can add the following line:

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

最后,若要输出按 CSV 格式的入口计数器收集的所有统计信息,我们可以使用以下内容:Finally, to output all the statistics collected by the gate counter in CSV format we can use the following:

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

另请参阅See also