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 MVP #165

Merged
merged 84 commits into from
Nov 13, 2023
Merged

v3 MVP #165

merged 84 commits into from
Nov 13, 2023

Conversation

rturrado
Copy link
Contributor

@rturrado rturrado commented Nov 5, 2023

And then the toy v1 parser became a v3 MVP.

Implemented the libqasm side of the v3 MVP.
Most of the code has been reused from the v1 parser: tree files, include files, and source files.
Main differences lie on:

  • ANTLR is used for the lexer and parser (like we did for the toy v1 parser).
  • Analyzer::analyze performs the analysis through an AnalyzeTreeGenAstVisitor::visitProgram call.
  • The analyzer doesn't have special q and b registers anymore, and neither has QubitRefs and BitRefs.
    Instead, we have Qubit, QubitArray, Bit, and BitArray types, and IndexRefs, which will point to a variable of one of the mentioned types.
    The mapping of these qubit/bit variables to a physical qubit/bit register, and the semantic checks related to that mapping are not implemented at the libqasm level, at least not yet.
  • The measure instruction, with the syntax <bit references> = measure <qubit references> is implemented.

Changes to v1x files

  • Added fmt formatters for types and values classes, and reimplemented operator<< using fmt::format.
  • Added noexcept, [[nodiscard]] to method signatures.
  • Changed auto to const auto & for many loop constructs.
  • Changed cqasm-analyzer to have only one constructor, which receives a Version.
  • Changed comments to fit 120 character lines instead of 80.
  • Changed some semantic.1.x.golden.txt files because api_version is now dumped at the top of the Program members, right before version.
  • Changed version comparisons to use helper methods (less_than, more_than...).
  • Moved api_version to the beginning of the program structure in cqasm-semantic.tree.
  • Renamed res/v1x/parsing folders by changing dashes to underscores, because gtest doesn't like test names with dashes.
  • Reordered or removed include headers.
  • Restored code to be case-insensitive again.
  • Used nested namescopes (e.g. cqasm::v1x::analyzer).

Changes to v3x files

  • Added res/v3x/parsing test files. Many of these files were moved from res/v1x/toy-v1x-parsing.
  • Added test/parsing.cpp.

Changes to common v1x/v3x files

  • Added helper methods equal, less_than, more_than, less_than_or_equal, and more_than_or_equal to Version.
  • Added test/cqasm-utils.cpp to test to_lowercase and equal_case_insensitive functions.
  • Modified cqasm-version-lexer.l to accept also C++ style comments. Note that a v1x parser will later complain if the input file contains this kind of comments.
  • Moved Overload templates from include/v1x/cqasm-resolver to include/cqasm-overload.
  • Moved read_file and write_file functions to test/utils.cpp.
  • Moved register_tests code, used both by test/v1x/parsing.cpp and test/v3x/parsing.cpp to test/test-register.hpp.

Changes to the build system

  • Added range-v3 dependencies.
  • Updated src/CMakeLists.txt to fetch content fmt and range-v3 in case the packages were not found.
  • Updated src/CMakeLists.txt to generate tree-gen classes for v3 (AST, types, values, and semantic AST).

…fetched via conan.

This is needed for libraries using libqasm.
All these changes to V1 cannot finally go into develop.
Otherwise we would be breaking the V1 parsing.
All these changes to V1 cannot finally go into develop.
Otherwise we would be breaking the V1 parsing.

test/v1x/parsing.cpp:
- Changed vector of API levels used in semantic checks to use only '3.0'.
- Changed the names of the registered instructions 'x90' and 'cnot' to 'X90' and 'CNOT', so that they are resolved correctly.
Updated v3 lexer and parser grammar files.
Updated BuildTreeGenAstVisitor files so that the library compiles, although the new methods are yet unimplemented.
- Updated grammar.
- Updated BuildTreeGenAstVisitor visitor.
- Updated tests.
- Added .tree files.
- changed 'analyze' function's ''file_parser' parameter name to 'parser', because the parser can actually invoke the file or the string parser.
- minor fixes suggested by Clang-tidy.
At the moment, I'm just copy-pasting from the v1 code, and simplifying the contents by removing everything I think I won't need.
…s}.tree.

At the moment, I'm just copy-pasting from the v1 code, and simplifying the contents by removing everything I think I won't need.
- Use of nested namespaces.
- Change some comments so that they have meaningful phrases per line.
- We have something that compiles.
- After having added all the (I think needed) .tree and .hpp files.
- I have tried not to reuse anything from v1x.
  For example, v3x/cqasm-values.tree defines cqasm::v3x::values/const_int, which is already defined in cqasm::v1x::values, with exactly the same logic.
- My main concern is with the 'array' type in v3x/cqasm-types.tree.
  I don't think tree-gen allows to define something like a templated class, this is, an 'array<T>'.
  We would need this in order to consider 'array' as a type, so that we could have, for example, arrays of arrays of int (array<array<int>>).
  So, with tree-gen, if I'm not mistaken, we will be forced to define a specific type for every array type. E.g. qubit_array.
  Matrices should be a different type as well, as in v1x.
For the MVP, the only allowed types are qubit, bit, qubit_array, and bit_array.
- Moved read_file and write_file functions to utils.{hpp,cpp}.
- Moved register_tests function to test-register.hpp.
- Added cqasm::test namespace.
- test/v1x/parsing.cpp: fixed ParsingTest::TestBody method.
  It was using 'api_version' 3.0 instead of 1.0, 1.1, and 1.2.
  It was using 'X90' and 'CNOT' instead of 'x90' and 'cnot'.
