Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/measure to reg #87

Merged
merged 6 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 3 additions & 2 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ Changelog
0.5.0 (unreleased)
------------------

* Updated pyqir version requirement to 0.9.0.
* removed depencency of pyqir-generator, pyqir-evaluator and pyqir-parser
* updated pyqir version requirement to 0.9.0.
* removed dependency of pyqir-generator, pyqir-evaluator and pyqir-parser
* update measurement to write to register directly

0.4.0 (October 2023)
--------------------
Expand Down
29 changes: 15 additions & 14 deletions pytket/qir/conversion/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,19 @@ def __init__(
),
)

# void mz_to_reg(qubit, i1* creg, int)
qartik marked this conversation as resolved.
Show resolved Hide resolved
self.mz_to_reg = self.module.module.add_external_function(
"mz_to_reg",
pyqir.FunctionType(
pyqir.Type.void(self.module.module.context),
[
pyqir.qubit_type(self.module.module.context),
self.qir_i1p_type,
self.qir_int_type,
],
),
)

self.reg_const = {}

for creg in self.circuit.c_registers:
Expand Down Expand Up @@ -842,24 +855,12 @@ def condition_block() -> None:
assert len(command.qubits) == 1
assert command.qubits[0].reg_name == "q"

module.qis.mz(
module.module.qubits[command.qubits[0].index[0]],
module.module.results[command.qubits[0].index[0]],
)

ssa_measureresult = self.module.builder.call(
self.read_bit_from_result,
[
module.module.results[command.qubits[0].index[0]],
],
)

self.module.builder.call(
self.set_creg_bit,
self.mz_to_reg,
[
module.module.qubits[command.qubits[0].index[0]],
self.ssa_vars[command.bits[0].reg_name],
pyqir.const(self.qir_int_type, command.bits[0].index[0]),
ssa_measureresult,
],
)

Expand Down
2 changes: 2 additions & 0 deletions tests/qir/test_pytket_qir.ll
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ declare i1* @create_creg(i64)

declare i64 @get_int_from_creg(i1*)

declare void @mz_to_reg(%Qubit*, i1*, i64)

declare void @__quantum__rt__int_record_output(i64, i8*)

declare void @__quantum__rt__tuple_start_record_output()
Expand Down
3 changes: 3 additions & 0 deletions tests/qir/test_pytket_qir_10.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
source_filename = "test_pytket_qir_10"

%Result = type opaque
%Qubit = type opaque

@0 = internal constant [2 x i8] c"a\00"
@1 = internal constant [2 x i8] c"b\00"
Expand Down Expand Up @@ -35,6 +36,8 @@ declare i1* @create_creg(i64)

declare i64 @get_int_from_creg(i1*)

declare void @mz_to_reg(%Qubit*, i1*, i64)

declare void @__quantum__rt__int_record_output(i64, i8*)

declare void @__quantum__rt__tuple_start_record_output()
Expand Down
3 changes: 3 additions & 0 deletions tests/qir/test_pytket_qir_11.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
source_filename = "test_pytket_qir_11"

%Result = type opaque
%Qubit = type opaque

@0 = internal constant [2 x i8] c"a\00"
@1 = internal constant [2 x i8] c"b\00"
Expand Down Expand Up @@ -35,6 +36,8 @@ declare i1* @create_creg(i64)

declare i64 @get_int_from_creg(i1*)

declare void @mz_to_reg(%Qubit*, i1*, i64)

declare void @__quantum__rt__int_record_output(i64, i8*)

declare void @__quantum__rt__tuple_start_record_output()
Expand Down
3 changes: 3 additions & 0 deletions tests/qir/test_pytket_qir_12.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
source_filename = "test_pytket_qir_12"

%Result = type opaque
%Qubit = type opaque

@0 = internal constant [2 x i8] c"a\00"

Expand Down Expand Up @@ -32,6 +33,8 @@ declare i1* @create_creg(i64)

declare i64 @get_int_from_creg(i1*)

declare void @mz_to_reg(%Qubit*, i1*, i64)

declare void @__quantum__rt__int_record_output(i64, i8*)

