Skip to content

Commit

Permalink
Factor lowering a list of qubits into a helper function (#221)
Browse files Browse the repository at this point in the history
* Factor lowering a list of qubits into a helper function

fixes: #163

* Update crates/oq3_semantics/src/syntax_to_semantics.rs

remove comments that were moved elsewhere

* Update crates/oq3_semantics/src/syntax_to_semantics.rs

Move a comment from elsewhere to here, where it makes more sense.

* Run cargo fmt

---------

Co-authored-by: John Lapeyre <[email protected]>
  • Loading branch information
atomgardner and jlapeyre authored May 31, 2024
1 parent 5d521be commit 614ee04
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 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 @@ -767,15 +757,7 @@ fn from_gate_call_expr(
modifiers: Vec<asg::GateModifier>,
context: &mut Context,
) -> 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 @@ -871,6 +853,19 @@ 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> {
// 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.
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 614ee04

Please sign in to comment.