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 <functional> | |||
18 | #include <memory> | |||
19 | ||||
20 | #include "Circuit/Circuit.hpp" | |||
21 | ||||
22 | namespace tket { | |||
23 | ||||
24 | /** | |||
25 | * A transformation of a circuit that preserves its semantics | |||
26 | */ | |||
27 | class Transform { | |||
28 | public: | |||
29 | /** | |||
30 | * A function that takes a circuit and (optionally) a relabelling of units. | |||
31 | * | |||
32 | * The relabelling, if present, maps the original unit IDs at the beginning | |||
33 | * and end of the circuit to new names, which may be in a different order at | |||
34 | * the beginning and end. | |||
35 | * | |||
36 | * The function returns false if no changes are made, otherwise true. | |||
37 | */ | |||
38 | typedef std::function<bool(Circuit&, std::shared_ptr<unit_bimaps_t>)> | |||
39 | Transformation; | |||
40 | ||||
41 | /** | |||
42 | * A function that takes a circuit and does not rename any units. | |||
43 | * | |||
44 | * The function returns false if no changes are made, otherwise true. | |||
45 | */ | |||
46 | typedef std::function<bool(Circuit&)> SimpleTransformation; | |||
47 | ||||
48 | typedef std::function<unsigned(const Circuit&)> Metric; | |||
49 | ||||
50 | /** The transformation applied. */ | |||
51 | Transformation apply_fn; | |||
52 | ||||
53 | /** Construct from a transformation function */ | |||
54 | ✗ | explicit Transform(const Transformation& trans) : apply_fn(trans) {} | ||
55 | ||||
56 | /** Construct from a transformation function that preserves unit IDs */ | |||
57 | 34522 | explicit Transform(const SimpleTransformation& trans) | ||
58 |
1/2✓ Branch 2 taken 34522 times.
✗ Branch 3 not taken.
|
34522 | : apply_fn([=](Circuit& circ, std::shared_ptr<unit_bimaps_t>) { | |
59 | 35698 | return trans(circ); | ||
60 | 34522 | }) {} | ||
61 | ||||
62 | /** | |||
63 | * Apply the transform to a circuit | |||
64 | * | |||
65 | * @param circ circuit to be transformed | |||
66 | * | |||
67 | * @return whether any changes were made | |||
68 | */ | |||
69 | ✗ | bool apply(Circuit& circ) const { return apply_fn(circ, nullptr); } | ||
70 | ||||
71 | /** | |||
72 | * Compose two transforms in sequence | |||
73 | * | |||
74 | * @param[in] lhs first transform | |||
75 | * @param[in] rhs second transform | |||
76 | * | |||
77 | * @return the composite transform | |||
78 | */ | |||
79 | friend Transform operator>>(const Transform& lhs, const Transform& rhs); | |||
80 | }; | |||
81 | ||||
82 | namespace Transforms { | |||
83 | ||||
84 | /** | |||
85 | * Identity transform (does nothing, returns false) | |||
86 | */ | |||
87 | inline const Transform id = | |||
88 | ✗ | Transform([](Circuit&, std::shared_ptr<unit_bimaps_t>) { return false; }); | ||
89 | ||||
90 | /** | |||
91 | * @brief A simple struct to store some two-qubit gate fidelities. | |||
92 | * | |||
93 | * We currently support CX, ZZMax and ZZPhase. | |||
94 | * | |||
95 | * This is meant to be easily extensible when further gate types will be | |||
96 | * supported. | |||
97 | * | |||
98 | */ | |||
99 | struct TwoQbFidelities { | |||
100 | std::optional<double> CX_fidelity; | |||
101 | std::optional<double> ZZMax_fidelity; | |||
102 | std::optional<std::function<double(double)>> ZZPhase_fidelity; | |||
103 | }; | |||
104 | ||||
105 | } // namespace Transforms | |||
106 | ||||
107 | } // namespace tket | |||
108 |