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
16 changes: 16 additions & 0 deletions examples/bell_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use libquil_sys::{compile_program, get_chip, parse_program, print_program};

const PROGRAM: &str = r#"
DECLARE ro BIT[2]
DECLARE theta REAL
RX(theta) 0
X 0
CNOT 0 1
"#;

fn main() {
let parsed_program = parse_program(PROGRAM.to_string());
let chip = get_chip();
let compiled_program = compile_program(&parsed_program, &chip);
print_program(&compiled_program)
}
57 changes: 32 additions & 25 deletions src/main.rs → src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,33 @@
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

use std::sync::Once;

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

struct Chip(chip_specification);
#[derive(Debug)]
pub struct Chip(chip_specification);

#[derive(Debug)]
pub struct Program(quil_program);

static START: Once = Once::new();

struct Program(quil_program);
/// Initializes libquilc using it's core image. No-op after the first call.
fn init_libquilc() {
START.call_once(|| {
let bytes = b"libquilc.core\0".to_vec();
let mut c_chars: Vec<i8> = bytes.iter().map(|c| *c as i8).collect();
let ptr = c_chars.as_mut_ptr();
unsafe {
init(ptr);
}
})
}

fn parse_program(program: String) -> Program {
/// Parses a String into a Program object for use with other libquil calls.
pub fn parse_program(program: String) -> Program {
init_libquilc();
let mut c_chars: Vec<i8> = program
.as_bytes()
.to_vec()
Expand All @@ -25,7 +45,9 @@ fn parse_program(program: String) -> Program {
Program(parsed_program)
}

fn compile_program(program: &Program, chip: &Chip) -> Program {
/// Compiles the program, optimized for the given Chip.
pub fn compile_program(program: &Program, chip: &Chip) -> Program {
init_libquilc();
let mut compiled_program: quil_program = std::ptr::null_mut();

unsafe {
Expand All @@ -35,7 +57,9 @@ fn compile_program(program: &Program, chip: &Chip) -> Program {
Program(compiled_program)
}

fn get_chip() -> Chip {
/// Get an arbritrary Chip.
pub fn get_chip() -> Chip {
init_libquilc();
let mut chip: chip_specification = std::ptr::null_mut();

unsafe {
Expand All @@ -45,27 +69,10 @@ fn get_chip() -> Chip {
Chip(chip)
}

fn print_program(program: &Program) {
/// Prints the given Program to stdout
pub fn print_program(program: &Program) {
init_libquilc();
unsafe {
quilc_print_program.unwrap()(program.0);
}
}

fn init_libquilc() {
let bytes = b"libquilc.core\0".to_vec();
let mut c_chars: Vec<i8> = bytes.iter().map(|c| *c as i8).collect();
let ptr = c_chars.as_mut_ptr();
unsafe {
init(ptr);
}
}

fn main() {
init_libquilc();

let program = parse_program("H 0".to_string());
print_program(&program);
let chip = get_chip();
let program = compile_program(&program, &chip);
print_program(&program);
}