Lint rule: When qubit reuse is not supported, prefer `MResetZ` over `M`
- Dominant language
- Rust
- Stars
- 1k
- Forks
- 212
- Avg merge
- 3d 8h
- Merged PRs (30d)
- 65
Description
We should add a rule to the linter that suggests that calls to `M` are replaced by `MResetZ` when the currently selected target's capabilities don't include `QubitReuse`.
```qsharp
use q = Qubit();
H(q);
// When Profile=Unrestricted, the qubit is reused
// When Profile=Base, this allocates a new hardware qubit
// Using MResetZ would make the behavior less ambiguous
let r = M(q);
```
[Playground link](https://microsoft.github.io/qsharp/?code=H4sIAAAAAAAACnWQUUvDUAyF3%2FsrzmMLsr07BiIqvhS2wRAce8jarA22925JrkPG%2Frus6nCCeUtyzhdOAvVsO6oYTzHimGUAEHccUEql0eLWR%2FNEwVM%2FehBqQjSXyiaD7u4xuH7MogTPix%2BnkksMKElCXuAWC7bU%2BWqN46A4VzLGHlPM00Y8LyaXxXO%2BLybZpR2P8dJywEzjVjqeLoOyuUrlXN%2FAW8b%2BTIAYlJNx%2Fa%2FznozPDjFQ18WKnA2EwAe0pPWB9Jv1m7A0CQ3KBRv7Kw4xdTV6euPh8oZbepeo6NgM1G%2BkkZjs4u%2FYoZiivI70J%2BHAvh4pe9KAla6%2F%2FnLKTtknPZnyfKYBAAA%3D&profile=base)
In this code, the linter could display a warning under `M` to the effect of "the currently chosen target does not support qubit use after measurement, prefer MResetZ".
----
**CONTRIBUTORS PLEASE READ**
**Getting started**
Welcome! Please take a look through our [README](https://github.com/microsoft/qsharp/blob/main/README.md) to orient yourself in the repo and find instructions on how to build.
For this issue you'll want to have a working knowledge of Rust and compilers.
For documentation on how to add lints, see:
https://github.com/microsoft/qsharp/blob/main/compiler/qsc_linter/src/lib.rs
For examples of existing lints, see:
https://github.com/microsoft/qsharp/blob/ed2a8fcab06acea8e2b8fcaf64f109bbd979f403/compiler/qsc_linter/src/lints/ast.rs#L10-L12
https://github.com/microsoft/qsharp/blob/ed2a8fcab06acea8e2b8fcaf64f109bbd979f403/compiler/qsc_linter/src/lints/hir.rs#L8
**Testing**
You can demonstrate that the lint works by running the playground locally (see the code example in the issue description).
Please add unit tests verifying the functionality you implemented.
Before you submit a pull request please run `python ./build.py` to ensure the project builds cleanly. See README for details.
**Reviews**
Once you have published your PR, the codeowners will automatically get tagged and we'll review shortly.
If you need clarification on an issue please tag @minestarks with your questions.
Contributor guide
Research direction
Start with compiler/qsc_linter/src/lib.rs and the existing examples in compiler/qsc_linter/src/lints/ast.rs and hir.rs. Review how lints inspect target capabilities, then add unit tests for M when QubitReuse is unavailable. Done means the linter warns under M to prefer MResetZ, and python ./build.py completes successfully.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 38/100