forked from Quantinuum/tket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredicates.cpp
More file actions
292 lines (280 loc) · 11.7 KB
/
Copy pathpredicates.cpp
File metadata and controls
292 lines (280 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright 2019-2024 Cambridge Quantum Computing
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "tket/Predicates/Predicates.hpp"
#include "binder_json.hpp"
#include "tket/Predicates/CompilationUnit.hpp"
#include "tket/Utils/UnitID.hpp"
#include "typecast.hpp"
namespace py = pybind11;
using json = nlohmann::json;
namespace tket {
static std::map<UnitID, UnitID> unit_bimap_to_map(const unit_bimap_t &bimap) {
std::map<UnitID, UnitID> res;
for (auto iter = bimap.left.begin(); iter != bimap.left.end(); ++iter) {
res.insert({iter->first, iter->second});
}
return res;
}
PYBIND11_MODULE(predicates, m) {
/* Predicates */
class PyPredicate : public Predicate {
public:
using Predicate::Predicate;
/* Trampolines (need one for each virtual function */
virtual bool verify(const Circuit &circ) const override {
PYBIND11_OVERLOAD_PURE(
bool, /* Return type */
Predicate, /* Parent class */
verify, /* Name of function in C++ (must match Python name) */
circ /* Argument(s) */
);
}
virtual bool implies(const Predicate &other) const override {
PYBIND11_OVERLOAD_PURE(
bool, /* Return type */
Predicate, /* Parent class */
implies, /* Name of function in C++ (must match Python name) */
other /* Argument(s) */
);
}
virtual PredicatePtr meet(const Predicate &other) const override {
PYBIND11_OVERLOAD_PURE(
PredicatePtr, /* Return type */
Predicate, /* Parent class */
meet, /* Name of function in C++ (must match Python name) */
other /* Argument(s) */
);
}
virtual std::string to_string() const override {
PYBIND11_OVERLOAD_PURE(
std::string, /* Return type */
Predicate, /* Parent class */
to_string /* Name of function in C++ (must match Python name) */
);
}
};
py::class_<Predicate, PredicatePtr, PyPredicate>(
m, "Predicate", "A predicate that may be satisfied by a circuit.")
.def(
"verify", &Predicate::verify,
":return: True if circuit satisfies predicate, else False",
py::arg("circuit"))
.def(
"implies", &Predicate::implies,
":return: True if predicate implies another one, else False",
py::arg("other"))
.def("__str__", [](const Predicate &) { return "<tket::Predicate>"; })
.def("__repr__", &Predicate::to_string)
.def(
"to_dict",
[](const PredicatePtr &predicate) {
return py::object(json(predicate)).cast<py::dict>();
},
"Return a JSON serializable dict representation of "
"the Predicate.\n\n"
":return: dict representation of the Predicate.")
.def_static(
"from_dict",
[](const py::dict &predicate_dict) {
return json(predicate_dict).get<PredicatePtr>();
},
"Construct Predicate instance from JSON serializable "
"dict representation of the Predicate.");
py::class_<GateSetPredicate, std::shared_ptr<GateSetPredicate>, Predicate>(
m, "GateSetPredicate",
"Predicate asserting that all operations are in the specified set of "
"types."
"\n\n"
"Note that the following are always permitted and do not need to be "
"included in the specified set:"
"\n\n"
"- 'meta' operations (inputs, outputs, barriers);\n"
"- ``OpType.Phase`` gates (which have no input or output wires)."
"\n\n"
"Classically conditioned operations are permitted provided that the "
"conditioned operation is of a permitted type.")
.def(
py::init<const OpTypeSet &>(), "Construct from a set of gate types.",
py::arg("allowed_types"))
.def_property_readonly("gate_set", &GateSetPredicate::get_allowed_types);
py::class_<
NoClassicalControlPredicate, std::shared_ptr<NoClassicalControlPredicate>,
Predicate>(
m, "NoClassicalControlPredicate",
"Predicate asserting that a circuit has no classical controls.")
.def(py::init<>(), "Constructor.");
py::class_<
NoFastFeedforwardPredicate, std::shared_ptr<NoFastFeedforwardPredicate>,
Predicate>(
m, "NoFastFeedforwardPredicate",
"Predicate asserting that a circuit has no fast feedforward.")
.def(py::init<>(), "Constructor.");
py::class_<
NoClassicalBitsPredicate, std::shared_ptr<NoClassicalBitsPredicate>,
Predicate>(
m, "NoClassicalBitsPredicate",
"Predicate asserting that a circuit has no classical wires.")
.def(py::init<>(), "Constructor.");
py::class_<
NoWireSwapsPredicate, std::shared_ptr<NoWireSwapsPredicate>, Predicate>(
m, "NoWireSwapsPredicate",
"Predicate asserting that a circuit has no wire swaps.")
.def(py::init<>(), "Constructor.");
py::class_<
MaxTwoQubitGatesPredicate, std::shared_ptr<MaxTwoQubitGatesPredicate>,
Predicate>(
m, "MaxTwoQubitGatesPredicate",
"Predicate asserting that a circuit has no gates with more than "
"two input wires.")
.def(py::init<>(), "Constructor.");
py::class_<
ConnectivityPredicate, std::shared_ptr<ConnectivityPredicate>, Predicate>(
m, "ConnectivityPredicate",
"Predicate asserting that a circuit satisfies a given connectivity "
"graph. The graph is always considered to be undirected.")
.def(
py::init<const Architecture &>(),
"Construct from an :py:class:`Architecture`.",
py::arg("architecture"));
py::class_<
DirectednessPredicate, std::shared_ptr<DirectednessPredicate>, Predicate>(
m, "DirectednessPredicate",
"Predicate asserting that a circuit satisfies a given connectivity "
"graph. The graph is always considered to be directed.")
.def(
py::init<const Architecture &>(),
"Construct from an :py:class:`Architecture`.",
py::arg("architecture"));
py::class_<
CliffordCircuitPredicate, std::shared_ptr<CliffordCircuitPredicate>,
Predicate>(
m, "CliffordCircuitPredicate",
"Predicate asserting that a circuit has only Clifford gates.")
.def(py::init<>(), "Constructor.");
py::class_<
UserDefinedPredicate, std::shared_ptr<UserDefinedPredicate>, Predicate>(
m, "UserDefinedPredicate", "User-defined predicate.")
.def(
py::init<const std::function<bool(const Circuit &)> &>(),
"Construct from a user-defined function from "
":py:class:`Circuit` to `bool`.",
py::arg("check_function"));
py::class_<
DefaultRegisterPredicate, std::shared_ptr<DefaultRegisterPredicate>,
Predicate>(
m, "DefaultRegisterPredicate",
"Predicate asserting that a circuit only uses the default quantum "
"and classical registers.")
.def(py::init<>(), "Constructor.");
py::class_<
MaxNQubitsPredicate, std::shared_ptr<MaxNQubitsPredicate>, Predicate>(
m, "MaxNQubitsPredicate",
"Predicate asserting that a circuit has at most n qubits.")
.def(py::init<unsigned>(), "Constructor.");
py::class_<
MaxNClRegPredicate, std::shared_ptr<MaxNClRegPredicate>, Predicate>(
m, "MaxNClRegPredicate",
"Predicate asserting that a circuit has at most n classical registers.")
.def(py::init<unsigned>(), "Constructor.");
py::class_<
PlacementPredicate, std::shared_ptr<PlacementPredicate>, Predicate>(
m, "PlacementPredicate",
"Predicate asserting that a circuit has been acted on by some "
"Placement object.")
.def(
py::init<const Architecture &>(),
"Construct from an :py:class:`Architecture`.",
py::arg("architecture"))
.def(
py::init<const node_set_t &>(), "Construct from a set of Node.",
py::arg("nodes"));
py::class_<
NoBarriersPredicate, std::shared_ptr<NoBarriersPredicate>, Predicate>(
m, "NoBarriersPredicate",
"Predicate asserting that a circuit contains no Barrier operations.")
.def(py::init<>(), "Constructor.");
py::class_<
CommutableMeasuresPredicate, std::shared_ptr<CommutableMeasuresPredicate>,
Predicate>(
m, "CommutableMeasuresPredicate",
"Predicate asserting that all measurements can be delayed to the end of "
"the circuit.")
.def(py::init<>(), "Constructor.");
py::class_<
NoMidMeasurePredicate, std::shared_ptr<NoMidMeasurePredicate>, Predicate>(
m, "NoMidMeasurePredicate",
"Predicate asserting that all measurements occur at the end of the "
"circuit.")
.def(py::init<>(), "Constructor.");
py::class_<
NoSymbolsPredicate, std::shared_ptr<NoSymbolsPredicate>, Predicate>(
m, "NoSymbolsPredicate",
"Predicate asserting that no gates in the circuit have symbolic "
"parameters.")
.def(py::init<>(), "Constructor.");
py::class_<
NormalisedTK2Predicate, std::shared_ptr<NormalisedTK2Predicate>,
Predicate>(
m, "NormalisedTK2Predicate",
"Asserts that all TK2 gates are normalised\n\n"
"A gate TK2(a, b, c) is considered normalised if\n\n"
" - If all expressions are non symbolic, then it must hold "
"`0.5 ≥ a ≥ b ≥ |c|`.\n"
" - In the ordering (a, b, c), any symbolic expression must appear "
"before non-symbolic ones. The remaining non-symbolic expressions must "
"still be ordered in non-increasing order and must be in the interval "
"[0, 1/2], with the exception of the last one that may be in "
"[-1/2, 1/2].\n")
.def(py::init<>(), "Constructor.");
/* Compilation units */
py::class_<CompilationUnit>(
m, "CompilationUnit",
"This class comprises a circuit and the predicates that the "
"circuit is required to satisfy, for example to run on a backend.")
.def(
py::init<const Circuit &>(),
"Construct from a circuit, with no predicates.", py::arg("circuit"))
.def(
py::init<
const Circuit &,
const py::tket_custom::SequenceVec<PredicatePtr> &>(),
"Construct from a circuit and some required predicates.",
py::arg("circuit"), py::arg("predicates"))
.def(
"check_all_predicates", &CompilationUnit::check_all_predicates,
":return: True if all predicates are satisfied, else False")
.def_property_readonly(
"circuit",
[](const CompilationUnit &cu) { return Circuit(cu.get_circ_ref()); },
"Return a copy of the circuit.")
.def_property_readonly(
"initial_map",
[](const CompilationUnit &cu) {
return unit_bimap_to_map(cu.get_initial_map_ref());
},
"Returns the map from the original qubits to the "
"corresponding qubits at the start of the current circuit.")
.def_property_readonly(
"final_map",
[](const CompilationUnit &cu) {
return unit_bimap_to_map(cu.get_final_map_ref());
},
"Returns the map from the original qubits to their "
"corresponding qubits at the end of the current circuit.")
.def(
"__str__",
[](const CompilationUnit &) { return "<tket::CompilationUnit>"; })
.def("__repr__", &CompilationUnit::to_string);
}
} // namespace tket