tket
Loading...
Searching...
No Matches
ClassicalOps.hpp
Go to the documentation of this file.
1// Copyright Quantinuum
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
22#include <memory>
23
24#include "Op.hpp"
25#include "OpPtr.hpp"
26
27namespace tket {
28
32class ClassicalOp : public Op {
33 public:
47 OpType type, unsigned n_i, unsigned n_io, unsigned n_o,
48 const std::string &name = "");
49
50 SymSet free_symbols() const override { return {}; }
51 unsigned n_qubits() const override { return 0; }
52
53 op_signature_t get_signature() const override { return sig_; }
54
55 nlohmann::json serialize() const override;
56
57 static Op_ptr deserialize(const nlohmann::json &j);
58
59 std::string get_name(bool latex = false) const override;
60
62 unsigned get_n_i() const { return n_i_; }
63
65 unsigned get_n_io() const { return n_io_; }
66
68 unsigned get_n_o() const { return n_o_; }
69
73 bool is_equal(const Op &other) const override;
74
75 protected:
76 const unsigned n_i_;
77 const unsigned n_io_;
78 const unsigned n_o_;
79 const std::string name_;
80 std::vector<EdgeType> sig_;
81};
82
87 public:
94
96 const SymEngine::map_basic_basic &) const override {
97 return std::make_shared<OpaqueClassicalOp>(*this);
98 }
99
103 nlohmann::json serialize() const override;
104
108 static Op_ptr deserialize(const nlohmann::json &j);
109
113 bool is_equal(const Op &other) const override;
114};
115
117 public:
132 OpType type, unsigned n_i, unsigned n_io, unsigned n_o,
133 const std::string &name = "");
134
142 virtual std::vector<bool> eval(const std::vector<bool> &x) const = 0;
143
147 bool is_equal(const Op &other) const override;
148};
149
154 public:
169 unsigned n, const std::vector<uint64_t> &values,
170 const std::string &name = "ClassicalTransform");
171
173 const SymEngine::map_basic_basic &) const override {
174 return std::make_shared<ClassicalTransformOp>(*this);
175 }
176
177 std::vector<bool> eval(const std::vector<bool> &x) const override;
178
179 std::vector<uint64_t> get_values() const { return values_; }
180
181 private:
182 const std::vector<uint64_t> values_;
183};
184
188class WASMOp : public ClassicalOp {
189 public:
202 WASMOp(
203 unsigned _n, unsigned _ww_n, std::vector<unsigned> _width_i_parameter,
204 std::vector<unsigned> _width_o_parameter, const std::string &_func_name,
205 const std::string &_wasm_uid);
206
208 const SymEngine::map_basic_basic &) const override {
209 return std::make_shared<WASMOp>(*this);
210 }
211
215 bool is_extern() const override { return true; }
216
220 nlohmann::json serialize() const override;
221
225 static Op_ptr deserialize(const nlohmann::json &j);
226
230 bool is_equal(const Op &other) const override;
231
235 unsigned get_n() const { return n_; }
236
240 unsigned get_ww_n() const { return ww_n_; }
241
245 unsigned get_n_i32() const { return n_i32_; }
246
251 std::vector<unsigned> get_width_i_parameter() const {
252 return width_i_parameter_;
253 }
254
259 std::vector<unsigned> get_width_o_parameter() const {
260 return width_o_parameter_;
261 }
262
266 std::string get_func_name() const { return func_name_; }
267
272 std::string get_wasm_file_uid() const { return wasm_file_uid_; }
273
274 private:
278 const unsigned n_;
279
283 const unsigned ww_n_;
284
288 const unsigned n_i32_;
289
293 const std::vector<unsigned> width_i_parameter_;
294
298 const std::vector<unsigned> width_o_parameter_;
299
303 const std::string func_name_;
304
308 const std::string wasm_file_uid_;
309};
310
315 public:
321 explicit SetBitsOp(const std::vector<bool> &values)
322 : ClassicalEvalOp(OpType::SetBits, 0, 0, values.size(), "SetBits"),
323 values_(values) {}
324
326 const SymEngine::map_basic_basic &) const override {
327 return std::make_shared<SetBitsOp>(*this);
328 }
329
330 std::string get_name(bool latex) const override;
331
332 std::vector<bool> get_values() const { return values_; }
333
334 std::vector<bool> eval(const std::vector<bool> &x) const override;
335
336 private:
337 std::vector<bool> values_;
338};
339
346 public:
347 explicit CopyBitsOp(unsigned n)
348 : ClassicalEvalOp(OpType::CopyBits, n, 0, n, "CopyBits") {}
349
351 const SymEngine::map_basic_basic &) const override {
352 return std::make_shared<CopyBitsOp>(*this);
353 }
354
355 std::vector<bool> eval(const std::vector<bool> &x) const override;
356};
357
364 public:
372 PredicateOp(OpType type, unsigned n, const std::string &name = "")
373 : ClassicalEvalOp(type, n, 0, 1, name) {}
374};
375
380 public:
392 unsigned n, uint64_t a = 0,
393 uint64_t b = std::numeric_limits<uint64_t>::max())
394 : PredicateOp(OpType::RangePredicate, n, "RangePredicate"), a(a), b(b) {}
395
397 const SymEngine::map_basic_basic &) const override {
398 return std::make_shared<RangePredicateOp>(*this);
399 }
400
401 std::string get_name(bool latex) const override;
402
403 uint64_t upper() const { return b; }
404
405 uint64_t lower() const { return a; }
406
407 std::vector<bool> eval(const std::vector<bool> &x) const override;
408
412 bool is_equal(const Op &other) const override;
413
414 private:
415 uint64_t a;
416 uint64_t b;
417};
418
423 public:
437 unsigned n, const std::vector<bool> &values,
438 const std::string &name = "ExplicitPredicate");
439
441 const SymEngine::map_basic_basic &) const override {
442 return std::make_shared<ExplicitPredicateOp>(*this);
443 }
444
445 std::vector<bool> eval(const std::vector<bool> &x) const override;
446
447 std::vector<bool> get_values() const { return values_; }
448
449 private:
450 const std::vector<bool> values_;
451};
452
457 public:
465 ModifyingOp(OpType type, unsigned n, const std::string &name)
466 : ClassicalEvalOp(type, n, 1, 0, name) {}
467};
468
473 public:
488 unsigned n, const std::vector<bool> &values,
489 const std::string &name = "ExplicitModifier");
490
492 const SymEngine::map_basic_basic &) const override {
493 return std::make_shared<ExplicitModifierOp>(*this);
494 }
495
496 std::vector<bool> eval(const std::vector<bool> &x) const override;
497
498 std::vector<bool> get_values() const { return values_; }
499
500 private:
501 const std::vector<bool> values_;
502};
503
511 public:
512 MultiBitOp(std::shared_ptr<const ClassicalEvalOp> op, unsigned n);
513
515 const SymEngine::map_basic_basic &) const override {
516 return std::make_shared<MultiBitOp>(*this);
517 }
518
519 std::string get_name(bool latex) const override;
520
521 std::shared_ptr<const ClassicalEvalOp> get_op() const { return op_; }
522
523 unsigned get_n() const { return n_; }
524
525 std::vector<bool> eval(const std::vector<bool> &x) const override;
526
530 bool is_equal(const Op &other) const override;
531
532 private:
533 std::shared_ptr<const ClassicalEvalOp> op_;
534 unsigned n_;
535};
536
540std::shared_ptr<ClassicalTransformOp> ClassicalX();
541
545std::shared_ptr<ClassicalTransformOp> ClassicalCX();
546
550std::shared_ptr<ExplicitPredicateOp> NotOp();
551
555std::shared_ptr<ExplicitPredicateOp> AndOp();
556
560std::shared_ptr<ExplicitPredicateOp> OrOp();
561
565std::shared_ptr<ExplicitPredicateOp> XorOp();
566
570std::shared_ptr<ExplicitModifierOp> AndWithOp();
571
575std::shared_ptr<ExplicitModifierOp> OrWithOp();
576
580std::shared_ptr<ExplicitModifierOp> XorWithOp();
581
582} // namespace tket
Operations.
bool is_equal(const Op &other) const override
Equality check between two ClassicalEvalOp instances.
virtual std::vector< bool > eval(const std::vector< bool > &x) const =0
Evaluation.
A purely classical operation.
const std::string name_
unsigned get_n_io() const
Number of input-output bits.
std::vector< EdgeType > sig_
nlohmann::json serialize() const override
const unsigned n_o_
op_signature_t get_signature() const override
Vector specifying type of data for each port on op.
SymSet free_symbols() const override
Set of all free symbols occurring in operation parameters.
bool is_equal(const Op &other) const override
Equality check between two ClassicalEvalOp instances.
static Op_ptr deserialize(const nlohmann::json &j)
unsigned get_n_o() const
Number of output-only bits.
std::string get_name(bool latex=false) const override
String representation.
unsigned get_n_i() const
Number of input-only bits.
const unsigned n_i_
unsigned n_qubits() const override
const unsigned n_io_
A general classical operation where all inputs are also outputs.
std::vector< uint64_t > get_values() const
std::vector< bool > eval(const std::vector< bool > &x) const override
Evaluation.
Op_ptr symbol_substitution(const SymEngine::map_basic_basic &) const override
Operation with values for symbols substituted.
An operation to copy some bit values.
CopyBitsOp(unsigned n)
std::vector< bool > eval(const std::vector< bool > &x) const override
Evaluation.
Op_ptr symbol_substitution(const SymEngine::map_basic_basic &) const override
Operation with values for symbols substituted.
A modifying operation defined explicitly by a truth table.
std::vector< bool > get_values() const
Op_ptr symbol_substitution(const SymEngine::map_basic_basic &) const override
Operation with values for symbols substituted.
std::vector< bool > eval(const std::vector< bool > &x) const override
Evaluation.
A predicate defined explicitly by a truth table.
std::vector< bool > eval(const std::vector< bool > &x) const override
Evaluation.
Op_ptr symbol_substitution(const SymEngine::map_basic_basic &) const override
Operation with values for symbols substituted.
std::vector< bool > get_values() const
A classical operation with one output bit which is also an input bit.
ModifyingOp(OpType type, unsigned n, const std::string &name)
Construct a ModifyingOp of specified arity.
A classical operation applied simultaneously to multiple bits.
unsigned get_n() const
Op_ptr symbol_substitution(const SymEngine::map_basic_basic &) const override
Operation with values for symbols substituted.
std::string get_name(bool latex) const override
String representation.
std::vector< bool > eval(const std::vector< bool > &x) const override
Evaluation.
bool is_equal(const Op &other) const override
Equality check between two MultiBitOp instances.
std::shared_ptr< const ClassicalEvalOp > get_op() const
Abstract class representing an operation type.
Definition Op.hpp:53
An opaque classical operation with no parameters and fixed signature.
static Op_ptr deserialize(const nlohmann::json &j)
Deserialize from JSON.
nlohmann::json serialize() const override
Serialize to JSON.
Op_ptr symbol_substitution(const SymEngine::map_basic_basic &) const override
Operation with values for symbols substituted.
bool is_equal(const Op &other) const override
Equality check between two OpaqueClassicalOp instances.
A classical operation with single output bit.
PredicateOp(OpType type, unsigned n, const std::string &name="")
Construct a PredicateOp of specified arity.
A predicate defined by a range of values in binary encoding.
uint64_t upper() const
std::vector< bool > eval(const std::vector< bool > &x) const override
Evaluation.
uint64_t lower() const
bool is_equal(const Op &other) const override
Equality check between two RangePredicateOp instances.
RangePredicateOp(unsigned n, uint64_t a=0, uint64_t b=std::numeric_limits< uint64_t >::max())
Construct from a lower and upper bound.
Op_ptr symbol_substitution(const SymEngine::map_basic_basic &) const override
Operation with values for symbols substituted.
std::string get_name(bool latex) const override
String representation.
An operation to set some bits to specified values.
SetBitsOp(const std::vector< bool > &values)
Construct from values.
std::vector< bool > get_values() const
std::string get_name(bool latex) const override
String representation.
Op_ptr symbol_substitution(const SymEngine::map_basic_basic &) const override
Operation with values for symbols substituted.
std::vector< bool > eval(const std::vector< bool > &x) const override
Evaluation.
Op containing a classical wasm function call.
bool is_equal(const Op &other) const override
Equality check between two WASMOp instances.
std::vector< unsigned > get_width_o_parameter() const
returns the vector of number of bit used for each of the output i32 variables
bool is_extern() const override
return if the op is external
static Op_ptr deserialize(const nlohmann::json &j)
deserialize json to wasmop
std::string get_wasm_file_uid() const
returns the uid of the wasm file the op is using, the file is stored on the python layer
unsigned get_ww_n() const
returns the number of wasm wire in the op
unsigned get_n_i32() const
returns the number of i32 the function is acting on
std::vector< unsigned > get_width_i_parameter() const
returns the vector of number of bit used for each of the input i32 variables
Op_ptr symbol_substitution(const SymEngine::map_basic_basic &) const override
Operation with values for symbols substituted.
unsigned get_n() const
returns the number of classical bits the wasm op is acting on
std::string get_func_name() const
returns the name of the function the wasm op is using
nlohmann::json serialize() const override
serialize wasmop to json
Defines tket::DeviceCharacterisation, used in NoiseAwarePlacement and in commute_SQ_gates_through_SWA...
Definition Path.cpp:22
std::shared_ptr< ExplicitModifierOp > OrWithOp()
In-place OR with another input.
std::shared_ptr< ExplicitPredicateOp > AndOp()
Binary AND operator.
OpType
Named operation types.
Definition OpType.hpp:29
@ RangePredicate
A classical predicate defined by a range of values in binary encoding.
@ SetBits
An operation to set some bits to specified values.
@ CopyBits
An operation to copy some bit values.
std::shared_ptr< ExplicitModifierOp > AndWithOp()
In-place AND with another input.
std::shared_ptr< ClassicalTransformOp > ClassicalCX()
Classical CNOT transform.
std::shared_ptr< ExplicitPredicateOp > OrOp()
Binary OR operator.
std::shared_ptr< const Op > Op_ptr
Definition OpPtr.hpp:24
std::shared_ptr< ExplicitPredicateOp > NotOp()
Unary NOT operator.
std::shared_ptr< ClassicalTransformOp > ClassicalX()
Classical NOT transform.
std::set< Sym, SymCompareLess > SymSet
std::shared_ptr< ExplicitModifierOp > XorWithOp()
In-place XOR with another input.
std::vector< EdgeType > op_signature_t
Definition EdgeType.hpp:67
std::shared_ptr< ExplicitPredicateOp > XorOp()
Binary XOR operator.