Skip to content

Commit

Permalink
|_selfk> now works in FnOp
Browse files Browse the repository at this point in the history
  • Loading branch information
GarryMorrison committed Mar 2, 2019
1 parent 3b52613 commit 3fcf86d
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/BaseOp.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class BaseOp {
virtual const std::string to_string() const = 0;
virtual Sequence Compile(ContextList& context, const Sequence& seq) const = 0;
virtual Sequence Compile(ContextList& context, const Sequence& seq, const ulong label_idx) const = 0;
virtual Sequence Compile(ContextList& context, const Sequence& seq, const std::vector<Sequence>& args) const = 0;
};

#endif
Expand Down
1 change: 1 addition & 0 deletions include/BracketOp.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class BracketOp : public BaseOp {
void push(OpSeq &op_seq) { op_seq_vec.push_back(op_seq); }
Sequence Compile(ContextList& context, const Sequence& seq) const;
Sequence Compile(ContextList& context, const Sequence& seq, const ulong label_idx) const;
Sequence Compile(ContextList& context, const Sequence& seq, const std::vector<Sequence>& args) const;
const std::string to_string() const;

};
Expand Down
1 change: 1 addition & 0 deletions include/EmptyOp.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class EmptyOp : public BaseOp {
EmptyOp() { }
Sequence Compile(ContextList& context, const Sequence& seq) const { return seq; };
Sequence Compile(ContextList& context, const Sequence& seq, const ulong label_idx) const { return seq; }
Sequence Compile(ContextList& context, const Sequence& seq, const std::vector<Sequence>& args) const { return seq; }
const std::string to_string() const { std::string s = ""; return s; };

};
Expand Down
1 change: 1 addition & 0 deletions include/FnOp.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class FnOp : public BaseOp {
void push(OpRule *op_rule) { op_rules.push_back(op_rule); }
Sequence Compile(ContextList& context, const Sequence& seq) const;
Sequence Compile(ContextList& context, const Sequence& seq, const ulong label_idx) const;
Sequence Compile(ContextList& context, const Sequence& seq, const std::vector<Sequence>& args) const;
const std::string to_string() const;

};
Expand Down
1 change: 1 addition & 0 deletions include/NumericOp.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class NumericOp : public BaseOp {
NumericOp(const double d) { value = d; };
Sequence Compile(ContextList& context, const Sequence& seq) const;
Sequence Compile(ContextList& context, const Sequence& seq, const ulong label_idx) const;
Sequence Compile(ContextList& context, const Sequence& seq, const std::vector<Sequence>& args) const;
const std::string to_string() const;

};
Expand Down
1 change: 1 addition & 0 deletions include/OpSeq.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class OpSeq : public BaseOp {
void append(BaseOp* b_op);
Sequence Compile(ContextList& context, const Sequence& seq) const;
Sequence Compile(ContextList& context, const Sequence& seq, const ulong label_idx) const;
Sequence Compile(ContextList& context, const Sequence& seq, const std::vector<Sequence>& args) const;
const std::string to_string() const;

};
Expand Down
1 change: 1 addition & 0 deletions include/PoweredOp.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class PoweredOp : public BaseOp {
PoweredOp(BaseOp* base_op, const unsigned int power);
Sequence Compile(ContextList& context, const Sequence& seq) const;
Sequence Compile(ContextList& context, const Sequence& seq, const ulong label_idx) const;
Sequence Compile(ContextList& context, const Sequence& seq, const std::vector<Sequence>& args) const;
const std::string to_string() const;
};

Expand Down
1 change: 1 addition & 0 deletions include/SimpleOp.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class SimpleOp : public BaseOp {
SimpleOp(const std::string& s) { op_idx = ket_map.get_idx(s); }
Sequence Compile(ContextList& context, const Sequence& seq) const;
Sequence Compile(ContextList& context, const Sequence& seq, const ulong label_idx) const;
Sequence Compile(ContextList& context, const Sequence& seq, const std::vector<Sequence>& args) const;
const std::string to_string() const;

};
Expand Down
16 changes: 16 additions & 0 deletions src/BracketOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ Sequence BracketOp::Compile(ContextList& context, const Sequence& seq, const ulo
return seq2;
}

Sequence BracketOp::Compile(ContextList& context, const Sequence& seq, const std::vector<Sequence>& args) const {
Sequence seq2;
for (const auto &op_seq : op_seq_vec) {
Sequence compiled_seq = op_seq.Compile(context, seq, args);
switch (op_seq.symbol_type()) {
case SPLUS: { seq2.add(compiled_seq); break; }
case SMINUS: { seq2.add(compiled_seq); break; }
case SSEQ: { seq2.append(compiled_seq); break; }
case SMERGE: { seq2.merge(compiled_seq); break; }
case SMERGE2: { seq2.merge(compiled_seq, " "); break; }
}
}
std::cout << "BracketOp::seq2: " << seq2.to_string() << std::endl;
return seq2;
}


