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 "OpTypeFunctions.hpp" | |||
16 | ||||
17 | #include <memory> | |||
18 | ||||
19 | #include "OpType.hpp" | |||
20 | ||||
21 | namespace tket { | |||
22 | ||||
23 | 24293847 | bool find_in_set(const OpType& val, const OpTypeSet& set) { | ||
24 |
1/2✓ Branch 2 taken 24293847 times.
✗ Branch 3 not taken.
|
24293847 | return set.find(val) != set.cend(); | |
25 | } | |||
26 | ||||
27 | 5983732 | const OpTypeSet& all_gate_types() { | ||
28 | static const OpTypeSet optypes{ | |||
29 | OpType::Z, OpType::X, OpType::Y, OpType::S, | |||
30 | OpType::Sdg, OpType::T, OpType::Tdg, OpType::V, | |||
31 | OpType::Vdg, OpType::SX, OpType::SXdg, OpType::H, | |||
32 | OpType::Rx, OpType::Ry, OpType::Rz, OpType::U3, | |||
33 | OpType::U2, OpType::U1, OpType::TK1, OpType::CX, | |||
34 | OpType::CY, OpType::CZ, OpType::CH, OpType::CV, | |||
35 | OpType::CVdg, OpType::CSX, OpType::CSXdg, OpType::CRz, | |||
36 | OpType::CRx, OpType::CRy, OpType::CU1, OpType::CU3, | |||
37 | OpType::PhaseGadget, OpType::CCX, OpType::SWAP, OpType::CSWAP, | |||
38 | OpType::noop, OpType::Measure, OpType::Reset, OpType::ECR, | |||
39 | OpType::ISWAP, OpType::PhasedX, OpType::ZZMax, OpType::XXPhase, | |||
40 | OpType::YYPhase, OpType::ZZPhase, OpType::CnRy, OpType::CnX, | |||
41 | OpType::CnZ, OpType::CnY, OpType::BRIDGE, OpType::Collapse, | |||
42 | OpType::ESWAP, OpType::FSim, OpType::Sycamore, OpType::ISWAPMax, | |||
43 | OpType::PhasedISWAP, OpType::XXPhase3, OpType::NPhasedX, OpType::TK2, | |||
44 |
4/8✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5983731 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
|
5983732 | OpType::Phase}; | |
45 | static std::unique_ptr<const OpTypeSet> gates = | |||
46 |
4/8✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5983731 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
5983732 | std::make_unique<const OpTypeSet>(optypes); | |
47 | 5983732 | return *gates; | ||
48 | } | |||
49 | ||||
50 | 170632 | const OpTypeSet& all_multi_qubit_types() { | ||
51 | static const OpTypeSet optypes{ | |||
52 | OpType::CX, OpType::CY, OpType::CZ, | |||
53 | OpType::CH, OpType::CV, OpType::CVdg, | |||
54 | OpType::CSX, OpType::CSXdg, OpType::CRz, | |||
55 | OpType::CRx, OpType::CRy, OpType::CU1, | |||
56 | OpType::CU3, OpType::PhaseGadget, OpType::CCX, | |||
57 | OpType::SWAP, OpType::CSWAP, OpType::ECR, | |||
58 | OpType::ISWAP, OpType::ZZMax, OpType::XXPhase, | |||
59 | OpType::YYPhase, OpType::ZZPhase, OpType::CnRy, | |||
60 | OpType::CnX, OpType::CnZ, OpType::CnY, | |||
61 | OpType::BRIDGE, OpType::ESWAP, OpType::FSim, | |||
62 | OpType::Sycamore, OpType::ISWAPMax, OpType::PhasedISWAP, | |||
63 |
4/8✓ Branch 0 taken 1 times.
✓ Branch 1 taken 170631 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
|
170632 | OpType::XXPhase3, OpType::NPhasedX, OpType::TK2}; | |
64 | static std::unique_ptr<const OpTypeSet> gates = | |||
65 |
4/8✓ Branch 0 taken 1 times.
✓ Branch 1 taken 170631 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
170632 | std::make_unique<const OpTypeSet>(optypes); | |
66 | 170632 | return *gates; | ||
67 | } | |||
68 | ||||
69 | // the set of OpTypes that implement Gate_ptr->get_tk1_angles() | |||
70 | 17529 | const OpTypeSet& all_single_qubit_unitary_types() { | ||
71 | static const OpTypeSet optypes{ | |||
72 | OpType::noop, OpType::Z, OpType::X, OpType::Y, OpType::S, | |||
73 | OpType::Sdg, OpType::T, OpType::Tdg, OpType::V, OpType::Vdg, | |||
74 | OpType::SX, OpType::SXdg, OpType::H, OpType::Rx, OpType::Ry, | |||
75 | OpType::Rz, OpType::U1, OpType::U2, OpType::U3, OpType::PhasedX, | |||
76 |
4/8✓ Branch 0 taken 1 times.
✓ Branch 1 taken 17528 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
|
17529 | OpType::TK1}; | |
77 | static std::unique_ptr<const OpTypeSet> gates = | |||
78 |
4/8✓ Branch 0 taken 1 times.
✓ Branch 1 taken 17528 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
17529 | std::make_unique<const OpTypeSet>(optypes); | |
79 | 17529 | return *gates; | ||
80 | } | |||
81 | ||||
82 | 332 | const OpTypeSet& all_single_qubit_types() { | ||
83 | static const OpTypeSet optypes{ | |||
84 | OpType::Z, OpType::X, OpType::Y, OpType::S, | |||
85 | OpType::Sdg, OpType::T, OpType::Tdg, OpType::V, | |||
86 | OpType::Vdg, OpType::SX, OpType::SXdg, OpType::H, | |||
87 | OpType::Rx, OpType::Ry, OpType::Rz, OpType::U3, | |||
88 | OpType::U2, OpType::U1, OpType::TK1, OpType::Measure, | |||
89 |
4/8✓ Branch 0 taken 1 times.
✓ Branch 1 taken 331 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
|
332 | OpType::Reset, OpType::Collapse, OpType::PhasedX, OpType::noop}; | |
90 | static std::unique_ptr<const OpTypeSet> gates = | |||
91 |
4/8✓ Branch 0 taken 1 times.
✓ Branch 1 taken 331 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
332 | std::make_unique<const OpTypeSet>(optypes); | |
92 | 332 | return *gates; | ||
93 | } | |||
94 | ||||
95 | 2208834 | const OpTypeSet& all_classical_types() { | ||
96 | static const OpTypeSet optypes = { | |||
97 | OpType::ClassicalTransform, OpType::SetBits, | |||
98 | OpType::CopyBits, OpType::RangePredicate, | |||
99 | OpType::ExplicitPredicate, OpType::ExplicitModifier, | |||
100 | OpType::MultiBit, OpType::WASM, | |||
101 |
4/8✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2208833 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
|
2208834 | }; | |
102 | static std::unique_ptr<const OpTypeSet> gates = | |||
103 |
4/8✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2208833 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
2208834 | std::make_unique<const OpTypeSet>(optypes); | |
104 | 2208834 | return *gates; | ||
105 | } | |||
106 | ||||
107 | 408848 | const OpTypeSet& all_projective_types() { | ||
108 | static const OpTypeSet optypes{ | |||
109 |
4/8✓ Branch 0 taken 1 times.
✓ Branch 1 taken 408847 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
|
408848 | OpType::Measure, OpType::Collapse, OpType::Reset}; | |
110 | static std::unique_ptr<const OpTypeSet> gates = | |||
111 |
4/8✓ Branch 0 taken 1 times.
✓ Branch 1 taken 408847 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
408848 | std::make_unique<const OpTypeSet>(optypes); | |
112 | 408848 | return *gates; | ||
113 | } | |||
114 | ||||
115 | 31 | const OpTypeSet& all_controlled_gate_types() { | ||
116 | static const OpTypeSet optypes{ | |||
117 | OpType::CX, OpType::CCX, OpType::CnX, OpType::CnZ, OpType::CnY, | |||
118 | OpType::CSX, OpType::CSXdg, OpType::CV, OpType::CVdg, OpType::CRx, | |||
119 | OpType::CnRy, OpType::CRy, OpType::CY, OpType::CRz, OpType::CZ, | |||
120 |
4/8✓ Branch 0 taken 1 times.
✓ Branch 1 taken 30 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
|
31 | OpType::CH, OpType::CU1, OpType::CU3}; | |
121 | static std::unique_ptr<const OpTypeSet> gates = | |||
122 |
4/8✓ Branch 0 taken 1 times.
✓ Branch 1 taken 30 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
31 | std::make_unique<const OpTypeSet>(optypes); | |
123 | 31 | return *gates; | ||
124 | } | |||
125 | ||||
126 | 3400690 | bool is_metaop_type(OpType optype) { | ||
127 | static const OpTypeSet metaops = { | |||
128 | OpType::Input, OpType::Output, OpType::ClInput, OpType::ClOutput, | |||
129 |
4/8✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3400689 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
|
3400690 | OpType::Barrier, OpType::Create, OpType::Discard}; | |
130 | 3400690 | return find_in_set(optype, metaops); | ||
131 | } | |||
132 | ||||
133 | 1598318 | bool is_initial_q_type(OpType optype) { | ||
134 |
3/4✓ Branch 0 taken 1537707 times.
✓ Branch 1 taken 60611 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1537707 times.
|
1598318 | return optype == OpType::Input || optype == OpType::Create; | |
135 | } | |||
136 | ||||
137 | 6442087 | bool is_final_q_type(OpType optype) { | ||
138 |
4/4✓ Branch 0 taken 5444413 times.
✓ Branch 1 taken 997674 times.
✓ Branch 2 taken 32 times.
✓ Branch 3 taken 5444381 times.
|
6442087 | return optype == OpType::Output || optype == OpType::Discard; | |
139 | } | |||
140 | ||||
141 | 1282245 | bool is_boundary_q_type(OpType optype) { | ||
142 |
4/4✓ Branch 1 taken 1233521 times.
✓ Branch 2 taken 48724 times.
✓ Branch 4 taken 48724 times.
✓ Branch 5 taken 1184797 times.
|
1282245 | return is_initial_q_type(optype) || is_final_q_type(optype); | |
143 | } | |||
144 | ||||
145 | 1097147 | bool is_boundary_c_type(OpType optype) { | ||
146 |
4/4✓ Branch 0 taken 1097122 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 25 times.
✓ Branch 3 taken 1097097 times.
|
1097147 | return optype == OpType::ClInput || optype == OpType::ClOutput; | |
147 | } | |||
148 | ||||
149 | 5983725 | bool is_gate_type(OpType optype) { | ||
150 | 5983725 | return find_in_set(optype, all_gate_types()); | ||
151 | } | |||
152 | ||||
153 | 2029838 | bool is_box_type(OpType optype) { | ||
154 | static const OpTypeSet boxes = { | |||
155 | OpType::CircBox, | |||
156 | OpType::Unitary1qBox, | |||
157 | OpType::Unitary2qBox, | |||
158 | OpType::Unitary3qBox, | |||
159 | OpType::ExpBox, | |||
160 | OpType::PauliExpBox, | |||
161 | OpType::CustomGate, | |||
162 | OpType::CliffBox, | |||
163 | OpType::PhasePolyBox, | |||
164 | OpType::QControlBox, | |||
165 | OpType::ClassicalExpBox, | |||
166 | OpType::ProjectorAssertionBox, | |||
167 | OpType::StabiliserAssertionBox, | |||
168 | OpType::UnitaryTableauBox, | |||
169 |
4/8✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2029837 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
|
2029838 | OpType::ToffoliBox}; | |
170 | 2029838 | return find_in_set(optype, boxes); | ||
171 | } | |||
172 | ||||
173 | 2013022 | bool is_flowop_type(OpType optype) { | ||
174 | static const OpTypeSet flowops = { | |||
175 |
4/8✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2013021 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
|
2013022 | OpType::Label, OpType::Branch, OpType::Goto, OpType::Stop}; | |
176 | 2013022 | return find_in_set(optype, flowops); | ||
177 | } | |||
178 | ||||
179 | 2012801 | bool is_rotation_type(OpType optype) { | ||
180 | static const OpTypeSet rotation_gates = { | |||
181 | OpType::Rx, OpType::Ry, OpType::Rz, OpType::U1, | |||
182 | OpType::CnRy, OpType::CRz, OpType::CRx, OpType::CRy, | |||
183 | OpType::CU1, OpType::XXPhase, OpType::YYPhase, OpType::ZZPhase, | |||
184 |
4/8✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2012800 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
|
2012801 | OpType::ESWAP, OpType::ISWAP, OpType::XXPhase3}; | |
185 | 2012801 | return find_in_set(optype, rotation_gates); | ||
186 | } | |||
187 | ||||
188 | 2012801 | bool is_parameterised_pauli_rotation_type(OpType optype) { | ||
189 | static const OpTypeSet parameterised_pauli_rotations = { | |||
190 |
4/8✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2012800 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
|
2012801 | OpType::Rx, OpType::Ry, OpType::Rz, OpType::U1}; | |
191 | 2012801 | return find_in_set(optype, parameterised_pauli_rotations); | ||
192 | } | |||
193 | ||||
194 | 170632 | bool is_multi_qubit_type(OpType optype) { | ||
195 | 170632 | return find_in_set(optype, all_multi_qubit_types()); | ||
196 | } | |||
197 | ||||
198 | 313 | bool is_single_qubit_type(OpType optype) { | ||
199 | 313 | return find_in_set(optype, all_single_qubit_types()); | ||
200 | } | |||
201 | ||||
202 | // the set of OpTypes that implement Gate_ptr->get_tk1_angles() | |||
203 | 17529 | bool is_single_qubit_unitary_type(OpType optype) { | ||
204 | 17529 | return find_in_set(optype, all_single_qubit_unitary_types()); | ||
205 | } | |||
206 | ||||
207 | 2012801 | bool is_oneway_type(OpType optype) { | ||
208 | // This set should contain only gates for which an dagger is nonsensical | |||
209 | // or we do not yet have the dagger gate as an OpType. | |||
210 | // If the gate can have an dagger, define it in the dagger() method. | |||
211 | static const OpTypeSet no_defined_inverse = { | |||
212 | OpType::Input, OpType::Output, OpType::Measure, | |||
213 | OpType::ClInput, OpType::ClOutput, OpType::Barrier, | |||
214 | OpType::Reset, OpType::Collapse, OpType::CustomGate, | |||
215 |
4/8✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2012800 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
|
2012801 | OpType::PhasePolyBox, OpType::Create, OpType::Discard}; | |
216 | 2012801 | return find_in_set(optype, no_defined_inverse); | ||
217 | } | |||
218 | ||||
219 | 2020888 | bool is_clifford_type(OpType optype) { | ||
220 | static const OpTypeSet clifford_gates = { | |||
221 | OpType::Z, OpType::X, OpType::Y, OpType::S, | |||
222 | OpType::Sdg, OpType::V, OpType::Vdg, OpType::SX, | |||
223 | OpType::SXdg, OpType::H, OpType::CX, OpType::CY, | |||
224 | OpType::CZ, OpType::SWAP, OpType::BRIDGE, OpType::noop, | |||
225 | OpType::ZZMax, OpType::ECR, OpType::ISWAPMax, OpType::UnitaryTableauBox, | |||
226 |
4/8✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2020887 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
|
2020888 | OpType::Phase}; | |
227 | 2020888 | return find_in_set(optype, clifford_gates); | ||
228 | } | |||
229 | ||||
230 | 408838 | bool is_projective_type(OpType optype) { | ||
231 | 408838 | return find_in_set(optype, all_projective_types()); | ||
232 | } | |||
233 | ||||
234 | 2208796 | bool is_classical_type(OpType optype) { | ||
235 | 2208796 | return find_in_set(optype, all_classical_types()); | ||
236 | } | |||
237 | ||||
238 | 31 | bool is_controlled_gate_type(OpType optype) { | ||
239 | 31 | return find_in_set(optype, all_controlled_gate_types()); | ||
240 | } | |||
241 | } // namespace tket | |||
242 |