declare void @__quantum__rt__tuple_start_record_output()
Expand Down
3 changes: 3 additions & 0 deletions tests/qir/test_pytket_qir_13.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
source_filename = "test_pytket_qir_13"

%Result = type opaque
%Qubit = type opaque

@0 = internal constant [2 x i8] c"a\00"
@1 = internal constant [2 x i8] c"b\00"
Expand Down Expand Up @@ -41,6 +42,8 @@ declare i1* @create_creg(i64)

declare i64 @get_int_from_creg(i1*)

declare void @mz_to_reg(%Qubit*, i1*, i64)

declare void @__quantum__rt__int_record_output(i64, i8*)

declare void @__quantum__rt__tuple_start_record_output()
Expand Down
2 changes: 2 additions & 0 deletions tests/qir/test_pytket_qir_14.ll
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ declare i1* @create_creg(i64)

declare i64 @get_int_from_creg(i1*)

declare void @mz_to_reg(%Qubit*, i1*, i64)

declare void @__quantum__rt__int_record_output(i64, i8*)

declare void @__quantum__rt__tuple_start_record_output()
Expand Down
2 changes: 2 additions & 0 deletions tests/qir/test_pytket_qir_14_b.ll
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,8 @@ declare i1* @create_creg(i64)

declare i64 @get_int_from_creg(i1*)

declare void @mz_to_reg(%Qubit*, i1*, i64)

declare void @__quantum__rt__int_record_output(i64, i8*)

declare void @__quantum__rt__tuple_start_record_output()
Expand Down
3 changes: 3 additions & 0 deletions tests/qir/test_pytket_qir_15.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
source_filename = "test_pytket_qir_15"

%Result = type opaque
%Qubit = type opaque

@0 = internal constant [2 x i8] c"a\00"
@1 = internal constant [2 x i8] c"c\00"
Expand Down Expand Up @@ -39,6 +40,8 @@ declare i1* @create_creg(i64)

declare i64 @get_int_from_creg(i1*)

declare void @mz_to_reg(%Qubit*, i1*, i64)

declare void @__quantum__rt__int_record_output(i64, i8*)

declare void @__quantum__rt__tuple_start_record_output()
Expand Down
21 changes: 8 additions & 13 deletions tests/qir/test_pytket_qir_17.ll
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,13 @@ entry:
%1 = call i1* @create_creg(i64 1)
call void @__quantum__qis__h__body(%Qubit* null)
call void @__quantum__qis__cnot__body(%Qubit* null, %Qubit* inttoptr (i64 1 to %Qubit*))
call void @__quantum__qis__mz__body(%Qubit* null, %Result* null)
%2 = call i1 @__quantum__qis__read_result__body(%Result* null)
call void @set_creg_bit(i1* %0, i64 0, i1 %2)
call void @__quantum__qis__mz__body(%Qubit* inttoptr (i64 1 to %Qubit*), %Result* inttoptr (i64 1 to %Result*))
%3 = call i1 @__quantum__qis__read_result__body(%Result* inttoptr (i64 1 to %Result*))
call void @set_creg_bit(i1* %1, i64 0, i1 %3)
call void @mz_to_reg(%Qubit* null, i1* %0, i64 0)
call void @mz_to_reg(%Qubit* inttoptr (i64 1 to %Qubit*), i1* %1, i64 0)
call void @__quantum__rt__tuple_start_record_output()
%4 = call i64 @get_int_from_creg(i1* %0)
call void @__quantum__rt__int_record_output(i64 %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @0, i32 0, i32 0))
%5 = call i64 @get_int_from_creg(i1* %1)
call void @__quantum__rt__int_record_output(i64 %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @1, i32 0, i32 0))
%2 = call i64 @get_int_from_creg(i1* %0)
call void @__quantum__rt__int_record_output(i64 %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @0, i32 0, i32 0))
%3 = call i64 @get_int_from_creg(i1* %1)
call void @__quantum__rt__int_record_output(i64 %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @1, i32 0, i32 0))
call void @__quantum__rt__tuple_end_record_output()
ret void
}
Expand All @@ -40,6 +36,8 @@ declare i1* @create_creg(i64)

declare i64 @get_int_from_creg(i1*)

declare void @mz_to_reg(%Qubit*, i1*, i64)

