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},
108 idx bitset_size = this->
size();
109 for (
idx i = 0; i < bitset_size; ++i) {
123 bool get(
idx pos)
const noexcept {
135 for (
idx i = 0; i < bitset_storage_size; ++i) {
149 bool all() const noexcept {
152 for (
idx i = 0; i < bitset_storage_size; ++i) {
166 bool any() const noexcept {
return !(this->
none()); }
192 for (
idx i = 0; i < bitset_storage_size; ++i) {
208 std::random_device rd;
209 std::mt19937 gen{rd()};
210 std::bernoulli_distribution d{p};
212 this->
set(pos, d(gen));
224 idx bitset_size = this->
size();
225 for (
idx i = 0; i < bitset_size; ++i) {
251 for (
idx i = 0; i < bitset_storage_size; ++i) {
277 for (
idx i = 0; i < bitset_storage_size; ++i) {
292 assert(this->
size() == rhs.size());
295 for (
idx i = 0; i < n; ++i) {
296 if (v_[i] != rhs.v_[i]) {
311 return !(*
this == rhs);
323 idx bitset_size = this->
size();
324 for (
idx i = 0; i < bitset_size; ++i) {
325 if (this->
get(i) != rhs.get(i))
343 template <
class CharT =
char,
class Traits = std::
char_traits<CharT>,
344 class Allocator = std::allocator<CharT>>
345 std::basic_string<CharT, Traits, Allocator>
346 to_string(CharT zero = CharT(
'0'), CharT one = CharT(
'1'))
const {
347 std::basic_string<CharT, Traits, Allocator> result;
348 idx bitset_size = this->
size();
349 result.resize(bitset_size);
351 for (
idx i = bitset_size; i-- > 0;) {
353 result[bitset_size - i - 1] = zero;
355 result[bitset_size - i - 1] = one;
369 std::ostream&
display(std::ostream& os)
const override {
370 idx bitset_size = this->
size();
371 for (
idx i = bitset_size; i-- > 0;) {
478 if (this->
get(pos[0]) != this->
get(pos[1])) {
495 if (this->
get(pos[0])) {
496 this->SWAP({pos[1], pos[2]});
509 gate_count.
NOT = gate_count.
X = 0;
510 gate_count.
CNOT = gate_count.
SWAP = 0;
511 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:462
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:385
bool operator==(const Dynamic_bitset &rhs) const noexcept
Equality operator.
Definition: reversible.h:291
Bit_circuit & reset() noexcept
Reset the circuit all zero, clear all gates.
Definition: reversible.h:508
std::vector< value_type > storage_type
Type of the storage.
Definition: reversible.h:44
Bit_circuit & NOT(idx pos)
Bit flip.
Definition: reversible.h:434
bool any() const noexcept
Checks whether any bit is set.
Definition: reversible.h:166
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:149
Bit_circuit & SWAP(const std::vector< idx > &pos)
Swap bits.
Definition: reversible.h:477
idx operator-(const Dynamic_bitset &rhs) const noexcept
Number of places the two bitsets differ (Hamming distance)
Definition: reversible.h:321
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:369
Bit_circuit & X(idx pos)
Bit flip.
Definition: reversible.h:420
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:447
std::basic_string< CharT, Traits, Allocator > to_string(CharT zero=CharT('0'), CharT one=CharT('1')) const
String representation.
Definition: reversible.h:346
Classical reversible circuit simulator.
Definition: reversible.h:383
Dynamic_bitset & flip() noexcept
Flips all bits.
Definition: reversible.h:275
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:132
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:238
Bit_circuit & FRED(const std::vector< idx > &pos)
Fredkin gate (Controlled-SWAP)
Definition: reversible.h:494
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:310
Bit_circuit(const Dynamic_bitset &dynamic_bitset)
Conversion constructor, used to initialize a qpp::Bit_circuit with a qpp::Dynamic_bitset.
Definition: reversible.h:410
Dynamic_bitset & reset() noexcept
Sets all bits to false.
Definition: reversible.h:249
Dynamic_bitset & flip(idx pos)
Flips the bit at position pos.
Definition: reversible.h:264
Dynamic_bitset & rand(idx pos, double p=0.5)
Sets the bit at position pos according to a Bernoulli(p) distribution.
Definition: reversible.h:207
Dynamic_bitset & rand(double p=0.5)
Sets all bits according to a Bernoulli(p) distribution.
Definition: reversible.h:223
idx offset_(idx pos) const
Offset of the pos bit in the storage space relative to its index.
Definition: reversible.h:65