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 "Characterisation/DeviceCharacterisation.hpp" | |||
16 | ||||
17 | #include <optional> | |||
18 | ||||
19 | namespace tket { | |||
20 | ||||
21 | // simple get key wrapped in std::optional | |||
22 | template <typename T> | |||
23 | 44028 | static std::optional<typename T::mapped_type> maybe_get( | ||
24 | const T& error_map, const typename T::key_type& key) { | |||
25 |
1/2✓ Branch 1 taken 22014 times.
✗ Branch 2 not taken.
|
44028 | const auto& it = error_map.find(key); | |
26 | 88548 | return (it != error_map.end()) ? std::make_optional(it->second) | ||
27 |
3/4✓ Branch 0 taken 246 times.
✓ Branch 1 taken 21768 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
|
88062 | : std::nullopt; | |
28 | } | |||
29 | ||||
30 | // single-qubit case | |||
31 | 4232 | gate_error_t DeviceCharacterisation::get_error(const Node& n) const { | ||
32 |
1/2✓ Branch 1 taken 4232 times.
✗ Branch 2 not taken.
|
4232 | std::optional<gate_error_t> maybe_err = maybe_get(default_node_errors_, n); | |
33 |
2/2✓ Branch 1 taken 12 times.
✓ Branch 2 taken 4220 times.
|
4232 | return maybe_err ? *maybe_err : 0.; | |
34 | } | |||
35 | 2 | gate_error_t DeviceCharacterisation::get_error( | ||
36 | const Node& n, const OpType& op) const { | |||
37 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | std::optional<op_errors_t> maybe_dict = maybe_get(op_node_errors_, n); | |
38 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
1/2✓ Decision 'true' taken 2 times.
✗ Decision 'false' not taken.
|
2 | if (maybe_dict) { |
39 |
1/2✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | std::optional<gate_error_t> maybe_err = maybe_get(*maybe_dict, op); | |
40 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
1/2✓ Decision 'true' taken 2 times.
✗ Decision 'false' not taken.
|
2 | if (maybe_err) { |
41 | 2 | return *maybe_err; | ||
42 | } | |||
43 | } | |||
44 | ✗ | return get_error(n); | ||
45 | 2 | } | ||
46 | ||||
47 | // two-qubit case | |||
48 | 13544 | gate_error_t DeviceCharacterisation::get_error( | ||
49 | const Architecture::Connection& link) const { | |||
50 |
1/2✓ Branch 1 taken 13544 times.
✗ Branch 2 not taken.
|
13544 | std::optional<gate_error_t> maybe_err = maybe_get(default_link_errors_, link); | |
51 |
2/2✓ Branch 1 taken 192 times.
✓ Branch 2 taken 13352 times.
|
13544 | return maybe_err ? *maybe_err : 0.; | |
52 | } | |||
53 | 1 | gate_error_t DeviceCharacterisation::get_error( | ||
54 | const Architecture::Connection& link, const OpType& op) const { | |||
55 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | std::optional<op_errors_t> maybe_dict = maybe_get(op_link_errors_, link); | |
56 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1/2✓ Decision 'true' taken 1 times.
✗ Decision 'false' not taken.
|
1 | if (maybe_dict) { |
57 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | std::optional<gate_error_t> maybe_err = maybe_get(*maybe_dict, op); | |
58 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1/2✓ Decision 'true' taken 1 times.
✗ Decision 'false' not taken.
|
1 | if (maybe_err) { |
59 | 1 | return *maybe_err; | ||
60 | } | |||
61 | } | |||
62 | ✗ | return get_error(link); | ||
63 | 1 | } | ||
64 | ||||
65 | 4232 | readout_error_t DeviceCharacterisation::get_readout_error(const Node& n) const { | ||
66 |
1/2✓ Branch 1 taken 4232 times.
✗ Branch 2 not taken.
|
4232 | std::optional<gate_error_t> maybe_err = maybe_get(default_readout_errors_, n); | |
67 |
2/2✓ Branch 1 taken 36 times.
✓ Branch 2 taken 4196 times.
|
4232 | return maybe_err ? *maybe_err : 0.; | |
68 | } | |||
69 | ||||
70 | 2 | bool DeviceCharacterisation::operator==( | ||
71 | const DeviceCharacterisation& other) const { | |||
72 | 2 | return (this->default_node_errors_ == other.default_node_errors_) && | ||
73 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | (this->default_link_errors_ == other.default_link_errors_) && | |
74 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | (this->default_readout_errors_ == other.default_readout_errors_) && | |
75 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
|
6 | (this->op_node_errors_ == other.op_node_errors_) && | |
76 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
4 | (this->op_link_errors_ == other.op_link_errors_); | |
77 | } | |||
78 | ||||
79 | 9 | void to_json(nlohmann::json& j, const DeviceCharacterisation& dc) { | ||
80 |
1/2✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
9 | j["def_node_errors"] = dc.default_node_errors_; | |
81 |
1/2✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
9 | j["def_link_errors"] = dc.default_link_errors_; | |
82 |
1/2✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
9 | j["readouts"] = dc.default_readout_errors_; | |
83 |
1/2✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
9 | j["op_node_errors"] = dc.op_node_errors_; | |
84 |
1/2✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
9 | j["op_link_errors"] = dc.op_link_errors_; | |
85 | 9 | } | ||
86 | ||||
87 | 3 | void from_json(const nlohmann::json& j, DeviceCharacterisation& dc) { | ||
88 | 3 | dc.default_node_errors_ = j.at("def_node_errors").get<avg_node_errors_t>(); | ||
89 | 3 | dc.default_link_errors_ = j.at("def_link_errors").get<avg_link_errors_t>(); | ||
90 | 3 | dc.default_readout_errors_ = j.at("readouts").get<avg_readout_errors_t>(); | ||
91 | 3 | dc.op_node_errors_ = j.at("op_node_errors").get<op_node_errors_t>(); | ||
92 | 3 | dc.op_link_errors_ = j.at("op_link_errors").get<op_link_errors_t>(); | ||
93 | 3 | } | ||
94 | ||||
95 | } // namespace tket | |||
96 |