const std::string BracketOp::to_string() const {
std::string s = "(";
Expand Down
52 changes: 52 additions & 0 deletions src/FnOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,58 @@ Sequence FnOp::Compile(ContextList& context, const Sequence& seq, const ulong la
return brule->Compile(context, args);
}

// find a way to remove this duplication!!
Sequence FnOp::Compile(ContextList& context, const Sequence& seq, const std::vector<Sequence>& input_args) const {
std::vector<Sequence> args;
args.push_back(seq);
for (const auto op_rule: op_rules) {
Sequence tmp = op_rule->Compile(context, input_args);
args.push_back(tmp);
std::cout << "arg: " << tmp.to_string() << std::endl;
}
switch (op_rules.size()) { // is it possible to tidy up this section?
case 1: { if (fn_map.whitelist_1.find(op_idx) != fn_map.whitelist_1.end()) {
auto our_fn = fn_map.whitelist_1[op_idx];
return our_fn(args[0], args[1]);
} else if (fn_map.context_whitelist_1.find(op_idx) != fn_map.context_whitelist_1.end()) {
auto our_fn = fn_map.context_whitelist_1[op_idx];
return our_fn(context, args[0], args[1]);
}
break;
}
case 2: { if (fn_map.whitelist_2.find(op_idx) != fn_map.whitelist_2.end()) {
auto our_fn = fn_map.whitelist_2[op_idx];
return our_fn(args[0], args[1], args[2]);
} else if (fn_map.context_whitelist_2.find(op_idx) != fn_map.context_whitelist_2.end()) {
auto our_fn = fn_map.context_whitelist_2[op_idx];
return our_fn(context, args[0], args[1], args[2]);
}
break;
}
case 3: { if (fn_map.whitelist_3.find(op_idx) != fn_map.whitelist_3.end()) {
auto our_fn = fn_map.whitelist_3[op_idx];
return our_fn(args[0], args[1], args[2], args[3]);
} else if (fn_map.context_whitelist_3.find(op_idx) != fn_map.context_whitelist_3.end()) {
auto our_fn = fn_map.context_whitelist_3[op_idx];
return our_fn(context, args[0], args[1], args[2], args[3]);
}
break;
}
case 4: { if (fn_map.whitelist_4.find(op_idx) != fn_map.whitelist_4.end()) {
auto our_fn = fn_map.whitelist_4[op_idx];
return our_fn(args[0], args[1], args[2], args[3], args[4]);
} else if (fn_map.context_whitelist_4.find(op_idx) != fn_map.context_whitelist_4.end()) {
auto our_fn = fn_map.context_whitelist_4[op_idx];
return our_fn(context, args[0], args[1], args[2], args[3], args[4]);
}
break;
}
}
BaseRule *brule = context.fn_recall(op_idx, op_rules.size());
std::cout << "brule: " << brule->to_string() << std::endl;
return brule->Compile(context, args);
}


const std::string FnOp::to_string() const {
std::string s = ket_map.get_str(op_idx) + "(";
Expand Down
6 changes: 6 additions & 0 deletions src/NumericOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ Sequence NumericOp::Compile(ContextList& context, const Sequence& seq, const ulo
return result;
}

Sequence NumericOp::Compile(ContextList& context, const Sequence& seq, const std::vector<Sequence>& args) const {
Sequence result(seq);
result.multiply(value);
return result;
}


const std::string NumericOp::to_string() const {
return std::string(std::to_string(value)); // why?
Expand Down
9 changes: 9 additions & 0 deletions src/OpSeq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ Sequence OpSeq::Compile(ContextList& context, const Sequence& seq, const ulong l
return result;
}

Sequence OpSeq::Compile(ContextList& context, const Sequence& seq, const std::vector<Sequence>& args) const {
Sequence result = seq;
for (auto it = op_seq.rbegin(); it != op_seq.rend(); ++it) {
result = (*it)->Compile(context, result, args);
}
if (op_symbol == SMINUS) { result.multiply(-1); } // not 100% sure this should be here.
return result;
}


const std::string OpSeq::to_string() const {
std::string s = "";
Expand Down
8 changes: 8 additions & 0 deletions src/PoweredOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ Sequence PoweredOp::Compile(ContextList& context, const Sequence& seq, const ulo
return result;
}

Sequence PoweredOp::Compile(ContextList& context, const Sequence& seq, const std::vector<Sequence>& args) const {
Sequence result = seq;
for (unsigned int i = 0; i < pow; i++) {
result = b_op->Compile(context, result, args);
}
return result;
}


const std::string PoweredOp::to_string() const {
std::string s = "";
Expand Down
4 changes: 4 additions & 0 deletions src/SimpleOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ Sequence SimpleOp::Compile(ContextList& context, const Sequence& seq, const ulon
return this->Compile(context, seq);
}

Sequence SimpleOp::Compile(ContextList& context, const Sequence& seq, const std::vector<Sequence>& args) const {
return this->Compile(context, seq);
}


const std::string SimpleOp::to_string() const {
return ket_map.get_str(op_idx);
Expand Down
2 changes: 1 addition & 1 deletion src/SingleOpRule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Sequence SingleOpRule::Compile(ContextList& context, const ulong label_idx) cons
Sequence SingleOpRule::Compile(ContextList& context, const std::vector<Sequence>& args) const {
Sequence seq;
Sequence b_seq = b_rule->Compile(context, args);
seq = op_seq->Compile(context, b_seq);
seq = op_seq->Compile(context, b_seq, args);
return seq;
}

Expand Down
8 changes: 8 additions & 0 deletions sw-examples/add.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
foo (*,*) #=> 3|_self0> + 5|_self1> + 7|_self2>

the |result> => foo(|a>, |b>) |x>

add (*,*) #=> arithmetic(|_self1>, |+>, |_self2>) |>

the |2nd result> => add(|3>, |5>) |>

0 comments on commit 3fcf86d

Please sign in to comment.