32 #ifndef CLASSES_REVERSIBLE_H 33 #define CLASSES_REVERSIBLE_H 48 std::vector<value_type>
v_;
74 : storage_size_{N / (
sizeof(
value_type) * CHAR_BIT) + 1}, N_{N},
107 std::size_t result = 0;
108 for (
idx i = 0; i <
size(); ++i) {
122 bool get(
idx pos)
const noexcept {
147 bool all() const noexcept {
163 bool any() const noexcept {
return !(this->
none()); }
204 std::random_device rd;
205 std::mt19937 gen{rd()};
206 std::bernoulli_distribution d{p};
208 this->
set(pos, d(gen));
220 for (
idx i = 0; i <
size(); ++i) {
285 assert(this->
size() == rhs.size());
288 for (
idx i = 0; i < n; ++i) {
289 if (v_[i] != rhs.v_[i]) {
304 return !(*
this == rhs);
316 for (
idx i = 0; i <
size(); ++i) {
317 if (this->
get(i) != rhs.get(i))
335 template <
class CharT =
char,
class Traits = std::
char_traits<CharT>,
336 class Allocator = std::allocator<CharT>>
337 std::basic_string<CharT, Traits, Allocator>
338 to_string(CharT zero = CharT(
'0'), CharT one = CharT(
'1'))
const {
339 std::basic_string<CharT, Traits, Allocator> result;
340 idx bitset_size = this->
size();
341 result.resize(bitset_size);
343 for (
idx i = bitset_size; i-- > 0;) {
345 result[bitset_size - i - 1] = zero;
347 result[bitset_size - i - 1] = one;
361 std::ostream&
display(std::ostream& os)
const override {
363 for (
idx i = size; i-- > 0;) {
470 if (this->
get(pos[0]) != this->
get(pos[1])) {
487 if (this->
get(pos[0])) {
488 this->SWAP({pos[1], pos[2]});
501 gate_count.
NOT = gate_count.
X = 0;
502 gate_count.
CNOT = gate_count.
SWAP = 0;
503 gate_count.
FRED = gate_count.
TOF = 0;
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:454
idx storage_size() const noexcept
Size of the underlying storage space (in units of value_type, unsigned int by default) ...
Definition: reversible.h:99
idx index_(idx pos) const
Index of the pos bit in the storage space.
Definition: reversible.h:56
const storage_type & data() const
Raw storage space of the bitset.
Definition: reversible.h:84
Quantum++ main namespace.
Definition: codes.h:35
Definition: reversible.h:377
bool operator==(const Dynamic_bitset &rhs) const noexcept
Equality operator.
Definition: reversible.h:284
Bit_circuit & reset() noexcept
Reset the circuit all zero, clear all gates.
Definition: reversible.h:500
std::vector< value_type > storage_type
Type of the storage.
Definition: reversible.h:44
Bit_circuit & NOT(idx pos)
Bit flip.
Definition: reversible.h:426
bool any() const noexcept
Checks whether any bit is set.
Definition: reversible.h:163
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:147
Bit_circuit & SWAP(const std::vector< idx > &pos)
Swap bits.
Definition: reversible.h:469
idx operator-(const Dynamic_bitset &rhs) const noexcept
Number of places the two bitsets differ (Hamming distance)
Definition: reversible.h:314
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:46
idx count() const noexcept
Number of bits set to one in the bitset (Hamming weight)
Definition: reversible.h:106
std::ostream & display(std::ostream &os) const override
qpp::IDisplay::display() override, displays the bitset bit by bit
Definition: reversible.h:361
Bit_circuit & X(idx pos)
Bit flip.
Definition: reversible.h:412
idx size() const noexcept
Number of bits stored in the bitset.
Definition: reversible.h:91
Bit_circuit & CNOT(const std::vector< idx > &pos)
Controlled-NOT.
Definition: reversible.h:439
std::basic_string< CharT, Traits, Allocator > to_string(CharT zero=CharT('0'), CharT one=CharT('1')) const
String representation.
Definition: reversible.h:338
Classical reversible circuit simulator.
Definition: reversible.h:375
Dynamic_bitset & flip() noexcept
Flips all bits.
Definition: reversible.h:269
Dynamic_bitset(idx N)
Constructor, initializes all bits to false (zero)
Definition: reversible.h:73
bool none() const noexcept
Checks whether none of the bits are set.
Definition: reversible.h:131
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:233
Bit_circuit & FRED(const std::vector< idx > &pos)
Fredkin gate (Controlled-SWAP)
Definition: reversible.h:486
std::size_t idx
Non-negative integer index.
Definition: types.h:39
bool operator!=(const Dynamic_bitset &rhs) const noexcept
Inequality operator.
Definition: reversible.h:303
Bit_circuit(const Dynamic_bitset &dynamic_bitset)
Conversion constructor, used to initialize a qpp::Bit_circuit with a qpp::Dynamic_bitset.
Definition: reversible.h:402
Dynamic_bitset & reset() noexcept
Sets all bits to false.
Definition: reversible.h:244
Dynamic_bitset & flip(idx pos)
Flips the bit at position pos.
Definition: reversible.h:258
Dynamic_bitset & rand(idx pos, double p=0.5)
Sets the bit at position pos according to a Bernoulli(p) distribution.
Definition: reversible.h:203
Dynamic_bitset & rand(double p=0.5)
Sets all bits according to a Bernoulli(p) distribution.
Definition: reversible.h:219
idx offset_(idx pos) const
Offset of the pos bit in the storage space relative to its index.
Definition: reversible.h:65