在 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,但我不知道如何使该函数返回诸如“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,顾名思义,只返回一个 Int。我需要返回两个整数。

感谢任何帮助,谢谢!

解决方法

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

所以你的操作和返回语句的签名是

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);
    }

    
}