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

V3 Extended: Functions #198

Merged
merged 33 commits into from
Mar 5, 2024
Merged

V3 Extended: Functions #198

merged 33 commits into from
Mar 5, 2024

Conversation

rturrado
Copy link
Contributor

@rturrado rturrado commented Feb 26, 2024

Add user-defined functions to the grammar.
Functions are defined as def <name>(<parameters>) -> <return type> { <statements> }.
The return type is optional.
There is a return statement.

Functions are called as <name>(<arguments>).
Arguments need to be promotable to the types of the parameters.
Function calls are both expressions and statements.

Changest to the grammar

  • Add function declarations, global and local blocks, global and local block statements, return statements and expression statements.
  • Add types and simplified variable definitions and initializations.

Changes to the AST

  • Add global and local blocks, and block statements.

Changes to the v3x/analyzer

  • Add a stack of scopes.

Changes to v3x/resolver

  • Rename mapping to variable.
  • Rename FunctionTable to FunctionImplTable, for functions for which we have a C++ implementation.
  • Add FunctionTable, for functions defined in the cQASM file.

Changes to v3x/types

  • Rename real to float.

Changes to v3x/values

  • Add a ValueBase base class so that the arguments of a function call can refer to it.
  • Rename real to float.

Changes to v1x files

  • Move AnalyzerHelper and Scope classes out to separate files.
  • Use constants for type names instead of "magic" string literals.

Syntactic AST: an instruction can be a gate, an assignment instruction, or a measure instruction.

Semantic AST: a regular instruction can be a gate or a measure instruction.

BuildTreeGenAstVisitor.cpp: visitFunctionDeclaration, visitFunctionParameters, and visitStatementBlock are unimplemented.
- Syntactic AST Instruction has been renamed to Gate.
- After modifying the parser rules, some of the parser error messages are different.
Add return statement to function blocks.
Add function declaration integration tests.
- Remove VOID keyword.

Changes to the v3 parser:
- Add returnStatement.
- A statement can be a blockStatement, a functionDeclaration, or a returnStatement.

Changes to the syntactic AST:
- Add type, block_statement, function, return_statement, and variable_list nodes.
- A type has a name and a size.
- A variable has a name and a type (before, a name, a type, and a size).
- A block_statement can be a variable, an initialization, or an instruction.

Change v3 Real type to Float.
- The syntactic AST has a Type node.
- The syntactic AST Variable nodes have a name and a type (before, a name, a type, and a size).
- The semantic AST uses Float for types and values, not Real anymore.

Change res/v3x/parsing/function files:
- Remove '-> void' return type.
- bell function does not apply Hadamard on qubit 1.
- Remove conditions from statements.
- Change assignment_instruction to assignment_statement.
- Change function's return_type from One<type> to Maybe<type>.
- A function has statements and a return_statement.

Changes to the semantic AST:
- Remove conditions from instructions.
- A statement can be an instruction, an assignment_statement, or a return_statement.
- An instruction continues to be whether a gate or a measure_instruction.
- Add function node.
…nstructor.

Enable {v1x,v3x}/Instruction classes to be used via fmt.
- Add global and local block. A local block cannot contain function declarations.
- Add type, quantum type, and classical type. A variable definition is a type and an identifier. A variable declaration is a classical type, an identifier, and an expression.

Changes to the v3x/syntactic AST:
- A program has a version and a global block.
- A function has a name, parameters, a return type, and a local block.

Changes to the v3x/semantic AST:
- A program has a version, a block, a list of functions, and a list of variables.
- A function has a name, a return type, a block, and a list of variables.

Changes to the v3x/values:
- Add function value.

Changes to the v3x/Analyzer:
- Add a stack of scopes.
- Add methods to access the global scope, and the current scope.
- Add methods to access the current block, the current list of variables, and the global list of functions.
- Add methods to push and pop scopes.
- Add methods to add statements, variables and functions to the current and global scopes.

Changes to BuildTreeGenAstVisitor:
- Add expandNodeAnnotation method. This is used for array types.

Changes to AnalyzeTreeGenAstVisitor:
- Add visit_function method.
- Add build_semantic_type, to build a semantic type from a syntactic type.
- Visitor methods do not update result_.root directly, but via the add_{statement,variable,function} methods.

Changes to the v3x/resolver:
- Add ResolutionFailure exception.
- Change mapping name to variable.

