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 "Command.hpp" | |||
16 | #include "Utils/Json.hpp" | |||
17 | ||||
18 | namespace tket { | |||
19 | ||||
20 | 76 | void to_json(nlohmann::json& j, const Command& com) { | ||
21 | 76 | const Op_ptr op = com.get_op_ptr(); | ||
22 |
1/2✓ Branch 1 taken 76 times.
✗ Branch 2 not taken.
|
76 | const std::optional<std::string> opgroup = com.get_opgroup(); | |
23 |
2/4✓ Branch 1 taken 76 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 76 times.
✗ Branch 5 not taken.
|
76 | j["op"] = op; | |
24 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 73 times.
|
2/2✓ Decision 'true' taken 3 times.
✓ Decision 'false' taken 73 times.
|
76 | if (opgroup) { |
25 |
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["opgroup"] = opgroup.value(); | |
26 | } | |||
27 | ||||
28 |
1/2✓ Branch 2 taken 76 times.
✗ Branch 3 not taken.
|
76 | const op_signature_t& sig = op->get_signature(); | |
29 |
1/2✓ Branch 1 taken 76 times.
✗ Branch 2 not taken.
|
76 | const unit_vector_t& args = com.get_args(); | |
30 | ||||
31 | 76 | nlohmann::json j_args; | ||
32 |
2/2✓ Branch 1 taken 149 times.
✓ Branch 2 taken 76 times.
|
2/2✓ Decision 'true' taken 149 times.
✓ Decision 'false' taken 76 times.
|
225 | for (size_t i = 0; i < sig.size(); i++) { |
33 |
2/2✓ Branch 1 taken 126 times.
✓ Branch 2 taken 23 times.
|
2/2✓ Decision 'true' taken 126 times.
✓ Decision 'false' taken 23 times.
|
149 | if (sig[i] == EdgeType::Quantum) { |
34 | 126 | const Qubit& qb = static_cast<const Qubit&>(args[i]); | ||
35 |
2/4✓ Branch 1 taken 126 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 126 times.
✗ Branch 5 not taken.
|
126 | j_args.push_back(qb); | |
36 | } else { | |||
37 | 23 | const Bit& cb = static_cast<const Bit&>(args[i]); | ||
38 |
2/4✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 23 times.
✗ Branch 5 not taken.
|
23 | j_args.push_back(cb); | |
39 | } | |||
40 | } | |||
41 | ||||
42 |
2/4✓ Branch 1 taken 76 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 76 times.
✗ Branch 5 not taken.
|
76 | j["args"] = j_args; | |
43 | 76 | } | ||
44 | 1856 | void from_json(const nlohmann::json& j, Command& com) { | ||
45 |
2/4✓ Branch 1 taken 1856 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1856 times.
✗ Branch 5 not taken.
|
1856 | const auto op = j.at("op").get<Op_ptr>(); | |
46 | 1856 | std::optional<std::string> opgroup; | ||
47 |
3/4✓ Branch 1 taken 1856 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 1853 times.
|
2/2✓ Decision 'true' taken 3 times.
✓ Decision 'false' taken 1853 times.
|
1856 | if (j.contains("opgroup")) { |
48 |
2/4✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
3 | opgroup = j.at("opgroup").get<std::string>(); | |
49 | } | |||
50 |
1/2✓ Branch 2 taken 1856 times.
✗ Branch 3 not taken.
|
1856 | const op_signature_t& sig = op->get_signature(); | |
51 | ||||
52 |
1/2✓ Branch 1 taken 1856 times.
✗ Branch 2 not taken.
|
1856 | const nlohmann::json& j_args = j.at("args"); | |
53 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 1856 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 1856 times.
|
1856 | if (sig.size() != j_args.size()) { |
54 | ✗ | JsonError("Number of args does not match signature of op."); | ||
55 | } | |||
56 | 1856 | unit_vector_t args; | ||
57 |
2/2✓ Branch 1 taken 3129 times.
✓ Branch 2 taken 1856 times.
|
2/2✓ Decision 'true' taken 3129 times.
✓ Decision 'false' taken 1856 times.
|
4985 | for (size_t i = 0; i < sig.size(); i++) { |
58 |
2/2✓ Branch 1 taken 3114 times.
✓ Branch 2 taken 15 times.
|
2/2✓ Decision 'true' taken 3114 times.
✓ Decision 'false' taken 15 times.
|
3129 | if (sig[i] == EdgeType::Quantum) { |
59 |
3/6✓ Branch 1 taken 3114 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3114 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 3114 times.
✗ Branch 8 not taken.
|
3114 | args.push_back(j_args[i].get<Qubit>()); | |
60 | } else { | |||
61 |
3/6✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 15 times.
✗ Branch 8 not taken.
|
15 | args.push_back(j_args[i].get<Bit>()); | |
62 | } | |||
63 | } | |||
64 | ||||
65 |
4/8✓ Branch 1 taken 1856 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1856 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1856 times.
✗ Branch 8 not taken.
✓ Branch 11 taken 1856 times.
✗ Branch 12 not taken.
|
1856 | com = Command(op, args, opgroup); | |
66 | 1856 | } | ||
67 | ||||
68 | } // namespace tket | |||
69 |