Quantum++  v1.1
A modern C++11 quantum computing library
codes.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 - 2019 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_CODES_H_
33 #define CLASSES_CODES_H_
34 
35 namespace qpp {
40 class Codes final : public internal::Singleton<const Codes> // const Singleton
41 {
42  friend class internal::Singleton<const Codes>;
43 
44  public:
49  enum class Type // exception types
50  { FIVE_QUBIT = 1,
53  };
54 
55  private:
59  Codes() {} // = default; // clang++ spits the error below if defaulted:
60  // error:
61  // default initialization of an object of const type 'const qpp::Codes'
62  // requires a user-provided default constructor
63 
67  ~Codes() = default;
68 
69  public:
78  ket codeword(Type type, idx i) const {
79  ket result;
80  switch (type) {
81  // [[5,1,3]] Five qubit code (as in Nielsen and Chuang)
82  case Type::FIVE_QUBIT:
83  switch (i) {
84  case 0:
85  result = (mket({0, 0, 0, 0, 0}) + mket({1, 0, 0, 1, 0}) +
86  mket({0, 1, 0, 0, 1}) + mket({1, 0, 1, 0, 0}) +
87  mket({0, 1, 0, 1, 0}) - mket({1, 1, 0, 1, 1}) -
88  mket({0, 0, 1, 1, 0}) - mket({1, 1, 0, 0, 0}) -
89  mket({1, 1, 1, 0, 1}) - mket({0, 0, 0, 1, 1}) -
90  mket({1, 1, 1, 1, 0}) - mket({0, 1, 1, 1, 1}) -
91  mket({1, 0, 0, 0, 1}) - mket({0, 1, 1, 0, 0}) -
92  mket({1, 0, 1, 1, 1}) + mket({0, 0, 1, 0, 1})) /
93  4.;
94  break;
95  case 1:
96  result = (mket({1, 1, 1, 1, 1}) + mket({0, 1, 1, 0, 1}) +
97  mket({1, 0, 1, 1, 0}) + mket({0, 1, 0, 1, 1}) +
98  mket({1, 0, 1, 0, 1}) - mket({0, 0, 1, 0, 0}) -
99  mket({1, 1, 0, 0, 1}) - mket({0, 0, 1, 1, 1}) -
100  mket({0, 0, 0, 1, 0}) - mket({1, 1, 1, 0, 0}) -
101  mket({0, 0, 0, 0, 1}) - mket({1, 0, 0, 0, 0}) -
102  mket({0, 1, 1, 1, 0}) - mket({1, 0, 0, 1, 1}) -
103  mket({0, 1, 0, 0, 0}) + mket({1, 1, 0, 1, 0})) /
104  4.;
105  break;
106  default:
107  throw exception::NoCodeword("qpp::Codes::codeword()");
108  }
109  break;
110  // [[7,1,3]] Steane code according to Nielsen and Chuang)
112  switch (i) {
113  case 0:
114  result =
115  (mket({0, 0, 0, 0, 0, 0, 0}) + mket({1, 0, 1, 0, 1, 0, 1}) +
116  mket({0, 1, 1, 0, 0, 1, 1}) + mket({1, 1, 0, 0, 1, 1, 0}) +
117  mket({0, 0, 0, 1, 1, 1, 1}) + mket({1, 0, 1, 1, 0, 1, 0}) +
118  mket({0, 1, 1, 1, 1, 0, 0}) +
119  mket({1, 1, 0, 1, 0, 0, 1})) /
120  std::sqrt(8.);
121 
122  break;
123  case 1:
124  result =
125  (mket({1, 1, 1, 1, 1, 1, 1}) + mket({0, 1, 0, 1, 0, 1, 0}) +
126  mket({1, 0, 0, 1, 1, 0, 0}) + mket({0, 0, 1, 1, 0, 0, 1}) +
127  mket({1, 1, 1, 0, 0, 0, 0}) + mket({0, 1, 0, 0, 1, 0, 1}) +
128  mket({1, 0, 0, 0, 0, 1, 1}) +
129  mket({0, 0, 1, 0, 1, 1, 0})) /
130  std::sqrt(8.);
131  break;
132  default:
133  throw exception::NoCodeword("qpp::Codes::codeword()");
134  }
135  break;
136  // [[9,1,3]] Shor code
138  ket shora, shorb;
139  shora = mket({0, 0, 0}) + mket({
140  1, 1, 1,
141  });
142  shorb = mket({0, 0, 0}) - mket({
143  1, 1, 1,
144  });
145  switch (i) {
146  case 0:
147  result = kron(shora, kron(shora, shora)) / std::sqrt(8.);
148  break;
149  case 1:
150  result = kron(shorb, kron(shorb, shorb)) / std::sqrt(8.);
151  break;
152  default:
153  throw exception::NoCodeword("qpp::Codes::codeword()");
154  }
155  }
156 
157  return result;
158  }
159 }; /* class Codes */
160 
161 } /* namespace qpp */
162 
163 #endif /* CLASSES_CODES_H_ */
Codes()
Default constructor.
Definition: codes.h:59
Singleton policy class, used internally to implement the singleton pattern via CRTP (Curiously recurr...
Definition: singleton.h:80
Eigen::VectorXcd ket
Complex (double precision) dynamic Eigen column vector.
Definition: types.h:54
Quantum++ main namespace.
Definition: codes.h:35
Type
Code types, add more codes here if needed.
Definition: codes.h:49
const Singleton class that defines quantum error correcting codes
Definition: codes.h:40
~Codes()=default
Default destructor.
[[7,1,3]] Steane qubit code
dyn_mat< typename T::Scalar > kron(const T &head)
Kronecker product.
Definition: functions.h:868
[[9,1,3]] Shor qubit code
Codeword does not exist exception.
Definition: exception.h:500
ket mket(const std::vector< idx > &mask, const std::vector< idx > &dims)
Multi-partite qudit ket.
Definition: functions.h:1405
ket codeword(Type type, idx i) const
Returns the codeword of the specified code type.
Definition: codes.h:78
std::size_t idx
Non-negative integer index.
Definition: types.h:39
[[5,1,3]] qubit code