Skip to content

Commit

Permalink
Implement barrier from parsing to ASG (#54)
Browse files Browse the repository at this point in the history
Closes #39
  • Loading branch information
jlapeyre authored Jan 19, 2024
1 parent 3036563 commit e6540e6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
2 changes: 1 addition & 1 deletion crates/oq3_parser/src/grammar/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ fn version_(p: &mut Parser<'_>) -> bool {
fn barrier_(p: &mut Parser<'_>, m: Marker) {
p.bump(T![barrier]);
if !p.at(T![;]) {
params::param_list_gate_qubits(p);
params::arg_list_gate_call_qubits(p);
}
p.expect(SEMICOLON);
m.complete(p, BARRIER);
Expand Down
17 changes: 16 additions & 1 deletion crates/oq3_semantics/src/asg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ pub enum Stmt {
Alias, // stub
AnnotatedStmt(AnnotatedStmt),
Assignment(Assignment),
Barrier, // stub
Barrier(Barrier),
Block(Block),
Box, // stub
Break,
Expand Down Expand Up @@ -589,6 +589,21 @@ impl MeasureExpression {
}
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Barrier {
qubits: Vec<TExpr>,
}

impl Barrier {
pub fn new(qubits: Vec<TExpr>) -> Barrier {
Barrier { qubits }
}

pub fn qubits(&self) -> &Vec<TExpr> {
&self.qubits
}
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct GateCall {
name: SymbolIdResult,
Expand Down
12 changes: 11 additions & 1 deletion crates/oq3_semantics/src/syntax_to_semantics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ fn from_gate_operand(gate_operand: synast::GateOperand, context: &mut Context) -
}
synast::GateOperand::Identifier(ref identifier) => {
let (astidentifier, typ) = ast_identifier(identifier, context);
if !matches!(typ, Type::Qubit | Type::HardwareQubit) {
if !matches!(typ, Type::Qubit | Type::HardwareQubit | Type::QubitArray(_)) {
context.insert_error(IncompatibleTypesError, &gate_operand);
}
asg::GateOperand::Identifier(astidentifier).to_texpr(typ)
Expand Down Expand Up @@ -485,6 +485,16 @@ fn from_item(item: synast::Item, context: &mut Context) -> Option<asg::Stmt> {
)))
}

synast::Item::Barrier(barrier) => {
let gate_operands: Vec<_> = barrier
.qubit_list()
.unwrap()
.gate_operands()
.map(|qubit| from_gate_operand(qubit, context))
.collect();
Some(asg::Stmt::Barrier(asg::Barrier::new(gate_operands)))
}

synast::Item::Include(include) => {
if context.symbol_table().current_scope_type() != ScopeType::Global {
context.insert_error(IncludeNotInGlobalScopeError, &include);
Expand Down
12 changes: 12 additions & 0 deletions crates/oq3_semantics/tests/from_string_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,15 @@ h q[1];
assert!(errors.is_empty());
assert_eq!(program.len(), 3);
}

#[test]
fn test_from_string_barrier() {
let code = r#"
qubit[5] q;
barrier q;
barrier $0, $1;
"#;
let (program, errors, _symbol_table) = parse_string(code);
assert!(errors.is_empty());
assert_eq!(program.len(), 3);
}

0 comments on commit e6540e6

Please sign in to comment.