Distinct 输入检查器Distinct Inputs Checker

Distinct Inputs Checker 是 "量程计算机跟踪模拟器" 的一部分。The Distinct Inputs Checker is a part of the quantum computer Trace Simulator. 它专用于检测代码中的潜在 bug。It is designed for detecting potential bugs in the code. 请考虑以下 Q # 代码部分,以说明此包检测到的问题:Consider the following piece of Q# code to illustrate the issues detected by this package:

operation ApplyBoth(
    q1 : Qubit,
    q2 : Qubit,
    op1 : (Qubit => Unit),
    op2 : (Qubit => Unit))
: Unit {
    op1(q1);
    op2(q2);
}

当用户查看此程序时,它们假设调用 op1op2 的顺序并不重要,因为 q1q2 是不同的 qubits 和操作在不同的 qubits 路程上操作。When the user looks at this program, they assume that the order in which op1 and op2 are called does not matter because q1 and q2 are different qubits and operations acting on different qubits commute. 现在,我们来看一个示例,其中使用了此操作:Let us now consider an example, where this operation is used:

operation ApplyWithNonDistinctInputs() : Unit {
    using (qubits = Qubit[3]) {
        let op1 = CNOT(_, qubits[1]);
        let op2 = CNOT(qubits[1], _);
        ApplyBoth(qubits[0], qubits[2], op1, op2);
    }
}

现在 op1op2 均使用部分应用程序获取,并共享 qubit。Now op1 and op2 are both obtained using partial application and share a qubit. 当用户在上面的示例中调用 ApplyBoth 时,操作的结果将取决于 ApplyBothop1op2 的顺序。When the user calls ApplyBoth in the example above the result of the operation will depend on the order of op1 and op2 inside ApplyBoth. 这无疑是用户预期会发生的情况。This is definitely not what the user would expect to happen. Distinct Inputs Checker 将在启用后检测到此类情况,并将引发 DistinctInputsCheckerExceptionThe Distinct Inputs Checker will detect such situations when enabled and will throw DistinctInputsCheckerException. 有关更多详细信息,请参阅DistinctInputsCheckerException上的 API 文档。See the API documentation on DistinctInputsCheckerException for more details.

在C#程序中使用不同的输入检查器Using the Distinct Inputs Checker in your C# Program

下面是在启用 Distinct Inputs Checker 的C#情况下使用量程计算机跟踪模拟器的驱动程序代码示例:The following is an example of C# driver code for using the quantum computer trace simulator with the Distinct Inputs Checker enabled:

using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Simulation.Simulators;
using Microsoft.Quantum.Simulation.Simulators.QCTraceSimulators;

namespace Quantum.MyProgram
{
    class Driver
    {
        static void Main(string[] args)
        {
            var traceSimCfg = new QCTraceSimulatorConfiguration();
            traceSimCfg.useDistinctInputsChecker = true; //enables distinct inputs checker
            QCTraceSimulator sim = new QCTraceSimulator(traceSimCfg);
            var res = MyQuantumProgram.Run().Result;
            System.Console.WriteLine("Press any key to continue...");
            System.Console.ReadKey();
        }
    }
}

QCTraceSimulatorConfiguration 存储量程计算机跟踪模拟器的配置,并可作为 QCTraceSimulator 构造函数的参数提供。The class QCTraceSimulatorConfiguration stores the configuration of the quantum computer trace simulator and can be provided as an argument for the QCTraceSimulator constructor. 如果 useDistinctInputsChecker 设置为 true,则 Distinct Inputs Checker 处于启用状态。When useDistinctInputsChecker is set to true the Distinct Inputs Checker is enabled. 有关更多详细信息,请参阅QCTraceSimulatorQCTRACESIMULATORCONFIGURATION上的 API 文档。See the API documentation on QCTraceSimulator and QCTraceSimulatorConfiguration for more details.

另请参阅See also