declare void @__quantum__rt__int_record_output(i64, i8*)

declare void @__quantum__rt__tuple_start_record_output()
Expand All @@ -50,10 +48,7 @@ declare void @__quantum__qis__h__body(%Qubit*)

declare void @__quantum__qis__cnot__body(%Qubit*, %Qubit*)

declare void @__quantum__qis__mz__body(%Qubit*, %Result* writeonly) #1

attributes #0 = { "entry_point" "num_required_qubits"="2" "num_required_results"="2" "output_labeling_schema" "qir_profiles"="custom" }
attributes #1 = { "irreversible" }

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

Expand Down
2 changes: 2 additions & 0 deletions tests/qir/test_pytket_qir_2.ll
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ declare i1* @create_creg(i64)

declare i64 @get_int_from_creg(i1*)

declare void @mz_to_reg(%Qubit*, i1*, i64)

declare void @__quantum__rt__int_record_output(i64, i8*)

declare void @__quantum__rt__tuple_start_record_output()
Expand Down
2 changes: 2 additions & 0 deletions tests/qir/test_pytket_qir_3.ll
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ declare i1* @create_creg(i64)

declare i64 @get_int_from_creg(i1*)

declare void @mz_to_reg(%Qubit*, i1*, i64)

declare void @__quantum__rt__int_record_output(i64, i8*)

declare void @__quantum__rt__tuple_start_record_output()
Expand Down
2 changes: 2 additions & 0 deletions tests/qir/test_pytket_qir_4.ll
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ declare i1* @create_creg(i64)

declare i64 @get_int_from_creg(i1*)

declare void @mz_to_reg(%Qubit*, i1*, i64)

declare void @__quantum__rt__int_record_output(i64, i8*)

declare void @__quantum__rt__tuple_start_record_output()
Expand Down
2 changes: 2 additions & 0 deletions tests/qir/test_pytket_qir_5.ll
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ declare i1* @create_creg(i64)

declare i64 @get_int_from_creg(i1*)

declare void @mz_to_reg(%Qubit*, i1*, i64)

declare void @__quantum__rt__int_record_output(i64, i8*)

declare void @__quantum__rt__tuple_start_record_output()
Expand Down
29 changes: 13 additions & 16 deletions tests/qir/test_pytket_qir_6.ll
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ entry:
call void @__quantum__qis__x__body(%Qubit* null)
call void @__quantum__qis__h__body(%Qubit* inttoptr (i64 1 to %Qubit*))
call void @__quantum__qis__h__body(%Qubit* inttoptr (i64 2 to %Qubit*))
call void @__quantum__qis__mz__body(%Qubit* null, %Result* null)
%9 = call i1 @__quantum__qis__read_result__body(%Result* null)
call void @set_creg_bit(i1* %2, i64 4, i1 %9)
%10 = call i1 @get_creg_bit(i1* %2, i64 4)
br i1 %10, label %then, label %else
call void @mz_to_reg(%Qubit* null, i1* %2, i64 4)
%9 = call i1 @get_creg_bit(i1* %2, i64 4)
br i1 %9, label %then, label %else

then: ; preds = %entry
call void @__quantum__qis__z__body(%Qubit* null)
Expand All @@ -40,14 +38,14 @@ else: ; preds = %entry
continue: ; preds = %else, %then
call void @__quantum__qis__h__body(%Qubit* null)
call void @__quantum__rt__tuple_start_record_output()
%11 = call i64 @get_int_from_creg(i1* %0)
call void @__quantum__rt__int_record_output(i64 %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @0, i32 0, i32 0))
%12 = call i64 @get_int_from_creg(i1* %1)
call void @__quantum__rt__int_record_output(i64 %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @1, i32 0, i32 0))
%13 = call i64 @get_int_from_creg(i1* %2)
call void @__quantum__rt__int_record_output(i64 %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @2, i32 0, i32 0))
%14 = call i64 @get_int_from_creg(i1* %3)
call void @__quantum__rt__int_record_output(i64 %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @3, i32 0, i32 0))
%10 = call i64 @get_int_from_creg(i1* %0)
call void @__quantum__rt__int_record_output(i64 %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @0, i32 0, i32 0))
%11 = call i64 @get_int_from_creg(i1* %1)
call void @__quantum__rt__int_record_output(i64 %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @1, i32 0, i32 0))
%12 = call i64 @get_int_from_creg(i1* %2)
call void @__quantum__rt__int_record_output(i64 %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @2, i32 0, i32 0))
%13 = call i64 @get_int_from_creg(i1* %3)
call void @__quantum__rt__int_record_output(i64 %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @3, i32 0, i32 0))
call void @__quantum__rt__tuple_end_record_output()
ret void
}
Expand All @@ -64,6 +62,8 @@ declare i1* @create_creg(i64)

