quantrs2-tytan 0.1.3

High-level quantum annealing interface inspired by Tytan for the QuantRS2 framework
Documentation
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//! Parser for the problem DSL.

use super::ast::*;
use super::error::ParseError;
use super::lexer::Token;

/// Parser for DSL syntax
#[derive(Debug, Clone)]
pub struct Parser {
    /// Current tokens
    tokens: Vec<Token>,
    /// Current position
    position: usize,
    /// Error messages
    errors: Vec<ParseError>,
}

impl Parser {
    /// Create a new parser
    pub const fn new() -> Self {
        Self {
            tokens: Vec::new(),
            position: 0,
            errors: Vec::new(),
        }
    }

    /// Set tokens for parsing
    pub fn set_tokens(&mut self, tokens: Vec<Token>) {
        self.tokens = tokens;
        self.position = 0;
        self.errors.clear();
    }

    /// Parse tokens into AST
    pub fn parse(&mut self) -> Result<AST, ParseError> {
        self.parse_program()
    }

    /// Parse program
    fn parse_program(&mut self) -> Result<AST, ParseError> {
        let mut declarations = Vec::new();
        let mut objective = None;
        let mut constraints = Vec::new();

        while !self.is_at_end() {
            match self.current_token() {
                Token::Var | Token::Param => {
                    declarations.push(self.parse_declaration()?);
                }
                Token::Minimize | Token::Maximize => {
                    if objective.is_some() {
                        return Err(ParseError {
                            message: "Multiple objectives not supported yet".to_string(),
                            line: 0,
                            column: 0,
                        });
                    }
                    objective = Some(self.parse_objective()?);
                }
                Token::Subject => {
                    self.advance(); // consume 'subject'
                    self.expect(Token::To)?;

                    while !self.is_at_end() && !matches!(self.current_token(), Token::Eof) {
                        constraints.push(self.parse_constraint()?);
                    }
                }
                Token::NewLine | Token::Comment(_) => {
                    self.advance();
                }
                _ => {
                    return Err(ParseError {
                        message: format!("Unexpected token: {:?}", self.current_token()),
                        line: 0,
                        column: 0,
                    });
                }
            }
        }

        let obj = objective.ok_or_else(|| ParseError {
            message: "No objective function found".to_string(),
            line: 0,
            column: 0,
        })?;

        Ok(AST::Program {
            declarations,
            objective: obj,
            constraints,
        })
    }

    /// Parse declaration
    fn parse_declaration(&mut self) -> Result<Declaration, ParseError> {
        match self.current_token() {
            Token::Var => self.parse_variable_declaration(),
            Token::Param => self.parse_parameter_declaration(),
            _ => Err(ParseError {
                message: "Expected variable or parameter declaration".to_string(),
                line: 0,
                column: 0,
            }),
        }
    }

    /// Parse variable declaration
    fn parse_variable_declaration(&mut self) -> Result<Declaration, ParseError> {
        self.advance(); // consume 'var'

        let name = self.expect_identifier()?;
        let var_type = self.parse_variable_type()?;

        self.expect(Token::Semicolon)?;

        Ok(Declaration::Variable {
            name,
            var_type,
            domain: None,
            attributes: std::collections::HashMap::new(),
        })
    }

    /// Parse parameter declaration
    fn parse_parameter_declaration(&mut self) -> Result<Declaration, ParseError> {
        self.advance(); // consume 'param'

        let name = self.expect_identifier()?;
        self.expect(Token::Equal)?;
        let value = self.parse_value()?;

        self.expect(Token::Semicolon)?;

        Ok(Declaration::Parameter {
            name,
            value,
            description: None,
        })
    }

    /// Parse variable type
    fn parse_variable_type(&mut self) -> Result<super::types::VarType, ParseError> {
        use super::types::VarType;

        match self.current_token() {
            Token::Binary => {
                self.advance();
                Ok(VarType::Binary)
            }
            Token::Integer => {
                self.advance();
                Ok(VarType::Integer)
            }
            Token::Continuous => {
                self.advance();
                Ok(VarType::Continuous)
            }
            _ => Err(ParseError {
                message: "Expected variable type (binary, integer, continuous)".to_string(),
                line: 0,
                column: 0,
            }),
        }
    }

    /// Parse value
    fn parse_value(&mut self) -> Result<Value, ParseError> {
        match self.current_token() {
            Token::Number(n) => {
                let value = *n;
                self.advance();
                Ok(Value::Number(value))
            }
            Token::Boolean(b) => {
                let value = *b;
                self.advance();
                Ok(Value::Boolean(value))
            }
            Token::String(s) => {
                let value = s.clone();
                self.advance();
                Ok(Value::String(value))
            }
            Token::LeftBracket => {
                self.advance(); // consume '['
                let mut elements = Vec::new();

                while !matches!(self.current_token(), Token::RightBracket) {
                    elements.push(self.parse_value()?);

                    if matches!(self.current_token(), Token::Comma) {
                        self.advance();
                    } else {
                        break;
                    }
                }

                self.expect(Token::RightBracket)?;
                Ok(Value::Array(elements))
            }
            _ => Err(ParseError {
                message: "Expected value".to_string(),
                line: 0,
                column: 0,
            }),
        }
    }

    /// Parse objective
    fn parse_objective(&mut self) -> Result<Objective, ParseError> {
        match self.current_token() {
            Token::Minimize => {
                self.advance();
                let expr = self.parse_expression()?;
                self.expect(Token::Semicolon)?;
                Ok(Objective::Minimize(expr))
            }
            Token::Maximize => {
                self.advance();
                let expr = self.parse_expression()?;
                self.expect(Token::Semicolon)?;
                Ok(Objective::Maximize(expr))
            }
            _ => Err(ParseError {
                message: "Expected minimize or maximize".to_string(),
                line: 0,
                column: 0,
            }),
        }
    }

