Skip to content

Commit

Permalink
fix ssa variable generation (#91)
Browse files Browse the repository at this point in the history
* update qir files

* fix ssa variables generated but unused

* Update _metadata.py
  • Loading branch information
cqc-melf authored Nov 14, 2023
1 parent 6073ea4 commit 34ee9b0
Show file tree
Hide file tree
Showing 16 changed files with 394 additions and 483 deletions.
2 changes: 1 addition & 1 deletion _metadata.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__extension_version__ = "0.5.0"
__extension_version__ = "0.6.0rc0"
__extension_name__ = "pytket-qir"
7 changes: 6 additions & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
Changelog
~~~~~~~~~

0.6.0 (unreleased)
------------------

* update measurement to write to register directly
* remove unused ssa variables generated in output

0.5.0 (November 2023)
---------------------

* 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
4 changes: 2 additions & 2 deletions pytket/qir/conversion/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ def circuit_to_module(
lower_qir,
self._get_i64_ssa_reg(registername),
)

upper_cond = module.module.builder.icmp(
pyqir.IntPredicate.SGT,
self._get_i64_ssa_reg(registername),
Expand Down Expand Up @@ -874,9 +875,8 @@ def condition_block() -> None:
result_index = (
0 # defines the default value for ops that returns bool, see below
)

outputs = command.args[-1].reg_name
ssa_left = (self._get_i64_ssa_reg(list(self.ssa_vars)[0]),)
ssa_right = (self._get_i64_ssa_reg(list(self.ssa_vars)[0]),)

if type(op.get_exp()) in _TK_CLOPS_TO_PYQIR_REG:
# classical ops acting on registers returning register
Expand Down
10 changes: 4 additions & 6 deletions tests/qir/test_pytket_qir_12.ll
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ define void @main() #0 {
entry:
%0 = call i1* @create_creg(i64 8)
%1 = call i64 @get_int_from_creg(i1* %0)
%2 = call i64 @get_int_from_creg(i1* %0)
%3 = call i64 @get_int_from_creg(i1* %0)
%4 = shl i64 %3, 1
call void @set_creg_to_int(i1* %0, i64 %4)
%2 = shl i64 %1, 1
call void @set_creg_to_int(i1* %0, i64 %2)
call void @__quantum__rt__tuple_start_record_output()
%5 = call i64 @get_int_from_creg(i1* %0)
call void @__quantum__rt__int_record_output(i64 %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @0, i32 0, i32 0))
%3 = call i64 @get_int_from_creg(i1* %0)
call void @__quantum__rt__int_record_output(i64 %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @0, i32 0, i32 0))
call void @__quantum__rt__tuple_end_record_output()
ret void
}
Expand Down
20 changes: 8 additions & 12 deletions tests/qir/test_pytket_qir_13.ll
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,16 @@ entry:
%0 = call i1* @create_creg(i64 8)
%1 = call i1* @create_creg(i64 8)
%2 = call i64 @get_int_from_creg(i1* %0)
%3 = call i64 @get_int_from_creg(i1* %0)
%3 = shl i64 %2, 1
call void @set_creg_to_int(i1* %0, i64 %3)
%4 = call i64 @get_int_from_creg(i1* %0)
%5 = shl i64 %4, 1
call void @set_creg_to_int(i1* %0, i64 %5)
%6 = call i64 @get_int_from_creg(i1* %0)
%7 = call i64 @get_int_from_creg(i1* %0)
%8 = call i64 @get_int_from_creg(i1* %0)
%9 = lshr i64 %8, 3
call void @set_creg_to_int(i1* %1, i64 %9)
%5 = lshr i64 %4, 3
call void @set_creg_to_int(i1* %1, i64 %5)
call void @__quantum__rt__tuple_start_record_output()
%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))
%6 = call i64 @get_int_from_creg(i1* %0)
call void @__quantum__rt__int_record_output(i64 %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @0, i32 0, i32 0))
%7 = call i64 @get_int_from_creg(i1* %1)
call void @__quantum__rt__int_record_output(i64 %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @1, i32 0, i32 0))
call void @__quantum__rt__tuple_end_record_output()
ret void
}
Expand Down
206 changes: 95 additions & 111 deletions tests/qir/test_pytket_qir_14.ll
Original file line number Diff line number Diff line change
Expand Up @@ -65,53 +65,45 @@ entry:
%14 = call i1 @get_creg_bit(i1* %0, i64 7)
call void @set_creg_bit(i1* %1, i64 7, i1 %14)
%15 = call i64 @get_int_from_creg(i1* %0)
%16 = call i64 @get_int_from_creg(i1* %0)
%17 = call i64 @get_int_from_creg(i1* %0)
%18 = call i64 @get_int_from_creg(i1* %1)
%19 = add i64 %17, %18
call void @set_creg_to_int(i1* %2, i64 %19)
%20 = call i64 @get_int_from_creg(i1* %0)
%16 = call i64 @get_int_from_creg(i1* %1)
%17 = add i64 %15, %16
call void @set_creg_to_int(i1* %2, i64 %17)
%18 = call i64 @get_int_from_creg(i1* %0)
%19 = call i64 @get_int_from_creg(i1* %1)
%20 = sub i64 %18, %19
call void @set_creg_to_int(i1* %2, i64 %20)
%21 = call i64 @get_int_from_creg(i1* %0)
%22 = call i64 @get_int_from_creg(i1* %0)
%23 = call i64 @get_int_from_creg(i1* %1)
%24 = sub i64 %22, %23
call void @set_creg_to_int(i1* %2, i64 %24)
%22 = shl i64 %21, 1
call void @set_creg_to_int(i1* %0, i64 %22)
%23 = call i64 @get_int_from_creg(i1* %0)
%24 = lshr i64 %23, 1
call void @set_creg_to_int(i1* %1, i64 %24)
%25 = call i64 @get_int_from_creg(i1* %0)
%26 = call i64 @get_int_from_creg(i1* %0)
%26 = icmp eq i64 1, %25
call void @set_creg_bit(i1* %3, i64 4, i1 %26)
%27 = call i64 @get_int_from_creg(i1* %0)
%28 = shl i64 %27, 1
call void @set_creg_to_int(i1* %0, i64 %28)
%28 = icmp sgt i64 2, %27
%29 = call i64 @get_int_from_creg(i1* %0)
%30 = call i64 @get_int_from_creg(i1* %0)
%31 = call i64 @get_int_from_creg(i1* %0)
%32 = lshr i64 %31, 1
call void @set_creg_to_int(i1* %1, i64 %32)
%33 = call i64 @get_int_from_creg(i1* %0)
%34 = icmp eq i64 1, %33
call void @set_creg_bit(i1* %3, i64 4, i1 %34)
%35 = call i64 @get_int_from_creg(i1* %0)
%36 = icmp sgt i64 2, %35
%37 = call i64 @get_int_from_creg(i1* %0)
%38 = icmp sgt i64 %37, 4294967295
%39 = and i1 %36, %38
call void @set_creg_bit(i1* %3, i64 5, i1 %39)
%40 = call i64 @get_int_from_creg(i1* %0)
%41 = icmp eq i64 0, %40
call void @set_creg_bit(i1* %3, i64 6, i1 %41)
%42 = call i64 @get_int_from_creg(i1* %0)
%43 = icmp sgt i64 1, %42
%44 = call i64 @get_int_from_creg(i1* %0)
%45 = icmp sgt i64 %44, 4294967295
%46 = and i1 %43, %45
call void @set_creg_bit(i1* %3, i64 7, i1 %46)
%47 = call i64 @get_int_from_creg(i1* %0)
%48 = icmp sgt i64 0, %47
%49 = call i64 @get_int_from_creg(i1* %0)
%50 = icmp sgt i64 %49, 1
%51 = and i1 %48, %50
call void @set_creg_bit(i1* %3, i64 8, i1 %51)
%52 = call i1 @get_creg_bit(i1* %0, i64 0)
br i1 %52, label %then, label %else
%30 = icmp sgt i64 %29, 4294967295
%31 = and i1 %28, %30
call void @set_creg_bit(i1* %3, i64 5, i1 %31)
%32 = call i64 @get_int_from_creg(i1* %0)
%33 = icmp eq i64 0, %32
call void @set_creg_bit(i1* %3, i64 6, i1 %33)
%34 = call i64 @get_int_from_creg(i1* %0)
%35 = icmp sgt i64 1, %34
%36 = call i64 @get_int_from_creg(i1* %0)
%37 = icmp sgt i64 %36, 4294967295
%38 = and i1 %35, %37
call void @set_creg_bit(i1* %3, i64 7, i1 %38)
%39 = call i64 @get_int_from_creg(i1* %0)
%40 = icmp sgt i64 0, %39
%41 = call i64 @get_int_from_creg(i1* %0)
%42 = icmp sgt i64 %41, 1
%43 = and i1 %40, %42
call void @set_creg_bit(i1* %3, i64 8, i1 %43)
%44 = call i1 @get_creg_bit(i1* %0, i64 0)
br i1 %44, label %then, label %else

