microsoft / qsharp-language

Official repository for design of the quantum programming language Q# and its core libraries
MIT License
235 stars 56 forks source link

Resources calculation for quantum arithmetic functions #121

Closed rajkk1 closed 2 years ago

rajkk1 commented 2 years ago

Hello!

I have set up and ran some basic resource estimations from the tutorial for the Apply Majority algorithm on Q# Jupyter notebooks using e.g.

// |x1 x2 x3>|y> -> |x1 x2 x3>|y + MAJ(x1, x2, x3)>
operation ApplyMajorityUsingCNOTTransformation(x1: Qubit, x2: Qubit, x3: Qubit, y: Qubit): Unit is Adj + Ctl {
    within {
        CNOT(x2, x1);
        CNOT(x2, x3);
    } apply {
        CCNOT(x1, x3, y);
        CNOT(x2, y);
    }
}
operation UsingCNOTTTransformation() : Unit {
    use (x1, x2, x3, y) = (Qubit(), Qubit(), Qubit(), Qubit()) {
        ApplyMajorityUsingCNOTTransformation(x1, x2, x3, y);
    }
}

%estimate UsingCNOTTTransformation

I was hoping to try out the same for some quantum arithmetic functions (e.g. AddFxp, some multiplication functions, etc.) but I’m having some trouble understanding how to implement them. Are there any good resources describing how to implement a simple fixed point addition function (and count the resources?)

Thank you!

msoeken commented 2 years ago

Hi @rajkk1, thanks for your question. For AddFxP you could do the following in Jupyter notebooks

%package Microsoft.Quantum.Numerics
open Microsoft.Quantum.Arithmetic;

operation EstimateAddFxP() : Unit {
  use a = Qubit[6];
  use b = Qubit[6];

  // fixed point representation with 4 bits before and 2 bits after the period
  let fpA = FixedPoint(4, a);
  let fpB = FixedPoint(4, b);

  // Add fpA into fpB
  AddFxP(fpA, fpB);
}
%estimate EstimateAddFxP

You can also find a Resources Estimation notebook for arithmetic in the Numerics package in the index of https://aka.ms/try-qsharp. The underlying sample can be found in our Samples repository.

Let me know if you have further questions.

rajkk1 commented 2 years ago

Thanks for the reply! When running %package Microsoft.Quantum.Metrics I obtain the output

Adding package Microsoft.Quantum.Numerics: done!
Microsoft.Quantum.Standard::0.20.2110171573
Microsoft.Quantum.Standard.Visualization::0.20.2110171573
Microsoft.Quantum.Numerics::0.20.2110171573

which seems reasonable. However when running the next block I get

C:\snippet_.qs(8,13): error QS3213: Invalid initializer expression. Possible initializers are "Qubit()", "Qubit[expr]", or tuples thereof.
C:\snippet_.qs(9,13): error QS3213: Invalid initializer expression. Possible initializers are "Qubit()", "Qubit[expr]", or tuples thereof.

Thanks for the help!

msoeken commented 2 years ago

@rajkk1, I had a mistake in the example code which I now fixed and also tested. I have updated my initial answer (there were two use statements which need to be let statements).

rajkk1 commented 2 years ago

Ah neat, thank you for the change and your timely help! Works great now :)