Skip to content

Commit

Permalink
Bind missing base profile calls
Browse files Browse the repository at this point in the history
__quantum__rt__initialize() and __quantum__rt__tuple_record_output()
existed deeper in the code but were not bound in the ExecutionEngine,
so this commit addresses this.

I also added some QIR examples generated with the Qwerty compiler to
examples/.
  • Loading branch information
ausbin authored and sethrj committed Jun 25, 2024
1 parent 001447b commit 2955636
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 21 deletions.
43 changes: 43 additions & 0 deletions examples/qwerty/bv1101.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
; ModuleID = 'qwerty'
source_filename = "qwerty"

@tag_ret = internal constant [4 x i8] c"ret\00"
@tag_discarded = internal constant [10 x i8] c"discarded\00"

define i64 @kernel() #0 {
call void @__quantum__rt__initialize(ptr null)
call void @__quantum__qis__x__body(ptr null)
call void @__quantum__qis__x__body(ptr inttoptr (i64 1 to ptr))
call void @__quantum__qis__x__body(ptr inttoptr (i64 3 to ptr))
call void @__quantum__qis__mz__body(ptr null, ptr null)
call void @__quantum__qis__mz__body(ptr inttoptr (i64 1 to ptr), ptr inttoptr (i64 1 to ptr))
call void @__quantum__qis__mz__body(ptr inttoptr (i64 2 to ptr), ptr inttoptr (i64 2 to ptr))
call void @__quantum__qis__mz__body(ptr inttoptr (i64 3 to ptr), ptr inttoptr (i64 3 to ptr))
call void @__quantum__rt__tuple_record_output(i64 4, ptr @tag_ret)
call void @__quantum__rt__result_record_output(ptr null, ptr null)
call void @__quantum__rt__result_record_output(ptr inttoptr (i64 1 to ptr), ptr null)
call void @__quantum__rt__result_record_output(ptr inttoptr (i64 2 to ptr), ptr null)
call void @__quantum__rt__result_record_output(ptr inttoptr (i64 3 to ptr), ptr null)
call void @__quantum__rt__tuple_record_output(i64 0, ptr @tag_discarded)
ret i64 0
}

declare void @__quantum__qis__x__body(ptr %0)

declare void @__quantum__rt__initialize(ptr %0)

declare void @__quantum__qis__mz__body(ptr %0, ptr %1)

declare void @__quantum__rt__tuple_record_output(i64 %0, ptr %1)

declare void @__quantum__rt__result_record_output(ptr %0, ptr %1)

attributes #0 = { "entry_point" "output_labeling_schema"="qwerty_v1" "qir_profiles"="base_profile" "required_num_qubits"="4" "required_num_results"="4" }

!llvm.module.flags = !{!0, !1, !2, !3, !4}

!0 = !{i32 2, !"Debug Info Version", i32 3}
!1 = !{i32 1, !"dynamic_qubit_management", i1 false}
!2 = !{i32 1, !"dynamic_result_management", i1 false}
!3 = !{i32 1, !"qir_major_version", i32 1}
!4 = !{i32 1, !"qir_minor_version", i32 0}
49 changes: 49 additions & 0 deletions examples/qwerty/ghz5.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
; ModuleID = 'qwerty'
source_filename = "qwerty"

@tag_ret = internal constant [4 x i8] c"ret\00"
@tag_discarded = internal constant [10 x i8] c"discarded\00"

