1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//! Qudit Gate Language (QGL) compiler.
//!
//! This module provides a JIT compiler for QGL, a domain-specific
//! language for describing unitary expressions, built on LLVM.
//!
//! # Grammar
//!
//! The grammar for QGL is as follows:
//!
//! ```text
//!
//! (* Start Symbol *)
//!
//! qobj ::= identifier radices? '(' varlist ')' '{' expression '}'
//!
//! radices ::= '<' constlist '>'
//!
//! (* Expression Grammar *)
//!
//! tensor ::= '[' matrix (',' matrix)* ','? ']' ;
//!
//! matrix ::= '[' row (',' row)* ','? ']' ;
//!
//! row ::= '[' exprlist ']' ;
//!
//! exprlist ::= (expression (',' expression)* ','?)? ;
//!
//! expression ::= term (('+' | '-') term)* ;
//!
//! term ::= '~'* factor (('*' | '/') factor)* ;
//!
//! factor ::= primary ('^' primary)* ;
//!
//! primary ::= variable
//! | constant
//! | function_call
//! | row
//! | matrix
//! | tensor
//! | '(' expression ')' ;
//!
//! variable ::= identifier ;
//!
//! varlist ::= (variable (',' variable)* ','?)? ;
//!
//! constlist ::= (constant (',' constant)* ','?)? ;
//!
//! constant ::= digit+ (('.' digit+)?) ;
//!
//! function_call ::= identifier '(' exprlist ')' ;
//!
//! identifier ::= letter (letter | digit)* ;
//!
//! digit ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' ;
//!
//! letter ::= 'a'..'z' | 'A'..'Z' | '_' | greek_letters ;
//! ```
//!
//! Single-line comments starting with `#` are ignored. Certain greek letters
//! and special characters are parsed as their associated constants, such
//! as `π` for pi and `e` for Euler's number. The full list of recognized
//! constants is as follows:
//!
//! - `π` (pi)
//! - `e` (Euler's number)
//! - `i` (imaginary unit)
//!
//! # Examples
//!
//! A simple Hadamard gate definition:
//!
//! ```qgl
//! utry H() {
//! [
//! [ 1, 1 ],
//! [ 1, ~1 ]
//! ] / sqrt(2)
//! }
//! ```
//!
//! A qutrit phase gate:
//!
//! ```qgl
//! utry P<3>(θ0, θ1) {
//! [
//! [ 1, 0, 0 ],
//! [ 0, e^(iθ0), 0 ],
//! [ 0, 0, e^(iθ1) ],
//! ]
//! }
//! ```
pub
pub use Expression;
pub use ParsedDefinition;
// pub fn parse_unitary(input: &str) -> Result<UnitaryDefinition, String> {
// let mut parser = parser::Parser::new(input);
// let udef_result = parser.parse();
// let udef = match udef_result {
// Ok(udef) => udef,
// Err(e) => return Err(e.error),
// };
// if udef.len() != 1 {
// return Err(format!(
// "Expected exactly one unitary definition, found {}",
// udef.len()
// ));
// }
// let udef = udef.into_iter().next().unwrap();
// Ok(udef)
// }