QuIDS: Quantum Irregular Dynamic Simulator
quantum_computer.hpp
1
2#include <iostream>
3
4#include "../quids.hpp"
5
6namespace quids::rules::quantum_computer {
7 using namespace std::complex_literals;
8
9 namespace utils {
10 void print(quids::it_t const &iter) {
11 for (auto oid = 0; oid < iter.num_object; ++oid) {
12 uint size;
13 mag_t mag;
14 char const *begin;
15 iter.get_object(oid, begin, size, mag);
16
17 std::cout << "\t" << mag.real() << (mag.imag() < 0 ? " - " : " + ") << std::abs(mag.imag()) << "i ";
18 for (auto it = begin; it != begin + size; ++it)
19 std::cout << (*it ? '1' : '0');
20 std::cout << "\n";
21 }
22 }
23 }
24
25 modifier_t inline cnot(uint32_t control_bit, uint32_t bit) {
26 return [=](char* begin, char* end, mag_t &mag) {
27 begin[bit] ^= begin[control_bit];
28 };
29 }
30
31 class hadamard : public quids::rule {
32 size_t bit;
33
34 public:
35 hadamard(size_t bit_) : bit(bit_) {}
36 inline void get_num_child(char const *parent_begin, char const *parent_end, uint &num_child, uint &max_child_size) const override {
37 num_child = 2;
38 max_child_size = std::distance(parent_begin, parent_end);
39 }
40 inline void populate_child(char const *parent_begin, char const *parent_end, char* const child_begin, uint const child_id, uint &size, mag_t &mag) const override {
41 static const PROBA_TYPE sqrt2 = 1/std::sqrt(2);
42 mag *= parent_begin[bit] && child_id ? -sqrt2 : sqrt2;
43
44 size = std::distance(parent_begin, parent_end);
45 for (auto i = 0; i < size; ++i)
46 child_begin[i] = parent_begin[i];
47
48 child_begin[bit] ^= !child_id;
49 }
50 };
51
52 modifier_t inline Xgate(size_t bit) {
53 return [=](char* begin, char* end, mag_t &mag) {
54 begin[bit] = !begin[bit];
55 };
56 }
57
58 modifier_t inline Ygate(size_t bit) {
59 return [=](char* begin, char* end, mag_t &mag) {
60 mag *= 1.0i;
61 if (begin[bit])
62 mag *= -1;
63
64 begin[bit] = !begin[bit];
65 };
66 }
67
68 modifier_t inline Zgate(size_t bit) {
69 return [=](char* begin, char* end, mag_t &mag) {
70 if (begin[bit])
71 mag *= -1;
72
73 begin[bit] = !begin[bit];
74 };
75 }
76}
class represneting a dynamic (or rule).
Definition: quids.hpp:100
std::complex< PROBA_TYPE > mag_t
complex magnitude type
Definition: quids.hpp:73
class iteration it_t
iteration class type
Definition: quids.hpp:75
std::function< void(char *parent_begin, char *parent_end, mag_t &mag)> modifier_t
simple "modifier" type (single input, single output of same size dynamic)
Definition: quids.hpp:81