Skip to content

Commit db16d09

Browse files
authored
Merge pull request #51 from A-DaRo/feature/nv-processor-fixes
Fix failing examples and implement missing instructions in NVProcessor
2 parents 35ad866 + 30eb0dd commit db16d09

File tree

4 files changed

+65
-10
lines changed

4 files changed

+65
-10
lines changed

examples/advanced/fidelity_constraint/example_fidelity.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
from typing import Any, Dict, Generator
66

77
import netsquid as ns
8+
from netqasm.sdk.connection import BaseNetQASMConnection
9+
from netqasm.sdk.futures import RegFuture
10+
from netqasm.sdk.qubit import Qubit
811

912
from pydynaa import EventExpression
1013
from squidasm.run.stack.config import StackNetworkConfig
@@ -53,16 +56,25 @@ def run(
5356
conn = context.connection
5457
epr_socket = context.epr_sockets[self.PEER]
5558

56-
eprs = epr_socket.create_keep(
57-
number=2, min_fidelity_all_at_end=70, max_tries=20
58-
)
59+
outcomes = conn.new_array(length=2)
60+
61+
def post_create(_: BaseNetQASMConnection, q: Qubit, index: RegFuture):
62+
q.measure(future=outcomes.get_future_index(index))
5963

60-
m0 = eprs[0].measure()
61-
m1 = eprs[1].measure()
64+
epr_socket.create_keep(
65+
number=2,
66+
min_fidelity_all_at_end=70,
67+
max_tries=20,
68+
sequential=True,
69+
post_routine=post_create,
70+
)
6271

6372
yield from conn.flush()
6473

65-
return {"m0": int(m0), "m1": int(m1)}
74+
m0 = int(outcomes.get_future_index(0))
75+
m1 = int(outcomes.get_future_index(1))
76+
77+
return {"m0": m0, "m1": m1}
6678

6779

6880
class ServerProgram(Program):
@@ -83,14 +95,25 @@ def run(
8395
conn = context.connection
8496
epr_socket = context.epr_sockets[self.PEER]
8597

86-
eprs = epr_socket.recv_keep(number=2, min_fidelity_all_at_end=70, max_tries=20)
98+
outcomes = conn.new_array(length=2)
8799

88-
m0 = eprs[0].measure()
89-
m1 = eprs[1].measure()
100+
def post_recv(_: BaseNetQASMConnection, q: Qubit, index: RegFuture):
101+
q.measure(future=outcomes.get_future_index(index))
102+
103+
epr_socket.recv_keep(
104+
number=2,
105+
min_fidelity_all_at_end=70,
106+
max_tries=20,
107+
sequential=True,
108+
post_routine=post_recv,
109+
)
90110

91111
yield from conn.flush()
92112

93-
return {"m0": int(m0), "m1": int(m1)}
113+
m0 = int(outcomes.get_future_index(0))
114+
m1 = int(outcomes.get_future_index(1))
115+
116+
return {"m0": m0, "m1": m1}
94117

95118

96119
PI = math.pi

squidasm/nqasm/executor/nv.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
INSTR_ROT_X,
1313
INSTR_ROT_Y,
1414
INSTR_ROT_Z,
15+
INSTR_SWAP,
1516
)
1617
from netsquid.nodes.node import Node as NetSquidNode
1718

@@ -27,6 +28,7 @@
2728
nv.RotZInstruction: INSTR_ROT_Z,
2829
nv.ControlledRotXInstruction: INSTR_CXDIR,
2930
nv.ControlledRotYInstruction: INSTR_CYDIR,
31+
nv.MovInstruction: INSTR_SWAP,
3032
}
3133

3234

squidasm/sim/network/network.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ def __init__(
551551
PhysicalInstruction(ns_instructions.INSTR_ROT_Z, duration=2),
552552
PhysicalInstruction(ns_instructions.INSTR_CXDIR, duration=5),
553553
PhysicalInstruction(ns_instructions.INSTR_CYDIR, duration=5),
554+
PhysicalInstruction(ns_instructions.INSTR_SWAP, duration=5),
554555
]
555556

556557
super().__init__(

squidasm/sim/stack/processor.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ def execute_subroutine(
173173
self, subroutine: Subroutine
174174
) -> Generator[EventExpression, None, None]:
175175
"""Execute a NetQASM subroutine on this processor."""
176+
self._logger.info(f"Executing subroutine {subroutine}")
176177
app_id = subroutine.app_id
177178
assert app_id in self.app_memories
178179
app_mem = self.app_memories[app_id]
@@ -230,6 +231,8 @@ def _interpret_instruction(
230231
pass
231232
elif isinstance(instr, core.SingleQubitInstruction):
232233
return self._interpret_single_qubit_instr(app_id, instr)
234+
elif isinstance(instr, vanilla.MovInstruction) or isinstance(instr, nv.MovInstruction):
235+
return self._interpret_mov(app_id, instr)
233236
elif isinstance(instr, core.TwoQubitInstruction):
234237
return self._interpret_two_qubit_instr(app_id, instr)
235238
elif isinstance(instr, core.RotationInstruction):
@@ -288,6 +291,12 @@ def _interpret_set(self, app_id: int, instr: core.SetInstruction) -> None:
288291
self._logger.debug(f"Set register {instr.reg} to {instr.imm}")
289292
self.app_memories[app_id].set_reg_value(instr.reg, instr.imm.value)
290293

294+
def _interpret_mov(self, app_id: int, instr: vanilla.MovInstruction) -> None:
295+
self._logger.debug(f"Moving value from {instr.reg1} to {instr.reg0}")
296+
app_mem = self.app_memories[app_id]
297+
val = app_mem.get_reg_value(instr.reg1)
298+
app_mem.set_reg_value(instr.reg0, val)
299+
291300
def _interpret_qalloc(self, app_id: int, instr: core.QAllocInstruction) -> None:
292301
app_mem = self.app_memories[app_id]
293302

@@ -883,3 +892,23 @@ def _interpret_controlled_rotation_instr(
883892
yield from self._do_controlled_rotation(app_id, instr, INSTR_CYDIR)
884893
else:
885894
raise RuntimeError(f"Unsupported instruction {instr}")
895+
896+
def _interpret_two_qubit_instr(
897+
self, app_id: int, instr: core.SingleQubitInstruction
898+
) -> Generator[EventExpression, None, None]:
899+
app_mem = self.app_memories[app_id]
900+
virt_id0 = app_mem.get_reg_value(instr.reg0)
901+
phys_id0 = app_mem.phys_id_for(virt_id0)
902+
virt_id1 = app_mem.get_reg_value(instr.reg1)
903+
phys_id1 = app_mem.phys_id_for(virt_id1)
904+
905+
if isinstance(instr, vanilla.CnotInstruction):
906+
prog = QuantumProgram()
907+
prog.apply(INSTR_CNOT, qubit_indices=[phys_id0, phys_id1])
908+
yield self.qdevice.execute_program(prog)
909+
elif isinstance(instr, vanilla.CphaseInstruction):
910+
prog = QuantumProgram()
911+
prog.apply(INSTR_CZ, qubit_indices=[phys_id0, phys_id1])
912+
yield self.qdevice.execute_program(prog)
913+
else:
914+
raise RuntimeError(f"Unsupported instruction {instr}")

0 commit comments

Comments
 (0)