Skip to content

Commit

Permalink
Add Rust bindings stub
Browse files Browse the repository at this point in the history
  • Loading branch information
alecandido committed Jan 22, 2024
1 parent e5ced83 commit dfefb5e
Show file tree
Hide file tree
Showing 4 changed files with 338 additions and 0 deletions.
287 changes: 287 additions & 0 deletions crate/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions crate/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "qibolab"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
pyo3 = { version = "0.20.2", features = ["auto-initialize"] }

[dev-dependencies]
anyhow = "1.0.79"
22 changes: 22 additions & 0 deletions crate/examples/example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use anyhow::Result;
use qibolab::execute_qasm;

const CODE: &str = r#"
OPENQASM 2.0;
include "qelib1.inc";
qreg q[3];
creg a[2];
cx q[0],q[2];
x q[1];
swap q[0],q[1];
cx q[1],q[0];
measure q[0] -> a[0];
measure q[2] -> a[1];,
"#;

fn main() -> Result<()> {
let res = execute_qasm(CODE.to_owned(), "dummy".to_owned(), 10)?;
println!("{:#?}", res);

Ok(())
}
17 changes: 17 additions & 0 deletions crate/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use pyo3::prelude::*;

pub fn execute_qasm(circuit: String, platform: String, nshots: u32) -> PyResult<Vec<u32>> {
// TODO: move to the example, here for debug
println!(
"---\nExecuting:\n'''{}'''\n\non: {}\nwith: {} shots\n---\n",
circuit, platform, nshots
);

Python::with_gil(|py| {
let qibolab = PyModule::import(py, "qibolab")?;
qibolab
.getattr("execute_qasm")?
.call1((circuit, platform, nshots))?
.extract()
})
}

0 comments on commit dfefb5e

Please sign in to comment.