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

Add REIDS language support #67

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading