Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reimplement Parser #2149

Draft
wants to merge 94 commits into
base: main
Choose a base branch
from
Draft

Reimplement Parser #2149

wants to merge 94 commits into from

Conversation

idavis
Copy link
Collaborator

@idavis idavis commented Feb 4, 2025

Lexing

  • Basic raw tokens
  • Basic cooked tokens
  • Cook pragma and annotation

Parsing

Lowering

Compiling

Fit and Finish

  • Move Q# related semantic errors from Lowerer to Compiler
  • Add better error messages to const evaluator. We can pass &mut Lowerer as an argument so that it can push semantic errors. Better error messages for const evaluator #2277
  • Review compiler error messages
  • Review parser error messages
  • Review lexer error messages
  • Delete old compiler internals that are no longer used and remove stale refs
  • Benchmark parser

Postponed

  • Update ast_builder calls with module names instead of old ns names
  • bit x = 0; bit y = ~x; is valid QASM3, but it gets compiled to let x = Zero; let y = ~~~x; which is invalid Q# code, since the Result type in Q# doesn't support unary bitwise negation. This is the same for all other operations that bit should support, Q# Result only supports equality.
  • We are currently not enforcing that qasm3 uint types must be positive, since they are compiled to Q# Int which are signed. We might need a UInt type similar to the Angle type introduced in Add angle support to the new compiler #2267 to be able to enforce this constraint.
  • uint 63 bitness with 64 bigint. Separate ops for uint?
  • Lower Ident as Rc<Symbol> and not as SymbolId to avoid an unnecessary SymbolTable lookup? In that way we don't even need to pass the symbol table to the compiler.
  • Use the formal parameters' SymbolIds when creating the function type, so that we can give the user a better error message when they pass an argument that fails implicit casting to a function. There is a catch: we need to insert the function symbol into the symbol table before pushing the scope where the formal parameters live, but we need the SymbolIds of the formal parameters to construct the function symbol.

@idavis idavis requested a review from orpuente-MS February 4, 2025 02:11
@idavis idavis self-assigned this Feb 4, 2025
Index,
}

// TODO: This seems to be an unnecessary wrapper.

Check notice

Code scanning / devskim

A "TODO" or similar was left in source code, possibly indicating incomplete functionality Note

Suspicious comment
Keyword(Keyword),
}

// TODO: This seems to be an unnecessary wrapper.

Check notice

Code scanning / devskim

A "TODO" or similar was left in source code, possibly indicating incomplete functionality Note

Suspicious comment
rc::Rc,
};

// TODO: Profile this with iai-callgrind in a large OpenQASM3

Check notice

Code scanning / devskim

A "TODO" or similar was left in source code, possibly indicating incomplete functionality Note

Suspicious comment
@idavis idavis force-pushed the feature/qasm3 branch 2 times, most recently from eb82b24 to b803573 Compare March 18, 2025 21:48

let ast_ty = map_qsharp_type_to_ast_ty(&output_ty);
signature.output = format!("{output_ty}");
// TODO: This can create a collision on multiple compiles when interactive

Check notice

Code scanning / devskim

A "TODO" or similar was left in source code, possibly indicating incomplete functionality Note

Suspicious comment

// input decls should have been pushed to symbol table,
// but should not be the stmts list.
// TODO: This may be an issue for tooling as there isn't a way to have a forward

Check notice

Code scanning / devskim

A "TODO" or similar was left in source code, possibly indicating incomplete functionality Note

Suspicious comment
)
}

// TODO: which these are parsed as different types, they are effectively the same

Check notice

Code scanning / devskim

A "TODO" or similar was left in source code, possibly indicating incomplete functionality Note

Suspicious comment
Type::BitArray(..) => {
rewrap_lit!(lit, Bitstring(val, _), Int(i64::try_from(val).ok()?))
}
// TODO: UInt Overflowing behavior.

Check notice

Code scanning / devskim

A "TODO" or similar was left in source code, possibly indicating incomplete functionality Note

Suspicious comment
// already is a i64. Therefore, there is nothing to do?
Type::Int(..) | Type::UInt(..) => Some(lit),
Type::Float(..) => rewrap_lit!(lit, Float(val), {
// TODO: we need to issue the same lint in Q#.

Check notice

Code scanning / devskim

A "TODO" or similar was left in source code, possibly indicating incomplete functionality Note

Suspicious comment
Type::BitArray(..) => {
rewrap_lit!(lit, Bitstring(val, _), Int(i64::try_from(val).ok()?))
}
// TODO: Int Overflowing behavior.

Check notice

Code scanning / devskim

A "TODO" or similar was left in source code, possibly indicating incomplete functionality Note

Suspicious comment
// same result anyways. Need to think through this.
Type::Int(..) | Type::UInt(..) => Some(lit),
Type::Float(..) => rewrap_lit!(lit, Float(val), {
// TODO: we need to issue the same lint in Q#.

Check notice

Code scanning / devskim

A "TODO" or similar was left in source code, possibly indicating incomplete functionality Note

Suspicious comment
This PR completes the lowering and compilation for:
 - [x] gate definitions
 - [x] gphase
 - [x] for stmt
 - [x] if stmt
 - [x] switch stmt
 - [x] return stmt
 - [x] classical function calls
 - [x] classical function decls

It also does some partial work to make the compiler infallible, by
returning `ExprKind::Err` instead of None when compiling expressions.
orpuente-MS and others added 3 commits March 27, 2025 11:37
Add implicit casts to function and gate arguments.
This PR adds angle support to the new compiler.

---------

Co-authored-by: Ian Davis <[email protected]>
}
Type::Float(..) => {
rewrap_lit!((lhs, rhs), (Float(lhs), Float(rhs)), {
// TODO: we need to issue the same lint in Q#.

Check notice

Code scanning / devskim

A "TODO" or similar was left in source code, possibly indicating incomplete functionality Note

Suspicious comment
}
Type::Float(..) => {
rewrap_lit!((lhs, rhs), (Float(lhs), Float(rhs)), {
// TODO: we need to issue the same lint in Q#.

Check notice

Code scanning / devskim

A "TODO" or similar was left in source code, possibly indicating incomplete functionality Note

Suspicious comment
match ty {
Type::Bool(..) => rewrap_lit!(lit, Bool(val), Float(if val { 1.0 } else { 0.0 })),
Type::Int(..) | Type::UInt(..) => rewrap_lit!(lit, Int(val), {
// TODO: we need to issue the same lint in Q#.

Check notice

Code scanning / devskim

A "TODO" or similar was left in source code, possibly indicating incomplete functionality Note

Suspicious comment
orpuente-MS and others added 24 commits April 1, 2025 06:11
Compile functions and operations const evaluating any references to
symbol in an external scope.
This PR:
- [x] Unignores 32/52 unit tests that are now supported.
- [x] Passes a couple of spans to the ast_builder that were previously
set to default.
- [x] Makes constant measurements like in `const bit res = measure q;` a
parser error.
- [x] Only generates gate functors when `@SimulatableIntrinsic` is not
present.
- [x] break stmt
- [x] continue stmt
- [x] lower duration literals
- [x] fixes bug when handling `void` return type
- [x] return stmt casts to the return type of the function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants