microsoft / QuantumLibraries

Q# libraries for the Quantum Development Kit

Home Page:https://docs.microsoft.com/quantum

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error using DivideI

KMcGoldrick opened this issue · comments

[Enter feedback here]
I get the error "/snippet_.qs(59,13): error QS5022: No identifier with the name "DivideI" exists."

when using DivideI in the following code
open Microsoft.Quantum.Arithmetic;

within{
ApplyXorInPlace(dividend, xs);
DivideI(xs, ys, result);
ApplyToEachA(X, xs!);
}
apply{
Controlled X(xs!, target);
}


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Hi @KMcGoldrick, DivideI is in the Microsoft.Quantum.Numerics package that you would need to include. It seems that you are using IQ#. There you can include the package with the following line:

%package Microsoft.Quantum.Numerics

Thanks @msoeken,

I tried the recommendation, by adding the last open statement.
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Measurement;
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Arithmetic;
open Microsoft.Quantum.Arrays;
open Microsoft.Quantum.Preparation;
open Microsoft.Quantum.Numerics;

This resulted in the following errors:

/snippet_.qs(9,10): error QS6104: No namespace with the name "Microsoft.Quantum.Numerics" exists.
/snippet_.qs(62,13): error QS5022: No identifier with the name "DivideI" exists.

Below is the full code:
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Measurement;
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Arithmetic;
open Microsoft.Quantum.Arrays;
open Microsoft.Quantum.Preparation;
//open Microsoft.Quantum.Numerics;

@EntryPoint()
operation FactorizeWithGrovers(number : Int) : Unit {
    Message($"Kevin's FactorizeWithGrover's.");       

    // Print out the answer.
    // Define the oracle that for the factoring problem.
    
    let markingOracle = MarkDivisor(number, _, _);
    let phaseOracle = ApplyMarkingOracleAsPhaseOracle(markingOracle, _);
    // Bit-size of the number to factorize.
    let size = BitSizeI(number);
    // Estimate of the number of solutions.
    let nSolutions = 4;
    // The number of iterations can be computed using the formula.
    let nIterations = Round(PI() / 4.0 * Sqrt(IntAsDouble(size) / IntAsDouble(nSolutions)));

    // Initialize the register to run the algorithm
    use (register, output) = (Qubit[size], Qubit());
    mutable isCorrect = false;
    mutable answer = 0;
    // Use a Repeat-Until-Succeed loop to iterate until the solution is valid.
    repeat {
        RunGroversSearch(register, phaseOracle, nIterations);
        let res = MultiM(register);
        set answer = BoolArrayAsInt(ResultArrayAsBoolArray(res));
        // See if the result is a solution with the oracle.
        markingOracle(register, output);
        if MResetZ(output) == One and answer != 1 and answer != number {
            set isCorrect = true;
        }
        ResetAll(register);
        //Message($"Repeat....Until");
    } until isCorrect;

    // Print out the answer.
    Message($"The number {answer} is a factor of {number}.");

}

operation MarkDivisor (
    dividend : Int,
    divisorRegister : Qubit[],
    target : Qubit
) : Unit is Adj+Ctl {
    let size = BitSizeI(dividend);
    use (dividendQubits, resultQubits) = (Qubit[size], Qubit[size]);
    let xs = LittleEndian(dividendQubits);
    let ys = LittleEndian(divisorRegister);
    let result = LittleEndian(resultQubits);
    within{
        ApplyXorInPlace(dividend, xs);
        DivideI(xs, ys, result);
        ApplyToEachA(X, xs!);
    }
    apply{
        Controlled X(xs!, target);
    }
}

operation PrepareUniformSuperpositionOverDigits(digitReg : Qubit[]) : Unit is Adj + Ctl {
    PrepareArbitraryStateCP(ConstantArray(10, ComplexPolar(1.0, 0.0)), LittleEndian(digitReg));
}

operation ApplyMarkingOracleAsPhaseOracle(
    markingOracle : (Qubit[], Qubit) => Unit is Adj,
    register : Qubit[]
) : Unit is Adj {
    use target = Qubit();
    within {
        X(target);
        H(target);
    } apply {
        markingOracle(register, target);
    }
}

operation RunGroversSearch(register : Qubit[], phaseOracle : ((Qubit[]) => Unit is Adj), iterations : Int) : Unit {
    ApplyToEach(H, register);
    for _ in 1 .. iterations {
        phaseOracle(register);
        ReflectAboutUniform(register);
    }
}

operation ReflectAboutUniform(inputQubits : Qubit[]) : Unit {
    within {
        ApplyToEachA(H, inputQubits);
        ApplyToEachA(X, inputQubits);
    } apply {
        Controlled Z(Most(inputQubits), Tail(inputQubits));
    }
}

Thanks for any assistance you can provide.

The DivideI operation is in the Microsoft.Quantum.Arithmetic namespace, but in the Microsoft.Quantum.Numerics package. In IQ# you need to reference to the package by

%package Microsoft.Quantum.Numerics

or in a .NET environment include it in your .csproj file. Please let me know if this resolves the problem.

Got it resolved. Thanks