Search code examples
azurequantum-computingq#

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


So, basically, I did the tutorial to create a random number on the website of Microsoft Azure and now I am trying to add some functionalities, including their suggestion add a minimum number.

The initial code to generate just one number, max, is:

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

Everything works well. Now, I want to generate two numbers so I would like to create a function TwoSampleRandomNumbersInRange but I can't figure out how to make the function return a result such as "Int, Int", I tried a few things including the follow:

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

To generate two numbers, I tried this:

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

...but the syntax for the output isn't right.

I also need the output:

set output = ResultArrayAsInt(bits);

to have two numbers but ResultArrayAsInt, as the name says, just returns an Int. I need to return two integers.

Any help appreciated, thanks!


Solution

  • The return of an operation has to be a data type, in this case to represent a pair of integers you need a tuple of integers: (Int, Int).

    So the signature of your operation and the return statement will be

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