Eliminate redundant round-trip casts in OpenQASM-to-Q# compilation#2990
Merged
minestarks merged 3 commits intomainfrom Mar 9, 2026
Merged
Eliminate redundant round-trip casts in OpenQASM-to-Q# compilation#2990minestarks merged 3 commits intomainfrom
minestarks merged 3 commits intomainfrom
Conversation
minestarks
commented
Mar 4, 2026
orpuente-MS
reviewed
Mar 5, 2026
Contributor
|
@minestarks I've opened a new pull request, #3004, to work on those changes. Once the pull request is ready, I'll request review from you. |
The `Type` enum in `qsc_openqasm_compiler` carried a `bool` const
qualifier on most variants (e.g. `Int(bool)`, `Result(bool)`,
`BoolArray(ArrayDimensions, bool)`). Since Q# has no const bindings,
this flag was never meaningful in the Q# type representation and only
added noise — requiring a dedicated `without_const()` method just to
normalize types for equality comparisons.
## Changes
- **`types.rs`**: Strip the `bool` const parameter from all `Type`
variants; remove `without_const()`.
- **`compiler.rs`**:
- `semantic_type_for_qsharp_type`: no longer extracts or forwards
`is_const` into `Type` construction.
- `make_qsharp_array_ty`: removes hardcoded `false` const args.
- `maps_to_same_qsharp_type`: simplifies to direct `==` comparison (the
note about ignoring const qualifiers is now moot).
- **`ast_builder.rs`**: `map_qsharp_type_to_ast_ty` updated to match new
variant shapes.
```rust
// Before
pub enum Type {
Int(bool),
Result(bool),
ResultArray(ArrayDimensions, bool),
// ...
}
// After
pub enum Type {
Int,
Result,
ResultArray(ArrayDimensions),
// ...
}
```
Constness is still read from the OpenQASM parser symbol
(`symbol.ty.is_const()`) where it matters — to decide Q# declaration
mutability (`let` vs `mutable`) — it just no longer flows into the
intermediate `Type` representation.
<!-- START COPILOT CODING AGENT TIPS -->
---
🔒 GitHub Advanced Security automatically protects Copilot coding agent
pull requests. You can protect all pull requests by enabling Advanced
Security for your repositories. [Learn more about Advanced
Security.](https://gh.io/cca-advanced-security)
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: minestarks <16928427+minestarks@users.noreply.github.com>
orpuente-MS
approved these changes
Mar 9, 2026
github-merge-queue bot
pushed a commit
that referenced
this pull request
Mar 23, 2026
This is a bug we discovered while working on #2990 . It doesn't have any user-visible effects (since constness is ignored when transpiling to Q#), but it impacts the corectness of the internal OpenQASM AST. `try_promote_bitarray_to_int` determines the result type when a binary operation involves an `int`/`uint` and a `bit[N]`. It used `.clone()` on whichever operand was already an integer type, which blindly inherited that operand's const qualifier. This meant an expression like `const int + bit[4]` would produce `const int` instead of `int` — the non-const `bit[4]` operand was ignored for constness. Co-authored-by: Mine Starks <>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2987
Bitwise operations on
bittypes (^,&,|) were generating redundant nested casts in the Q# output. The OpenQASM semantic lowerer inserts implicitBit → UInt(1)andUInt(1) → Bitcasts around the operands and result, which the compiler was faithfully translating intoIntAsResult(ResultAsInt(...))wrappers — a no-op round-trip.Before:
After: