qdk icon indicating copy to clipboard operation
qdk copied to clipboard

More actionable error messages for common mistakes

Open minestarks opened this issue 2 years ago • 0 comments

I'm compiling these into a single issue for ease of triage, but can break them out into separate issues.

These mistakes are all taken from code generated by GPT-4 with the prompt "give me 100 Q# code samples". The reason I think these are worth considering is that I think they represent typical user expectations (based on older versions of Q# or just other programming languages). Subjectively, these were all non-obvious to me as a Q# beginner.

1. Operation defined at top-level instead of in a namespace

Code

operation Foo() : Unit {
}

playground link

Current error message

syntax error: expected EOF, found identifier

Suggestion for improvement

"operations must be declared within a namespace" or similar

An accompanying quick fix could wrap the whole file in a namespace.

2. let q = Qubit() syntax

Code

namespace Foo {
    operation Bar() : Unit {
        let q = Qubit();
    }
}

playground link

Current error message

name error: Qubit not found in this scope

Suggestion for improvement

"use the use q = Qubit() syntax to allocate a qubit" or similar

An accompanying quick fix could change let to use.

3. Use of the default-initialized array constructor (new Qubit[n])

Code

namespace Foo {
    operation Bar() : Unit {
        use qs = new Qubit[1];
    }
}

playground link

Current error message

syntax error: expected qubit initializer, found identifier

Suggestion for improvement

"This syntax is deprecated, use the use q = Qubit[n] syntax instead" or similar

A quick fix could remove the new keyword.

4. Parentheses around for loop header

Code

namespace Foo {
    operation Bar() : Unit {
        for (i in 0..1) {
        }
    }
}

playground link

Current error message

syntax error: expected ), found identifier

Suggestion for improvement

"Do not use parentheses in for loop headers. Use for x in 0..1 syntax instead" or similar

A quick fix could remove the parentheses

5. Allocating a qubit and setting an array element in the same line

Code

namespace Foo {
    operation Bar() : Unit {
        use qs = Qubit[1];
        set qs[0] = Qubit();
    }
}

playground link

Current error message

name error: Qubit not found in this scope

Suggestion for improvement

I'm actually not exactly sure why this is an error, but the error message could explain why.

A quick fix could add a line allocating a qubit above the statement, and use that temporary variable when assigning to the array element, e.g. use q = Qubit(); set qs[0] = q

minestarks avatar May 22 '23 19:05 minestarks