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
84 public:
99 OpType type, unsigned n_i, unsigned n_io, unsigned n_o,
100 const std::string &name = "");
101
109 virtual std::vector<bool> eval(const std::vector<bool> &x) const = 0;
110
114 bool is_equal(const Op &other) const override;
115};
116
121 public:
136 unsigned n, const std::vector<uint64_t> &values,
137 const std::string &name = "ClassicalTransform");
138
140 const SymEngine::map_basic_basic &) const override {
141 return std::make_shared<ClassicalTransformOp>(*this);
142 }
143
144 std::vector<bool> eval(const std::vector<bool> &x) const override;
145
146 std::vector<uint64_t> get_values() const { return values_; }
147
148 private:
149 const std::vector<uint64_t> values_;
150};
151
155class WASMOp : public ClassicalOp {
156 public:
169 WASMOp(
170 unsigned _n, unsigned _ww_n, std::vector<unsigned> _width_i_parameter,
171 std::vector<unsigned> _width_o_parameter, const std::string &_func_name,
172 const std::string &_wasm_uid);
173
175 const SymEngine::map_basic_basic &) const override {
176 return std::make_shared<WASMOp>(*this);
177 }
178
182 bool is_extern() const override { return true; }
183
187 nlohmann::json serialize() const override;
188
192 static Op_ptr deserialize(const nlohmann::json &j);
193
197 bool is_equal(const Op &other) const override;
198
202 unsigned get_n() const { return n_; }
203
207 unsigned get_ww_n() const { return ww_n_; }
208
212 unsigned get_n_i32() const { return n_i32_; }
213
218 std::vector<unsigned> get_width_i_parameter() const {
219 return width_i_parameter_;
220 }
221
226 std::vector<unsigned> get_width_o_parameter() const {
227 return width_o_parameter_;
228 }
229
233 std::string get_func_name() const { return func_name_; }
234
239 std::string get_wasm_file_uid() const { return wasm_file_uid_; }
240
241 private:
245 const unsigned n_;
246
250 const unsigned ww_n_;
251
255 const unsigned n_i32_;
256
260 const std::vector<unsigned> width_i_parameter_;
261
265 const std::vector<unsigned> width_o_parameter_;
266
270 const std::string func_name_;
271
275 const std::string wasm_file_uid_;
276};
277
282 public:
288 explicit SetBitsOp(const std::vector<bool> &values)
289 : ClassicalEvalOp(OpType::SetBits, 0, 0, values.size(), "SetBits"),
290 values_(values) {}
291
293 const SymEngine::map_basic_basic &) const override {
294 return std::make_shared<SetBitsOp>(*this);
295 }
296
297 std::string get_name(bool latex) const override;
298
299 std::vector<bool> get_values() const { return values_; }
300
301 std::vector<bool> eval(const std::vector<bool> &x) const override;
302
303 private:
304 std::vector<bool> values_;
305};
306
313 public:
314 explicit CopyBitsOp(unsigned n)
315 : ClassicalEvalOp(OpType::CopyBits, n, 0, n, "CopyBits") {}
316
318 const SymEngine::map_basic_basic &) const override {
319 return std::make_shared<CopyBitsOp>(*this);
320 }
321
322 std::vector<bool> eval(const std::vector<bool> &x) const override;
323};
324
331 public:
339 PredicateOp(OpType type, unsigned n, const std::string &name = "")
340 : ClassicalEvalOp(type, n, 0, 1, name) {}
341};
342
347 public:
359 unsigned n, uint64_t a = 0,
360 uint64_t b = std::numeric_limits<uint64_t>::max())
361 : PredicateOp(OpType::RangePredicate, n, "RangePredicate"), a(a), b(b) {}
362
364 const SymEngine::map_basic_basic &) const override {
365 return std::make_shared<RangePredicateOp>(*this);
366 }
367
368 std::string get_name(bool latex) const override;
369
370 uint64_t upper() const { return b; }
371
372 uint64_t lower() const { return a; }
373
374 std::vector<bool> eval(const std::vector<bool> &x) const override;
375
379 bool is_equal(const Op &other) const override;
380
381 private:
382 uint64_t a;
383 uint64_t b;
384};
385
390 public:
404 unsigned n, const std::vector<bool> &values,
405 const std::string &name = "ExplicitPredicate");
406
408 const SymEngine::map_basic_basic &) const override {
409 return std::make_shared<ExplicitPredicateOp>(*this);
410 }
411
412 std::vector<bool> eval(const std::vector<bool> &x) const override;
413
414 std::vector<bool> get_values() const { return values_; }
415
416 private:
417 const std::vector<bool> values_;
418};
419
424 public:
432 ModifyingOp(OpType type, unsigned n, const std::string &name)
433 : ClassicalEvalOp(type, n, 1, 0, name) {}
434};
435
440 public:
455 unsigned n, const std::vector<bool> &values,
456 const std::string &name = "ExplicitModifier");
457
459 const SymEngine::map_basic_basic &) const override {
460 return std::make_shared<ExplicitModifierOp>(*this);
461 }
462
463 std::vector<bool> eval(const std::vector<bool> &x) const override;
464
465 std::vector<bool> get_values() const { return values_; }
466
467 private:
468 const std::vector<bool> values_;
469};
470
478 public:
479 MultiBitOp(std::shared_ptr<const ClassicalEvalOp> op, unsigned n);
480
482 const SymEngine::map_basic_basic &) const override {
483 return std::make_shared<MultiBitOp>(*this);
484 }
485
486 std::string get_name(bool latex) const override;
487
488 std::shared_ptr<const ClassicalEvalOp> get_op() const { return op_; }
489
490 unsigned get_n() const { return n_; }
491
492 std::vector<bool> eval(const std::vector<bool> &x) const override;
493
497 bool is_equal(const Op &other) const override;
498
499 private:
500 std::shared_ptr<const ClassicalEvalOp> op_;
501 unsigned n_;
502};
503
507std::shared_ptr<ClassicalTransformOp> ClassicalX();
508
512std::shared_ptr<ClassicalTransformOp> ClassicalCX();
513
517std::shared_ptr<ExplicitPredicateOp> NotOp();
518
522std::shared_ptr<ExplicitPredicateOp> AndOp();
523
527std::shared_ptr<ExplicitPredicateOp> OrOp();
528
532std::shared_ptr<ExplicitPredicateOp> XorOp();
533
537std::shared_ptr<ExplicitModifierOp> AndWithOp();
538
542std::shared_ptr<ExplicitModifierOp> OrWithOp();
543
547std::shared_ptr<ExplicitModifierOp> XorWithOp();
548
549} // 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
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:61
std::shared_ptr< ExplicitPredicateOp > XorOp()
Binary XOR operator.