then: ; preds = %entry
br label %continue
Expand All @@ -120,41 +112,33 @@ else: ; preds = %entry
br label %continue

continue: ; preds = %else, %then
%53 = call i64 @get_int_from_creg(i1* %0)
%45 = call i1 @get_creg_bit(i1* %0, i64 0)
%46 = call i1 @get_creg_bit(i1* %1, i64 0)
%47 = xor i1 %45, %46
call void @set_creg_bit(i1* %3, i64 1, i1 %47)
%48 = call i64 @get_int_from_creg(i1* %0)
%49 = call i64 @get_int_from_creg(i1* %1)
%50 = xor i64 %48, %49
call void @set_creg_to_int(i1* %4, i64 %50)
%51 = call i64 @get_int_from_creg(i1* %0)
%52 = call i64 @get_int_from_creg(i1* %1)
%53 = and i64 %51, %52
call void @set_creg_to_int(i1* %5, i64 %53)
%54 = call i64 @get_int_from_creg(i1* %0)
%55 = call i1 @get_creg_bit(i1* %0, i64 0)
%56 = call i1 @get_creg_bit(i1* %1, i64 0)
%57 = xor i1 %55, %56
call void @set_creg_bit(i1* %3, i64 1, i1 %57)
%58 = call i64 @get_int_from_creg(i1* %0)
%59 = call i64 @get_int_from_creg(i1* %0)
%60 = call i64 @get_int_from_creg(i1* %0)
%61 = call i64 @get_int_from_creg(i1* %1)
%62 = xor i64 %60, %61
call void @set_creg_to_int(i1* %4, i64 %62)
%63 = call i64 @get_int_from_creg(i1* %0)
%64 = call i64 @get_int_from_creg(i1* %0)
%65 = call i64 @get_int_from_creg(i1* %0)
%66 = call i64 @get_int_from_creg(i1* %1)
%67 = and i64 %65, %66
call void @set_creg_to_int(i1* %5, i64 %67)
%68 = call i64 @get_int_from_creg(i1* %0)
%69 = call i64 @get_int_from_creg(i1* %0)
%70 = call i64 @get_int_from_creg(i1* %0)
%71 = call i64 @get_int_from_creg(i1* %1)
%72 = or i64 %70, %71
call void @set_creg_to_int(i1* %6, i64 %72)
%73 = call i64 @get_int_from_creg(i1* %4)
%74 = icmp eq i64 1, %73
call void @set_creg_bit(i1* %3, i64 0, i1 %74)
%75 = call i64 @get_int_from_creg(i1* %5)
%76 = icmp eq i64 1, %75
call void @set_creg_bit(i1* %3, i64 2, i1 %76)
%77 = call i64 @get_int_from_creg(i1* %6)
%78 = icmp eq i64 1, %77
call void @set_creg_bit(i1* %3, i64 3, i1 %78)
%79 = call i1 @get_creg_bit(i1* %3, i64 0)
br i1 %79, label %then1, label %else2
%55 = call i64 @get_int_from_creg(i1* %1)
%56 = or i64 %54, %55
call void @set_creg_to_int(i1* %6, i64 %56)
%57 = call i64 @get_int_from_creg(i1* %4)
%58 = icmp eq i64 1, %57
call void @set_creg_bit(i1* %3, i64 0, i1 %58)
%59 = call i64 @get_int_from_creg(i1* %5)
%60 = icmp eq i64 1, %59
call void @set_creg_bit(i1* %3, i64 2, i1 %60)
%61 = call i64 @get_int_from_creg(i1* %6)
%62 = icmp eq i64 1, %61
call void @set_creg_bit(i1* %3, i64 3, i1 %62)
%63 = call i1 @get_creg_bit(i1* %3, i64 0)
br i1 %63, label %then1, label %else2

