microsoft / QuantumLibraries

Q# libraries for the Quantum Development Kit
https://docs.microsoft.com/quantum
MIT License
543 stars 179 forks source link

ToffoliSimulator outputs different result when executing `Adjoint ApplyAnd` #677

Open weucode opened 1 year ago

weucode commented 1 year ago

Describe the bug When running the following Q# program, the result of running on ToffoliSimulator is different from running on QuantumSimulator and SparseSimulator.

This behavior might be due to the different implementations among the three simulators. Since ToffoliSimulator does not support H gate, a specific version of ApplyAnd is designed on this page. However, when the values of t1, t2, qs are |1>, |1>, |0>, the return value of CCNOT(t1, t2, qs) is |1>, |1>, |1>, which violate the initial input rule that qs must be |0>, resulting in different outputs among three simulators.

To Reproduce

namespace Project {
    open Microsoft.Quantum.Intrinsic;
    open Microsoft.Quantum.Diagnostics;
    open Microsoft.Quantum.Canon;
    open Microsoft.Quantum.Measurement;
    open Microsoft.Quantum.Math;

    @EntryPoint()
    operation RunProgram() : Unit {
        use t1 = Qubit();
        X(t1);
        use t2 = Qubit();
        X(t2);
        use qs = Qubit();
        Adjoint ApplyAnd(t1, t2, qs);
        DumpMachine();
        ResetAll([t1,t2,qs]);
    }
}

Outputs The output of QuantumSimulator and SparseSimulator:

|3⟩ 1.000000 + 0.000000 i == ******************\ [ 1.000000 ] --- [ 0.00000 rad ]

The output of ToffoliSimulator:

Offset State Data ======== ========== 00000000 111

System information