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

Factor lowering a list of qubits into a helper function #221

Merged
merged 5 commits into from
May 31, 2024
Merged
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
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.
jlapeyre marked this conversation as resolved.
Show resolved Hide resolved
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
Loading