Line | Branch | Decision | Exec | Source |
---|---|---|---|---|
1 | // Copyright 2019-2022 Cambridge Quantum Computing | |||
2 | // | |||
3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |||
4 | // you may not use this file except in compliance with the License. | |||
5 | // You may obtain a copy of the License at | |||
6 | // | |||
7 | // http://www.apache.org/licenses/LICENSE-2.0 | |||
8 | // | |||
9 | // Unless required by applicable law or agreed to in writing, software | |||
10 | // distributed under the License is distributed on an "AS IS" BASIS, | |||
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
12 | // See the License for the specific language governing permissions and | |||
13 | // limitations under the License. | |||
14 | ||||
15 | #include "Conditional.hpp" | |||
16 | ||||
17 | #include "OpType/OpType.hpp" | |||
18 | ||||
19 | namespace tket { | |||
20 | ||||
21 | 1040 | Conditional::Conditional(const Op_ptr& op, unsigned width, unsigned value) | ||
22 |
1/2✓ Branch 1 taken 1040 times.
✗ Branch 2 not taken.
|
1040 | : Op(OpType::Conditional), op_(op), width_(width), value_(value) {} | |
23 | ||||
24 | 3 | Conditional::Conditional(const Conditional& other) | ||
25 | 3 | : Op(other), op_(other.op_), width_(other.width_), value_(other.value_) {} | ||
26 | ||||
27 | 2086 | Conditional::~Conditional() {} | ||
28 | ||||
29 | ✗ | Op_ptr Conditional::symbol_substitution( | ||
30 | const SymEngine::map_basic_basic& sub_map) const { | |||
31 | ✗ | return std::make_shared<Conditional>( | ||
32 | ✗ | op_->symbol_substitution(sub_map), width_, value_); | ||
33 | } | |||
34 | ||||
35 | ✗ | SymSet Conditional::free_symbols() const { return op_->free_symbols(); } | ||
36 | ||||
37 | 18 | bool Conditional::is_equal(const Op& op_other) const { | ||
38 |
1/2✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
|
18 | const Conditional& other = dynamic_cast<const Conditional&>(op_other); | |
39 | ||||
40 |
5/12✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✓ Branch 6 taken 18 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 18 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 18 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 18 times.
✗ Branch 14 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
|
54 | return *op_ == *other.get_op() && width_ == other.get_width() && | |
41 |
3/6✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 18 times.
✗ Branch 6 not taken.
|
54 | value_ == other.get_value(); | |
42 | } | |||
43 | ||||
44 | 1 | unsigned Conditional::n_qubits() const { return op_->n_qubits(); } | ||
45 | ||||
46 | 4892 | op_signature_t Conditional::get_signature() const { | ||
47 |
1/2✓ Branch 2 taken 4892 times.
✗ Branch 3 not taken.
|
4892 | op_signature_t signature(width_, EdgeType::Boolean); | |
48 |
1/2✓ Branch 2 taken 4892 times.
✗ Branch 3 not taken.
|
4892 | op_signature_t inner_sig = op_->get_signature(); | |
49 |
1/2✓ Branch 5 taken 4892 times.
✗ Branch 6 not taken.
|
4892 | signature.insert(signature.end(), inner_sig.begin(), inner_sig.end()); | |
50 | 9784 | return signature; | ||
51 | 4892 | } | ||
52 | ||||
53 | 3 | nlohmann::json Conditional::serialize() const { | ||
54 | 3 | nlohmann::json j; | ||
55 | 3 | nlohmann::json j_cond; | ||
56 |
3/6✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 3 times.
✗ Branch 8 not taken.
|
3 | j_cond["op"] = get_op(); | |
57 |
2/4✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 3 times.
✗ Branch 6 not taken.
|
3 | j_cond["width"] = get_width(); | |
58 |
2/4✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 3 times.
✗ Branch 6 not taken.
|
3 | j_cond["value"] = get_value(); | |
59 |
2/4✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
3 | j["type"] = OpType::Conditional; | |
60 |
2/4✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
3 | j["conditional"] = j_cond; | |
61 | 6 | return j; | ||
62 | 3 | } | ||
63 | ||||
64 | 3 | Op_ptr Conditional::deserialize(const nlohmann::json& j) { | ||
65 |
2/4✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
3 | nlohmann::json j_cond = j.at("conditional"); | |
66 |
2/4✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
3 | Op_ptr cond_op = j_cond.at("op").get<Op_ptr>(); | |
67 | 3 | return std::make_shared<Conditional>( | ||
68 |
2/4✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
3 | cond_op, j_cond.at("width").get<unsigned>(), | |
69 |
3/6✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 3 times.
✗ Branch 8 not taken.
|
9 | j_cond.at("value").get<unsigned>()); | |
70 | 3 | } | ||
71 | ||||
72 | 98 | std::string Conditional::get_command_str(const unit_vector_t& args) const { | ||
73 |
1/2✓ Branch 1 taken 98 times.
✗ Branch 2 not taken.
|
98 | std::stringstream out; | |
74 |
1/2✓ Branch 1 taken 98 times.
✗ Branch 2 not taken.
|
98 | out << "IF (["; | |
75 |
1/2✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
|
1/2✓ Decision 'true' taken 98 times.
✗ Decision 'false' not taken.
|
98 | if (width_ > 0) { |
76 |
3/6✓ Branch 1 taken 98 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 98 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 98 times.
✗ Branch 8 not taken.
|
98 | out << args.at(0).repr(); | |
77 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 98 times.
|
2/2✓ Decision 'true' taken 22 times.
✓ Decision 'false' taken 98 times.
|
120 | for (unsigned i = 1; i < width_; ++i) { |
78 |
4/8✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 22 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 22 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 22 times.
✗ Branch 11 not taken.
|
22 | out << ", " << args.at(i).repr(); | |
79 | } | |||
80 | } | |||
81 |
2/4✓ Branch 1 taken 98 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 98 times.
✗ Branch 5 not taken.
|
98 | out << "] == " << value_; | |
82 |
1/2✓ Branch 5 taken 98 times.
✗ Branch 6 not taken.
|
98 | unit_vector_t inner_args(args.begin() + width_, args.end()); | |
83 |
3/6✓ Branch 1 taken 98 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 98 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 98 times.
✗ Branch 9 not taken.
|
98 | out << ") THEN " << op_->get_command_str(inner_args); | |
84 |
1/2✓ Branch 1 taken 98 times.
✗ Branch 2 not taken.
|
196 | return out.str(); | |
85 | 98 | } | ||
86 | ||||
87 | ✗ | Op_ptr Conditional::dagger() const { | ||
88 | ✗ | const Op_ptr inner_dagger = op_->dagger(); | ||
89 | ✗ | return std::make_shared<Conditional>(inner_dagger, width_, value_); | ||
90 | } | |||
91 | ||||
92 | 2756 | Op_ptr Conditional::get_op() const { return op_; } | ||
93 | ||||
94 | 983 | unsigned Conditional::get_width() const { return width_; } | ||
95 | ||||
96 | 535 | unsigned Conditional::get_value() const { return value_; } | ||
97 | ||||
98 | ✗ | Conditional::Conditional() | ||
99 | ✗ | : Op(OpType::Conditional), op_(), width_(0), value_(0) {} | ||
100 | ||||
101 | } // namespace tket | |||
102 |