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 "StandardSquash.hpp" | |||
16 | ||||
17 | #include <memory> | |||
18 | ||||
19 | #include "BasicOptimisation.hpp" | |||
20 | #include "Circuit/DAGDefs.hpp" | |||
21 | #include "Gate/Rotation.hpp" | |||
22 | #include "OpType/OpTypeInfo.hpp" | |||
23 | #include "SingleQubitSquash.hpp" | |||
24 | #include "Utils/Expression.hpp" | |||
25 | ||||
26 | namespace tket { | |||
27 | ||||
28 | namespace Transforms { | |||
29 | ||||
30 | 88 | StandardSquasher::StandardSquasher( | ||
31 | 88 | const OpTypeSet &singleqs, const Func &tk1_replacement) | ||
32 | 88 | : singleqs_(singleqs), | ||
33 |
1/2✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
|
88 | squash_fn_(tk1_replacement), | |
34 |
1/2✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
|
88 | combined_(), | |
35 |
2/4✓ Branch 2 taken 88 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 88 times.
✗ Branch 6 not taken.
|
176 | phase_(0.) { | |
36 |
2/2✓ Branch 5 taken 180 times.
✓ Branch 6 taken 88 times.
|
2/2✓ Decision 'true' taken 180 times.
✓ Decision 'false' taken 88 times.
|
268 | for (OpType ot : singleqs_) { |
37 |
2/4✓ Branch 1 taken 180 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 180 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 180 times.
|
180 | if (!is_single_qubit_type(ot)) |
38 | ✗ | throw BadOpType( | ||
39 | ✗ | "OpType given to standard_squash is not a single qubit gate", ot); | ||
40 | } | |||
41 | 88 | } | ||
42 | ||||
43 | 67 | bool StandardSquasher::accepts(Gate_ptr gp) const { | ||
44 | 67 | OpType type = gp->get_type(); | ||
45 |
5/8✓ Branch 2 taken 67 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 65 times.
✓ Branch 6 taken 2 times.
✓ Branch 8 taken 65 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 65 times.
✗ Branch 11 not taken.
|
67 | return (singleqs_.find(type) != singleqs_.end()) && !is_projective_type(type); | |
46 | } | |||
47 | ||||
48 | 354 | void StandardSquasher::append(Gate_ptr gp) { | ||
49 |
1/2✓ Branch 2 taken 354 times.
✗ Branch 3 not taken.
|
354 | std::vector<Expr> angs = gp->get_tk1_angles(); | |
50 |
4/8✓ Branch 1 taken 354 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 354 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 354 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 354 times.
✗ Branch 11 not taken.
|
354 | combined_.apply(Rotation(OpType::Rz, angs.at(2))); | |
51 |
4/8✓ Branch 1 taken 354 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 354 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 354 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 354 times.
✗ Branch 11 not taken.
|
354 | combined_.apply(Rotation(OpType::Rx, angs.at(1))); | |
52 |
4/8✓ Branch 1 taken 354 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 354 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 354 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 354 times.
✗ Branch 11 not taken.
|
354 | combined_.apply(Rotation(OpType::Rz, angs.at(0))); | |
53 |
2/4✓ Branch 1 taken 354 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 354 times.
✗ Branch 5 not taken.
|
354 | phase_ += angs.at(3); | |
54 | 354 | } | ||
55 | ||||
56 | 248 | std::pair<Circuit, Gate_ptr> StandardSquasher::flush( | ||
57 | std::optional<Pauli>) const { | |||
58 |
1/2✓ Branch 1 taken 248 times.
✗ Branch 2 not taken.
|
248 | auto [a, b, c] = combined_.to_pqp(OpType::Rz, OpType::Rx); | |
59 |
1/2✓ Branch 1 taken 248 times.
✗ Branch 2 not taken.
|
248 | Circuit replacement = squash_fn_(c, b, a); | |
60 |
7/8✓ Branch 1 taken 248 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 884 times.
✓ Branch 6 taken 248 times.
✓ Branch 8 taken 884 times.
✓ Branch 9 taken 248 times.
✓ Branch 11 taken 248 times.
✓ Branch 12 taken 248 times.
|
1380 | BGL_FORALL_VERTICES(rv, replacement.dag, DAG) { | |
61 |
1/2✓ Branch 1 taken 884 times.
✗ Branch 2 not taken.
|
884 | OpType v_type = replacement.get_OpType_from_Vertex(rv); | |
62 |
4/6✓ Branch 1 taken 884 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 388 times.
✓ Branch 4 taken 496 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 388 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 1272 times.
|
1272 | if (!is_boundary_q_type(v_type) && |
63 |
2/4✓ Branch 2 taken 388 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 884 times.
|
1272 | singleqs_.find(v_type) == singleqs_.end()) { | |
64 | ✗ | throw BadOpType( | ||
65 | "tk1_replacement given to standard_squash " | |||
66 | "does not preserve gate set", | |||
67 | ✗ | v_type); | ||
68 | } | |||
69 | } | |||
70 |
2/4✓ Branch 1 taken 248 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 248 times.
✗ Branch 5 not taken.
|
248 | replacement.add_phase(phase_); | |
71 |
1/2✓ Branch 1 taken 248 times.
✗ Branch 2 not taken.
|
496 | return {replacement, nullptr}; | |
72 | 248 | } | ||
73 | ||||
74 | 657 | void StandardSquasher::clear() { | ||
75 | 657 | combined_ = Rotation(); | ||
76 | 657 | phase_ = 0.; | ||
77 | 657 | } | ||
78 | ||||
79 | 10 | std::unique_ptr<AbstractSquasher> StandardSquasher::clone() const { | ||
80 | 10 | return std::make_unique<StandardSquasher>(*this); | ||
81 | } | |||
82 | ||||
83 | 13 | static bool standard_squash( | ||
84 | Circuit &circ, const OpTypeSet &singleqs, | |||
85 | const std::function<Circuit(const Expr &, const Expr &, const Expr &)> | |||
86 | &tk1_replacement) { | |||
87 |
1/2✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
|
13 | auto squasher = std::make_unique<StandardSquasher>(singleqs, tk1_replacement); | |
88 |
1/2✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
|
26 | return SingleQubitSquash(std::move(squasher), circ, false).squash(); | |
89 | 13 | } | ||
90 | ||||
91 | 13 | Transform squash_factory( | ||
92 | const OpTypeSet &singleqs, | |||
93 | const std::function<Circuit(const Expr &, const Expr &, const Expr &)> | |||
94 | &tk1_replacement) { | |||
95 | 13 | return Transform([=](Circuit &circ) { | ||
96 | 13 | return standard_squash(circ, singleqs, tk1_replacement); | ||
97 |
2/4✓ Branch 3 taken 13 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 13 times.
✗ Branch 7 not taken.
|
13 | }); | |
98 | } | |||
99 | ||||
100 | } // namespace Transforms | |||
101 | ||||
102 | } // namespace tket | |||
103 |