microsoft / qsharp-compiler

Q# compiler, command line tool, and Q# language server
https://docs.microsoft.com/quantum
MIT License
684 stars 172 forks source link

Generated QIR is not valid when doing multi-qubit measurement inside a repeat statement #1581

Closed cesarzc closed 1 year ago

cesarzc commented 1 year ago

Describe the bug

Generated QIR is not valid when doing multi-qubit measurement inside a repeat statement.

To Reproduce

Steps to reproduce the behavior:

  1. Compiling a Q# project with the following code and setting the projects <QirGeneration>true</QirGeneration> property generates QIR that fails verification:

    namespace Issue {
    open Microsoft.Quantum.Intrinsic;
    open Microsoft.Quantum.Measurement;
    
    @EntryPoint()
    operation Main() : Int {
        use register = Qubit[5];
        mutable count = 0;
        repeat {
            let results = MultiM(register);
            if (results[0] == Zero)
            {
                set count += 1;
            }
    
        } until (count < 5); 
    
        return count;
    }
    }
  2. After the QIR code is generated, use the llvm-as command on to generate bitcode: llvm-as-13 qir/Program.ll -o qir/Program.bc

  3. The following error is produced:

    llvm-as-13: assembly parsed, but does not verify as correct!
    Instruction does not dominate all uses!
    %10 = call i64 @__quantum__rt__array_get_size_1d(%Array* %results)
    %9 = sub i64 %10, 1