Maestro 0.3.11
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
TensorQueries.h
Go to the documentation of this file.
1#pragma once
2
3#include "../../Types.h"
4#include <Eigen/Dense>
5#include <complex>
6#include <limits>
7#include <stdexcept>
8#include <string>
9#include <vector>
10
12{
13// Check shifts and host buffer arithmetic before attempting dense allocation.
14inline size_t DenseSize(size_t qubits)
15{
16 if (qubits >= std::numeric_limits<size_t>::digits)
17 throw std::length_error("Full state output exceeds the basis-index range");
18 const size_t count = size_t{1} << qubits;
19 if (count > std::vector<std::complex<double>>().max_size() || count > std::vector<double>().max_size() / 2)
20 throw std::length_error("Full state output is too large");
21 return count;
22}
23
24inline int Count(size_t count)
25{
26 if (count > static_cast<size_t>(std::numeric_limits<int>::max()))
27 throw std::length_error("Tensor query exceeds the backend count limit");
28 return static_cast<int>(count);
29}
30
31inline void Operators(const Types::qubits_vector &qubits, const std::vector<Eigen::MatrixXcd> &matrices, size_t width)
32{
33 if (qubits.size() != matrices.size())
34 throw std::invalid_argument("Each operator must have one target qubit");
35 Count(qubits.size());
36 if (matrices.size() > std::vector<double>().max_size() / 8)
37 throw std::length_error("Operator matrices exceed the buffer size limit");
38 for (size_t i = 0; i < qubits.size(); ++i)
39 {
40 if (qubits[i] >= width)
41 throw std::out_of_range("Operator target qubit is out of range");
42 if (matrices[i].rows() != 2 || matrices[i].cols() != 2 || !matrices[i].allFinite())
43 throw std::invalid_argument("Expectation operators must be finite 2x2 matrices");
44 }
45}
46
47// Owns only pointers into the input strings, which must outlive the native
48// call.
50{
51 std::vector<const char *> strings;
52 std::vector<int> lengths;
53
54 explicit PauliBatch(const std::vector<std::string> &paulis)
55 {
56 Count(paulis.size());
57 strings.reserve(paulis.size());
58 lengths.reserve(paulis.size());
59 for (const auto &pauli : paulis)
60 {
61 strings.push_back(pauli.c_str());
62 lengths.push_back(Count(pauli.size()));
63 }
64 }
65};
66} // namespace Simulators::TensorQueries
void Operators(const Types::qubits_vector &qubits, const std::vector< Eigen::MatrixXcd > &matrices, size_t width)
size_t DenseSize(size_t qubits)
int Count(size_t count)
std::vector< qubit_t > qubits_vector
The type of a vector of qubits.
Definition Types.h:24
std::vector< const char * > strings
PauliBatch(const std::vector< std::string > &paulis)