深度计数器Depth Counter

Depth Counter 是 "量程计算机跟踪模拟器" 的一部分。The Depth Counter is a part of the quantum computer Trace Simulator. 它用于收集量程程序中调用的每个操作的深度计数。It is used to gather counts of the depth of 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. 用户可以通过 QCTraceSimulatorConfiguration的 "gateTimes" 字段设置每个基元操作的深度。Users can set the depth for each of the primitive operations via the gateTimes field of QCTraceSimulatorConfiguration.

默认情况下,除了深度为1的 T 门外,所有操作的深度为0。By default, all operations have depth 0 except the T gate which has depth 1. 这意味着,在默认情况下,只计算 T 操作的 T 深度(通常是必需的)。This means that by default, only the T depth of operations is computed (which is often desirable). 收集的统计信息在操作调用关系图的所有边缘上聚合在一起。Collected statistics are aggregated over all the edges of the operations call graph.

现在,我们来计算 CCNOT 操作的 T 深度。Let us now compute the T depth of the CCNOT operation. 我们将使用下面的 Q # 示例代码:We will use the following Q# sample code:

open Microsoft.Quantum.Intrinsic;

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

在C#程序中使用深度计数器Using Depth Counter within a C# Program

若要检查 CCNOT 是否具有 T 深度5并且 ApplySampleWithCCNOT 具有 T 深度6,我们可以使用C#以下代码:To check that CCNOT has T depth 5 and ApplySampleWithCCNOT has T depth 6 we can use the following C# code:

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

double tDepth = sim.GetMetric<Intrinsic.CCNOT, ApplySampleWithCCNOT>(DepthCounter.Metrics.Depth);
double tDepthAll = sim.GetMetric<ApplySampleWithCCNOT>(DepthCounter.Metrics.Depth);

程序的第一个部分执行 ApplySampleWithCCNOTThe first part of the program executes ApplySampleWithCCNOT. 在第二部分中,我们使用方法 QCTraceSimulator.GetMetric 获取 CCNOTApplySampleWithCCNOTT 深度:In the second part, we use the method QCTraceSimulator.GetMetric to get the T depth of CCNOT and ApplySampleWithCCNOT:

double tDepth = sim.GetMetric<Intrinsic.CCNOT, ApplySampleWithCCNOT>(DepthCounter.Metrics.Depth);
double tDepthAll = sim.GetMetric<ApplySampleWithCCNOT>(DepthCounter.Metrics.Depth);

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

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

另请参阅See also