Quantum++  v1.0-rc2
A modern C++11 quantum computing library
codes.h
Go to the documentation of this file.
1 /*
2  * Quantum++
3  *
4  * Copyright (c) 2013 - 2017 Vlad Gheorghiu (vgheorgh@gmail.com)
5  *
6  * This file is part of Quantum++.
7  *
8  * Quantum++ is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * Quantum++ is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with Quantum++. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
27 #ifndef CLASSES_CODES_H_
28 #define CLASSES_CODES_H_
29 
30 namespace qpp
31 {
36 class Codes final : public internal::Singleton<const Codes> // const Singleton
37 {
38  friend class internal::Singleton<const Codes>;
39 
40 public:
45  enum class Type // exception types
46  {
47  FIVE_QUBIT = 1,
50  };
51 
52 private:
57  {} // = default; // clang++ spits the error below if defaulted:
58  // error:
59  // default initialization of an object of const type 'const qpp::Codes'
60  // requires a user-provided default constructor
61 
65  ~Codes() = default;
66 
67 public:
76  ket codeword(Type type, idx i) const
77  {
78  ket result;
79  switch (type)
80  {
81  // [[5,1,3]] Five qubit code according to Nielsen and Chuang)
82  case Type::FIVE_QUBIT:
83  switch (i)
84  {
85  case 0:
86  result = (mket({0, 0, 0, 0, 0}) +
87  mket({1, 0, 0, 1, 0}) +
88  mket({0, 1, 0, 0, 1}) +
89  mket({1, 0, 1, 0, 0}) +
90  mket({0, 1, 0, 1, 0}) -
91  mket({1, 1, 0, 1, 1}) -
92  mket({0, 0, 1, 1, 0}) -
93  mket({1, 1, 0, 0, 0}) -
94  mket({1, 1, 1, 0, 1}) -
95  mket({0, 0, 0, 1, 1}) -
96  mket({1, 1, 1, 1, 0}) -
97  mket({0, 1, 1, 1, 1}) -
98  mket({1, 0, 0, 0, 1}) -
99  mket({0, 1, 1, 0, 0}) -
100  mket({1, 0, 1, 1, 1}) +
101  mket({0, 0, 1, 0, 1}))
102  / 4.;
103  break;
104  case 1:
105  result = (mket({1, 1, 1, 1, 1}) +
106  mket({0, 1, 1, 0, 1}) +
107  mket({1, 0, 1, 1, 0}) +
108  mket({0, 1, 0, 1, 1}) +
109  mket({1, 0, 1, 0, 1}) -
110  mket({0, 0, 1, 0, 0}) -
111  mket({1, 1, 0, 0, 1}) -
112  mket({0, 0, 1, 1, 1}) -
113  mket({0, 0, 0, 1, 0}) -
114  mket({1, 1, 1, 0, 0}) -
115  mket({0, 0, 0, 0, 1}) -
116  mket({1, 0, 0, 0, 0}) -
117  mket({0, 1, 1, 1, 0}) -
118  mket({1, 0, 0, 1, 1}) -
119  mket({0, 1, 0, 0, 0}) +
120  mket({1, 1, 0, 1, 0}))
121  / 4.;
122  break;
123  default:
124  throw exception::NoCodeword("qpp::Codes::codeword()");
125  }
126  break;
127  // [[7,1,3]] Steane code according to Nielsen and Chuang)
129  switch (i)
130  {
131  case 0:
132  result = (mket({0, 0, 0, 0, 0, 0, 0}) +
133  mket({1, 0, 1, 0, 1, 0, 1}) +
134  mket({0, 1, 1, 0, 0, 1, 1}) +
135  mket({1, 1, 0, 0, 1, 1, 0}) +
136  mket({0, 0, 0, 1, 1, 1, 1}) +
137  mket({1, 0, 1, 1, 0, 1, 0}) +
138  mket({0, 1, 1, 1, 1, 0, 0}) +
139  mket({1, 1, 0, 1, 0, 0, 1})) /
140  std::sqrt(8.);
141 
142  break;
143  case 1:
144  result = (mket({1, 1, 1, 1, 1, 1, 1}) +
145  mket({0, 1, 0, 1, 0, 1, 0}) +
146  mket({1, 0, 0, 1, 1, 0, 0}) +
147  mket({0, 0, 1, 1, 0, 0, 1}) +
148  mket({1, 1, 1, 0, 0, 0, 0}) +
149  mket({0, 1, 0, 0, 1, 0, 1}) +
150  mket({1, 0, 0, 0, 0, 1, 1}) +
151  mket({0, 0, 1, 0, 1, 1, 0})) /
152  std::sqrt(8.);
153  break;
154  default:
155  throw exception::NoCodeword("qpp::Codes::codeword()");
156  }
157  break;
158  // [[9,1,3]] Shor code
160  ket shora, shorb;
161  shora = mket({0, 0, 0}) + mket({1, 1, 1,});
162  shorb = mket({0, 0, 0}) - mket({1, 1, 1,});
163  switch (i)
164  {
165  case 0:
166  result = kron(shora, kron(shora, shora))
167  / std::sqrt(8.);
168  break;
169  case 1:
170  result = kron(shorb, kron(shorb, shorb))
171  / std::sqrt(8.);
172  break;
173  default:
174  throw exception::NoCodeword("qpp::Codes::codeword()");
175  }
176  }
177 
178  return result;
179  }
180 }; /* class Codes */
181 
182 } /* namespace qpp */
183 
184 #endif /* CLASSES_CODES_H_ */
Codes()
Default constructor.
Definition: codes.h:56
Singleton policy class, used internally to implement the singleton pattern via CRTP (Curiously recurr...
Definition: singleton.h:76
Eigen::VectorXcd ket
Complex (double precision) dynamic Eigen column vector.
Definition: types.h:50
Quantum++ main namespace.
Definition: codes.h:30
Type
Code types, add more codes here if needed.
Definition: codes.h:45
const Singleton class that defines quantum error correcting codes
Definition: codes.h:36
~Codes()=default
Default destructor.
[[7,1,3]] Steane qubit code
dyn_mat< typename T::Scalar > kron(const T &head)
Kronecker product.
Definition: functions.h:899
[[9,1,3]] Shor qubit code
Codeword does not exist exception.
Definition: exception.h:550
ket mket(const std::vector< idx > &mask, const std::vector< idx > &dims)
Multi-partite qudit ket.
Definition: functions.h:1465
ket codeword(Type type, idx i) const
Returns the codeword of the specified code type.
Definition: codes.h:76
std::size_t idx
Non-negative integer index.
Definition: types.h:35
[[5,1,3]] qubit code