    /// Parse constraint
    fn parse_constraint(&mut self) -> Result<Constraint, ParseError> {
        let expression = self.parse_constraint_expression()?;
        self.expect(Token::Semicolon)?;

        Ok(Constraint {
            name: None,
            expression,
            tags: Vec::new(),
        })
    }

    /// Parse constraint expression
    fn parse_constraint_expression(&mut self) -> Result<ConstraintExpression, ParseError> {
        let left = self.parse_expression()?;

        let op = match self.current_token() {
            Token::Equal => ComparisonOp::Equal,
            Token::NotEqual => ComparisonOp::NotEqual,
            Token::Less => ComparisonOp::Less,
            Token::Greater => ComparisonOp::Greater,
            Token::LessEqual => ComparisonOp::LessEqual,
            Token::GreaterEqual => ComparisonOp::GreaterEqual,
            _ => {
                return Err(ParseError {
                    message: "Expected comparison operator".to_string(),
                    line: 0,
                    column: 0,
                })
            }
        };

        self.advance(); // consume operator
        let right = self.parse_expression()?;

        Ok(ConstraintExpression::Comparison { left, op, right })
    }

    /// Parse expression
    fn parse_expression(&mut self) -> Result<Expression, ParseError> {
        self.parse_additive()
    }

    /// Parse additive expression
    fn parse_additive(&mut self) -> Result<Expression, ParseError> {
        let mut expr = self.parse_multiplicative()?;

        while matches!(self.current_token(), Token::Plus | Token::Minus) {
            let op = match self.current_token() {
                Token::Plus => BinaryOperator::Add,
                Token::Minus => BinaryOperator::Subtract,
                _ => unreachable!(),
            };
            self.advance();
            let right = self.parse_multiplicative()?;
            expr = Expression::BinaryOp {
                op,
                left: Box::new(expr),
                right: Box::new(right),
            };
        }

        Ok(expr)
    }

    /// Parse multiplicative expression
    fn parse_multiplicative(&mut self) -> Result<Expression, ParseError> {
        let mut expr = self.parse_primary()?;

        while matches!(self.current_token(), Token::Times | Token::Divide) {
            let op = match self.current_token() {
                Token::Times => BinaryOperator::Multiply,
                Token::Divide => BinaryOperator::Divide,
                _ => unreachable!(),
            };
            self.advance();
            let right = self.parse_primary()?;
            expr = Expression::BinaryOp {
                op,
                left: Box::new(expr),
                right: Box::new(right),
            };
        }

        Ok(expr)
    }

    /// Parse primary expression
    fn parse_primary(&mut self) -> Result<Expression, ParseError> {
        match self.current_token() {
            Token::Number(n) => {
                let value = *n;
                self.advance();
                Ok(Expression::Literal(Value::Number(value)))
            }
            Token::Boolean(b) => {
                let value = *b;
                self.advance();
                Ok(Expression::Literal(Value::Boolean(value)))
            }
            Token::String(s) => {
                let value = s.clone();
                self.advance();
                Ok(Expression::Literal(Value::String(value)))
            }
            Token::Identifier(name) => {
                let var_name = name.clone();
                self.advance();

                // Check for indexing
                if matches!(self.current_token(), Token::LeftBracket) {
                    self.advance(); // consume '['
                    let mut indices = Vec::new();

                    loop {
                        indices.push(self.parse_expression()?);

                        if matches!(self.current_token(), Token::Comma) {
                            self.advance();
                        } else {
                            break;
                        }
                    }

                    self.expect(Token::RightBracket)?;
                    Ok(Expression::IndexedVar {
                        name: var_name,
                        indices,
                    })
                } else {
                    Ok(Expression::Variable(var_name))
                }
            }
            Token::LeftParen => {
                self.advance(); // consume '('
                let expr = self.parse_expression()?;
                self.expect(Token::RightParen)?;
                Ok(expr)
            }
            _ => Err(ParseError {
                message: "Expected expression".to_string(),
                line: 0,
                column: 0,
            }),
        }
    }

    /// Current token
    fn current_token(&self) -> &Token {
        self.tokens.get(self.position).unwrap_or(&Token::Eof)
    }

    /// Advance to next token
    fn advance(&mut self) {
        if !self.is_at_end() {
            self.position += 1;
        }
    }

    /// Check if at end of tokens
    fn is_at_end(&self) -> bool {
        self.position >= self.tokens.len() || matches!(self.current_token(), Token::Eof)
    }

    /// Expect specific token
    fn expect(&mut self, expected: Token) -> Result<(), ParseError> {
        if std::mem::discriminant(self.current_token()) == std::mem::discriminant(&expected) {
            self.advance();
            Ok(())
        } else {
            Err(ParseError {
                message: format!("Expected {:?}, found {:?}", expected, self.current_token()),
                line: 0,
                column: 0,
            })
        }
    }

    /// Expect identifier and return its name
    fn expect_identifier(&mut self) -> Result<String, ParseError> {
        match self.current_token() {
            Token::Identifier(name) => {
                let result = name.clone();
                self.advance();
                Ok(result)
            }
            _ => Err(ParseError {
                message: "Expected identifier".to_string(),
                line: 0,
                column: 0,
            }),
        }
    }
}

impl Default for Parser {
    fn default() -> Self {
        Self::new()
    }
}