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

implement compile_protoquil #15

Merged
merged 2 commits into from
Dec 2, 2022
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
43 changes: 43 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ pub fn compile_program(program: &Program, chip: &Chip) -> Program {
Program(compiled_program)
}

pub fn compile_protoquil(program: &Program, chip: &Chip) -> Program {
init_libquilc();
let mut compiled_program: quil_program = std::ptr::null_mut();

unsafe {
quilc_compile_protoquil.unwrap()(program.0, chip.0, &mut compiled_program);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this fail? Is there an errno we should be checking or anything?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point to bring up. I wondered about this while writing the original bindings, and, IIRC, you do get back an error but there isn't much context to go along with it. One of those things that needs improving on the quil-lang side.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, what Mark said. The error is a generic ERROR_FAIL message. Implementation here. We could implement it in all the methods now, but seems better to revisit when we actually get some real errors back from the lib side.

}

Program(compiled_program)
}

/// Get an arbritrary Chip.
pub fn get_chip() -> Chip {
init_libquilc();
Expand All @@ -76,3 +87,35 @@ pub fn print_program(program: &Program) {
quilc_print_program.unwrap()(program.0);
}
}

#[cfg(test)]
mod tests {
use super::*;

fn new_quil_program() -> Program {
let sample_quil = r#"
DECLARE ro BIT[2]
DECLARE theta REAL
RX(theta) 0
X 0
CNOT 0 1
MEASURE 0 ro[0]
MEASURE 1 ro[1]
"#
.to_string();

parse_program(sample_quil)
}

#[test]
fn test_compile_protoquil() {
let program = new_quil_program();
let chip = get_chip();
compile_protoquil(&program, &chip);

// Since there is no way to inspect the return compiled program yet,
// just make sure the code doesn't panic before getting to this point.
// See: https://github.com/rigetti/libquil-sys/issues/12
assert!(false)
}
}