declare i64 @get_int_from_creg(i1*)

declare void @mz_to_reg(%Qubit*, i1*, i64)

declare void @__quantum__rt__int_record_output(i64, i8*)

declare void @__quantum__rt__tuple_start_record_output()
Expand All @@ -74,12 +74,9 @@ declare void @__quantum__qis__x__body(%Qubit*)

declare void @__quantum__qis__h__body(%Qubit*)

declare void @__quantum__qis__mz__body(%Qubit*, %Result* writeonly) #1

declare void @__quantum__qis__z__body(%Qubit*)

attributes #0 = { "entry_point" "num_required_qubits"="3" "num_required_results"="3" "output_labeling_schema" "qir_profiles"="custom" }
attributes #1 = { "irreversible" }

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

Expand Down
3 changes: 3 additions & 0 deletions tests/qir/test_pytket_qir_7.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
source_filename = "test_pytket_qir_7"

%Result = type opaque
%Qubit = type opaque

@0 = internal constant [2 x i8] c"a\00"
@1 = internal constant [2 x i8] c"b\00"
Expand Down Expand Up @@ -123,6 +124,8 @@ declare i1* @create_creg(i64)

declare i64 @get_int_from_creg(i1*)

declare void @mz_to_reg(%Qubit*, i1*, i64)

declare void @__quantum__rt__int_record_output(i64, i8*)

declare void @__quantum__rt__tuple_start_record_output()
Expand Down
3 changes: 3 additions & 0 deletions tests/qir/test_pytket_qir_8.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
source_filename = "test_pytket_qir_8"

%Result = type opaque
%Qubit = type opaque

@0 = internal constant [2 x i8] c"a\00"

Expand Down Expand Up @@ -39,6 +40,8 @@ declare i1* @create_creg(i64)

declare i64 @get_int_from_creg(i1*)

declare void @mz_to_reg(%Qubit*, i1*, i64)

declare void @__quantum__rt__int_record_output(i64, i8*)

declare void @__quantum__rt__tuple_start_record_output()
Expand Down
3 changes: 3 additions & 0 deletions tests/qir/test_pytket_qir_9.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
source_filename = "test_pytket_qir_9"

%Result = type opaque
%Qubit = type opaque

@0 = internal constant [2 x i8] c"a\00"
@1 = internal constant [2 x i8] c"b\00"
Expand Down Expand Up @@ -35,6 +36,8 @@ declare i1* @create_creg(i64)

declare i64 @get_int_from_creg(i1*)

declare void @mz_to_reg(%Qubit*, i1*, i64)

declare void @__quantum__rt__int_record_output(i64, i8*)

declare void @__quantum__rt__tuple_start_record_output()
Expand Down
2 changes: 2 additions & 0 deletions tests/qir/test_pytket_qir_barrier.ll
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ declare i1* @create_creg(i64)

declare i64 @get_int_from_creg(i1*)

declare void @mz_to_reg(%Qubit*, i1*, i64)

declare void @__quantum__rt__int_record_output(i64, i8*)

declare void @__quantum__rt__tuple_start_record_output()
Expand Down
2 changes: 2 additions & 0 deletions tests/qir/test_pytket_qir_barrier_ii.ll
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ declare i1* @create_creg(i64)

declare i64 @get_int_from_creg(i1*)

declare void @mz_to_reg(%Qubit*, i1*, i64)

declare void @__quantum__rt__int_record_output(i64, i8*)

declare void @__quantum__rt__tuple_start_record_output()
Expand Down
Loading