Skip to content

Commit

Permalink
Create REIDSFlavour and REIDSSubroutineTranspiler
Browse files Browse the repository at this point in the history
  • Loading branch information
Thom747 committed Mar 5, 2024
1 parent 06bef47 commit 4e8b83a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
9 changes: 9 additions & 0 deletions netqasm/lang/instr/flavour.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,12 @@ def instrs(self):

def __init__(self):
super().__init__(self.instrs)


class REIDSFlavour(Flavour):
@property
def instrs(self):
return []

def __init__(self):
super().__init__(self.instrs)
50 changes: 47 additions & 3 deletions netqasm/sdk/transpile.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import Dict, List, Optional, Set, Tuple, Union

from netqasm.lang.instr import DebugInstruction, NetQASMInstruction, core, nv, vanilla
from netqasm.lang.instr.flavour import REIDSFlavour
from netqasm.lang.operand import Immediate, Register, RegisterName
from netqasm.lang.subroutine import Subroutine
from netqasm.runtime.settings import get_is_using_hardware
Expand All @@ -17,7 +18,8 @@

class SubroutineTranspiler(abc.ABC):
def __init__(self, subroutine: Subroutine, debug: bool = False):
pass
self._subroutine: Subroutine = subroutine
self._debug: bool = debug

@abc.abstractmethod
def transpile(self) -> Subroutine:
Expand All @@ -31,10 +33,9 @@ class NVSubroutineTranspiler(SubroutineTranspiler):
"""

def __init__(self, subroutine: Subroutine, debug=False):
self._subroutine: Subroutine = subroutine
super().__init__(subroutine, debug)
self._used_registers: Set[Register] = set()
self._register_values: Dict[Register, Immediate] = dict()
self._debug: bool = debug

def get_reg_value(self, reg: Register) -> Immediate:
"""Get the value of a register at this moment"""
Expand Down Expand Up @@ -614,6 +615,49 @@ def _map_single_gate(
)


class REIDSSubroutineTranspiler(SubroutineTranspiler):
"""
A transpiler that converts a subroutine with the vanilla flavour
to a subroutine with the REIDS flavour.
"""

def __init__(self, subroutine: Subroutine, debug: bool = False):
super().__init__(subroutine, debug)
self._flavour = REIDSFlavour()

def transpile(self) -> Subroutine:
add_no_op_at_end: bool = False

for instr in self._subroutine.instructions:
try:
self._flavour.id_map[instr.id]
except KeyError as e:
raise ValueError(
f"Instruction {instr} not supported: Unsupported instruction for REIDS flavour."
) from e

if (
isinstance(instr, core.BranchUnaryInstruction)
or isinstance(instr, core.BranchBinaryInstruction)
or isinstance(instr, core.JmpInstruction)
):
original_line = instr.line.value
if original_line == len(self._subroutine.instructions):
# There was a label in the original subroutine at the very end.
# Since this label is now removed, we should put a "no-op"
# instruction there so there is something to jump to.
add_no_op_at_end = True

if add_no_op_at_end:
self._subroutine.instructions += [
core.SetInstruction(
lineno=None, reg=Register(RegisterName.C, 15), imm=Immediate(1337)
)
]

return self._subroutine


def get_hardware_num_denom(
instr: core.RotationInstruction,
) -> Tuple[Immediate, Immediate]:
Expand Down

0 comments on commit 4e8b83a

Please sign in to comment.