Skip to content
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
20 changes: 18 additions & 2 deletions compiler/qsc_qasm/src/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,8 +740,24 @@ pub(crate) fn build_measure_call(
stmt_span: Span,
) -> ast::Expr {
build_call_with_param(
"__quantum__qis__m__body",
&["QIR", "Intrinsic"],
"M",
&["Std", "Intrinsic"],
expr,
name_span,
operand_span,
stmt_span,
)
}

pub(crate) fn build_measureeachz_call(
expr: ast::Expr,
name_span: Span,
operand_span: Span,
stmt_span: Span,
) -> ast::Expr {
build_call_with_param(
"MeasureEachZ",
&["Std", "Measurement"],
expr,
name_span,
operand_span,
Expand Down
27 changes: 19 additions & 8 deletions compiler/qsc_qasm/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ use crate::{
build_lit_angle_expr, build_lit_bigint_expr, build_lit_bool_expr, build_lit_complex_expr,
build_lit_double_expr, build_lit_int_expr, build_lit_result_array_expr_from_bitstring,
build_lit_result_expr, build_managed_qubit_alloc, build_math_call_from_exprs,
build_math_call_no_params, build_measure_call, build_operation_with_stmts,
build_path_ident_expr, build_path_ident_ty, build_qasm_import_decl,
build_qasm_import_items, build_qasmstd_convert_call_with_two_params, build_range_expr,
build_reset_all_call, build_reset_call, build_return_expr, build_return_unit,
build_stmt_semi_from_expr, build_stmt_semi_from_expr_with_span, build_ternary_update_expr,
build_math_call_no_params, build_measure_call, build_measureeachz_call,
build_operation_with_stmts, build_path_ident_expr, build_path_ident_ty,
build_qasm_import_decl, build_qasm_import_items,
build_qasmstd_convert_call_with_two_params, build_range_expr, build_reset_all_call,
build_reset_call, build_return_expr, build_return_unit, build_stmt_semi_from_expr,
build_stmt_semi_from_expr_with_span, build_ternary_update_expr,
build_top_level_ns_with_items, build_tuple_expr, build_unary_op_expr,
build_unmanaged_qubit_alloc, build_unmanaged_qubit_alloc_array, build_while_stmt,
build_wrapped_block_expr, managed_qubit_alloc_array, map_qsharp_type_to_ast_ty,
Expand Down Expand Up @@ -1164,7 +1165,7 @@ impl QasmCompiler {
semast::ExprKind::Cast(cast) => self.compile_cast_expr(cast),
semast::ExprKind::IndexExpr(index_expr) => self.compile_index_expr(index_expr),
semast::ExprKind::Paren(pexpr) => self.compile_paren_expr(pexpr, expr.span),
semast::ExprKind::Measure(expr) => self.compile_measure_expr(expr),
semast::ExprKind::Measure(mexpr) => self.compile_measure_expr(mexpr, &expr.ty),
}
}

Expand Down Expand Up @@ -1445,12 +1446,22 @@ impl QasmCompiler {
wrap_expr_in_parens(expr, span)
}

fn compile_measure_expr(&mut self, expr: &MeasureExpr) -> qsast::Expr {
fn compile_measure_expr(
&mut self,
expr: &MeasureExpr,
ty: &crate::semantic::types::Type,
) -> qsast::Expr {
assert!(matches!(ty, Type::BitArray(..) | Type::Bit(..)));

let call_span = expr.span;
let name_span = expr.measure_token_span;
let arg = self.compile_gate_operand(&expr.operand);
let operand_span = expr.operand.span;
build_measure_call(arg, name_span, operand_span, call_span)
if matches!(ty, Type::Bit(..)) {
build_measure_call(arg, name_span, operand_span, call_span)
} else {
build_measureeachz_call(arg, name_span, operand_span, call_span)
}
}

fn compile_gate_operand(&mut self, op: &GateOperand) -> qsast::Expr {
Expand Down
16 changes: 15 additions & 1 deletion compiler/qsc_qasm/src/semantic/lowerer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2640,15 +2640,19 @@ impl Lowerer {
}

fn lower_measure_expr(&mut self, expr: &syntax::MeasureExpr) -> semantic::Expr {
let operand = self.lower_gate_operand(&expr.operand);
let ty = get_measurement_ty_from_gate_operand(&operand);

let measurement = semantic::MeasureExpr {
span: expr.span,
measure_token_span: expr.measure_token_span,
operand: self.lower_gate_operand(&expr.operand),
};

semantic::Expr {
span: expr.span,
kind: Box::new(semantic::ExprKind::Measure(measurement)),
ty: Type::Bit(false),
ty,
}
}

Expand Down Expand Up @@ -3876,6 +3880,16 @@ fn wrap_expr_in_cast_expr(ty: Type, rhs: semantic::Expr) -> semantic::Expr {
}
}

fn get_measurement_ty_from_gate_operand(operand: &semantic::GateOperand) -> Type {
if let semantic::GateOperandKind::Expr(ref expr) = &operand.kind {
if let Type::QubitArray(size) = expr.ty {
return Type::BitArray(size, false);
}
}

Type::Bit(false)
}

/// +----------------+-------------------------------------------------------------+
/// | Allowed casts | Casting To |
/// +----------------+-------+-----+------+-------+-------+-----+----------+-------+
Expand Down
4 changes: 2 additions & 2 deletions compiler/qsc_qasm/src/tests/expression/function_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ fn funcall_with_qubit_argument() -> miette::Result<(), Vec<Report>> {
expect![[r#"
import Std.OpenQASM.Intrinsic.*;
operation parity(qs : Qubit[]) : Result {
mutable a = QIR.Intrinsic.__quantum__qis__m__body(qs[0]);
mutable b = QIR.Intrinsic.__quantum__qis__m__body(qs[1]);
mutable a = Std.Intrinsic.M(qs[0]);
mutable b = Std.Intrinsic.M(qs[1]);
return if Std.OpenQASM.Convert.ResultAsInt(a) ^^^ Std.OpenQASM.Convert.ResultAsInt(b) == 0 {
One
} else {
Expand Down
22 changes: 11 additions & 11 deletions compiler/qsc_qasm/src/tests/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ fn using_re_semantics_removes_output() -> miette::Result<(), Vec<Report>> {
rz(Std.OpenQASM.Angle.DoubleAsAngle(theta, 53), q[0]);
h(q[0]);
cx(q[0], q[1]);
set c w/= 0 <- QIR.Intrinsic.__quantum__qis__m__body(q[0]);
set c w/= 1 <- QIR.Intrinsic.__quantum__qis__m__body(q[1]);
set c w/= 0 <- Std.Intrinsic.M(q[0]);
set c w/= 1 <- Std.Intrinsic.M(q[1]);
}
}"#]]
.assert_eq(&qsharp);
Expand Down Expand Up @@ -98,8 +98,8 @@ fn using_qasm_semantics_captures_all_classical_decls_as_output() -> miette::Resu
rz(Std.OpenQASM.Angle.DoubleAsAngle(theta, 53), q[0]);
h(q[0]);
cx(q[0], q[1]);
set c w/= 0 <- QIR.Intrinsic.__quantum__qis__m__body(q[0]);
set c w/= 1 <- QIR.Intrinsic.__quantum__qis__m__body(q[1]);
set c w/= 0 <- Std.Intrinsic.M(q[0]);
set c w/= 1 <- Std.Intrinsic.M(q[1]);
(c, gamma, delta)
}
}"#]]
Expand Down Expand Up @@ -147,8 +147,8 @@ fn using_qiskit_semantics_only_bit_array_is_captured_and_reversed(
rz(Std.OpenQASM.Angle.DoubleAsAngle(theta, 53), q[0]);
h(q[0]);
cx(q[0], q[1]);
set c w/= 0 <- QIR.Intrinsic.__quantum__qis__m__body(q[0]);
set c w/= 1 <- QIR.Intrinsic.__quantum__qis__m__body(q[1]);
set c w/= 0 <- Std.Intrinsic.M(q[0]);
set c w/= 1 <- Std.Intrinsic.M(q[1]);
Std.Arrays.Reversed(c)
}
}"#]]
Expand Down Expand Up @@ -208,11 +208,11 @@ c2[2] = measure q[4];
x(q[2]);
id(q[3]);
x(q[4]);
set c w/= 0 <- QIR.Intrinsic.__quantum__qis__m__body(q[0]);
set c w/= 1 <- QIR.Intrinsic.__quantum__qis__m__body(q[1]);
set c2 w/= 0 <- QIR.Intrinsic.__quantum__qis__m__body(q[2]);
set c2 w/= 1 <- QIR.Intrinsic.__quantum__qis__m__body(q[3]);
set c2 w/= 2 <- QIR.Intrinsic.__quantum__qis__m__body(q[4]);
set c w/= 0 <- Std.Intrinsic.M(q[0]);
set c w/= 1 <- Std.Intrinsic.M(q[1]);
set c2 w/= 0 <- Std.Intrinsic.M(q[2]);
set c2 w/= 1 <- Std.Intrinsic.M(q[3]);
set c2 w/= 2 <- Std.Intrinsic.M(q[4]);
(Std.Arrays.Reversed(c2), Std.Arrays.Reversed(c))
}
}"#]]
Expand Down
4 changes: 2 additions & 2 deletions compiler/qsc_qasm/src/tests/statement/if_stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn can_use_cond_with_implicit_cast_to_bool() -> miette::Result<(), Vec<Report>>
import Std.OpenQASM.Intrinsic.*;
let q = QIR.Runtime.__quantum__rt__qubit_allocate();
h(q);
mutable result = QIR.Intrinsic.__quantum__qis__m__body(q);
mutable result = Std.Intrinsic.M(q);
if Std.OpenQASM.Convert.ResultAsBool(result) {
Reset(q);
};
Expand All @@ -50,7 +50,7 @@ fn can_use_negated_cond_with_implicit_cast_to_bool() -> miette::Result<(), Vec<R
import Std.OpenQASM.Intrinsic.*;
let q = QIR.Runtime.__quantum__rt__qubit_allocate();
h(q);
mutable result = QIR.Intrinsic.__quantum__qis__m__body(q);
mutable result = Std.Intrinsic.M(q);
if not Std.OpenQASM.Convert.ResultAsBool(result) {
Reset(q);
};
Expand Down
2 changes: 1 addition & 1 deletion compiler/qsc_qasm/src/tests/statement/include.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn programs_with_includes_can_be_parsed() -> miette::Result<(), Vec<Report>> {
mutable c = [Zero];
let q = QIR.Runtime.AllocateQubitArray(1);
my_gate(q[0]);
set c w/= 0 <- QIR.Intrinsic.__quantum__qis__m__body(q[0]);
set c w/= 0 <- Std.Intrinsic.M(q[0]);
Std.Arrays.Reversed(c)
}
}"#]]
Expand Down
Loading