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

Implement barrier from parsing to ASG #54

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}