Quantum++  v0.8.2
C++11 quantum computing library
codes.h
Go to the documentation of this file.
1 /*
2  * Quantum++
3  *
4  * Copyright (c) 2013 - 2015 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 {
32 
37 class Codes final : public internal::Singleton<const Codes> // const Singleton
38 {
39  friend class internal::Singleton<const Codes>;
40 
41 public:
46  enum class Type // exception types
47  {
48  FIVE_QUBIT = 1,
51  };
52 
53 private:
58  { } // = default; // clang++ spits the error below if defaulted:
59  // error:
60  // default initialization of an object of const type 'const qpp::Codes'
61  // requires a user-provided default constructor
62 
66  ~Codes() = default;
67 
68 public:
77  ket codeword(Type type, idx i) const
78  {
79  ket result;
80  switch (type)
81  {
82  // [[5,1,3]] Five qubit code according to Nielsen and Chuang)
83  case Type::FIVE_QUBIT:
84  switch (i)
85  {
86  case 0:
87  result = (mket({0, 0, 0, 0, 0}) +
88  mket({1, 0, 0, 1, 0}) +
89  mket({0, 1, 0, 0, 1}) +
90  mket({1, 0, 1, 0, 0}) +
91  mket({0, 1, 0, 1, 0}) -
92  mket({1, 1, 0, 1, 1}) -
93  mket({0, 0, 1, 1, 0}) -
94  mket({1, 1, 0, 0, 0}) -
95  mket({1, 1, 1, 0, 1}) -
96  mket({0, 0, 0, 1, 1}) -
97  mket({1, 1, 1, 1, 0}) -
98  mket({0, 1, 1, 1, 1}) -
99  mket({1, 0, 0, 0, 1}) -
100  mket({0, 1, 1, 0, 0}) -
101  mket({1, 0, 1, 1, 1}) +
102  mket({0, 0, 1, 0, 1}))
103  / 4.;
104  break;
105  case 1:
106  result = (mket({1, 1, 1, 1, 1}) +
107  mket({0, 1, 1, 0, 1}) +
108  mket({1, 0, 1, 1, 0}) +
109  mket({0, 1, 0, 1, 1}) +
110  mket({1, 0, 1, 0, 1}) -
111  mket({0, 0, 1, 0, 0}) -
112  mket({1, 1, 0, 0, 1}) -
113  mket({0, 0, 1, 1, 1}) -
114  mket({0, 0, 0, 1, 0}) -
115  mket({1, 1, 1, 0, 0}) -
116  mket({0, 0, 0, 0, 1}) -
117  mket({1, 0, 0, 0, 0}) -
118  mket({0, 1, 1, 1, 0}) -
119  mket({1, 0, 0, 1, 1}) -
120  mket({0, 1, 0, 0, 0}) +
121  mket({1, 1, 0, 1, 0}))
122  / 4.;
123  break;
124  default:
125  throw Exception("qpp::Codes::codeword()",
127  }
128  break;
129  // [[7,1,3]] Steane code according to Nielsen and Chuang)
131  switch (i)
132  {
133  case 0:
134  result = (mket({0, 0, 0, 0, 0, 0, 0}) +
135  mket({1, 0, 1, 0, 1, 0, 1}) +
136  mket({0, 1, 1, 0, 0, 1, 1}) +
137  mket({1, 1, 0, 0, 1, 1, 0}) +
138  mket({0, 0, 0, 1, 1, 1, 1}) +
139  mket({1, 0, 1, 1, 0, 1, 0}) +
140  mket({0, 1, 1, 1, 1, 0, 0}) +
141  mket({1, 1, 0, 1, 0, 0, 1})) /
142  std::sqrt(8.);
143 
144  break;
145  case 1:
146  result = (mket({1, 1, 1, 1, 1, 1, 1}) +
147  mket({0, 1, 0, 1, 0, 1, 0}) +
148  mket({1, 0, 0, 1, 1, 0, 0}) +
149  mket({0, 0, 1, 1, 0, 0, 1}) +
150  mket({1, 1, 1, 0, 0, 0, 0}) +
151  mket({0, 1, 0, 0, 1, 0, 1}) +
152  mket({1, 0, 0, 0, 0, 1, 1}) +
153  mket({0, 0, 1, 0, 1, 1, 0})) /
154  std::sqrt(8.);
155  break;
156  default:
157  throw Exception("qpp::Codes::codeword()",
159  }
160  break;
161  // [[9,1,3]] Shor code
163  ket shora, shorb;
164  shora = mket({0, 0, 0}) + mket({1, 1, 1,});
165  shorb = mket({0, 0, 0}) - mket({1, 1, 1,});
166  switch (i)
167  {
168  case 0:
169  result = kron(shora, kron(shora, shora))
170  / std::sqrt(8.);
171  break;
172  case 1:
173  result = kron(shorb, kron(shorb, shorb))
174  / std::sqrt(8.);
175  break;
176  default:
177  throw Exception("qpp::Codes::codeword()",
179  }
180  }
181 
182  return result;
183  }
184 }; /* class Codes */
185 
186 } /* namespace qpp */
187 
188 #endif /* CLASSES_CODES_H_ */
Codes()
Default constructor.
Definition: codes.h:57
Singleton policy class, used internally to implement the singleton pattern via CRTP (Curiously recurr...
Definition: singleton.h:77
Eigen::VectorXcd ket
Complex (double precision) dynamic Eigen column vector.
Definition: types.h:56
Quantum++ main namespace.
Definition: codes.h:30
Type
Code types, add more codes here if needed.
Definition: codes.h:46
const Singleton class that defines quantum error correcting codes
Definition: codes.h:37
~Codes()=default
Default destructor.
[[7,1,3]] Steane qubit code
dyn_mat< typename T::Scalar > kron(const T &head)
Kronecker product.
Definition: functions.h:798
[[9,1,3]] Shor qubit code
Generates custom exceptions, used when validating function parameters.
Definition: exception.h:39
ket mket(const std::vector< idx > &mask, const std::vector< idx > &dims)
Multi-partite qudit ket.
Definition: functions.h:1334
std::size_t idx
Non-negative integer index.
Definition: types.h:36
ket codeword(Type type, idx i) const
Returns the codeword of the specified code type.
Definition: codes.h:77
[[5,1,3]] qubit code