32 #ifndef CLASSES_REVERSIBLE_H 33 #define CLASSES_REVERSIBLE_H 48 std::vector<value_type>
v_;
114 idx bitset_size = this->
size();
115 for (
idx i = 0; i < bitset_size; ++i) {
129 bool get(
idx pos)
const noexcept {
141 for (
idx i = 0; i < bitset_storage_size; ++i) {
155 bool all() const noexcept {
158 for (
idx i = 0; i < bitset_storage_size; ++i) {
172 bool any() const noexcept {
return !(this->
none()); }
198 for (
idx i = 0; i < bitset_storage_size; ++i) {
214 std::random_device rd;
215 std::mt19937 gen{rd()};
216 std::bernoulli_distribution d{p};
218 this->
set(pos, d(gen));
230 idx bitset_size = this->
size();
231 for (
idx i = 0; i < bitset_size; ++i) {
257 for (
idx i = 0; i < bitset_storage_size; ++i) {
283 for (
idx i = 0; i < bitset_storage_size; ++i) {
298 assert(this->
size() == rhs.size());
301 for (
idx i = 0; i < n; ++i) {
302 if (
v_[i] != rhs.v_[i]) {
317 return !(*
this == rhs);
329 idx bitset_size = this->
size();
330 for (
idx i = 0; i < bitset_size; ++i) {
331 if (this->
get(i) != rhs.get(i))
349 template <
class CharT =
char,
class Traits = std::
char_traits<CharT>,
350 class Allocator = std::allocator<CharT>>
351 std::basic_string<CharT, Traits, Allocator>
352 to_string(CharT zero = CharT(
'0'), CharT one = CharT(
'1'))
const {
353 std::basic_string<CharT, Traits, Allocator> result;
354 idx bitset_size = this->
size();
355 result.resize(bitset_size);
357 for (
idx i = bitset_size; i-- > 0;) {
359 result[bitset_size - i - 1] = zero;
361 result[bitset_size - i - 1] = one;
375 std::ostream&
display(std::ostream& os)
const override {
376 idx bitset_size = this->
size();
377 for (
idx i = bitset_size; i-- > 0;) {
484 if (this->
get(pos[0]) != this->
get(pos[1])) {
501 if (this->
get(pos[0])) {
502 this->
SWAP({pos[1], pos[2]});
unsigned int value_type
Type of the storage elements.
Definition: reversible.h:43
Bit_circuit & TOF(const std::vector< idx > &pos)
Toffoli gate.
Definition: reversible.h:468
idx storage_size() const noexcept
Size of the underlying storage space (in units of value_type, unsigned int by default) ...
Definition: reversible.h:105
idx index_(idx pos) const
Index of the pos bit in the storage space.
Definition: reversible.h:56
idx SWAP
Definition: reversible.h:398
idx & X
Definition: reversible.h:394
const storage_type & data() const
Raw storage space of the bitset.
Definition: reversible.h:90
Quantum++ main namespace.
Definition: circuits.h:35
Definition: reversible.h:391
bool operator==(const Dynamic_bitset &rhs) const noexcept
Equality operator.
Definition: reversible.h:297
idx TOF
Definition: reversible.h:402
Bit_circuit & reset() noexcept
Reset the circuit all zero, clear all gates.
Definition: reversible.h:514
std::vector< value_type > storage_type
Type of the storage.
Definition: reversible.h:44
Bit_circuit & NOT(idx pos)
Bit flip.
Definition: reversible.h:440
idx FRED
Definition: reversible.h:401
bool any() const noexcept
Checks whether any bit is set.
Definition: reversible.h:172
idx N_
Number of bits.
Definition: reversible.h:47
idx storage_size_
Storage size.
Definition: reversible.h:46
bool all() const noexcept
Checks whether all bits are set.
Definition: reversible.h:155
virtual ~Dynamic_bitset()=default
Default virtual destructor.
struct qpp::Bit_circuit::Gate_count gate_count
Gate counters.
Bit_circuit & SWAP(const std::vector< idx > &pos)
Swap bits.
Definition: reversible.h:483
idx operator-(const Dynamic_bitset &rhs) const noexcept
Number of places the two bitsets differ (Hamming distance)
Definition: reversible.h:327
Dynamic bitset class, allows the specification of the number of bits at runtime (unlike std::bitset<N...
Definition: reversible.h:41
Abstract class (interface) that mandates the definition of virtual std::ostream& display(std::ostream...
Definition: idisplay.h:47
idx count() const noexcept
Number of bits set to one in the bitset (Hamming weight)
Definition: reversible.h:112
std::ostream & display(std::ostream &os) const override
qpp::IDisplay::display() override, displays the bitset bit by bit
Definition: reversible.h:375
Bit_circuit & X(idx pos)
Bit flip.
Definition: reversible.h:426
idx size() const noexcept
Number of bits stored in the bitset.
Definition: reversible.h:97
Bit_circuit & CNOT(const std::vector< idx > &pos)
Controlled-NOT.
Definition: reversible.h:453
std::basic_string< CharT, Traits, Allocator > to_string(CharT zero=CharT('0'), CharT one=CharT('1')) const
String representation.
Definition: reversible.h:352
Classical reversible circuit simulator.
Definition: reversible.h:389
Dynamic_bitset & flip() noexcept
Flips all bits.
Definition: reversible.h:281
idx CNOT
Definition: reversible.h:397
Dynamic_bitset(idx N)
Constructor, initializes all bits to false (zero)
Definition: reversible.h:74
idx NOT
Definition: reversible.h:393
bool none() const noexcept
Checks whether none of the bits are set.
Definition: reversible.h:138
std::vector< value_type > v_
Storage space.
Definition: reversible.h:48
Dynamic_bitset & reset(idx pos)
Sets the bit at position pos to false.
Definition: reversible.h:244
Bit_circuit & FRED(const std::vector< idx > &pos)
Fredkin gate (Controlled-SWAP)
Definition: reversible.h:500
std::size_t idx
Non-negative integer index, make sure you use an unsigned type.
Definition: types.h:39
bool operator!=(const Dynamic_bitset &rhs) const noexcept
Inequality operator.
Definition: reversible.h:316
Bit_circuit(const Dynamic_bitset &dynamic_bitset)
Conversion constructor, used to initialize a qpp::Bit_circuit with a qpp::Dynamic_bitset.
Definition: reversible.h:416
Dynamic_bitset & reset() noexcept
Sets all bits to false.
Definition: reversible.h:255
Dynamic_bitset & flip(idx pos)
Flips the bit at position pos.
Definition: reversible.h:270
Dynamic_bitset & rand(idx pos, double p=0.5)
Sets the bit at position pos according to a Bernoulli(p) distribution.
Definition: reversible.h:213
Dynamic_bitset & rand(double p=0.5)
Sets all bits according to a Bernoulli(p) distribution.
Definition: reversible.h:229
idx offset_(idx pos) const
Offset of the pos bit in the storage space relative to its index.
Definition: reversible.h:66