-
Notifications
You must be signed in to change notification settings - Fork 1
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
add wasm support to the conversion #66
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
__extension_version__ = "0.2.0rc15" | ||
__extension_version__ = "0.2.0rc16" | ||
__extension_name__ = "pytket-qir" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,7 @@ | |
import pyqir | ||
from pyqir import IntPredicate, Value | ||
|
||
from pytket import Bit, Circuit, Qubit, predicates # type: ignore | ||
from pytket import Bit, Circuit, Qubit, predicates, wasm # type: ignore | ||
from pytket.circuit import ( # type: ignore | ||
BitRegister, | ||
ClassicalExpBox, | ||
|
@@ -107,6 +107,7 @@ def __init__( | |
module: tketqirModule, | ||
wasm_int_type: int, | ||
qir_int_type: int, | ||
wfh: Optional[wasm.WasmFileHandler] = None, | ||
) -> None: | ||
self.circuit = circuit | ||
self.module = module | ||
|
@@ -120,6 +121,12 @@ def __init__( | |
self.cregs = _retrieve_registers(self.circuit.bits, BitRegister) | ||
self.target_gateset = self.module.gateset.base_gateset | ||
|
||
self.wasm_dict: dict[str, str] = {} | ||
self.wasm_dict[ | ||
"!llvm.module.flags" | ||
] = 'attributes #1 = { "wasm" }\n\n!llvm.module.flags' | ||
self.int_type_str = f"i{qir_int_type}" | ||
|
||
self.target_gateset.add(OpType.PhasedX) | ||
self.target_gateset.add(OpType.ZZPhase) | ||
self.target_gateset.add(OpType.ZZMax) | ||
|
@@ -244,6 +251,42 @@ def __init__( | |
self.circuit.n_qubits + 1 | ||
) | ||
|
||
# void functionname() | ||
if wfh is not None: | ||
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"i{self.int_type_str} " | ||
else: | ||
raise ValueError( | ||
"wasm function which return more than" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess we should make an issue for this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes I will do that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done at CQCL/pytket-quantinuum#201 |
||
+ " 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_dict[wasm_func_interface] = f"{wasm_func_interface} #1" | ||
|
||
self.additional_quantum_gates: dict[OpType, pyqir.Function] = {} | ||
|
||
for creg in self.circuit.c_registers: | ||
|
@@ -495,6 +538,9 @@ def _get_ssa_from_cl_bit_op( | |
else: | ||
raise ValueError("unsupported bisewise operation") | ||
|
||
def get_wasm_sar(self) -> dict[str, str]: | ||
return self.wasm_dict | ||
|
||
def circuit_to_module( | ||
self, circuit: Circuit, module: tketqirModule, record_output: bool = False | ||
) -> tketqirModule: | ||
|
@@ -635,7 +681,20 @@ def condition_block() -> None: | |
) | ||
|
||
elif isinstance(op, WASMOp): | ||
raise ValueError("WASM not supported yet") | ||
paramreg, resultreg = self._get_c_regs_from_com(command) | ||
|
||
paramssa = [self._get_i64_ssa_reg(p) for p in paramreg] | ||
|
||
result = self.module.builder.call( # type: ignore | ||
self.wasm[command.op.func_name], | ||
[*paramssa], | ||
) | ||
|
||
if len(resultreg) == 1: | ||
self.module.builder.call( | ||
self.set_creg_to_int, | ||
[self.ssa_vars[resultreg[0]], result], | ||
) | ||
|
||
elif op.type == OpType.ZZPhase: | ||
assert len(command.bits) == 0 | ||
|
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
; ModuleID = 'test_pytket_qir_wasm' | ||
source_filename = "test_pytket_qir_wasm" | ||
|
||
%Qubit = type opaque | ||
%Result = type opaque | ||
|
||
define void @main() #0 { | ||
entry: | ||
call void @__quantum__qis__h__body(%Qubit* null) | ||
call void @__quantum__rt__tuple_start_record_output() | ||
call void @__quantum__rt__tuple_end_record_output() | ||
ret void | ||
} | ||
|
||
declare i1 @get_creg_bit(i1*, i32) | ||
|
||
declare void @set_creg_bit(i1*, i32, i1) | ||
|
||
declare void @set_creg_to_int(i1*, i32) | ||
|
||
declare i1 @__quantum__qis__read_result__body(%Result*) | ||
|
||
declare i1* @create_creg(i32) | ||
|
||
declare i32 @get_int_from_creg(i1*) | ||
|
||
declare void @__quantum__rt__int_record_output(i32, i8*) | ||
|
||
declare void @__quantum__rt__tuple_start_record_output() | ||
|
||
declare void @__quantum__rt__tuple_end_record_output() | ||
|
||
declare void @init() #1 | ||
|
||
declare i32 @add_one(i32) | ||
|
||
declare i32 @multi(i32, i32) | ||
|
||
declare i32 @add_two(i32) | ||
|
||
declare i32 @add_eleven(i32) | ||
|
||
declare void @no_return(i32) #1 | ||
|
||
declare i32 @no_parameters() | ||
|
||
declare i32 @new_function() | ||
|
||
declare void @__quantum__qis__h__body(%Qubit*) | ||
|
||
attributes #0 = { "entry_point" "num_required_qubits"="1" "num_required_results"="1" "output_labeling_schema" "qir_profiles"="custom" } | ||
|
||
attributes #1 = { "wasm" } | ||
|
||
!llvm.module.flags = !{!0, !1, !2, !3} | ||
|
||
!0 = !{i32 1, !"qir_major_version", i32 1} | ||
!1 = !{i32 7, !"qir_minor_version", i32 0} | ||
!2 = !{i32 1, !"dynamic_qubit_management", i1 false} | ||
!3 = !{i32 1, !"dynamic_result_management", i1 false} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is probably easier to understand this when looking at the individual commits