Reorganized test folder to accommodate future v3x tests.
- Moved read_file and write_file functions to utils.{hpp,cpp}.
- Moved register_tests function to test-register.hpp.
- Added cqasm::test namespace.
- test/v1x/parsing.cpp: fixed ParsingTest::TestBody method.
  It was using 'api_version' 3.0 instead of 1.0, 1.1, and 1.2.
  It was using 'X90' and 'CNOT' instead of 'x90' and 'cnot'.
- Use of nested namespaces.
- Change some comments so that they have meaningful phrases per line.
- Changed v3 grammar: qubit and bit array sizes can only be of type integer at the moment.
- Changed tree-gen classes:
  - A variable has optionally a size (when it is of array type).
  - A measurement_instruction is not a subtype of an instruction (gate).
- All v3 code refers now to v3 files and classes.
- Added cqasm-analyzer, although the analysis is unimplemented.
- All tests are passing
…ree.

Changes to the grammar:
- Changed measure_instruction into measure_statement.
- A version number doesn't need now a minor number.
Added src/v3x/cqasm-types.cpp and src/v3x/cqasm-values.cpp.
Added test/v3x/parsing.cpp and registered v3x tests.
…t array, or index reference to one or many qubits), and one output operand (bit, bit array, or index reference to one or many bits).
… remain case-sensitive. The code calling them (v1x or v3x) can be case-insensitive by converting to lowercase before adding and resolving an overload.

Fixed some object slicing issues pointed out by clang-tidy (mainly assignments from One<T> to Maybe<T>).
Renamed utils::{lowercase,case_insensitive_equals} to utils::{to_lowercase,equal_case_insensitive}.
@rturrado rturrado requested a review from pablolh November 5, 2023 15:34
@jvansomeren
Copy link
Collaborator

jvansomeren commented Nov 5, 2023 via email

@rturrado
Copy link
Contributor Author

rturrado commented Nov 5, 2023

Hi @jvansomeren,

Thanks for the comment!

The implementation of v3 in libqasm is completely independent to OpenQL. I am aware that, if I start with the changes to the OpenQL IR, I may hit some issues that would force me me to update the current implementation of v3 in libqasm (in an iterative process). But, again, those changes in OpenQL could be done in a separate branch, and never committed to develop until we make sure we don't break any of the current functionality.

Of course, I don't pretend either to break OpenQL support for v1.2.

This PR has changed some v1 files, but the changes are minor: comments, clang-tidy suggestions, use of nested namespaces... The only "important" change is to have moded the api_version member in the ast::Program class, which modifies the dump of this object.

Best regards,
Roberto.

- we have removed the first_output_operand_index from an ast::Instruction, and
- we have changed the error message for when the number of input indices and the number of output indices differ.
…ruction.

This flag is false by default, and set to true for a measure instruction.
Reimplemented check_input_indices_equals_output_indices as check_same_size_input_output_indices, without using std::span.
Removed first_output_operand_index from a semantic AST instruction.
include/cqasm-overload.hpp Outdated Show resolved Hide resolved
include/cqasm-version.hpp Outdated Show resolved Hide resolved
src/v3x/AnalyzeTreeGenAstVisitor.cpp Outdated Show resolved Hide resolved
src/v3x/AnalyzeTreeGenAstVisitor.cpp Outdated Show resolved Hide resolved
It seems a header in a Python library is defining things like STRING, and this macro expansion is hitting some enums in a different header in the ANTLR runtime library. Weird.
This fix avoids v10 code to include v3x/cqasm.hpp.
- We have removed output_operands from an instruction.
Implemented operator== and operator<=> for Version.
Changed equal, less_than, more_than, less_than_or_equal, and more_than_or_equal to ==, <, >, <=, and >=.

Renamed Overload.{tag_,param_types_} to tag and param_types.
Made Analyzer::api_version public and removed accessor function.
@pablolh
Copy link
Contributor

pablolh commented Nov 10, 2023

looks good to me, please ping me know when/if I need to approve :)

…it_indices_have_same_size.

Used 'i' (for 'indices') instead of 'f'  (for 'force' -to be equal, an old name for this flag-) when serializing request_qubit_and_bit_indices_have_same_size.
python/CMakeLists.txt:
- Changed CMake minimum required version to 3.12.
- Changed C++ standard to C++20 (like for libqasm).
- Removed code that checked if CMake version was smaller than 3.12.
- Deleted python/compat folder, which was only used when CMake version was smaller than 3.12.
- Added v3x Python generated files to installation.

Moved include/cqasm-py.hpp to include/v1x.
Moved src/cqasm-py.cpp to src/v1x, and refactored code.
Added include/v3x/cqasm-py.hpp.
Added src/v3x/cqasm-py.cpp.
@pablolh
Copy link
Contributor

pablolh commented Nov 13, 2023

Let's merge this and finalize in another PR (it's already too large haha)
Nice that you could complete a template for v3 Python API

@rturrado rturrado merged commit 286ef72 into develop Nov 13, 2023
14 of 15 checks passed
@rturrado
Copy link
Contributor Author

Let's merge this and finalize in another PR (it's already too large haha) Nice that you could complete a template for v3 Python API

Oh, no, I'm still fighting with that, I have just started:

  • I wanted the user to be able to do something like import libQasm.v1 and import libQasm.v3.
  • And I also wanted to add some tests for the v3 API.

@rturrado rturrado deleted the v3-mvp branch March 8, 2024 14:18
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.

3 participants