TODO: register_function is unimplemented.
TODO: update res/v3x/parsing files, to follow the syntactic and semantic AST changes.
TODO: not sure if we will be able to unify these two tables (types of functions) in the future.

Changes to the values tree-gen file:
- Add a function_ref and a function_call value.
  Functions are registered as function_ref, pointing to a semantic AST Function.
  A function call value will contain information about the function (in the form of a value), and its arguments.
- Add a ValueBase base class.
  This is needed for referencing a generic value from the arguments of a function.

Changes to the semantic AST:
- Refer to values::ValueBase and types::TypeBase instead of values::Node and types::Node.

Changes to the values file:
- Value is now a One<ValueBase>, not a One<Node> anymore.
- Values is now an Any<ValueBase>, not an Any<Node> anymore.

Changes to AnalyzeTreeGenAstVisitor:
- visit_function registers a function using the name, the type of the parameters and a FunctionRef value.
- Rename analyze_as to visit_as.

Changes to the v3x/Analyzer:
- We now have function implementations and functions.
  Function implementations are functions for which we have a C++ implementation.
  Functions are those defined in the cQASM file.
- Rename call_function to resolve_function.

Changes to the v3x/resolver:
- We now have function implementations and functions.

Changes to v3x/functions:
- Register functions as function implementations.
- There can be zero or more statement separators after a local block.

Changes to the syntactic AST:
- A return statement is a block statement.
- The parameters of a function are a local block.
- Remove variable_list.

Changes to BuildTreeGenAstVisitor:
- A local block statement can be a variable declaration, an instruction, an assignment statement or a return statement.

Changes to AnalyzeTreeGenAstVisitor:
- Push and pop scopes manually when needed, not automatically at visit_local_block.
- For example, when analyzing a function, push a scope, analyze the parameters, analyze the block, and pop the scope.
- Remove extra code from types_of.
- Fix visit_assignment_statement.
- Fix visit_function.
- Fix visit_return_statement.

Changes to the v3x/Analyzer:
- Add some more const functions.

Changes to the v3x/values file:
- Update functions considering function_ref and function_call values.
Because of these changes to the syntactic AST:
- Add annotations for Type nodes.
- Add assignment statement.
- Change statements/StatementList/items to block/GlobalBlock/statements.
- Remove condition from nodes.

And these changes to the semantic AST:
- Add functions.
- Change mapping to variable.
- Change AssignmentInstruction to AssignmentStatement.
- Remove condition from nodes.
- Add expressionStatement. In order to enable function calls as statements.

Changes to the syntactic AST:
- Change the type of the arguments of a function call expression to Maybe<expression_list>.

Changes to the semantic AST:
- Add expression statement.
- A function call statement only holds a return value, if any.

Changes to BuildTreeGenAstVisitor:
- Add visitExpressionStatement. Expression statements use their expression's annotations.

Changes to AnalyzeTreeGenAstVisitor:
- Add visit_expression_statement. The analyzer checks that an expression statement can only be a function call.
- A function call expression may not have arguments.

Fix a couple of bugs in src/cqasm-annotations.cpp and src/v1x/cqasm-analyzer-helper.cpp.
That I had previously introduced in this branch.
…xtended-functions

# Conflicts:
#	test/v3x/python/test_parse_result.py
@rturrado rturrado requested a review from pablolh February 26, 2024 08:33
Update res/v1x/parsing files.
This file belongs to a folder that is unused at the moment. However, the change is legit.
Changes to the v3x/analyzer:
- Make privat members protected, so that they can be used in testing.
- Update instruction resolution exception error message.
- Rename register_default_functions_into to register_default_function_impls_into.

Update res/v3x/parsing files.
Changes to v3x::primitives::Axis:
- Set default value to axis x.

Changes to v3x::values, promote function:
- Change asserts for checks. So that, if they fail, promote just returns an empty value.
Copy link

@pablolh pablolh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Roberto
First of all sorry for the delay.
It's quite difficult to review since I don't know what is new and what is moved around.
However, I didn't see any problem
I'm ready to approve if you deem those few comments irrelevant, please let me know then

include/v3x/AnalyzeTreeGenAstVisitor.hpp Outdated Show resolved Hide resolved
include/v3x/cqasm-primitives.hpp Show resolved Hide resolved
src/cqasm-annotations.cpp Show resolved Hide resolved
@rturrado rturrado merged commit 4565670 into develop Mar 5, 2024
17 checks passed
@rturrado rturrado deleted the v3-extended-functions branch March 5, 2024 16:25
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