then1: ; preds = %continue
call void @__quantum__qis__x__body(%Qubit* null)
Expand All @@ -164,8 +148,8 @@ else2: ; preds = %continue
br label %continue3

continue3: ; preds = %else2, %then1
%80 = call i1 @get_creg_bit(i1* %3, i64 1)
br i1 %80, label %then4, label %else5
%64 = call i1 @get_creg_bit(i1* %3, i64 1)
br i1 %64, label %then4, label %else5

then4: ; preds = %continue3
call void @__quantum__qis__x__body(%Qubit* null)
Expand All @@ -175,8 +159,8 @@ else5: ; preds = %continue3
br label %continue6

continue6: ; preds = %else5, %then4
%81 = call i1 @get_creg_bit(i1* %3, i64 2)
br i1 %81, label %then7, label %else8
%65 = call i1 @get_creg_bit(i1* %3, i64 2)
br i1 %65, label %then7, label %else8

then7: ; preds = %continue6
call void @__quantum__qis__x__body(%Qubit* null)
Expand All @@ -186,8 +170,8 @@ else8: ; preds = %continue6
br label %continue9

continue9: ; preds = %else8, %then7
%82 = call i1 @get_creg_bit(i1* %3, i64 3)
br i1 %82, label %then10, label %else11
%66 = call i1 @get_creg_bit(i1* %3, i64 3)
br i1 %66, label %then10, label %else11

