Quantum trace simulator: distinct inputs checker

The distinct inputs checker is a part of the Quantum Development Kit Quantum trace simulator. You can use it to detect potential bugs in the code caused by conflicts with shared qubits.

Conflicts with shared qubits

Consider the following piece of Q# code to illustrate the issues detected by the distinct inputs checker:

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

When you look at this program, you can assume that the order in which it calls op1 and op2 does not matter, because q1 and q2 are different qubits and operations acting on different qubits commute.

Now, consider this example:

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

Note that op1 and op2 are both obtained using partial application and share a qubit. When you call ApplyBoth in this example, the result of the operation depends on the order of op1 and op2 inside ApplyBoth - not what you would expect to happen. When you enable the distinct inputs checker, it detects such situations and throws a DistinctInputsCheckerException. For more information, see DistinctInputsCheckerException in the Q# API library.

Invoking the distinct inputs checker

To run the quantum trace simulator with the distinct inputs checker you must create a QCTraceSimulatorConfiguration instance, set the UseDistinctInputsChecker property to true, and then create a new QCTraceSimulator instance with QCTraceSimulatorConfiguration as the parameter.

var config = new QCTraceSimulatorConfiguration();
config.UseDistinctInputsChecker = true;
var sim = new QCTraceSimulator(config);

Using the distinct inputs checker in a C# host program

The following is an example of C# host program that uses the quantum 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();
        }
    }
}

See also