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 "CompilationUnit.hpp" | |||
16 | ||||
17 | #include <memory> | |||
18 | ||||
19 | #include "Utils/UnitID.hpp" | |||
20 | namespace tket { | |||
21 | ||||
22 | 161 | CompilationUnit::CompilationUnit(const Circuit& circ) : circ_(circ) { | ||
23 |
1/2✓ Branch 1 taken 161 times.
✗ Branch 2 not taken.
|
161 | initialize_maps(); | |
24 | 161 | } | ||
25 | ||||
26 | 29 | CompilationUnit::CompilationUnit( | ||
27 | 29 | const Circuit& circ, const PredicatePtrMap& preds) | ||
28 |
1/2✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
|
29 | : circ_(circ), target_preds(preds) { | |
29 |
1/2✓ Branch 1 taken 29 times.
✗ Branch 2 not taken.
|
29 | initialize_maps(); | |
30 |
1/2✓ Branch 1 taken 29 times.
✗ Branch 2 not taken.
|
29 | initialize_cache(); | |
31 | 29 | } | ||
32 | ||||
33 | ✗ | CompilationUnit::CompilationUnit( | ||
34 | ✗ | const Circuit& circ, const std::vector<PredicatePtr>& preds) | ||
35 | ✗ | : circ_(circ) { | ||
36 |
0/1? Decision couldn't be analyzed.
|
✗ | for (const PredicatePtr& pp : preds) target_preds.insert(make_type_pair(pp)); | |
37 | ✗ | initialize_maps(); | ||
38 | ✗ | initialize_cache(); | ||
39 | } | |||
40 | ||||
41 | 726 | TypePredicatePair CompilationUnit::make_type_pair(const PredicatePtr& ptr) { | ||
42 | 726 | const Predicate& p = *ptr; | ||
43 | 726 | std::type_index ti = typeid(p); | ||
44 | 726 | return {ti, ptr}; | ||
45 | } | |||
46 | ||||
47 | 216 | bool CompilationUnit::calc_predicate(const Predicate& pred) const { | ||
48 | 216 | return pred.verify(circ_); | ||
49 | } | |||
50 | ||||
51 | 26 | bool CompilationUnit::check_all_predicates() const { | ||
52 |
2/2✓ Branch 5 taken 30 times.
✓ Branch 6 taken 21 times.
|
2/2✓ Decision 'true' taken 30 times.
✓ Decision 'false' taken 21 times.
|
51 | for (const TypePredicatePair& ref_pred : target_preds) { |
53 |
3/4✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 25 times.
|
2/2✓ Decision 'true' taken 21 times.
✓ Decision 'false' taken 9 times.
|
30 | if (!calc_predicate(*ref_pred.second)) return false; |
54 | } | |||
55 | 21 | return true; | ||
56 | } | |||
57 | ||||
58 | ✗ | std::string CompilationUnit::to_string() const { | ||
59 | ✗ | std::string str = "~~~CompilationUnit~~~\n<tket::Circuit qubits=" + | ||
60 | ✗ | std::to_string(circ_.n_qubits()) + | ||
61 | ✗ | ", gates=" + std::to_string(circ_.n_gates()) + ">\n"; | ||
62 | ||||
63 |
0/2✗ Decision 'true' not taken.
✗ Decision 'false' not taken.
|
✗ | if (!target_preds.empty()) { | |
64 | ✗ | str += "Target Predicates:\n"; | ||
65 |
0/2✗ Decision 'true' not taken.
✗ Decision 'false' not taken.
|
✗ | for (const TypePredicatePair& pp : target_preds) { | |
66 | ✗ | str += (" " + pp.second->to_string() + "\n"); | ||
67 | } | |||
68 | } else | |||
69 | ✗ | str += "Target Predicates empty\n"; | ||
70 |
0/2✗ Decision 'true' not taken.
✗ Decision 'false' not taken.
|
✗ | if (!cache_.empty()) { | |
71 | ✗ | str += "Cache:\n"; | ||
72 | ✗ | for (const std::pair<const std::type_index, std::pair<PredicatePtr, bool>>& | ||
73 | ✗ | tp : cache_) { | ||
74 | ✗ | str += (" " + tp.second.first->to_string() + " :: "); | ||
75 | ✗ | str += tp.second.second ? "True\n" : "False\n"; | ||
76 | } | |||
77 | } else | |||
78 | ✗ | str += "Cache empty\n"; | ||
79 | ✗ | return str; | ||
80 | } | |||
81 | ||||
82 | ✗ | void CompilationUnit::empty_cache() const { cache_ = {}; } | ||
83 | ||||
84 | 29 | void CompilationUnit::initialize_cache() const { | ||
85 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 29 times.
|
29 | if (!cache_.empty()) |
86 | ✗ | throw std::logic_error("PredicateCache must be empty to be initialized"); | ||
87 |
2/2✓ Branch 4 taken 32 times.
✓ Branch 5 taken 29 times.
|
2/2✓ Decision 'true' taken 32 times.
✓ Decision 'false' taken 29 times.
|
61 | for (const TypePredicatePair& pr : target_preds) { |
88 | 32 | const Predicate& p = *(pr.second); | ||
89 | 32 | std::type_index ti = typeid(p); | ||
90 |
2/4✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 32 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 32 times.
|
32 | if (cache_.find(ti) != cache_.end()) |
91 | ✗ | throw std::logic_error("Duplicate verify type in Predicate list"); | ||
92 |
1/2✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
|
32 | bool to_cache = calc_predicate(p); | |
93 |
1/2✓ Branch 3 taken 32 times.
✗ Branch 4 not taken.
|
32 | cache_.insert({ti, {pr.second, to_cache}}); | |
94 | } | |||
95 | 29 | } | ||
96 | ||||
97 | 190 | void CompilationUnit::initialize_maps() { | ||
98 |
1/4✗ Branch 1 not taken.
✓ Branch 2 taken 190 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
1/2✓ Decision 'true' taken 190 times.
✗ Decision 'false' not taken.
|
190 | if (maps) throw std::logic_error("Maps already initialized"); |
99 | 190 | maps = std::make_shared<unit_bimaps_t>(); | ||
100 |
3/4✓ Branch 1 taken 190 times.
✗ Branch 2 not taken.
✓ Branch 7 taken 1535 times.
✓ Branch 8 taken 190 times.
|
0/1? Decision couldn't be analyzed.
|
1725 | for (const UnitID& u : circ_.all_units()) { |
101 |
2/4✓ Branch 2 taken 1535 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1535 times.
✗ Branch 6 not taken.
|
1535 | maps->initial.insert({u, u}); | |
102 |
2/4✓ Branch 2 taken 1535 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1535 times.
✗ Branch 6 not taken.
|
1535 | maps->final.insert({u, u}); | |
103 | 190 | } | ||
104 | 190 | } | ||
105 | ||||
106 | } // namespace tket | |||
107 |