define i64 @kernel() #0 {
call void @__quantum__rt__initialize(ptr null)
call void @__quantum__qis__h__body(ptr null)
call void @__quantum__qis__cx__body(ptr null, ptr inttoptr (i64 1 to ptr))
call void @__quantum__qis__cx__body(ptr null, ptr inttoptr (i64 2 to ptr))
call void @__quantum__qis__cx__body(ptr null, ptr inttoptr (i64 3 to ptr))
call void @__quantum__qis__cx__body(ptr null, ptr inttoptr (i64 4 to ptr))
call void @__quantum__qis__mz__body(ptr null, ptr null)
call void @__quantum__qis__mz__body(ptr inttoptr (i64 1 to ptr), ptr inttoptr (i64 1 to ptr))
call void @__quantum__qis__mz__body(ptr inttoptr (i64 2 to ptr), ptr inttoptr (i64 2 to ptr))
call void @__quantum__qis__mz__body(ptr inttoptr (i64 3 to ptr), ptr inttoptr (i64 3 to ptr))
call void @__quantum__qis__mz__body(ptr inttoptr (i64 4 to ptr), ptr inttoptr (i64 4 to ptr))
call void @__quantum__rt__tuple_record_output(i64 5, ptr @tag_ret)
call void @__quantum__rt__result_record_output(ptr null, ptr null)
call void @__quantum__rt__result_record_output(ptr inttoptr (i64 1 to ptr), ptr null)
call void @__quantum__rt__result_record_output(ptr inttoptr (i64 2 to ptr), ptr null)
call void @__quantum__rt__result_record_output(ptr inttoptr (i64 3 to ptr), ptr null)
call void @__quantum__rt__result_record_output(ptr inttoptr (i64 4 to ptr), ptr null)
call void @__quantum__rt__tuple_record_output(i64 0, ptr @tag_discarded)
ret i64 0
}

declare void @__quantum__qis__h__body(ptr %0)

declare void @__quantum__qis__cx__body(ptr %0, ptr %1)

declare void @__quantum__rt__initialize(ptr %0)

declare void @__quantum__qis__mz__body(ptr %0, ptr %1)

declare void @__quantum__rt__tuple_record_output(i64 %0, ptr %1)

declare void @__quantum__rt__result_record_output(ptr %0, ptr %1)

attributes #0 = { "entry_point" "output_labeling_schema"="qwerty_v1" "qir_profiles"="base_profile" "required_num_qubits"="5" "required_num_results"="5" }

!llvm.module.flags = !{!0, !1, !2, !3, !4}