then10: ; preds = %continue9
call void @__quantum__qis__x__body(%Qubit* null)
Expand All @@ -197,8 +181,8 @@ else11: ; preds = %continue9
br label %continue12

continue12: ; preds = %else11, %then10
%83 = call i1 @get_creg_bit(i1* %0, i64 0)
br i1 %83, label %then13, label %else14
%67 = call i1 @get_creg_bit(i1* %0, i64 0)
br i1 %67, label %then13, label %else14

then13: ; preds = %continue12
call void @__quantum__qis__x__body(%Qubit* null)
Expand All @@ -208,8 +192,8 @@ else14: ; preds = %continue12
br label %continue15

continue15: ; preds = %else14, %then13
%84 = call i1 @get_creg_bit(i1* %3, i64 4)
br i1 %84, label %then16, label %else17
%68 = call i1 @get_creg_bit(i1* %3, i64 4)
br i1 %68, label %then16, label %else17

then16: ; preds = %continue15
br label %continue18
Expand All @@ -219,8 +203,8 @@ else17: ; preds = %continue15
br label %continue18

continue18: ; preds = %else17, %then16
%85 = call i1 @get_creg_bit(i1* %0, i64 0)
br i1 %85, label %then19, label %else20
%69 = call i1 @get_creg_bit(i1* %0, i64 0)
br i1 %69, label %then19, label %else20

then19: ; preds = %continue18
br label %continue21
Expand All @@ -230,8 +214,8 @@ else20: ; preds = %continue18
br label %continue21

continue21: ; preds = %else20, %then19
%86 = call i1 @get_creg_bit(i1* %3, i64 5)
br i1 %86, label %then22, label %else23
%70 = call i1 @get_creg_bit(i1* %3, i64 5)
br i1 %70, label %then22, label %else23

