XACC
layouts.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 - 2020 Vlad Gheorghiu.
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 
34 #ifndef CLASSES_LAYOUTS_H_
35 #define CLASSES_LAYOUTS_H_
36 
37 namespace qpp {
38 
46 class ILayout {
47  public:
56  virtual idx operator()(const std::vector<idx>& xs) const = 0;
57 
64  virtual std::vector<idx> to_coordinates(idx i) const = 0;
65 
71  virtual std::vector<idx> get_dims() const = 0;
72 
76  virtual ~ILayout() = default;
77 }; /* class ILayout */
78 
84 class Lattice : public ILayout {
85  protected:
86  std::vector<idx> dims_;
87  public:
92  Lattice(const std::vector<idx>& dims) : dims_{dims} {
93  // EXCEPTION CHECKS
94 
95  if (dims.empty())
96  throw exception::ZeroSize("qpp::Lattice::Lattice()");
97  // END EXCEPTION CHECKS
98  }
99 
106  template <class... Ts>
107  Lattice(Ts... ds) : Lattice(std::vector<idx>{static_cast<idx>(ds)...}) {}
108 
117  idx operator()(const std::vector<idx>& xs) const override {
118  // EXCEPTION CHECKS
119 
120  if (xs.size() != dims_.size())
121  throw exception::DimsNotEqual("qpp::Lattice::operator()");
122  for (idx i = 0; i < dims_.size(); ++i)
123  if (xs[i] >= dims_[i])
124  throw exception::OutOfRange("qpp::Lattice::operator()");
125  // END EXCEPTION CHECKS
126 
127  return internal::multiidx2n(xs.data(), dims_.size(), dims_.data());
128  }
129 
139  template <class... Ts>
140  idx operator()(Ts... xs) const {
141  return operator()(std::vector<idx>{static_cast<idx>(xs)...});
142  }
143 
150  std::vector<idx> to_coordinates(idx i) const override {
151  // EXCEPTION CHECKS
152 
153  if (i >= prod(dims_))
154  throw exception::OutOfRange("qpp::Lattice::to_coordinates()");
155  // END EXCEPTION CHECKS
156 
157  std::vector<idx> result(dims_.size());
158  internal::n2multiidx(i, dims_.size(), dims_.data(), result.data());
159 
160  return result;
161  }
162 
168  std::vector<idx> get_dims() const override { return dims_; }
169 }; /* class Lattice */
170 
177 class PeriodicBoundaryLattice : public Lattice {
178  public:
179  using Lattice::Lattice;
180  using Lattice::operator();
181 
190  idx operator()(const std::vector<idx>& xs) const override {
191  // EXCEPTION CHECKS
192 
193  if (xs.size() != dims_.size())
194  throw exception::DimsNotEqual("qpp::Lattice::operator()");
195  // END EXCEPTION CHECKS
196 
197  std::vector<idx> xs_copy = xs;
198  for (idx i = 0; i < dims_.size(); ++i)
199  xs_copy[i] = xs[i] % dims_[i];
200  return Lattice::operator()(xs_copy);
201  }
202 }; /* class PeriodicBoundaryLattice */
203 
204 } /* namespace qpp */
205 
206 #endif /* CLASSES_LAYOUTS_H_ */
Quantum++ main namespace.
Definition: circuits.h:35