QuISP
Loading...
Searching...
No Matches
Runtime.h
Go to the documentation of this file.
1#pragma once
2#include <cassert>
3#include <cstddef>
4#include <deque>
5#include <iostream>
6#include <stdexcept>
7#include <tuple>
8#include <unordered_map>
9#include <variant>
10#include <vector>
11
12#include "InstructionVisitor.h"
13#include "RuleSet.h"
14#include "macro_utils.h"
15
16#include "Value.h"
17#include "opcode.h"
18#include "types.h"
19
20namespace quisp::runtime {
21struct Register {
22 int32_t value = 0;
23};
24
26using QubitResources = std::multimap<std::pair<QNodeAddr, RuleId>, IQubitRecord*>;
27
29using QubitResourcesWithSequenceNumbers = std::map<std::tuple<QNodeAddr, RuleId, SequenceNumber>, IQubitRecord*>;
30using SequenceNumberTracker = std::map<IQubitRecord*, std::tuple<QNodeAddr, RuleId, SequenceNumber>>;
31
33using SequenceNumberCounter = std::map<std::pair<QNodeAddr, RuleId>, SequenceNumber>;
34
36using MessageResources = std::map<RuleId, std::deque<MessageRecord>>;
37
39using QubitNameMap = std::unordered_map<QubitId, IQubitRecord*>;
40
42using Memory = std::unordered_map<MemoryKey, MemoryValue>;
43
48class Runtime {
49 public:
58 struct ICallBack {
59 virtual ~ICallBack() {}
60
61 // Resource Management
62 virtual void freeAndResetQubit(IQubitRecord*) = 0;
63 virtual bool isQubitLocked(IQubitRecord* const) = 0;
64 virtual void lockQubit(IQubitRecord* const, unsigned long rs_id, int rule_id, int action_index) = 0;
65 virtual int getActionIndex(IQubitRecord* const) = 0;
66
67 // Quantum Operations
72 virtual void gateX(IQubitRecord*) = 0;
73 virtual void gateZ(IQubitRecord*) = 0;
74 virtual void gateY(IQubitRecord*) = 0;
75 virtual void gateCNOT(IQubitRecord* control_qubit_rec, IQubitRecord* target_qubit_rec) = 0;
76 virtual int purifyX(IQubitRecord* qubit_rec, IQubitRecord* trash_qubit_rec) = 0;
77 virtual int purifyZ(IQubitRecord* qubit_rec, IQubitRecord* trash_qubit_rec) = 0;
78 virtual int purifyY(IQubitRecord* qubit_rec, IQubitRecord* trash_qubit_rec) = 0;
79
80 // Messaging
81 virtual void sendLinkTomographyResult(const unsigned long ruleset_id, const Rule& rule, const int action_index, const QNodeAddr partner_addr, int count,
82 MeasurementOutcome outcome, int max_count, Time start_time) = 0;
83 // TODO: change to this
84 // virtual void sendTomographyResult(const QNodeAddr partner_addr, const int shared_rule_tag, const int sequence_number, const int measurement_result, const basis) = 0;
85 virtual void sendPurificationResult(const unsigned long ruleset_id, const QNodeAddr partner_addr, const int shared_rule_tag, const int sequence_number,
86 const int measurement_result, PurType protocol) = 0;
87 virtual void sendSwappingResult(const unsigned long ruleset_id, const QNodeAddr partner_addr, const QNodeAddr new_partner_addr, const int shared_rule_tag,
88 const int sequence_number, const int frame_correction) = 0;
89 // Debugging
90 virtual std::string getNodeInfo() { return ""; };
91 };
92
93 Runtime();
94 Runtime(const RuleSet& ruleset, ICallBack* callback);
95 Runtime(const Runtime&);
96 Runtime& operator=(Runtime&& runtime);
97 ~Runtime();
98
99 void assignRuleSet(const RuleSet& ruleset);
100
104 void cleanup();
105
107 void exec();
108
110 void execProgram(const Program& program);
111
113 void execInstruction(const InstructionTypes& op);
114
122 void assignMessageToRuleSet(int shared_rule_tag, MessageRecord& msg_content);
123
131 void assignQubitToRuleSet(QNodeAddr partner_addr, IQubitRecord* qubit_record);
132
140 void assignQubitToRule(QNodeAddr partner_addr, RuleId rule_id, IQubitRecord* qubit_record);
141
143 QubitResources::iterator findQubit(IQubitRecord*);
144
153 const Register& getReg(RegId reg_id) const;
154
161 int32_t getRegVal(RegId reg_id) const;
162
169 void setRegVal(RegId reg_id, int32_t val);
170
176 void jumpTo(const Label& label);
177
179
182
189 void storeVal(MemoryKey key, MemoryValue val);
190
197 void loadVal(MemoryKey key, RegId reg_id);
198
207
210
218 bool isQubitLocked(IQubitRecord* const);
219
232
243
253 IQubitRecord* getQubitByQubitId(QubitId qubit_id) const;
254
261 void setQubit(IQubitRecord* qubit_record, QubitId qubit_id);
262
270 void promoteQubit(IQubitRecord* qubit_record);
271
280 void promoteQubitWithNewPartner(IQubitRecord* qubit_record, QNodeAddr new_partner_addr);
282
285
293 void measureQubit(QubitId qubit_id, MemoryKey result_key, Basis basis);
301 void measureQubit(QubitId qubit_id, RegId result_reg, Basis basis);
309 void measureQubit(QubitId qubit_id, RegId result_reg, int bitset_index, Basis basis);
310
312 void freeQubit(QubitId);
313
315 void gateX(QubitId);
316
318 void gateZ(QubitId);
319
321 void gateY(QubitId);
322
324 void gateCNOT(QubitId control_qubit_id, QubitId target_qubit_id);
325
327 void purifyX(RegId result, int bitset_index, QubitId qubit_id, QubitId trash_qubit_id);
328
330 void purifyZ(RegId result, int bitset_index, QubitId qubit_id, QubitId trash_qubit_id);
331
333 void purifyY(RegId result, int bitset_index, QubitId qubit_id, QubitId trash_qubit_id);
335
338 void debugRuntimeState();
339 void debugSource(const Program& program) const;
340 std::string debugInstruction(const InstructionTypes& instr) const;
342
347
351
356
358 int send_tag = -1;
359 int receive_tag = -1;
360
368 unsigned int pc = 0;
369
376
385
387
394
398 std::unordered_map<int, RuleId> shared_tag_to_rule_id;
399 std::unordered_map<RuleId, int> rule_id_to_shared_tag;
400
407
416
424
430 std::set<QNodeAddr> partners;
431
439 LabelMap const* label_map = nullptr;
441
452
456 bool should_exit = false;
457
464 bool terminated = false;
465
471 bool qubit_found = false;
472
478 bool message_found = false;
479
486 bool debugging = false;
488};
489} // namespace quisp::runtime
The QubitRecord interface.
Definition IQubitRecord.h:17
memory value
Definition Value.h:21
The Program is a list of Instructions with metadata. The Runtime can execute the Program.
Definition RuleSet.h:17
The Runtime executable Rule in a RuleSet.
Definition RuleSet.h:40
The RuleSet.
Definition RuleSet.h:80
Runtime class is responsible for executing the given RuleSet and the states' management.
Definition Runtime.h:48
LabelMap const * label_map
The label_map is a map of the instruction index and its label to allow to jump or branch.
Definition Runtime.h:439
MessageResources messages
currently evaluating rule id
Definition Runtime.h:386
QubitNameMap named_qubits
This contains a map for a QubitId and a qubit.
Definition Runtime.h:406
bool qubit_found
The GET_QUBIT instruction sets this flag. if it's true, the GET_QUBIT instruction successfully found ...
Definition Runtime.h:471
void purifyY(RegId result, int bitset_index, QubitId qubit_id, QubitId trash_qubit_id)
perform Y purification and store the measurement result
Definition Runtime.cc:393
RuleSet ruleset
The assigned RuleSet for this Runtime instance.
Definition Runtime.h:423
InstructionVisitor visitor
The visitor handles all instruction types for Program execution.
Definition Runtime.h:346
unsigned int pc
program counter for Program execution.
Definition Runtime.h:368
void promoteQubit(IQubitRecord *qubit_record)
promote the qubit to the next rule.
Definition Runtime.cc:157
Memory memory
The memory contains a variety of values during the RuleSet execution.
Definition Runtime.h:415
ReturnCode return_code
The return_code describes the program's exit status.
Definition Runtime.h:451
bool message_found
The GET_MESSAGE instruction sets this flag. if it's true, the GET_MESSAGE instruction successfully fo...
Definition Runtime.h:478
void gateZ(QubitId)
apply Z gate
Definition Runtime.cc:347
void cleanup()
this method resets the state before each Program execution.
Definition Runtime.cc:108
IQubitRecord * getQubitByQubitId(QubitId qubit_id) const
Get the Qubit By QubitId.
Definition Runtime.cc:224
void exec()
public method to execute the assigned RuleSet.
Definition Runtime.cc:49
QubitResourcesWithSequenceNumbers sequence_number_to_qubit
currently evaluating rule id
Definition Runtime.h:388
int receive_tag
currently evaluating rule id
Definition Runtime.h:359
SequenceNumberCounter resource_counter
This stores the latest sequence no. of resource assigned to the Stage/Rule.
Definition Runtime.h:393
int32_t getRegVal(RegId reg_id) const
Get the Register's value.
Definition Runtime.cc:199
void assignQubitToRule(QNodeAddr partner_addr, RuleId rule_id, IQubitRecord *qubit_record)
assign the entangled qubit to the rule
Definition Runtime.cc:191
void purifyX(RegId result, int bitset_index, QubitId qubit_id, QubitId trash_qubit_id)
perform X purification and store the measurement result
Definition Runtime.cc:371
void gateCNOT(QubitId control_qubit_id, QubitId target_qubit_id)
apply CNOT gate
Definition Runtime.cc:363
void assignMessageToRuleSet(int shared_rule_tag, MessageRecord &msg_content)
assign the message to the rule
Definition Runtime.cc:128
IQubitRecord * getQubitBySequenceNumber(QNodeAddr, RuleId, SequenceNumber)
Get the qubit record by sequence number.
Definition Runtime.cc:217
void execProgram(const Program &program)
execute the given Program in a Rule
Definition Runtime.cc:81
bool should_exit
if this flag is true, the Runtime stops the Program execution.
Definition Runtime.h:456
void measureQubit(QubitId qubit_id, MemoryKey result_key, Basis basis)
measure qubit with the given basis and store the result to the memory.
Definition Runtime.cc:254
~Runtime()
Definition Runtime.cc:47
void assignRuleSet(const RuleSet &ruleset)
Definition Runtime.cc:122
void gateX(QubitId)
apply X gate
Definition Runtime.cc:339
SequenceNumberTracker qubit_to_sequence_number
currently evaluating rule id
Definition Runtime.h:389
std::unordered_map< RuleId, int > rule_id_to_shared_tag
currently evaluating rule id
Definition Runtime.h:399
IQubitRecord * getQubitByPartnerAddr(QNodeAddr, int index)
Get the qubit record by partner's QNodeAddr.
Definition Runtime.cc:206
int send_tag
currently evaluating send/receive tag of this rule id (if it is defined)
Definition Runtime.h:358
QubitResources::iterator findQubit(IQubitRecord *)
find the qubit iterator that match with this rule_id and sequence_number
Definition Runtime.cc:148
void setRegVal(RegId reg_id, int32_t val)
Set the given value to the Register.
Definition Runtime.cc:200
Runtime()
Definition Runtime.cc:26
void debugSource(const Program &program) const
Definition Runtime.cc:429
Register registers[5]
Registers are temporary memory for Program execution.
Definition Runtime.h:375
bool debugging
if the flag is true, show debug information during RuleSet execution.
Definition Runtime.h:486
void purifyZ(RegId result, int bitset_index, QubitId qubit_id, QubitId trash_qubit_id)
perform Z purification and store the measurement result
Definition Runtime.cc:382
std::string debugInstruction(const InstructionTypes &instr) const
Definition Runtime.cc:438
RuleId rule_id
currently evaluating rule id
Definition Runtime.h:355
void setQubit(IQubitRecord *qubit_record, QubitId qubit_id)
bind the assigned Qubit to the given qubit id to use it in a Program.
Definition Runtime.cc:201
ICallBack * callback
The callback provides a way to access the RuleEngine.
Definition Runtime.h:349
void assignQubitToRuleSet(QNodeAddr partner_addr, IQubitRecord *qubit_record)
assign the entangled qubit to the RuleSet. The Runtime assign it to the first rule and the Rule can u...
Definition Runtime.cc:138
void gateY(QubitId)
apply Y gate
Definition Runtime.cc:355
void loadVal(MemoryKey key, RegId reg_id)
load the value from memory, and put it into the given register.
Definition Runtime.cc:241
bool terminated
This flag is enabled when the RuleSet finishes its tasks.
Definition Runtime.h:464
const Register & getReg(RegId reg_id) const
Get the Register in registers by RegId.
Definition Runtime.cc:198
void storeVal(MemoryKey key, MemoryValue val)
store the value into memory.
Definition Runtime.cc:240
Runtime & operator=(Runtime &&runtime)
Definition Runtime.cc:28
QubitResources qubits
This stores the entangled qubits that are assigned to the ruleset.
Definition Runtime.h:384
std::unordered_map< int, RuleId > shared_tag_to_rule_id
[shared_rule_tag] => [rule_id]
Definition Runtime.h:398
void debugRuntimeState()
Definition Runtime.cc:405
void promoteQubitWithNewPartner(IQubitRecord *qubit_record, QNodeAddr new_partner_addr)
promote the qubit that has new entangled partner.
Definition Runtime.cc:169
void jumpTo(const Label &label)
jump to the instruction that has the given label
Definition Runtime.cc:232
bool isQubitLocked(IQubitRecord *const)
check the qubit is locked or not. Purification uses the lock state in a qubit.
Definition Runtime.cc:404
void execInstruction(const InstructionTypes &op)
execute the one Instruction
Definition Runtime.cc:120
void freeQubit(QubitId)
free qubit and release it from the Rule and the RuleSet
Definition Runtime.cc:322
std::set< QNodeAddr > partners
The partners store the possible entangled partners' QNodeAddr. The RuleEngine looks at this variable ...
Definition Runtime.h:430
Definition InstructionVisitor.cc:7
omnetpp::SimTime Time
alias for omnetpp's simulation time.
Definition types.h:43
std::unordered_map< QubitId, IQubitRecord * > QubitNameMap
QubitId and qubit record map. This is initialized in before each Program execution.
Definition Runtime.h:39
Basis
basis for measurement instruction in a Program.
Definition types.h:116
std::unordered_map< MemoryKey, MemoryValue > Memory
Memory is a simple dict to store the value during RuleSet execution.
Definition Runtime.h:42
std::map< IQubitRecord *, std::tuple< QNodeAddr, RuleId, SequenceNumber > > SequenceNumberTracker
Definition Runtime.h:30
RegId
internal register id representation for Instructions.
Definition types.h:48
unsigned long SequenceNumber
describes the order which Qubit is assigned to a Stage/Rule. This allows us to make assumptions that ...
Definition types.h:25
std::map< std::tuple< QNodeAddr, RuleId, SequenceNumber >, IQubitRecord * > QubitResourcesWithSequenceNumbers
(partner's qnode addr, assigned RuleId, sequence number) => [local half of the stationary bell pair q...
Definition Runtime.h:29
std::map< std::pair< QNodeAddr, RuleId >, SequenceNumber > SequenceNumberCounter
ResourceId needs to be increment each time a qubit of certain properties (same <QNodeAddr>) is added ...
Definition Runtime.h:33
std::map< RuleId, std::deque< MessageRecord > > MessageResources
Store messages for each rule for decision making mainly used for WaitRules (e.g., purification,...
Definition Runtime.h:36
std::variant< #define INSTR(Opcode,...) #define INSTR_LAST(Opcode,...) # 1 "/github/workspace/quisp/runtime/def_instructions.h" 1 # 87 "/github/workspace/quisp/runtime/opcode.h" 2 > InstructionTypes
a variant that is capable of storing all instructions
Definition opcode.h:84
int RuleId
Definition types.h:18
int PurType
purification type for Instructions. see rules::PurType enum.
Definition types.h:45
std::multimap< std::pair< QNodeAddr, RuleId >, IQubitRecord * > QubitResources
(partner's qnode addr, assigned RuleId) => [local half of the bell pair qubit record]
Definition Runtime.h:26
std::vector< int32_t > MessageRecord
describes message received that takes part in decision making of RuleSet.
Definition types.h:32
ReturnCode
Program specify a ReturnCode during the execution. Then, the Runtime determines to perform the action...
Definition types.h:55
std::unordered_map< Label, int > LabelMap
utility type for storing a label and corresponding instruction index.
Definition types.h:113
this file contains the definitions of all the user-defined types widely used alongside the runtime::R...
Visitor class for instructions in a Program.
Definition InstructionVisitor.h:18
label to annotate the instruction index in a Program.
Definition types.h:95
a key of memory key-value store in a RuleSet
Definition types.h:103
internal class to describe QNode's address.
Definition types.h:70
describes Qubit id in a Program. This is like a local variable name in a runtime::Program....
Definition types.h:83
Definition Runtime.h:21
int32_t value
Definition Runtime.h:22
ICallBack is an interface for the callback of the Runtime.
Definition Runtime.h:58
virtual MeasurementOutcome measureQubitRandomly(IQubitRecord *)=0
virtual MeasurementOutcome measureQubitX(IQubitRecord *)=0
virtual MeasurementOutcome measureQubitY(IQubitRecord *)=0
virtual MeasurementOutcome measureQubitZ(IQubitRecord *)=0
virtual void sendLinkTomographyResult(const unsigned long ruleset_id, const Rule &rule, const int action_index, const QNodeAddr partner_addr, int count, MeasurementOutcome outcome, int max_count, Time start_time)=0
virtual int purifyX(IQubitRecord *qubit_rec, IQubitRecord *trash_qubit_rec)=0
virtual void gateX(IQubitRecord *)=0
virtual void sendSwappingResult(const unsigned long ruleset_id, const QNodeAddr partner_addr, const QNodeAddr new_partner_addr, const int shared_rule_tag, const int sequence_number, const int frame_correction)=0
virtual ~ICallBack()
Definition Runtime.h:59
virtual void freeAndResetQubit(IQubitRecord *)=0
virtual void sendPurificationResult(const unsigned long ruleset_id, const QNodeAddr partner_addr, const int shared_rule_tag, const int sequence_number, const int measurement_result, PurType protocol)=0
virtual int purifyY(IQubitRecord *qubit_rec, IQubitRecord *trash_qubit_rec)=0
virtual int getActionIndex(IQubitRecord *const)=0
virtual void gateY(IQubitRecord *)=0
virtual bool isQubitLocked(IQubitRecord *const)=0
virtual std::string getNodeInfo()
Definition Runtime.h:90
virtual int purifyZ(IQubitRecord *qubit_rec, IQubitRecord *trash_qubit_rec)=0
virtual void lockQubit(IQubitRecord *const, unsigned long rs_id, int rule_id, int action_index)=0
virtual void gateCNOT(IQubitRecord *control_qubit_rec, IQubitRecord *target_qubit_rec)=0
virtual void gateZ(IQubitRecord *)=0