Quantum trace simulator: invalidated qubits use checker

The invalidated qubits use 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 invalid qubits.

Invalid qubits

Consider the following piece of Q# code to illustrate the issues detected by the invalidated qubits use checker:

operation UseReleasedQubit() : Unit {
    mutable q = new Qubit[1];
    using (ans = Qubit()) {
        set q w/= 0 <- ans;
    }
    H(q[0]);
}

When you apply the H operation to q[0], it points to an already released qubit, which can cause undefined behavior. When the Invalidated Qubits Use Checker is enabled, it throws the exception InvalidatedQubitsUseCheckerException if the program applies an operation to an already released qubit. For more information, see InvalidatedQubitsUseCheckerException.

Invoking the invalidated qubits use checker

To run the quantum trace simulator with the invalidated qubits use checker you must create a QCTraceSimulatorConfiguration instance, set the UseInvalidatedQubitsUseChecker property to true, and then create a new QCTraceSimulator instance with QCTraceSimulatorConfiguration as the parameter.

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

Using the invalidated qubits use checker in a C# host program

The following is an example of C# host programs that uses the quantum trace simulator with the invalidated qubits use 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.UseInvalidatedQubitsUseChecker = true; // enables UseInvalidatedQubitsUseChecker
            QCTraceSimulator sim = new QCTraceSimulator(traceSimCfg);
            var res = MyQuantumProgram.Run().Result;
            System.Console.WriteLine("Press any key to continue...");
            System.Console.ReadKey();
        }
    }
}

See also