Skip to content

bitstring processing is more efficient #2408

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

Merged
merged 1 commit into from
May 15, 2025
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
23 changes: 3 additions & 20 deletions compiler/qsc_qasm/src/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,30 +211,13 @@ pub(crate) fn build_lit_result_expr(value: qsc_ast::ast::Result, span: Span) ->
}
}

pub(crate) fn build_lit_result_array_expr_from_bitstring<S: AsRef<str>>(
bitstring: S,
pub(crate) fn build_lit_result_array_expr<I: IntoIterator<Item = ast::Result>>(
values: I,
span: Span,
) -> Expr {
let values = bitstring
.as_ref()
.chars()
.filter_map(|c| {
if c == '0' {
Some(ast::Result::Zero)
} else if c == '1' {
Some(ast::Result::One)
} else {
None
}
})
.collect();
build_lit_result_array_expr(values, span)
}

pub(crate) fn build_lit_result_array_expr(values: Vec<qsc_ast::ast::Result>, span: Span) -> Expr {
let exprs: Vec<_> = values
.into_iter()
.map(|v| build_lit_result_expr(v, Span::default()))
.map(|value| build_lit_result_expr(value, Span::default()))
.collect();
build_expr_array_expr(exprs, span)
}
Expand Down
27 changes: 22 additions & 5 deletions compiler/qsc_qasm/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{
build_if_expr_then_block_else_expr, build_if_expr_then_expr_else_expr,
build_implicit_return_stmt, build_index_expr, build_indexed_assignment_statement,
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_double_expr, build_lit_int_expr, build_lit_result_array_expr,
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,
Expand Down Expand Up @@ -1540,12 +1540,29 @@ impl QasmCompiler {

fn compile_bitstring_literal(value: &BigInt, width: u32, span: Span) -> qsast::Expr {
let width = width as usize;
let bitstring = if value == &BigInt::ZERO && width == 0 {
"Bitstring(\"\")".to_string()
// Handle the special case where the value is zero and width is zero
if value == &BigInt::ZERO && width == 0 {
return build_lit_result_array_expr(vec![], span);
}

let binary = value.to_str_radix(2).into_bytes().into_iter().map(|b| {
// the string bytes are ASCII bytes, so we check their value offset from b'0'
if (b - b'0') == 0 {
qsast::Result::Zero
} else {
qsast::Result::One
}
});
// Pad the binary representation with leading zeros to match the width
let values = if binary.len() < width {
let mut padded = vec![qsast::Result::Zero; width - binary.len()];
padded.extend(binary);
padded
} else {
format!("Bitstring(\"{:0>width$}\")", value.to_str_radix(2))
binary.collect()
};
build_lit_result_array_expr_from_bitstring(bitstring, span)

build_lit_result_array_expr(values, span)
}

fn compile_complex_literal(real: f64, imag: f64, span: Span) -> qsast::Expr {
Expand Down
42 changes: 24 additions & 18 deletions compiler/qsc_qasm/src/tests/fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,24 +96,6 @@ fn fuzz_2368() {
compile_qasm_best_effort(source, Profile::Unrestricted);
}

#[test]
fn fuzz_2379() {
let source = "1[true:";
compile_qasm_best_effort(source, Profile::Unrestricted);
}

#[test]
fn fuzz_2391() {
let source = "c[:0s";
compile_qasm_best_effort(source, Profile::Unrestricted);
}

#[test]
fn fuzz_2392() {
let source = "e[π:";
compile_qasm_best_effort(source, Profile::Unrestricted);
}

#[test]
fn fuzz_2369() {
let source = r#"// Ope0 standard gate) mibrary
Expand Down Expand Up @@ -154,3 +136,27 @@ gaoooooootoe i {adnv @ pow(0.` docume5) @ pow(0.` docume5) nta inverse of sqrt(S
gaoooooootoe tg i {adnv @ pow(0.` docume)5@ pow(0.` docume5) ntation for full @ staildnv @ pow(0.` docume)5@ pow(0.` docume5) ntation for full @ stail."#;
compile_qasm_best_effort(source, Profile::Unrestricted);
}

#[test]
fn fuzz_2379() {
let source = "1[true:";
compile_qasm_best_effort(source, Profile::Unrestricted);
}

#[test]
fn fuzz_2391() {
let source = "c[:0s";
compile_qasm_best_effort(source, Profile::Unrestricted);
}

#[test]
fn fuzz_2392() {
let source = "e[π:";
compile_qasm_best_effort(source, Profile::Unrestricted);
}

#[test]
fn fuzz_2397() {
let source = "creg a[551615";
compile_qasm_best_effort(source, Profile::Unrestricted);
}