libqasm 0.6.6__cp310-cp310-win_amd64.whl → 0.6.8__cp310-cp310-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
cqasm/v3x/__init__.py CHANGED
@@ -4,16 +4,58 @@ import libqasm
4
4
 
5
5
 
6
6
  class Analyzer(libqasm.V3xAnalyzer):
7
- # parse_file and parse_string are static methods because they do not change the status of the analyzer
8
- # Instead, they just invoke free functions that create a temporary instance of a parser
9
- # analyze_file and analyze_string are not static methods because they change the status of the analyzer
7
+ """!
8
+ Main class for parsing and analyzing cQASM v3.0 files.
10
9
 
11
- # parse_file, parse_string, analyze_file, and analyze_string return a vector of strings
12
- # If the length of the vector is 1, the string is a serialization of the AST
13
- # Otherwise, it is a list of errors
10
+ This class works as a binding for accessing C++ code from Python.
11
+
12
+ The parsing methods are static because they do not change the status of the analyzer.
13
+ Instead, they just invoke free functions that create a temporary instance of a parser.
14
+
15
+ None of the parsing or the analyzing methods perform any version check.
16
+
17
+ `parse_file`, `parse_string`, `analyze_file`, and `analyze_string`:
18
+
19
+ - return a vector of strings.
20
+ If the length of the vector is 1, the string is a serialization
21
+ of the syntactic/semantic (in the case of parsing/analyzing) Abstract Syntax Tree (AST)
22
+ of the input program.
23
+ Otherwise, it is a list of errors.
24
+
25
+ `parse_file_to_json`, `parse_string_to_json`, `analyze_file_to_json`, and `analyze_string_to_json`:
26
+
27
+ - return a string in JSON format.
28
+ If the parsing was successful, the string contains a JSON representation
29
+ of the AST of the input program.
30
+ Otherwise, it will contain a list of errors.
31
+ The JSON representation of each error follows the Language Server Protocol (LSP) specification.
32
+ Every error is mapped to an LSP Diagnostic structure:
33
+ `severity` is hardcoded to 1 at the moment (value corresponding to an Error level).
34
+
35
+ `parse_string`, `parse_string_to_json`, `analyze_string, and `analyze_string_to_json`:
36
+
37
+ - have an optional second argument: `file_name`. It is only used when reporting errors.
38
+
39
+ **Example**:
40
+
41
+ result = libqasm.analyze_file("grover.cq")
42
+
43
+ **Example**:
44
+
45
+ program = r'''
46
+ version 3.0
47
+ qubit[2] q
48
+ bit[2] b
49
+ H q[0]
50
+ CNOT q[0], q[1]
51
+ b = measure q
52
+ '''
53
+ result = libqasm.parse_string_to_json(program, "bell.cq")
54
+ """
14
55
 
15
56
  @staticmethod
16
- def parse_file(*args):
57
+ def parse_file(*args) -> list[str]:
58
+ """! Parses a file containing a cQASM v3.0 program."""
17
59
  ret = libqasm.V3xAnalyzer.parse_file(*args)
18
60
  if len(ret) == 1:
19
61
  serialized_ast_str = str(ret[0])
@@ -23,7 +65,13 @@ class Analyzer(libqasm.V3xAnalyzer):
23
65
  return [str(error) for error in ret[1:]]
24
66
 
25
67
  @staticmethod
26
- def parse_string(*args):
68
+ def parse_file_to_json(*args) -> str:
69
+ """! Parses a file containing a cQASM v3.0 program."""
70
+ return libqasm.V3xAnalyzer.parse_file_to_json(*args)
71
+
72
+ @staticmethod
73
+ def parse_string(*args) -> list[str]:
74
+ """! Parses a string containing a cQASM v3.0 program."""
27
75
  ret = libqasm.V3xAnalyzer.parse_string(*args)
28
76
  if len(ret) == 1:
29
77
  serialized_ast_str = str(ret[0])
@@ -32,7 +80,13 @@ class Analyzer(libqasm.V3xAnalyzer):
32
80
  return deserialized_ast
33
81
  return [str(error) for error in ret[1:]]
34
82
 
35
- def analyze_file(self, *args):
83
+ @staticmethod
84
+ def parse_string_to_json(*args) -> str:
85
+ """! Parses a string containing a cQASM v3.0 program."""
86
+ return libqasm.V3xAnalyzer.parse_string_to_json(*args)
87
+
88
+ def analyze_file(self, *args) -> list[str]:
89
+ """! Parses and analyzes a file containing a cQASM v3.0 program."""
36
90
  ret = super().analyze_file(*args)
37
91
  if len(ret) == 1:
38
92
  serialized_ast_str = str(ret[0])
@@ -41,7 +95,12 @@ class Analyzer(libqasm.V3xAnalyzer):
41
95
  return deserialized_ast
42
96
  return [str(error) for error in ret[1:]]
43
97
 
44
- def analyze_string(self, *args):
98
+ def analyze_file_to_json(self, *args) -> str:
99
+ """! Parses and analyzes a file containing a cQASM v3.0 program."""
100
+ return super().analyze_file_to_json(*args)
101
+
102
+ def analyze_string(self, *args) -> list[str]:
103
+ """! Parses and analyzes a string containing a cQASM v3.0 program."""
45
104
  ret = super().analyze_string(*args)
46
105
  if len(ret) == 1:
47
106
  serialized_ast_str = str(ret[0])
@@ -50,16 +109,6 @@ class Analyzer(libqasm.V3xAnalyzer):
50
109
  return deserialized_ast
51
110
  return [str(error) for error in ret[1:]]
52
111
 
53
- @staticmethod
54
- def parse_file_to_json(*args):
55
- return libqasm.V3xAnalyzer.parse_file_to_json(*args)
56
-
57
- @staticmethod
58
- def parse_string_to_json(*args):
59
- return libqasm.V3xAnalyzer.parse_string_to_json(*args)
60
-
61
- def analyze_file_to_json(self, *args):
62
- return super().analyze_file_to_json(*args)
63
-
64
- def analyze_string_to_json(self, *args):
112
+ def analyze_string_to_json(self, *args) -> str:
113
+ """! Parses and analyzes a string containing a cQASM v3.0 program."""
65
114
  return super().analyze_string_to_json(*args)