Consider the following code on which I want to run the resource estimator:


open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Arrays;

operation AlgoTest() : Unit  {
    let n=3; // Number of qubits
    use q = Qubit[n];
    mutable resultArray = [Zero, size = 3];
    for i in IndexRange(q) {
        set resultArray w/= i <- M(q[i]);
        // If I replace preceeding line by this one: compilation fails 
        // M(q[i]);

As indicated by the comment, calling "qsharp.azure.execute(AlgoTest)" after works, but if I replace the line set resultArray w/= i <- M(q[i]); by M(q[i]); it returns me the following error:

fail: Microsoft.Quantum.IQSharp.Snippets[0] QS0005 (/snippet_.qs:Microsoft.VisualStudio.LanguageServer.Protocol.Range): This expression has type Result and its value is implicitly ignored. Use "let _ = expr;" or "Ignore(expr);" to discard the value explicitly. /snippet_.qs(18,17): error QS0005: This expression has type Result and its value is implicitly ignored. Use "let _ = expr;" or "Ignore(expr);" to discard the value explicitly.

My understanding is that M(q[i]); is returning a value which is not stored (and for some reason Q# doesn't like it). The error suggests me to replace M(q[i]); by Ignore(M(q[i])); (and indeed in such case it works).

My question is: Would replacing M(q[i]); by Ignore(M(q[i])); indeed allow us to perform the correct resource estimate at the end of the day? Would it provide me with a resource estimate for an algorithm on which a layer of Hadamard has been implemented, followed by a CNOT between qubits 0 & 1, followed by a measurement in the $Z$ basis of all qubits? Or, for some reason, because of the command "Ignore" it will not do a resource estimation on this.

The documentation only says that the value of the measurement is ignored but does it have any influence on the resource estimate (such as the measurements are not taken into account in the estimate)?

The motivation behind my questions is to understand the philosophy of Q# on simple examples.


1 Answer 1


Q# generally doesn't allow you to just abandon an expression, you have to do something with it - assign it to a variable (which is what you do with resultArray) or pass it to an operation/function (which is what Ignore allows you to do). This is done to help you find bugs in the code - if you do a measurement, you probably meant to use the results but forgot to. (You can, of course, assign it to a variable and then promptly forget to use the variable...)

Resource estimation is independent of that, it doesn't care what you do with measurement results, so as long as you have a measurement (and the code compiles), it will be accounted for.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.