Skip to content

Commit

Permalink
Factor lowering a list of qubits into a helper function
Browse files Browse the repository at this point in the history
fixes: #163
  • Loading branch information
atomgardner committed May 29, 2024
1 parent 87e5fbf commit 68c6cec
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions crates/oq3_semantics/src/syntax_to_semantics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,29 +402,19 @@ fn from_stmt(stmt: synast::Stmt, context: &mut Context) -> Option<asg::Stmt> {
}

synast::Stmt::Barrier(barrier) => {
let gate_operands = barrier.qubit_list().map(|operands| {
operands
.gate_operands()
.map(|qubit| from_gate_operand(qubit, context))
.collect()
});
Some(asg::Stmt::Barrier(asg::Barrier::new(gate_operands)))
let gate_operands = from_qubit_list(barrier.qubit_list(), context);
Some(asg::Stmt::Barrier(asg::Barrier::new(Some(gate_operands))))
}

synast::Stmt::DelayStmt(delay_stmt) => {
let gate_operands = delay_stmt.qubit_list().map(|operands| {
operands
.gate_operands()
.map(|qubit| from_gate_operand(qubit, context))
.collect()
});
let gate_operands = from_qubit_list(delay_stmt.qubit_list(), context);
let duration = from_expr(delay_stmt.designator().unwrap().expr(), context).unwrap();
if !matches!(duration.get_type(), Type::Duration(_)) {
context.insert_error(IncompatibleTypesError, &delay_stmt.designator().unwrap());
}
Some(asg::Stmt::Delay(asg::DelayStmt::new(
duration,
gate_operands.unwrap(),
gate_operands,
)))
}

Expand Down Expand Up @@ -753,13 +743,7 @@ fn from_gate_call_expr(
) -> Option<asg::Stmt> {
// Warning, I think map overlooks None. This can cause a bug in the present case.
// Because None means a coding error upstream. Better to blow up here.
let gate_operands: Vec<_> = gate_call_expr
.qubit_list()
.unwrap()
.gate_operands()
.map(|qubit| from_gate_operand(qubit, context))
.collect();

let gate_operands: Vec<_> = from_qubit_list(gate_call_expr.qubit_list(), context);
let param_list = gate_call_expr
.arg_list()
.map(|ex| inner_expression_list(ex.expression_list().unwrap(), context));
Expand Down Expand Up @@ -855,6 +839,14 @@ fn from_expression_list(
asg::ExpressionList::new(inner_expression_list(expression_list, context))
}

fn from_qubit_list(qubit_list: Option<synast::QubitList>, context: &mut Context) -> Vec<asg::TExpr> {
// Because None means a coding error upstream. Better to blow up here.
qubit_list.unwrap()
.gate_operands()
.map(|qubit| from_gate_operand(qubit, context))
.collect()
}

// Return a Vec of TExpr.
fn inner_expression_list(
expression_list: synast::ExpressionList,
Expand Down

0 comments on commit 68c6cec

Please sign in to comment.