Return Q Sharp (Q#)(量子开发工具包)中的两个数字

Return two numbers in Q Sharp (Q#) (Quantum Development Kit)

所以,基本上,我在 Microsoft Azure 的网站上完成了创建随机数的教程,现在我正在尝试添加一些功能,包括他们的建议添加最小数量。

仅生成一个数字 max 的初始代码是:

   operation SampleRandomNumberInRange(max : Int) : Int {
        // mutable means variables that can change during computation
        mutable output = 0;
        // repeat loop to generate random numbers until it generates one that is less or equal to max
        repeat {
            mutable bits = new Result[0];
            for idxBit in 1..BitSizeI(max) {
                set bits += [GenerateRandomBit()];
            }
            // ResultArrayAsInt is from Microsoft.Quantum.Convert library, converts string to positive integer
            set output = ResultArrayAsInt(bits);
        } until (output <= max);
        return output;
    }

    @EntryPoint()
    operation SampleRandomNumber() : Int {
        // let declares var which don't change during computation
        let max = 50;
        Message($"Sampling a random number between 0 and {max}: ");
        return SampleRandomNumberInRange(max);
    }

一切正常。现在,我想生成两个数字,所以我想创建一个函数 TwoSampleRandomNumbersInRange 但我不知道如何使函数 return 成为诸如“Int,Int”之类的结果,我尝试了一些方法,包括以下:

    operation TwoSampleRandomNumbersInRange(min: Int, max : Int) : Int {
        // mutable means variables that can change during computation
        mutable output = 0;
        // repeat loop to generate random numbers until it generates one that is less or equal to max
        repeat {
            mutable bits = new Result[0];
             
            for idxBit in 1..BitSizeI(max) {
                set bits += [GenerateRandomBit()];
            }

            for idxBit in 1..BitSizeI(min) {
                set bits += [GenerateRandomBit()];
            }
            // ResultArrayAsInt is from Microsoft.Quantum.Convert library, converts string to positive integer
            set output = ResultArrayAsInt(bits);
        } until (output >= min and output <= max);
        return output;
    }

为了生成两个数字,我试过这个:

operation TwoSampleRandomNumbersInRange(min: Int, max : Int) : Int, Int {
//code here
}

...但是输出的语法不正确。

我还需要输出:

set output = ResultArrayAsInt(bits);

有两个数字,但 ResultArrayAsInt,顾名思义,只是 return 一个 Int。我需要 return 两个整数。

感谢任何帮助,谢谢!

操作的 return 必须是数据类型,在这种情况下,要表示一对整数,您需要整数的 元组 (Int, Int).

所以你的操作签名和return声明将是

operation TwoSampleRandomNumbersInRange(min: Int, max : Int) : (Int, Int) {
    // code here
    return (integer1, integer2);
}

我找到了我自己问题的答案,我所要做的就是:

    operation SampleRandomNumberInRange(min: Int, max : Int) : Int {
        // mutable means variables that can change during computation
        mutable output = 0;
        // repeat loop to generate random numbers until it generates one that is less or equal to max
        repeat {
            mutable bits = new Result[0];
            for idxBit in 1..BitSizeI(max) {
                set bits += [GenerateRandomBit()];
            }
            // ResultArrayAsInt is from Microsoft.Quantum.Convert library, converts string to positive integer
            set output = ResultArrayAsInt(bits);
        } until (output >= min and output <= max);
        return output;
    }

    @EntryPoint()
    operation SampleRandomNumber() : Int {
        // let declares var which don't change during computation
        let max = 50;
        let min = 10;
        Message($"Sampling a random number between {min} and {max}: ");
        return SampleRandomNumberInRange(min, max);
    }

    
}