-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add bitcode/qir conversion functions to generator (#115)
* Adding new functions * fixing bitcode python return type.
- Loading branch information
Showing
7 changed files
with
243 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
|
||
from pyqir.generator import BasicQisBuilder, SimpleModule, ir_to_bitcode, bitcode_to_ir | ||
|
||
|
||
def get_module() -> SimpleModule: | ||
mod = SimpleModule("test", 1, 1) | ||
qis = BasicQisBuilder(mod.builder) | ||
qis.m(mod.qubits[0], mod.results[0]) | ||
return mod | ||
|
||
def test_ir_round_trip_is_identical() -> None: | ||
expected_ir = get_module().ir() | ||
bitcode = ir_to_bitcode(expected_ir, "test") | ||
converted_ir = bitcode_to_ir(bitcode, "test") | ||
assert expected_ir == converted_ir | ||
|
||
def test_ir_round_trip_is_not_identical_when_module_name_isnot_supplied() -> None: | ||
expected_ir = get_module().ir() | ||
bitcode = ir_to_bitcode(expected_ir) | ||
converted_ir = bitcode_to_ir(bitcode) | ||
assert expected_ir != converted_ir | ||
|
||
def test_module_name_persists_in_conversion() -> None: | ||
expected_ir = get_module().ir() | ||
bitcode = ir_to_bitcode(expected_ir, "test") | ||
converted_ir = bitcode_to_ir(bitcode, "test2") | ||
assert expected_ir != converted_ir | ||
assert "; ModuleID = 'test2'" in converted_ir | ||
|
||
def test_file_name_persists_in_conversion() -> None: | ||
expected_ir = get_module().ir() | ||
bitcode = ir_to_bitcode(expected_ir, "test", "some file") | ||
converted_ir = bitcode_to_ir(bitcode, "test", "some other file") | ||
assert expected_ir != converted_ir | ||
assert 'source_filename = "some other file"' in converted_ir | ||
|
||
def test_ir_to_bitcode_returns_bytes_type() -> None: | ||
expected_ir = get_module().ir() | ||
bitcode = ir_to_bitcode(expected_ir, "test") | ||
assert isinstance(bitcode, bytes) | ||
|
||
def test_bitcode_to_ir_returns_str_type() -> None: | ||
expected_ir = get_module().ir() | ||
bitcode = ir_to_bitcode(expected_ir, "test") | ||
converted_ir = bitcode_to_ir(bitcode, "test") | ||
assert isinstance(converted_ir, str) | ||
|
||
def test_bitcode_returns_bytes_type() -> None: | ||
bitcode = get_module().bitcode() | ||
assert isinstance(bitcode, bytes) | ||
|
||
def test_ir_returns_str_type() -> None: | ||
expected_ir = get_module().ir() | ||
assert isinstance(expected_ir, str) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,121 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
use inkwell::{context::Context, memory_buffer::MemoryBuffer}; | ||
|
||
use crate::module; | ||
|
||
pub mod emit; | ||
pub mod interop; | ||
pub mod qir; | ||
|
||
/// # Errors | ||
/// | ||
/// Will return `Err` if a module cannot be created from the supplied IR | ||
pub fn ir_to_bitcode( | ||
value: &str, | ||
module_name: &Option<String>, | ||
source_file_name: &Option<String>, | ||
) -> Result<Vec<u8>, String> { | ||
let context = Context::create(); | ||
let bytes = value.as_bytes(); | ||
let buffer_name = match module_name { | ||
Some(name) => name.as_str(), | ||
None => "", | ||
}; | ||
let memory_buffer = MemoryBuffer::create_from_memory_range_copy(bytes, buffer_name); | ||
let module = context | ||
.create_module_from_ir(memory_buffer) | ||
.map_err(|err| err.to_string())?; | ||
|
||
if let Some(source_name) = source_file_name { | ||
module.set_source_file_name(source_name.as_str()); | ||
} | ||
|
||
let bitcode = module.write_bitcode_to_memory().as_slice().to_owned(); | ||
Ok(bitcode) | ||
} | ||
|
||
/// # Errors | ||
/// | ||
/// Will return `Err` if a module cannot be created from the supplied bitcode | ||
pub fn bitcode_to_ir( | ||
value: &[u8], | ||
module_name: &Option<String>, | ||
source_file_name: &Option<String>, | ||
) -> Result<String, String> { | ||
let context = Context::create(); | ||
let buffer_name = match module_name.as_ref() { | ||
Some(name) => name.as_str(), | ||
None => "", | ||
}; | ||
let module = module::load_memory(value, buffer_name, &context)?; | ||
|
||
if let Some(source_name) = source_file_name.as_ref() { | ||
module.set_source_file_name(source_name.as_str()); | ||
} | ||
|
||
let ir = module.print_to_string().to_string(); | ||
|
||
Ok(ir) | ||
} | ||
|
||
#[cfg(test)] | ||
mod module_conversion_tests { | ||
use std::collections::HashMap; | ||
|
||
use crate::generation::emit; | ||
|
||
use super::interop::{ | ||
ClassicalRegister, Instruction, Measured, QuantumRegister, SemanticModel, | ||
}; | ||
|
||
use super::*; | ||
|
||
fn get_model( | ||
name: String, | ||
use_static_qubit_alloc: bool, | ||
use_static_result_alloc: bool, | ||
) -> SemanticModel { | ||
SemanticModel { | ||
name, | ||
registers: vec![ClassicalRegister::new("r".to_string(), 1)], | ||
qubits: vec![QuantumRegister::new("q".to_string(), 0)], | ||
instructions: vec![Instruction::M(Measured::new( | ||
"q0".to_string(), | ||
"r0".to_string(), | ||
))], | ||
use_static_qubit_alloc, | ||
use_static_result_alloc, | ||
external_functions: HashMap::new(), | ||
} | ||
} | ||
|
||
#[test] | ||
fn ir_round_trip_is_identical() -> Result<(), String> { | ||
let model = get_model("test".to_owned(), false, false); | ||
let actual_ir: String = emit::ir(&model)?; | ||
let bitcode = ir_to_bitcode(actual_ir.as_str(), &None, &None)?; | ||
let converted_ir = bitcode_to_ir( | ||
bitcode.as_slice(), | ||
&Some("test".to_owned()), | ||
&Some("test".to_owned()), | ||
)?; | ||
assert_eq!(actual_ir, converted_ir); | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn module_name_is_normalized() -> Result<(), String> { | ||
let model = get_model("tests".to_owned(), false, false); | ||
let actual_ir: String = emit::ir(&model)?; | ||
let bitcode = ir_to_bitcode(actual_ir.as_str(), &None, &None)?; | ||
let converted_ir = bitcode_to_ir( | ||
bitcode.as_slice(), | ||
&Some("tests".to_owned()), | ||
&Some("tests".to_owned()), | ||
)?; | ||
assert_eq!(actual_ir, converted_ir); | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters