Quantum++  v1.0
A modern C++11 quantum computing library
reversible.h
Go to the documentation of this file.
1 /*
2  * This file is part of Quantum++.
3  *
4  * MIT License
5  *
6  * Copyright (c) 2013 - 2018 Vlad Gheorghiu (vgheorgh@gmail.com)
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  */
26 
32 #ifndef CLASSES_REVERSIBLE_H
33 #define CLASSES_REVERSIBLE_H
34 
35 namespace qpp {
41 class Dynamic_bitset : public IDisplay {
42  public:
43  using value_type = unsigned int;
44  using storage_type = std::vector<value_type>;
45  protected:
47  idx N_;
48  std::vector<value_type> v_;
49 
56  idx index_(idx pos) const { return pos / (sizeof(value_type) * CHAR_BIT); }
57 
65  idx offset_(idx pos) const { return pos % (sizeof(value_type) * CHAR_BIT); }
66 
67  public:
74  : storage_size_{N / (sizeof(value_type) * CHAR_BIT) + 1}, N_{N},
75  v_(storage_size_) {}
76 
77  /* getters */
78 
84  const storage_type& data() const { return v_; }
85 
91  idx size() const noexcept { return N_; }
92 
99  idx storage_size() const noexcept { return storage_size_; }
100 
106  idx count() const noexcept {
107  std::size_t result = 0;
108  for (idx i = 0; i < size(); ++i) {
109  if (this->get(i))
110  ++result;
111  }
112 
113  return result;
114  }
115 
122  bool get(idx pos) const noexcept {
123  return 1 & (v_[index_(pos)] >> offset_(pos));
124  }
125 
131  bool none() const noexcept {
132  bool result = true;
133  for (idx i = 0; i < storage_size(); ++i) {
134  if (v_[i]) {
135  return false;
136  }
137  }
138 
139  return result;
140  }
141 
147  bool all() const noexcept {
148  bool result = true;
149  for (idx i = 0; i < storage_size(); ++i) {
150  if (~v_[i]) {
151  return false;
152  }
153  }
154 
155  return result;
156  }
157 
163  bool any() const noexcept { return !(this->none()); }
164 
165  /* setters */
173  Dynamic_bitset& set(idx pos, bool value = true) {
174  value ? v_[index_(pos)] |= (1 << offset_(pos))
175  : v_[index_(pos)] &= ~(1 << offset_(pos));
176 
177  // v_[index_(pos)] &= ~(!value << offset_(pos));
178 
179  return *this;
180  }
181 
187  Dynamic_bitset& set() noexcept {
188  for (idx i = 0; i < storage_size(); ++i) {
189  v_[i] = ~0;
190  }
191 
192  return *this;
193  }
194 
203  Dynamic_bitset& rand(idx pos, double p = 0.5) {
204  std::random_device rd;
205  std::mt19937 gen{rd()};
206  std::bernoulli_distribution d{p};
207 
208  this->set(pos, d(gen));
209 
210  return *this;
211  }
212 
219  Dynamic_bitset& rand(double p = 0.5) {
220  for (idx i = 0; i < size(); ++i) {
221  this->rand(i, p);
222  }
223 
224  return *this;
225  }
226 
234  v_[index_(pos)] &= ~(1 << offset_(pos));
235 
236  return *this;
237  }
238 
244  Dynamic_bitset& reset() noexcept {
245  for (idx i = 0; i < storage_size(); ++i) {
246  v_[i] = 0;
247  }
248 
249  return *this;
250  }
251 
259  v_[index_(pos)] ^= 1 << (offset_(pos));
260 
261  return *this;
262  }
263 
269  Dynamic_bitset& flip() noexcept {
270  for (idx i = 0; i < storage_size(); ++i) {
271  v_[i] = ~v_[i];
272  }
273 
274  return *this;
275  }
276 
277  /* operators */
284  bool operator==(const Dynamic_bitset& rhs) const noexcept {
285  assert(this->size() == rhs.size());
286  bool result = true;
287  idx n = std::min(this->storage_size(), rhs.storage_size());
288  for (idx i = 0; i < n; ++i) {
289  if (v_[i] != rhs.v_[i]) {
290  return false;
291  }
292  }
293 
294  return result;
295  }
296 
303  bool operator!=(const Dynamic_bitset& rhs) const noexcept {
304  return !(*this == rhs);
305  }
306 
314  idx operator-(const Dynamic_bitset& rhs) const noexcept {
315  idx result = 0;
316  for (idx i = 0; i < size(); ++i) {
317  if (this->get(i) != rhs.get(i))
318  ++result;
319  }
320 
321  return result;
322  }
323 
324  /* input/output */
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);
342 
343  for (idx i = bitset_size; i-- > 0;) {
344  if (!this->get(i)) {
345  result[bitset_size - i - 1] = zero;
346  } else {
347  result[bitset_size - i - 1] = one;
348  }
349  }
350 
351  return result;
352  }
353 
354  private:
361  std::ostream& display(std::ostream& os) const override {
362  idx size = this->size();
363  for (idx i = size; i-- > 0;) {
364  os << this->get(i);
365  }
366 
367  return os;
368  }
369 }; /* class Dynamic_bitset */
370 
375 class Bit_circuit : public Dynamic_bitset {
376  public:
377  struct Gate_count {
378  // 1 bit gates
379  idx NOT = 0;
380  idx& X = NOT;
381 
382  // 2 bit gates
383  idx CNOT = 0;
384  idx SWAP = 0;
385 
386  // 3 bit gates
387  idx FRED = 0;
388  idx TOF = 0;
389  } gate_count{};
390 
395 
402  Bit_circuit(const Dynamic_bitset& dynamic_bitset)
403  : Dynamic_bitset{dynamic_bitset} {};
404 
412  Bit_circuit& X(idx pos) {
413  this->flip(pos);
414  ++gate_count.X;
415 
416  return *this;
417  }
418 
427  this->flip(pos);
428  ++gate_count.NOT;
429 
430  return *this;
431  }
432 
439  Bit_circuit& CNOT(const std::vector<idx>& pos) {
440  v_[index_(pos[1])] ^= (1 & (v_[index_(pos[0])] >> offset_(pos[0])))
441  << offset_(pos[1]);
442  ++gate_count.CNOT;
443 
444  return *this;
445  }
446 
454  Bit_circuit& TOF(const std::vector<idx>& pos) {
455  v_[index_(pos[2])] ^= ((1 & (v_[index_(pos[1])] >> offset_(pos[1]))) &
456  (1 & (v_[index_(pos[0])] >> offset_(pos[0]))))
457  << offset_(pos[2]);
458  ++gate_count.TOF;
459 
460  return *this;
461  }
462 
469  Bit_circuit& SWAP(const std::vector<idx>& pos) {
470  if (this->get(pos[0]) != this->get(pos[1])) {
471  this->X(pos[0]);
472  this->X(pos[1]);
473  }
474  ++gate_count.SWAP;
475 
476  return *this;
477  }
478 
486  Bit_circuit& FRED(const std::vector<idx>& pos) {
487  if (this->get(pos[0])) {
488  this->SWAP({pos[1], pos[2]});
489  }
490  ++gate_count.FRED;
491 
492  return *this;
493  }
494 
500  Bit_circuit& reset() noexcept {
501  gate_count.NOT = gate_count.X = 0;
502  gate_count.CNOT = gate_count.SWAP = 0;
503  gate_count.FRED = gate_count.TOF = 0;
505 
506  return *this;
507  }
508 }; /* class Bit_circuit */
509 
510 } /* namespace qpp */
511 
512 #endif /* CLASSES_REVERSIBLE_H */
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