then22: ; preds = %continue21
call void @__quantum__qis__x__body(%Qubit* null)
Expand All @@ -241,8 +225,8 @@ else23: ; preds = %continue21
br label %continue24

continue24: ; preds = %else23, %then22
%87 = call i1 @get_creg_bit(i1* %3, i64 6)
br i1 %87, label %then25, label %else26
%71 = call i1 @get_creg_bit(i1* %3, i64 6)
br i1 %71, label %then25, label %else26

then25: ; preds = %continue24
call void @__quantum__qis__x__body(%Qubit* null)
Expand All @@ -252,8 +236,8 @@ else26: ; preds = %continue24
br label %continue27

continue27: ; preds = %else26, %then25
%88 = call i1 @get_creg_bit(i1* %3, i64 7)
br i1 %88, label %then28, label %else29
%72 = call i1 @get_creg_bit(i1* %3, i64 7)
br i1 %72, label %then28, label %else29

then28: ; preds = %continue27
call void @__quantum__qis__x__body(%Qubit* null)
Expand All @@ -263,8 +247,8 @@ else29: ; preds = %continue27
br label %continue30

continue30: ; preds = %else29, %then28
%89 = call i1 @get_creg_bit(i1* %3, i64 8)
br i1 %89, label %then31, label %else32
%73 = call i1 @get_creg_bit(i1* %3, i64 8)
br i1 %73, label %then31, label %else32

then31: ; preds = %continue30
call void @__quantum__qis__x__body(%Qubit* null)
Expand All @@ -275,20 +259,20 @@ else32: ; preds = %continue30

continue33: ; preds = %else32, %then31
call void @__quantum__rt__tuple_start_record_output()
%90 = call i64 @get_int_from_creg(i1* %0)
call void @__quantum__rt__int_record_output(i64 %90, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @0, i32 0, i32 0))
%91 = call i64 @get_int_from_creg(i1* %1)
call void @__quantum__rt__int_record_output(i64 %91, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @1, i32 0, i32 0))
%92 = call i64 @get_int_from_creg(i1* %2)
call void @__quantum__rt__int_record_output(i64 %92, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @2, i32 0, i32 0))
%93 = call i64 @get_int_from_creg(i1* %3)
call void @__quantum__rt__int_record_output(i64 %93, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @3, i32 0, i32 0))
%94 = call i64 @get_int_from_creg(i1* %4)
call void @__quantum__rt__int_record_output(i64 %94, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @4, i32 0, i32 0))
%95 = call i64 @get_int_from_creg(i1* %5)
call void @__quantum__rt__int_record_output(i64 %95, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @5, i32 0, i32 0))
%96 = call i64 @get_int_from_creg(i1* %6)
call void @__quantum__rt__int_record_output(i64 %96, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @6, i32 0, i32 0))
%74 = call i64 @get_int_from_creg(i1* %0)
call void @__quantum__rt__int_record_output(i64 %74, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @0, i32 0, i32 0))
%75 = call i64 @get_int_from_creg(i1* %1)
call void @__quantum__rt__int_record_output(i64 %75, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @1, i32 0, i32 0))
%76 = call i64 @get_int_from_creg(i1* %2)
call void @__quantum__rt__int_record_output(i64 %76, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @2, i32 0, i32 0))
%77 = call i64 @get_int_from_creg(i1* %3)
call void @__quantum__rt__int_record_output(i64 %77, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @3, i32 0, i32 0))
%78 = call i64 @get_int_from_creg(i1* %4)
call void @__quantum__rt__int_record_output(i64 %78, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @4, i32 0, i32 0))
%79 = call i64 @get_int_from_creg(i1* %5)
call void @__quantum__rt__int_record_output(i64 %79, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @5, i32 0, i32 0))
%80 = call i64 @get_int_from_creg(i1* %6)
call void @__quantum__rt__int_record_output(i64 %80, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @6, i32 0, i32 0))
call void @__quantum__rt__tuple_end_record_output()
ret void
}
Expand Down
Loading

0 comments on commit 34ee9b0

Please sign in to comment.