!0 = !{i32 2, !"Debug Info Version", i32 3}
!1 = !{i32 1, !"dynamic_qubit_management", i1 false}
!2 = !{i32 1, !"dynamic_result_management", i1 false}
!3 = !{i32 1, !"qir_major_version", i32 1}
!4 = !{i32 1, !"qir_minor_version", i32 0}
11 changes: 10 additions & 1 deletion src/qiree/Executor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,18 @@ void QIREE_QIS_FUNCTION(assertmeasurementprobability, ctl)(std::uintptr_t arg1,
//---------------------------------------------------------------------------//
// RUNTIME
//---------------------------------------------------------------------------//
void QIREE_RT_FUNCTION(initialize)(OptionalCString env)
{
return r_interface_->initialize(env);
}
void QIREE_RT_FUNCTION(array_record_output)(size_type s, OptionalCString tag)
{
return r_interface_->array_record_output(s, tag);
}

void QIREE_RT_FUNCTION(tuple_record_output)(size_type s, OptionalCString tag)
{
return r_interface_->tuple_record_output(s, tag);
}
void QIREE_RT_FUNCTION(result_record_output)(std::uintptr_t r,
OptionalCString tag)
{
Expand Down Expand Up @@ -391,7 +398,9 @@ Executor::Executor(Module&& module)
QIREE_BIND_QIS_FUNCTION(assertmeasurementprobability, body);
QIREE_BIND_QIS_FUNCTION(assertmeasurementprobability, ctl);

QIREE_BIND_RT_FUNCTION(initialize);
QIREE_BIND_RT_FUNCTION(array_record_output);
QIREE_BIND_RT_FUNCTION(tuple_record_output);
QIREE_BIND_RT_FUNCTION(result_record_output);
#undef QIREE_BIND_RT_FUNCTION
#undef QIREE_BIND_QIS_FUNCTION
Expand Down
11 changes: 5 additions & 6 deletions src/qiree/RuntimeInterface.hh
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,15 @@ class RuntimeInterface
//! Initialize the execution environment, resetting qubits
virtual void initialize(OptionalCString env) = 0;

//! Prepare to store N results (?)
//! Mark the following N results as being part of an array named tag
virtual void array_record_output(size_type, OptionalCString tag) = 0;

//! Record one result into the progrma output
virtual void result_record_output(Result result, OptionalCString tag) = 0;

//! Prepare to store N tuples
//! Mark the following N results as being part of a tuple named tag
virtual void tuple_record_output(size_type, OptionalCString tag) = 0;

protected:
//! Record one result into the program output
virtual void result_record_output(Result result, OptionalCString tag) = 0;

virtual ~RuntimeInterface() = default;
};

Expand Down
8 changes: 4 additions & 4 deletions test/qiree/QuantumTestImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,9 @@ void ResultTestImpl::array_record_output(size_type s, OptionalCString tag)
/*!
* Mark the start of an array and its size.
*/
void ResultTestImpl::result_record_output(Result result, OptionalCString tag)
void ResultTestImpl::tuple_record_output(size_type s, OptionalCString tag)
{
tr_->commands << "result_record_output(" << result;
tr_->commands << "tuple_record_output(" << s;
if (tag)
{
tr_->commands << ", " << tag;
Expand All @@ -311,9 +311,9 @@ void ResultTestImpl::result_record_output(Result result, OptionalCString tag)
/*!
* Mark the start of an array and its size.
*/
void ResultTestImpl::tuple_record_output(size_type s, OptionalCString tag)
void ResultTestImpl::result_record_output(Result result, OptionalCString tag)
{
tr_->commands << "tuple_record_output(" << s;
tr_->commands << "result_record_output(" << result;
if (tag)
{
tr_->commands << ", " << tag;
Expand Down
12 changes: 6 additions & 6 deletions test/qiree/QuantumTestImpl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,18 @@ class ResultTestImpl final : public RuntimeInterface
// Construct with pointer to modifiable test result
explicit ResultTestImpl(TestResult* result);

//! Initialize the execution environment, resetting qubits
// Initialize the execution environment, resetting qubits
void initialize(OptionalCString env) final;

// Prepare to store N results.
void array_record_output(size_type, OptionalCString tag) final;
// Mark the following N results as being part of an array named tag
void array_record_output(size_type, OptionalCString tag);

// Mark the following N results as being part of a tuple named tag
void tuple_record_output(size_type, OptionalCString tag);

// Store one result
void result_record_output(Result, OptionalCString tag) final;

// No one uses tuples??
void tuple_record_output(size_type, OptionalCString tag) final;

private:
TestResult* tr_;
};
Expand Down
12 changes: 8 additions & 4 deletions test/qirxacc/XaccQuantum.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "qiree/Types.hh"
#include "qiree_test.hh"
#include "qirxacc/XaccDefaultRuntime.hh"

namespace qiree
{
Expand Down Expand Up @@ -65,6 +66,7 @@ TEST_F(XaccQuantumTest, sim_rotation)

// Create a simulator that will write to the string stream
XaccQuantum xacc_sim{os};
XaccDefaultRuntime xacc_rt{os, xacc_sim};

// Call functions in the same sequence that rotation.ll would
xacc_sim.set_up([] {
Expand All @@ -75,9 +77,10 @@ TEST_F(XaccQuantumTest, sim_rotation)
}());
xacc_sim.h(Q{0});
xacc_sim.rx(pi / 6, Q{0});
xacc_sim.h(Q{0});
xacc_sim.mz(Q{0}, R{0});
xacc_sim.array_record_output(1, nullptr);
xacc_sim.result_record_output(R{0}, "tag");
xacc_rt.array_record_output(1, "yeehaw");
xacc_rt.result_record_output(R{0}, "tag");
xacc_sim.tear_down();

auto result = clean_output(os.str());
Expand All @@ -88,11 +91,12 @@ TEST_F(XaccQuantumTest, sim_rotation)
"size": 1,
"Information": {},
"Measurements": {
"": 1
"0": 1
}
}
}
qubit 0 experiment tag: {0: 0, 1: 0}
array yeehaw length 1
qubit 0 experiment tag: {0: 1, 1: 0}
)",
result)
<< result;
Expand Down

0 comments on commit 2955636

Please sign in to comment.