Skip to content

Commit

Permalink
LS Instruction short-hand codes (#266)
Browse files Browse the repository at this point in the history
* Reorder instrucitons to match the order in the shorthand notation

* Add shorthand writer

* Add shorthand writing tests
  • Loading branch information
gwwatkin committed Mar 7, 2022
1 parent 0c34f6d commit 481b699
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 15 deletions.
30 changes: 15 additions & 15 deletions src/lsqecc/ls_instructions/ls_instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,12 @@ def __repr__(self):


@dataclass
class RequestMagicState(LSInstruction):
patch_id: PatchId = -1

def __repr__(self):
return f"{type(self).__name__} {self.patch_id}"


@dataclass
class RequestYState(LSInstruction):
class MeasureSinglePatch(LSInstruction):
patch_id: PatchId = -1
op: PauliOperator = PauliOperator.Z

def __repr__(self):
return f"{type(self).__name__} {self.patch_id}"
return f"{type(self).__name__} {self.patch_id} {self.op}"


@dataclass
Expand All @@ -65,12 +58,11 @@ def __repr__(self):


@dataclass
class MeasureSinglePatch(LSInstruction):
class RequestMagicState(LSInstruction):
patch_id: PatchId = -1
op: PauliOperator = PauliOperator.Z

def __repr__(self):
return f"{type(self).__name__} {self.patch_id} {self.op}"
return f"{type(self).__name__} {self.patch_id}"


@dataclass
Expand All @@ -83,16 +75,24 @@ def __repr__(self):


@dataclass
class SGate(LSInstruction):
class HGate(LSInstruction):
patch_id: PatchId = -1

def __repr__(self):
return f"{type(self).__name__} {self.patch_id}"


@dataclass
class HGate(LSInstruction):
class SGate(LSInstruction):
patch_id: PatchId = -1

def __repr__(self):
return f"{type(self).__name__} {self.patch_id}"


@dataclass
class RequestYState(LSInstruction):
patch_id: PatchId = -1

def __repr__(self):
return f"{type(self).__name__} {self.patch_id}"
59 changes: 59 additions & 0 deletions src/lsqecc/ls_instructions/shorthand_file_writer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from typing import TextIO

import lsqecc.simulation.qubit_state as qs
from lsqecc.ls_instructions import ls_instructions as lsi


class ShorthandFileWriter:
def __init__(self, output_file: TextIO):
self.output_file = output_file

def write_instruction(self, instruction: lsi.LSInstruction):
if isinstance(instruction, lsi.DeclareLogicalQubitPatches):
self.output_file.write(repr(instruction))
elif isinstance(instruction, lsi.Init):
self.output_file.write("0 ")
self.output_file.write(str(instruction.patch_id))
self.output_file.write(" ")
self.output_file.write(ShorthandFileWriter.shorthand_state(instruction.state))
elif isinstance(instruction, lsi.MeasureSinglePatch):
self.output_file.write("1 ")
self.output_file.write(str(instruction.patch_id))
self.output_file.write(" ")
self.output_file.write(str(instruction.op))
elif isinstance(instruction, lsi.MultiBodyMeasure):
self.output_file.write("2 ")
self.output_file.write(
",".join(
str(patch_id) + ":" + str(op)
for patch_id, op in instruction.patch_pauli_map.items()
)
)
elif isinstance(instruction, lsi.RequestMagicState):
self.output_file.write("3 ")
self.output_file.write(str(instruction.patch_id))
elif isinstance(instruction, lsi.LogicalPauli):
self.output_file.write("4 ")
self.output_file.write(str(instruction.patch_id))
self.output_file.write(" ")
self.output_file.write(str(instruction.op))
elif isinstance(instruction, lsi.HGate):
self.output_file.write("5 ")
self.output_file.write(str(instruction.patch_id))
elif isinstance(instruction, lsi.SGate):
self.output_file.write("6 ")
self.output_file.write(str(instruction.patch_id))
elif isinstance(instruction, lsi.RequestYState):
self.output_file.write("7 ")
self.output_file.write(str(instruction.patch_id))
else:
raise NotImplementedError(f"No shorthand for instruction: {instruction}")
self.output_file.write("\n")

@staticmethod
def shorthand_state(symbolic_state: qs.QubitState) -> str:
if symbolic_state == qs.DefaultSymbolicStates.Zero:
return "0"
if symbolic_state == qs.DefaultSymbolicStates.Plus:
return "+"
return symbolic_state.ket_repr()
50 changes: 50 additions & 0 deletions tests/ls_instructions/test_shorthand_file_writer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from typing import TextIO, cast

import pytest

import lsqecc.simulation.qubit_state as qs
from lsqecc.ls_instructions import ls_instructions as lsi
from lsqecc.ls_instructions.shorthand_file_writer import ShorthandFileWriter
from lsqecc.pauli_rotations import PauliOperator


class MockFile:
def __init__(self):
self.content = ""

def write(self, s: str):
self.content += s


@pytest.mark.parametrize(
"instruction, expected_output",
[
(lsi.DeclareLogicalQubitPatches([0, 1, 2, 100]), "DeclareLogicalQubitPatches 0,1,2,100"),
(lsi.Init(patch_id=101, state=qs.DefaultSymbolicStates.Zero), "0 101 0"),
(lsi.Init(patch_id=102, state=qs.DefaultSymbolicStates.Plus), "0 102 +"),
(lsi.Init(patch_id=102, state=qs.DefaultSymbolicStates.UnknownState), "0 102 |?>"),
(lsi.MeasureSinglePatch(patch_id=103, op=PauliOperator.X), "1 103 X"),
(lsi.MeasureSinglePatch(patch_id=104, op=PauliOperator.Z), "1 104 Z"),
(
lsi.MultiBodyMeasure(patch_pauli_map={105: PauliOperator.X, 106: PauliOperator.Z}),
"2 105:X,106:Z",
),
(
lsi.MultiBodyMeasure(
patch_pauli_map={107: PauliOperator.X, 108: PauliOperator.Z, 109: PauliOperator.Z}
),
"2 107:X,108:Z,109:Z",
),
(lsi.RequestMagicState(patch_id=110), "3 110"),
(lsi.LogicalPauli(patch_id=111, op=PauliOperator.X), "4 111 X"),
(lsi.LogicalPauli(patch_id=112, op=PauliOperator.Z), "4 112 Z"),
(lsi.HGate(patch_id=113), "5 113"),
(lsi.SGate(patch_id=114), "6 114"),
(lsi.RequestYState(patch_id=115), "7 115"),
],
)
def test_write_instruction(instruction, expected_output):
mock_file = MockFile()
writer = ShorthandFileWriter(cast(TextIO, mock_file))
writer.write_instruction(instruction)
assert mock_file.content == expected_output + "\n"

0 comments on commit 481b699

Please sign in to comment.