Skip to content

Commit

Permalink
convert wasm without wfh (#187)
Browse files Browse the repository at this point in the history
* regenerate QIR files

* remove wfh

* clean up tests

* update changelog and version
  • Loading branch information
cqc-melf authored Oct 31, 2024
1 parent f277384 commit 52213de
Show file tree
Hide file tree
Showing 27 changed files with 81 additions and 283 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.16.0"
__extension_version__ = "0.17.0"
__extension_name__ = "pytket-qir"
5 changes: 5 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
Changelog
~~~~~~~~~

0.17.0 (October 2024)
---------------------

* Update qir generation for wasm functions to work without wasm file handler

0.16.0 (October 2024)
---------------------

Expand Down
9 changes: 2 additions & 7 deletions pytket/qir/conversion/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@
"""

from enum import Enum
from typing import TYPE_CHECKING, Optional, Union
from typing import TYPE_CHECKING, Union

import pyqir

from pytket.circuit import Circuit
from pytket.passes import (
scratch_reg_resize_pass,
)
from pytket.wasm import WasmFileHandler

from .baseprofileqirgenerator import BaseProfileQirGenerator
from .module import tketqirModule
Expand Down Expand Up @@ -58,7 +57,6 @@ def pytket_to_qir(
circ: Circuit,
name: str = "Generated from input pytket circuit",
qir_format: QIRFormat = QIRFormat.BINARY,
wfh: Optional[WasmFileHandler] = None,
int_type: int = 64,
cut_pytket_register: bool = False,
profile: QIRProfile = QIRProfile.PYTKET,
Expand Down Expand Up @@ -108,31 +106,28 @@ def pytket_to_qir(
module=m,
wasm_int_type=int_type,
qir_int_type=int_type,
wfh=wfh,
)
elif profile == QIRProfile.PYTKET:
qir_generator = PytketQirGenerator(
circuit=circ,
module=m,
wasm_int_type=int_type,
qir_int_type=int_type,
wfh=wfh,
)
elif profile == QIRProfile.ADAPTIVE or profile == QIRProfile.ADAPTIVE_CREGSIZE:
qir_generator = AdaptiveProfileQirGenerator(
circuit=circ,
module=m,
wasm_int_type=int_type,
qir_int_type=int_type,
wfh=wfh,
trunc=trunc,
)
else:
raise NotImplementedError("unexpected profile")

populated_module = qir_generator.circuit_to_module(qir_generator.circuit, True)

if wfh is not None:
if qir_generator.has_wasm:
wasm_sar_dict: dict[str, str] = qir_generator.get_wasm_sar()

initial_result = str(populated_module.module.ir())
Expand Down
5 changes: 1 addition & 4 deletions pytket/qir/conversion/baseprofileqirgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Optional

import pyqir
from pyqir import Value
Expand All @@ -25,7 +24,6 @@
Conditional,
Qubit,
)
from pytket.wasm import WasmFileHandler

from .module import tketqirModule
from .qirgenerator import (
Expand All @@ -42,10 +40,9 @@ def __init__(
module: tketqirModule,
wasm_int_type: int,
qir_int_type: int,
wfh: Optional[WasmFileHandler] = None,
) -> None:

super().__init__(circuit, module, wasm_int_type, qir_int_type, wfh)
super().__init__(circuit, module, wasm_int_type, qir_int_type)

self.measure_results: list[list] = []

Expand Down
6 changes: 2 additions & 4 deletions pytket/qir/conversion/profileqirgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Optional, cast
from typing import cast

import pyqir
from pyqir import BasicBlock, Value
Expand All @@ -27,7 +27,6 @@
OpType,
Qubit,
)
from pytket.wasm import WasmFileHandler

from .module import tketqirModule
from .qirgenerator import (
Expand All @@ -45,12 +44,11 @@ def __init__(
wasm_int_type: int,
qir_int_type: int,
trunc: bool,
wfh: Optional[WasmFileHandler] = None,
) -> None:

self.trunc = trunc

super().__init__(circuit, module, wasm_int_type, qir_int_type, wfh)
super().__init__(circuit, module, wasm_int_type, qir_int_type)

self.set_cregs: dict[str, list] = {} # Keep track of set registers.
self.ssa_vars: dict[str, list[tuple[Value, BasicBlock]]] = (
Expand Down
6 changes: 2 additions & 4 deletions pytket/qir/conversion/pytketqirgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Optional, cast
from typing import cast

import pyqir
from pyqir import Value
Expand All @@ -27,7 +27,6 @@
OpType,
Qubit,
)
from pytket.wasm import WasmFileHandler

from .module import tketqirModule
from .qirgenerator import (
Expand All @@ -49,10 +48,9 @@ def __init__(
module: tketqirModule,
wasm_int_type: int,
qir_int_type: int,
wfh: Optional[WasmFileHandler] = None,
) -> None:

super().__init__(circuit, module, wasm_int_type, qir_int_type, wfh)
super().__init__(circuit, module, wasm_int_type, qir_int_type)

self.set_cregs: dict[str, list] = {} # Keep track of set registers.
self.ssa_vars: dict[str, Value] = {} # Keep track of set ssa variables.
Expand Down
76 changes: 38 additions & 38 deletions pytket/qir/conversion/qirgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
from pytket.qasm.qasm import _retrieve_registers
from pytket.transform import Transform
from pytket.unit_id import UnitType
from pytket.wasm import WasmFileHandler

from .gatesets import (
FuncSpec,
Expand Down Expand Up @@ -184,7 +183,6 @@ def __init__(
module: tketqirModule,
wasm_int_type: int,
qir_int_type: int,
wfh: Optional[WasmFileHandler] = None,
) -> None:
self.circuit = circuit
self.module = module
Expand All @@ -203,6 +201,7 @@ def __init__(
self.block_count = 0
self.block_count_sb = 0

self.has_wasm = False
self.wasm_sar_dict: dict[str, str] = {}
self.wasm_sar_dict["!llvm.module.flags"] = (
'attributes #1 = { "wasm" }\n\n!llvm.module.flags'
Expand Down Expand Up @@ -261,42 +260,7 @@ def __init__(
self.circuit.n_qubits + 1
)

# void functionname()
if wfh is not None:
wfh.check()
self.wasm: dict[str, pyqir.Function] = {}
for fn in wfh._functions:
wasm_func_interface = "declare "
parametertype = [self.qir_int_type] * wfh._functions[fn][0]
if wfh._functions[fn][1] == 0:
returntype = pyqir.Type.void(self.module.module.context)
wasm_func_interface += "void "
elif wfh._functions[fn][1] == 1:
returntype = self.qir_int_type
wasm_func_interface += f"{self.int_type_str} "
else:
raise ValueError(
"wasm function which return more than"
+ " one value are not supported yet"
)

self.wasm[fn] = self.module.module.add_external_function(
f"{fn}",
pyqir.FunctionType(
returntype,
parametertype,
),
)

wasm_func_interface += f"@{fn}("
if wfh._functions[fn][0] > 0:
param_str = f"{self.int_type_str}, " * (wfh._functions[fn][0] - 1)
wasm_func_interface += param_str
wasm_func_interface += f"{self.int_type_str})"
else:
wasm_func_interface += ")"

self.wasm_sar_dict[wasm_func_interface] = f"{wasm_func_interface} #1"
self.wasm: dict[str, pyqir.Function] = {}

self.additional_quantum_gates: dict[OpType, pyqir.Function] = {}

Expand Down Expand Up @@ -559,10 +523,46 @@ def conv_conditional(self, command: Command, op: Conditional) -> None:
pass

def conv_WASMOp(self, op: WASMOp, args: list[Union[Bit, Qubit]]) -> None:
self.has_wasm = True

paramreg, resultreg = self._get_c_regs_from_com(op, args)

ssa_param = [self._get_i64_ssa_reg(p) for p in paramreg]

if op.func_name not in self.wasm:
wasm_func_interface = "declare "
parametertype = [self.qir_int_type] * len(paramreg)
if len(resultreg) == 0:
returntype = pyqir.Type.void(self.module.module.context)
wasm_func_interface += "void "
elif len(resultreg) == 1:
returntype = self.qir_int_type
wasm_func_interface += f"{self.int_type_str} "
else:
raise ValueError(
"wasm function which return more than"
+ " one value are not supported yet"
+ f"please don't use {op.func_name}"
)

self.wasm[op.func_name] = self.module.module.add_external_function(
f"{op.func_name}",
pyqir.FunctionType(
returntype,
parametertype,
),
)

wasm_func_interface += f"@{op.func_name}("
if len(paramreg) > 0:
param_str = f"{self.int_type_str}, " * (len(paramreg) - 1)
wasm_func_interface += param_str
wasm_func_interface += f"{self.int_type_str})"
else:
wasm_func_interface += ")"

self.wasm_sar_dict[wasm_func_interface] = f"{wasm_func_interface} #1"

result = self.module.builder.call(
self.wasm[op.func_name],
[*ssa_param],
Expand Down
18 changes: 0 additions & 18 deletions tests/qir/test_pytket_qir_wasm-QIRProfile.ADAPTIVE.ll
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,10 @@ declare i1 @__quantum__qis__read_result__body(%Result*)

declare void @__quantum__rt__int_record_output(i32, i8*)

declare void @init() #1

declare i32 @add_one(i32) #1

declare i32 @multi(i32, i32) #1

declare i32 @add_two(i32) #1

declare i32 @add_eleven(i32) #1

declare void @no_return(i32) #1

declare i32 @no_parameters() #1

declare i32 @new_function() #1

declare void @__quantum__qis__h__body(%Qubit*)

attributes #0 = { "entry_point" "output_labeling_schema" "qir_profiles"="custom" "required_num_qubits"="1" "required_num_results"="1" }

attributes #1 = { "wasm" }

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

!0 = !{i32 1, !"qir_major_version", i32 1}
Expand Down
18 changes: 0 additions & 18 deletions tests/qir/test_pytket_qir_wasm-QIRProfile.ADAPTIVE_CREGSIZE.ll
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,10 @@ declare i1 @__quantum__qis__read_result__body(%Result*)

declare void @__quantum__rt__int_record_output(i32, i8*)

declare void @init() #1

declare i32 @add_one(i32) #1

declare i32 @multi(i32, i32) #1

declare i32 @add_two(i32) #1

declare i32 @add_eleven(i32) #1

declare void @no_return(i32) #1

declare i32 @no_parameters() #1

declare i32 @new_function() #1

declare void @__quantum__qis__h__body(%Qubit*)

attributes #0 = { "entry_point" "output_labeling_schema" "qir_profiles"="custom" "required_num_qubits"="1" "required_num_results"="1" }

attributes #1 = { "wasm" }

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

!0 = !{i32 1, !"qir_major_version", i32 1}
Expand Down
18 changes: 0 additions & 18 deletions tests/qir/test_pytket_qir_wasm-QIRProfile.BASE.ll
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,12 @@ declare i1 @__quantum__qis__read_result__body(%Result*)

declare void @__quantum__rt__int_record_output(i32, i8*)

declare void @init() #1

declare i32 @add_one(i32) #1

declare i32 @multi(i32, i32) #1

declare i32 @add_two(i32) #1

declare i32 @add_eleven(i32) #1

declare void @no_return(i32) #1

declare i32 @no_parameters() #1

declare i32 @new_function() #1

declare void @__quantum__rt__result_record_output(%Result*, i8*)

declare void @__quantum__qis__h__body(%Qubit*)

attributes #0 = { "entry_point" "output_labeling_schema" "qir_profiles"="custom" "required_num_qubits"="1" "required_num_results"="1" }

attributes #1 = { "wasm" }

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

!0 = !{i32 1, !"qir_major_version", i32 1}
Expand Down
18 changes: 0 additions & 18 deletions tests/qir/test_pytket_qir_wasm-QIRProfile.PYTKET.ll
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,6 @@ declare i1 @__quantum__qis__read_result__body(%Result*)

declare void @__quantum__rt__int_record_output(i32, i8*)

declare void @init() #1

declare i32 @add_one(i32) #1

declare i32 @multi(i32, i32) #1

declare i32 @add_two(i32) #1

declare i32 @add_eleven(i32) #1

declare void @no_return(i32) #1

declare i32 @no_parameters() #1

declare i32 @new_function() #1

declare i1 @get_creg_bit(i1*, i32)

declare void @set_creg_bit(i1*, i32, i1)
Expand All @@ -46,8 +30,6 @@ declare void @__quantum__qis__h__body(%Qubit*)

attributes #0 = { "entry_point" "output_labeling_schema" "qir_profiles"="custom" "required_num_qubits"="1" "required_num_results"="1" }

attributes #1 = { "wasm" }

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

!0 = !{i32 1, !"qir_major_version", i32 1}
Expand Down
Loading

0 comments on commit 52213de

Please sign in to comment.