Skip to content

Commit

Permalink
3 guppy examples working
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-q committed Jun 14, 2024
1 parent 90c482c commit 49f9016
Show file tree
Hide file tree
Showing 11 changed files with 837 additions and 25 deletions.
30 changes: 18 additions & 12 deletions src/custom/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ use anyhow::{anyhow, Result};

struct IntOpEmitter<'c, 'd, H: HugrView>(&'d mut EmitFuncContext<'c, H>);

fn emit_icmp<'c, H: HugrView>(
context: &mut EmitFuncContext<'c, H>,
args: EmitOpArgs<'c, CustomOp, H>,
pred: inkwell::IntPredicate,
) -> Result<()> {
let true_val = emit_value(context, &Value::true_val())?;
let false_val = emit_value(context, &Value::false_val())?;
let builder = context.builder();
let [lhs, rhs] = TryInto::<[_; 2]>::try_into(args.inputs).unwrap();
let a = builder.build_int_compare(pred, lhs.into_int_value(), rhs.into_int_value(), "")?;
let a = builder.build_select(a, true_val, false_val, "")?;
args.outputs.finish(builder, [a.into()])
}

impl<'c, H: HugrView> EmitOp<'c, CustomOp, H> for IntOpEmitter<'c, '_, H> {
fn emit(&mut self, args: EmitOpArgs<'c, CustomOp, H>) -> Result<()> {
let iot = ConcreteIntOp::from_optype(&args.node().generalise())
Expand All @@ -37,18 +51,10 @@ impl<'c, H: HugrView> EmitOp<'c, CustomOp, H> for IntOpEmitter<'c, '_, H> {
args.outputs.finish(builder, [a.into()])
}
"ieq" => {
let true_val = emit_value(self.0, &Value::true_val())?;
let false_val = emit_value(self.0, &Value::false_val())?;
let builder = self.0.builder();
let [lhs, rhs] = TryInto::<[_; 2]>::try_into(args.inputs).unwrap();
let a = builder.build_int_compare(
inkwell::IntPredicate::EQ,
lhs.into_int_value(),
rhs.into_int_value(),
"",
)?;
let a = builder.build_select(a, true_val, false_val, "")?;
args.outputs.finish(builder, [a.into()])
emit_icmp(self.0, args, inkwell::IntPredicate::EQ)
}
"ilt_s" => {
emit_icmp(self.0, args, inkwell::IntPredicate::SLT)
}
"isub" => {
let builder = self.0.builder();
Expand Down
27 changes: 26 additions & 1 deletion src/custom/tket2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,32 @@ impl<'c, H: HugrView> EmitOp<'c, CustomOp, H> for Tket2Emitter<'c, '_, H> {
.finish(builder, [call.try_as_basic_value().unwrap_left()])?;
Ok(())
}
"Tdg" => {
let func_type = self.0.llvm_func_type(&FunctionType::new(QB_T, QB_T))?;
let x_func =
self.0
.module()
.add_function("___tdg", func_type, Some(Linkage::External));
let inputs = args.inputs.into_iter().map_into().collect_vec();
let builder = self.0.builder();
let call = builder.build_call(x_func, inputs.as_ref(), "tdg_call")?;
args.outputs
.finish(builder, [call.try_as_basic_value().unwrap_left()])?;
Ok(())
}
"T" => {
let func_type = self.0.llvm_func_type(&FunctionType::new(QB_T, QB_T))?;
let x_func =
self.0
.module()
.add_function("___t", func_type, Some(Linkage::External));
let inputs = args.inputs.into_iter().map_into().collect_vec();
let builder = self.0.builder();
let call = builder.build_call(x_func, inputs.as_ref(), "t_call")?;
args.outputs
.finish(builder, [call.try_as_basic_value().unwrap_left()])?;
Ok(())
}
"RzF64" => {
let func_type = self
.0
Expand Down Expand Up @@ -156,7 +182,6 @@ impl<'c, H: HugrView> EmitOp<'c, CustomOp, H> for Tket2Emitter<'c, '_, H> {
Ok(())
}
"Measure" => {
println!("measure {:?}", opaque);
let func_type = self
.0
.llvm_func_type(&FunctionType::new(QB_T, type_row![QB_T, BOOL_T]))?;
Expand Down
4 changes: 3 additions & 1 deletion tests/guppy_test_cases/planqc-3.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

@guppy(mod)
def rus(q: qubit, tries: int) -> qubit:
for _ in range(tries):
i = 0;
while i < tries:
# Prepare ancillary qubits
a, b = h(qubit()), h(qubit())

Expand All @@ -25,6 +26,7 @@ def rus(q: qubit, tries: int) -> qubit:

# Otherwise, apply correction
q = x(q)
i = i + 1

return q

Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/guppy__even_odd2@llvm14.noopt.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ info:
success: true
exit_code: 0
----- stdout -----
measure OpaqueOp { extension: IdentList("quantum.tket2"), op_name: "Measure", description: "", args: [], signature: FunctionType { input: TypeRow { types: [Type(Extension(CustomType { extension: IdentList("prelude"), id: "qubit", args: [], bound: Any }), Any)] }, output: TypeRow { types: [Type(Extension(CustomType { extension: IdentList("prelude"), id: "qubit", args: [], bound: Any }), Any), Type(Sum(Unit { size: 2 }), Eq)] }, extension_reqs: ExtensionSet({}) } }
; ModuleID = 'module'
source_filename = "module"

Expand Down
3 changes: 1 addition & 2 deletions tests/snapshots/guppy__even_odd2@llvm14.opt.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ source: tests/guppy.rs
info:
program: hugr-llvm
args:
- /tmp/.tmpspOfKF
- "-"
---
success: true
exit_code: 0
----- stdout -----
measure OpaqueOp { extension: IdentList("quantum.tket2"), op_name: "Measure", description: "", args: [], signature: FunctionType { input: TypeRow { types: [Type(Extension(CustomType { extension: IdentList("prelude"), id: "qubit", args: [], bound: Any }), Any)] }, output: TypeRow { types: [Type(Extension(CustomType { extension: IdentList("prelude"), id: "qubit", args: [], bound: Any }), Any), Type(Sum(Unit { size: 2 }), Eq)] }, extension_reqs: ExtensionSet({}) } }
; ModuleID = 'module'
source_filename = "module"

Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/guppy__planqc1@llvm14.noopt.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ info:
success: true
exit_code: 0
----- stdout -----
measure OpaqueOp { extension: IdentList("quantum.tket2"), op_name: "Measure", description: "", args: [], signature: FunctionType { input: TypeRow { types: [Type(Extension(CustomType { extension: IdentList("prelude"), id: "qubit", args: [], bound: Any }), Any)] }, output: TypeRow { types: [Type(Extension(CustomType { extension: IdentList("prelude"), id: "qubit", args: [], bound: Any }), Any), Type(Sum(Unit { size: 2 }), Eq)] }, extension_reqs: ExtensionSet({}) } }
; ModuleID = 'module'
source_filename = "module"

Expand Down
1 change: 0 additions & 1 deletion tests/snapshots/guppy__planqc1@llvm14.opt.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ info:
success: true
exit_code: 0
----- stdout -----
measure OpaqueOp { extension: IdentList("quantum.tket2"), op_name: "Measure", description: "", args: [], signature: FunctionType { input: TypeRow { types: [Type(Extension(CustomType { extension: IdentList("prelude"), id: "qubit", args: [], bound: Any }), Any)] }, output: TypeRow { types: [Type(Extension(CustomType { extension: IdentList("prelude"), id: "qubit", args: [], bound: Any }), Any), Type(Sum(Unit { size: 2 }), Eq)] }, extension_reqs: ExtensionSet({}) } }
; ModuleID = 'module'
source_filename = "module"

Expand Down
3 changes: 0 additions & 3 deletions tests/snapshots/guppy__planqc2@llvm14.noopt.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ info:
success: true
exit_code: 0
----- stdout -----
measure OpaqueOp { extension: IdentList("quantum.tket2"), op_name: "Measure", description: "", args: [], signature: FunctionType { input: TypeRow { types: [Type(Extension(CustomType { extension: IdentList("prelude"), id: "qubit", args: [], bound: Any }), Any)] }, output: TypeRow { types: [Type(Extension(CustomType { extension: IdentList("prelude"), id: "qubit", args: [], bound: Any }), Any), Type(Sum(Unit { size: 2 }), Eq)] }, extension_reqs: ExtensionSet({}) } }
measure OpaqueOp { extension: IdentList("quantum.tket2"), op_name: "Measure", description: "", args: [], signature: FunctionType { input: TypeRow { types: [Type(Extension(CustomType { extension: IdentList("prelude"), id: "qubit", args: [], bound: Any }), Any)] }, output: TypeRow { types: [Type(Extension(CustomType { extension: IdentList("prelude"), id: "qubit", args: [], bound: Any }), Any), Type(Sum(Unit { size: 2 }), Eq)] }, extension_reqs: ExtensionSet({}) } }
measure OpaqueOp { extension: IdentList("quantum.tket2"), op_name: "Measure", description: "", args: [], signature: FunctionType { input: TypeRow { types: [Type(Extension(CustomType { extension: IdentList("prelude"), id: "qubit", args: [], bound: Any }), Any)] }, output: TypeRow { types: [Type(Extension(CustomType { extension: IdentList("prelude"), id: "qubit", args: [], bound: Any }), Any), Type(Sum(Unit { size: 2 }), Eq)] }, extension_reqs: ExtensionSet({}) } }
; ModuleID = 'module'
source_filename = "module"

Expand Down
3 changes: 0 additions & 3 deletions tests/snapshots/guppy__planqc2@llvm14.opt.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ info:
success: true
exit_code: 0
----- stdout -----
measure OpaqueOp { extension: IdentList("quantum.tket2"), op_name: "Measure", description: "", args: [], signature: FunctionType { input: TypeRow { types: [Type(Extension(CustomType { extension: IdentList("prelude"), id: "qubit", args: [], bound: Any }), Any)] }, output: TypeRow { types: [Type(Extension(CustomType { extension: IdentList("prelude"), id: "qubit", args: [], bound: Any }), Any), Type(Sum(Unit { size: 2 }), Eq)] }, extension_reqs: ExtensionSet({}) } }
measure OpaqueOp { extension: IdentList("quantum.tket2"), op_name: "Measure", description: "", args: [], signature: FunctionType { input: TypeRow { types: [Type(Extension(CustomType { extension: IdentList("prelude"), id: "qubit", args: [], bound: Any }), Any)] }, output: TypeRow { types: [Type(Extension(CustomType { extension: IdentList("prelude"), id: "qubit", args: [], bound: Any }), Any), Type(Sum(Unit { size: 2 }), Eq)] }, extension_reqs: ExtensionSet({}) } }
measure OpaqueOp { extension: IdentList("quantum.tket2"), op_name: "Measure", description: "", args: [], signature: FunctionType { input: TypeRow { types: [Type(Extension(CustomType { extension: IdentList("prelude"), id: "qubit", args: [], bound: Any }), Any)] }, output: TypeRow { types: [Type(Extension(CustomType { extension: IdentList("prelude"), id: "qubit", args: [], bound: Any }), Any), Type(Sum(Unit { size: 2 }), Eq)] }, extension_reqs: ExtensionSet({}) } }
; ModuleID = 'module'
source_filename = "module"

Expand Down
Loading

0 comments on commit 49f9016

Please sign in to comment.