| 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 | #pragma once | |||
| 16 | ||||
| 17 | #include <optional> | |||
| 18 | #include <sstream> | |||
| 19 | #include <string> | |||
| 20 | #include <tklog/TketLog.hpp> | |||
| 21 | ||||
| 22 | #include "DAGDefs.hpp" | |||
| 23 | #include "Ops/OpPtr.hpp" | |||
| 24 | #include "Utils/Json.hpp" | |||
| 25 | #include "Utils/UnitID.hpp" | |||
| 26 | ||||
| 27 | namespace tket { | |||
| 28 | ||||
| 29 | class BadCommand : public std::logic_error { | |||
| 30 | public: | |||
| 31 | explicit BadCommand(const std::string &message) : std::logic_error(message) {} | |||
| 32 | }; | |||
| 33 | ||||
| 34 | class Command { | |||
| 35 | public: | |||
| 36 | ✗ | Command() : op_ptr(nullptr) {} | ||
| 37 | ✗ | Command( | ||
| 38 | const Op_ptr _gate, unit_vector_t _args, | |||
| 39 | const std::optional<std::string> op_group = std::nullopt, | |||
| 40 | const Vertex _vert = boost::graph_traits<DAG>::null_vertex()) | |||
| 41 | ✗ | : op_ptr(_gate), args(_args), opgroup(op_group), vert(_vert) {} | ||
| 42 | ✗ | bool operator==(const Command other) const { | ||
| 43 | return ( | |||
| 44 | ✗ | *op_ptr == *other.op_ptr && args == other.args && | ||
| 45 | ✗ | opgroup == other.opgroup); | ||
| 46 | } | |||
| 47 | ||||
| 48 | ✗ | Op_ptr get_op_ptr() const { return op_ptr; } | ||
| 49 | 1927 | std::optional<std::string> get_opgroup() const { return opgroup; } | ||
| 50 | ✗ | unit_vector_t get_args() const { return args; } | ||
| 51 | ✗ | qubit_vector_t get_qubits() const { | ||
| 52 | ✗ | qubit_vector_t qbs; | ||
| 53 | ✗ | op_signature_t sig = op_ptr->get_signature(); | ||
| 54 |
0/2✗ Decision 'true' not taken.
✗ Decision 'false' not taken.
|
✗ | for (unsigned i = 0; i < sig.size(); ++i) { | |
| 55 |
0/2✗ Decision 'true' not taken.
✗ Decision 'false' not taken.
|
✗ | if (sig[i] == EdgeType::Quantum) { | |
| 56 | ✗ | qbs.push_back(Qubit(args[i])); | ||
| 57 | } | |||
| 58 | } | |||
| 59 | ✗ | return qbs; | ||
| 60 | } | |||
| 61 | ✗ | bit_vector_t get_bits() const { | ||
| 62 | ✗ | bit_vector_t bs; | ||
| 63 | ✗ | op_signature_t sig = op_ptr->get_signature(); | ||
| 64 |
0/2✗ Decision 'true' not taken.
✗ Decision 'false' not taken.
|
✗ | for (unsigned i = 0; i < sig.size(); ++i) { | |
| 65 |
0/2✗ Decision 'true' not taken.
✗ Decision 'false' not taken.
|
✗ | if (sig[i] == EdgeType::Classical) { | |
| 66 | ✗ | bs.push_back(Bit(args[i])); | ||
| 67 | } | |||
| 68 | } | |||
| 69 | ✗ | return bs; | ||
| 70 | } | |||
| 71 | ✗ | Vertex get_vertex() const { return vert; } | ||
| 72 | ✗ | std::string to_str() const { | ||
| 73 | ✗ | std::stringstream cmd_str; | ||
| 74 |
0/2✗ Decision 'true' not taken.
✗ Decision 'false' not taken.
|
✗ | if (opgroup) { | |
| 75 | ✗ | cmd_str << "[" << opgroup.value() << "] "; | ||
| 76 | } | |||
| 77 | ✗ | cmd_str << op_ptr->get_command_str(args); | ||
| 78 | ✗ | return cmd_str.str(); | ||
| 79 | } | |||
| 80 | ✗ | friend std::ostream &operator<<(std::ostream &out, const Command &c) { | ||
| 81 | ✗ | out << c.to_str(); | ||
| 82 | ✗ | return out; | ||
| 83 | }; | |||
| 84 | ||||
| 85 | private: | |||
| 86 | Op_ptr op_ptr; | |||
| 87 | unit_vector_t args; // indexed by port numbering | |||
| 88 | std::optional<std::string> opgroup; | |||
| 89 | Vertex vert; // vertex in the DAG | |||
| 90 | }; | |||
| 91 | ||||
| 92 | JSON_DECL(Command) | |||
| 93 | ||||
| 94 | } // namespace tket | |||
| 95 |