pennylane-qrack 0.10.1__tar.gz → 0.10.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of pennylane-qrack might be problematic. Click here for more details.
- {pennylane-qrack-0.10.1 → pennylane_qrack-0.10.3}/Makefile +1 -1
- {pennylane-qrack-0.10.1/pennylane_qrack.egg-info → pennylane_qrack-0.10.3}/PKG-INFO +4 -1
- pennylane_qrack-0.10.3/catalyst/runtime/include/DataView.hpp +147 -0
- pennylane_qrack-0.10.3/catalyst/runtime/include/Exception.hpp +87 -0
- pennylane_qrack-0.10.3/catalyst/runtime/include/QuantumDevice.hpp +362 -0
- pennylane_qrack-0.10.3/catalyst/runtime/include/RuntimeCAPI.h +112 -0
- pennylane_qrack-0.10.3/catalyst/runtime/include/Types.h +165 -0
- {pennylane-qrack-0.10.1 → pennylane_qrack-0.10.3}/pennylane_qrack/_version.py +1 -1
- {pennylane-qrack-0.10.1 → pennylane_qrack-0.10.3}/pennylane_qrack/qrack_device.py +52 -25
- {pennylane-qrack-0.10.1 → pennylane_qrack-0.10.3/pennylane_qrack.egg-info}/PKG-INFO +4 -1
- {pennylane-qrack-0.10.1 → pennylane_qrack-0.10.3}/pennylane_qrack.egg-info/SOURCES.txt +9 -1
- {pennylane-qrack-0.10.1 → pennylane_qrack-0.10.3}/setup.py +5 -10
- pennylane_qrack-0.10.3/tests/test_apply.py +658 -0
- pennylane_qrack-0.10.3/tests/test_integration.py +88 -0
- pennylane_qrack-0.10.3/tests/test_units.py +81 -0
- {pennylane-qrack-0.10.1 → pennylane_qrack-0.10.3}/CHANGELOG.md +0 -0
- {pennylane-qrack-0.10.1 → pennylane_qrack-0.10.3}/CMakeLists.txt +0 -0
- {pennylane-qrack-0.10.1 → pennylane_qrack-0.10.3}/LICENSE +0 -0
- {pennylane-qrack-0.10.1 → pennylane_qrack-0.10.3}/MANIFEST.in +0 -0
- {pennylane-qrack-0.10.1 → pennylane_qrack-0.10.3}/README.rst +0 -0
- {pennylane-qrack-0.10.1 → pennylane_qrack-0.10.3}/pennylane_qrack/QrackDeviceConfig.toml +0 -0
- {pennylane-qrack-0.10.1 → pennylane_qrack-0.10.3}/pennylane_qrack/__init__.py +0 -0
- {pennylane-qrack-0.10.1 → pennylane_qrack-0.10.3}/pennylane_qrack/qrack_device.cpp +0 -0
- {pennylane-qrack-0.10.1 → pennylane_qrack-0.10.3}/pennylane_qrack.egg-info/dependency_links.txt +0 -0
- {pennylane-qrack-0.10.1 → pennylane_qrack-0.10.3}/pennylane_qrack.egg-info/entry_points.txt +0 -0
- {pennylane-qrack-0.10.1 → pennylane_qrack-0.10.3}/pennylane_qrack.egg-info/requires.txt +1 -1
- {pennylane-qrack-0.10.1 → pennylane_qrack-0.10.3}/pennylane_qrack.egg-info/top_level.txt +0 -0
- {pennylane-qrack-0.10.1 → pennylane_qrack-0.10.3}/requirements.txt +0 -0
- {pennylane-qrack-0.10.1 → pennylane_qrack-0.10.3}/setup.cfg +0 -0
|
@@ -24,7 +24,7 @@ help:
|
|
|
24
24
|
build-deps:
|
|
25
25
|
ifneq ($(OS),Windows_NT)
|
|
26
26
|
ifeq ($(QRACK_PRESENT),)
|
|
27
|
-
git clone https://github.com/unitaryfund/qrack.git; cd qrack; git checkout
|
|
27
|
+
git clone https://github.com/unitaryfund/qrack.git; cd qrack; git checkout 0dd1e6ce371b69e21bfa33003bc678b3d42713d5; cd ..
|
|
28
28
|
endif
|
|
29
29
|
mkdir -p qrack/build
|
|
30
30
|
ifeq ($(UNAME_S),Linux)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: pennylane-qrack
|
|
3
|
-
Version: 0.10.
|
|
3
|
+
Version: 0.10.3
|
|
4
4
|
Summary: PennyLane plugin for Qrack.
|
|
5
5
|
Home-page: http://github.com/vm6502q
|
|
6
6
|
Maintainer: vm6502q
|
|
@@ -26,6 +26,9 @@ Classifier: Topic :: Scientific/Engineering :: Physics
|
|
|
26
26
|
Provides: pennylane_qrack
|
|
27
27
|
Description-Content-Type: text/x-rst
|
|
28
28
|
License-File: LICENSE
|
|
29
|
+
Requires-Dist: pennylane>=0.32
|
|
30
|
+
Requires-Dist: pyqrack>=1.30.0
|
|
31
|
+
Requires-Dist: numpy>=1.16
|
|
29
32
|
|
|
30
33
|
PennyLane-Qrack Plugin
|
|
31
34
|
#######################
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
// Copyright 2023 Xanadu Quantum Technologies Inc.
|
|
2
|
+
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
#pragma once
|
|
16
|
+
|
|
17
|
+
#include <Exception.hpp>
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A multi-dimensional view for MemRef-like and std::vector<T> types.
|
|
21
|
+
*
|
|
22
|
+
* @tparam T The underlying data type
|
|
23
|
+
* @tparam R The Rank (R > 0)
|
|
24
|
+
*
|
|
25
|
+
* @note A forward iterator is implemented in this view for traversing over the entire
|
|
26
|
+
* elements of MemRef types rank-by-rank starting from the last dimension (R-1). For example,
|
|
27
|
+
* The DataView iterator for MemRef<T, 2> starts from index (0, 0) and traverses elements
|
|
28
|
+
* in the following order:
|
|
29
|
+
* (0, 0), ..., (0, sizes[1]-1), (1, 0), ..., (1, sizes[1]-1), ... (sizes[0]-1, sizes[1]-1).
|
|
30
|
+
*/
|
|
31
|
+
template <typename T, size_t R> class DataView {
|
|
32
|
+
private:
|
|
33
|
+
T *data_aligned;
|
|
34
|
+
size_t offset;
|
|
35
|
+
size_t sizes[R] = {0};
|
|
36
|
+
size_t strides[R] = {0};
|
|
37
|
+
|
|
38
|
+
public:
|
|
39
|
+
class iterator {
|
|
40
|
+
private:
|
|
41
|
+
const DataView<T, R> &view;
|
|
42
|
+
|
|
43
|
+
int64_t loc; // physical index
|
|
44
|
+
size_t indices[R] = {0};
|
|
45
|
+
|
|
46
|
+
public:
|
|
47
|
+
using iterator_category = std::forward_iterator_tag; // LCOV_EXCL_LINE
|
|
48
|
+
using value_type = T; // LCOV_EXCL_LINE
|
|
49
|
+
using difference_type = std::ptrdiff_t; // LCOV_EXCL_LINE
|
|
50
|
+
using pointer = T *; // LCOV_EXCL_LINE
|
|
51
|
+
using reference = T &; // LCOV_EXCL_LINE
|
|
52
|
+
|
|
53
|
+
iterator(const DataView<T, R> &_view, int64_t begin_idx) : view(_view), loc(begin_idx) {}
|
|
54
|
+
pointer operator->() const { return &view.data_aligned[loc]; }
|
|
55
|
+
reference operator*() const { return view.data_aligned[loc]; }
|
|
56
|
+
iterator &operator++()
|
|
57
|
+
{
|
|
58
|
+
int64_t next_axis = -1;
|
|
59
|
+
int64_t idx;
|
|
60
|
+
for (int64_t i = R; i > 0; --i) {
|
|
61
|
+
idx = i - 1;
|
|
62
|
+
if (indices[idx]++ < view.sizes[idx] - 1) {
|
|
63
|
+
next_axis = idx;
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
indices[idx] = 0;
|
|
67
|
+
loc -= (view.sizes[idx] - 1) * view.strides[idx];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
loc = next_axis == -1 ? -1 : loc + view.strides[next_axis];
|
|
71
|
+
return *this;
|
|
72
|
+
}
|
|
73
|
+
iterator operator++(int)
|
|
74
|
+
{
|
|
75
|
+
auto tmp = *this;
|
|
76
|
+
int64_t next_axis = -1;
|
|
77
|
+
int64_t idx;
|
|
78
|
+
for (int64_t i = R; i > 0; --i) {
|
|
79
|
+
idx = i - 1;
|
|
80
|
+
if (indices[idx]++ < view.sizes[idx] - 1) {
|
|
81
|
+
next_axis = idx;
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
indices[idx] = 0;
|
|
85
|
+
loc -= (view.sizes[idx] - 1) * view.strides[idx];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
loc = next_axis == -1 ? -1 : loc + view.strides[next_axis];
|
|
89
|
+
return tmp;
|
|
90
|
+
}
|
|
91
|
+
bool operator==(const iterator &other) const
|
|
92
|
+
{
|
|
93
|
+
return (loc == other.loc && view.data_aligned == other.view.data_aligned);
|
|
94
|
+
}
|
|
95
|
+
bool operator!=(const iterator &other) const { return !(*this == other); }
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
explicit DataView(std::vector<T> &buffer) : data_aligned(buffer.data()), offset(0)
|
|
99
|
+
{
|
|
100
|
+
static_assert(R == 1, "[Class: DataView] Assertion: R == 1");
|
|
101
|
+
sizes[0] = buffer.size();
|
|
102
|
+
strides[0] = 1;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
explicit DataView(T *_data_aligned, size_t _offset, size_t *_sizes, size_t *_strides)
|
|
106
|
+
: data_aligned(_data_aligned), offset(_offset)
|
|
107
|
+
{
|
|
108
|
+
static_assert(R > 0, "[Class: DataView] Assertion: R > 0");
|
|
109
|
+
if (_sizes && _strides) {
|
|
110
|
+
for (size_t i = 0; i < R; i++) {
|
|
111
|
+
sizes[i] = _sizes[i];
|
|
112
|
+
strides[i] = _strides[i];
|
|
113
|
+
}
|
|
114
|
+
} // else sizes = {0}, strides = {0}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
[[nodiscard]] auto size() const -> size_t
|
|
118
|
+
{
|
|
119
|
+
if (!data_aligned) {
|
|
120
|
+
return 0;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
size_t tsize = 1;
|
|
124
|
+
for (size_t i = 0; i < R; i++) {
|
|
125
|
+
tsize *= sizes[i];
|
|
126
|
+
}
|
|
127
|
+
return tsize;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
template <typename... I> T &operator()(I... idxs) const
|
|
131
|
+
{
|
|
132
|
+
static_assert(sizeof...(idxs) == R,
|
|
133
|
+
"[Class: DataView] Error in Catalyst Runtime: Wrong number of indices");
|
|
134
|
+
size_t indices[] = {static_cast<size_t>(idxs)...};
|
|
135
|
+
|
|
136
|
+
size_t loc = offset;
|
|
137
|
+
for (size_t axis = 0; axis < R; axis++) {
|
|
138
|
+
RT_ASSERT(indices[axis] < sizes[axis]);
|
|
139
|
+
loc += indices[axis] * strides[axis];
|
|
140
|
+
}
|
|
141
|
+
return data_aligned[loc];
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
iterator begin() { return iterator{*this, static_cast<int64_t>(offset)}; }
|
|
145
|
+
|
|
146
|
+
iterator end() { return iterator{*this, -1}; }
|
|
147
|
+
};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// Copyright 2023 Xanadu Quantum Technologies Inc.
|
|
2
|
+
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
#pragma once
|
|
16
|
+
|
|
17
|
+
#include <exception>
|
|
18
|
+
#include <iostream>
|
|
19
|
+
|
|
20
|
+
#include <sstream>
|
|
21
|
+
#include <string>
|
|
22
|
+
#include <type_traits>
|
|
23
|
+
#include <utility>
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @brief Macro that throws `RuntimeException` with given message.
|
|
27
|
+
*/
|
|
28
|
+
#define RT_FAIL(message) Catalyst::Runtime::_abort((message), __FILE__, __LINE__, __func__)
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @brief Macro that throws `RuntimeException` if expression evaluates
|
|
32
|
+
* to true.
|
|
33
|
+
*/
|
|
34
|
+
#define RT_FAIL_IF(expression, message) \
|
|
35
|
+
if ((expression)) { \
|
|
36
|
+
RT_FAIL(message); \
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @brief Macro that throws `RuntimeException` with the given expression
|
|
41
|
+
* and source location if expression evaluates to false.
|
|
42
|
+
*/
|
|
43
|
+
#define RT_ASSERT(expression) RT_FAIL_IF(!(expression), "Assertion: " #expression)
|
|
44
|
+
|
|
45
|
+
namespace Catalyst::Runtime {
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @brief This is the general exception thrown by Catalyst for runtime errors
|
|
49
|
+
* that is derived from `std::exception`.
|
|
50
|
+
*/
|
|
51
|
+
class RuntimeException : public std::exception {
|
|
52
|
+
private:
|
|
53
|
+
const std::string err_msg;
|
|
54
|
+
|
|
55
|
+
public:
|
|
56
|
+
explicit RuntimeException(std::string msg) noexcept
|
|
57
|
+
: err_msg{std::move(msg)} {} // LCOV_EXCL_LINE
|
|
58
|
+
~RuntimeException() override = default; // LCOV_EXCL_LINE
|
|
59
|
+
|
|
60
|
+
RuntimeException(const RuntimeException &) = default;
|
|
61
|
+
RuntimeException(RuntimeException &&) noexcept = default;
|
|
62
|
+
|
|
63
|
+
RuntimeException &operator=(const RuntimeException &) = delete;
|
|
64
|
+
RuntimeException &operator=(RuntimeException &&) = delete;
|
|
65
|
+
|
|
66
|
+
[[nodiscard]] auto what() const noexcept -> const char * override
|
|
67
|
+
{
|
|
68
|
+
return err_msg.c_str();
|
|
69
|
+
} // LCOV_EXCL_LINE
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* @brief Throws a `RuntimeException` with the given error message.
|
|
74
|
+
*
|
|
75
|
+
* @note This is not supposed to be called directly.
|
|
76
|
+
*/
|
|
77
|
+
[[noreturn]] inline void _abort(const char *message, const char *file_name, size_t line,
|
|
78
|
+
const char *function_name)
|
|
79
|
+
{
|
|
80
|
+
std::stringstream sstream;
|
|
81
|
+
sstream << "[" << file_name << "][Line:" << line << "][Function:" << function_name
|
|
82
|
+
<< "] Error in Catalyst Runtime: " << message;
|
|
83
|
+
|
|
84
|
+
throw RuntimeException(sstream.str());
|
|
85
|
+
} // LCOV_EXCL_LINE
|
|
86
|
+
|
|
87
|
+
} // namespace Catalyst::Runtime
|
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
// Copyright 2022-2023 Xanadu Quantum Technologies Inc.
|
|
2
|
+
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
#pragma once
|
|
16
|
+
|
|
17
|
+
#include <complex>
|
|
18
|
+
#include <memory>
|
|
19
|
+
#include <optional>
|
|
20
|
+
#include <random>
|
|
21
|
+
#include <vector>
|
|
22
|
+
|
|
23
|
+
#include "DataView.hpp"
|
|
24
|
+
#include "Types.h"
|
|
25
|
+
|
|
26
|
+
// A helper template macro to generate the <IDENTIFIER>Factory method by
|
|
27
|
+
// calling <CONSTRUCTOR>(kwargs). Check the Custom Devices guideline for details:
|
|
28
|
+
// https://docs.pennylane.ai/projects/catalyst/en/stable/dev/custom_devices.html
|
|
29
|
+
#define GENERATE_DEVICE_FACTORY(IDENTIFIER, CONSTRUCTOR) \
|
|
30
|
+
extern "C" Catalyst::Runtime::QuantumDevice *IDENTIFIER##Factory(const char *kwargs) \
|
|
31
|
+
{ \
|
|
32
|
+
return new CONSTRUCTOR(std::string(kwargs)); \
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
namespace Catalyst::Runtime {
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @brief struct API for backend quantum devices.
|
|
39
|
+
*
|
|
40
|
+
* This device API contains,
|
|
41
|
+
* - a set of methods to manage qubit allocations and deallocations, device shot
|
|
42
|
+
* noise, and quantum tape recording as well as reference values for the result
|
|
43
|
+
* data-type; these are used to implement Quantum Runtime (QR) instructions.
|
|
44
|
+
*
|
|
45
|
+
* - a set of methods for quantum operations, observables, measurements, and gradient
|
|
46
|
+
* of the device; these are used to implement Quantum Instruction Set (QIS) instructions.
|
|
47
|
+
*
|
|
48
|
+
*/
|
|
49
|
+
struct QuantumDevice {
|
|
50
|
+
QuantumDevice() = default; // LCOV_EXCL_LINE
|
|
51
|
+
virtual ~QuantumDevice() = default; // LCOV_EXCL_LINE
|
|
52
|
+
|
|
53
|
+
QuantumDevice &operator=(const QuantumDevice &) = delete;
|
|
54
|
+
QuantumDevice(const QuantumDevice &) = delete;
|
|
55
|
+
QuantumDevice(QuantumDevice &&) = delete;
|
|
56
|
+
QuantumDevice &operator=(QuantumDevice &&) = delete;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @brief Allocate a qubit.
|
|
60
|
+
*
|
|
61
|
+
* @return `QubitIdType`
|
|
62
|
+
*/
|
|
63
|
+
virtual auto AllocateQubit() -> QubitIdType = 0;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @brief Allocate a vector of qubits.
|
|
67
|
+
*
|
|
68
|
+
* @param num_qubits The number of qubits to allocate.
|
|
69
|
+
*
|
|
70
|
+
* @return `std::vector<QubitIdType>`
|
|
71
|
+
*/
|
|
72
|
+
virtual auto AllocateQubits(size_t num_qubits) -> std::vector<QubitIdType> = 0;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* @brief Release a qubit.
|
|
76
|
+
*
|
|
77
|
+
* @param qubit The id of the qubit
|
|
78
|
+
*/
|
|
79
|
+
virtual void ReleaseQubit(QubitIdType qubit) = 0;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* @brief Release all qubits.
|
|
83
|
+
*/
|
|
84
|
+
virtual void ReleaseAllQubits() = 0;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @brief Get the number of allocated qubits.
|
|
88
|
+
*
|
|
89
|
+
* @return `size_t`
|
|
90
|
+
*/
|
|
91
|
+
[[nodiscard]] virtual auto GetNumQubits() const -> size_t = 0;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* @brief Set the number of device shots.
|
|
95
|
+
*
|
|
96
|
+
* @param shots The number of noise shots
|
|
97
|
+
*/
|
|
98
|
+
virtual void SetDeviceShots(size_t shots) = 0;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* @brief Get the number of device shots.
|
|
102
|
+
*
|
|
103
|
+
* @return `size_t`
|
|
104
|
+
*/
|
|
105
|
+
[[nodiscard]] virtual auto GetDeviceShots() const -> size_t = 0;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* @brief Set the PRNG of the device.
|
|
109
|
+
*
|
|
110
|
+
* The Catalyst runtime enables seeded program execution on non-hardware devices.
|
|
111
|
+
* A random number generator instance is managed by the runtime to predictably
|
|
112
|
+
* generate results for non-deterministic programs, such as those involving `Measure`
|
|
113
|
+
* calls.
|
|
114
|
+
* Devices implementing support for this feature do not need to use the provided
|
|
115
|
+
* PRNG instance as their sole source of random numbers, but it is expected that the
|
|
116
|
+
* the same instance state will predictable and reproducibly generate the same
|
|
117
|
+
* program results. It is also expected that the provided PRNG state is evolved
|
|
118
|
+
* sufficiently so that two device executions sharing the same instance do not produce
|
|
119
|
+
* identical results.
|
|
120
|
+
* The provided PRNG instance is not thread-locked, and devices wishing to share it
|
|
121
|
+
* across threads will need to provide their own thread-safety.
|
|
122
|
+
*
|
|
123
|
+
* @param gen The std::mt19937 PRNG object.
|
|
124
|
+
*/
|
|
125
|
+
virtual void SetDevicePRNG([[maybe_unused]] std::mt19937 *gen){};
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* @brief Start recording a quantum tape if provided.
|
|
129
|
+
*
|
|
130
|
+
* @note This is backed by the `Catalyst::Runtime::CacheManager<ComplexT>` property in
|
|
131
|
+
* the device implementation.
|
|
132
|
+
*/
|
|
133
|
+
virtual void StartTapeRecording() = 0;
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* @brief Stop recording a quantum tape if provided.
|
|
137
|
+
*
|
|
138
|
+
* @note This is backed by the `Catalyst::Runtime::CacheManager<ComplexT>` property in
|
|
139
|
+
* the device implementation.
|
|
140
|
+
*/
|
|
141
|
+
virtual void StopTapeRecording() = 0;
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* @brief Result value for "Zero" used in the measurement process.
|
|
145
|
+
*
|
|
146
|
+
* @return `Result`
|
|
147
|
+
*/
|
|
148
|
+
[[nodiscard]] virtual auto Zero() const -> Result = 0;
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* @brief Result value for "One" used in the measurement process.
|
|
152
|
+
*
|
|
153
|
+
* @return `Result`
|
|
154
|
+
*/
|
|
155
|
+
[[nodiscard]] virtual auto One() const -> Result = 0;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* @brief A helper method to print the state vector of a device.
|
|
159
|
+
*/
|
|
160
|
+
virtual void PrintState() = 0;
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* @brief Prepare subsystems using the given ket vector in the computational basis.
|
|
164
|
+
*
|
|
165
|
+
* @param state A state vector of size 2**len(wires)
|
|
166
|
+
* @param wires The wire(s) the operation acts on
|
|
167
|
+
*/
|
|
168
|
+
virtual void SetState(DataView<std::complex<double>, 1> &, std::vector<QubitIdType> &)
|
|
169
|
+
{
|
|
170
|
+
RT_FAIL("Unsupported functionality");
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* @brief Prepares a single computational basis state.
|
|
175
|
+
*
|
|
176
|
+
* @param n Prepares the basis state |n>, where n is an array of integers from the set {0, 1}
|
|
177
|
+
* @param wires The wire(s) the operation acts on
|
|
178
|
+
*/
|
|
179
|
+
virtual void SetBasisState(DataView<int8_t, 1> &, std::vector<QubitIdType> &)
|
|
180
|
+
{
|
|
181
|
+
RT_FAIL("Unsupported functionality");
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* @brief Apply a single gate to the state vector of a device with its name if this is
|
|
186
|
+
* supported.
|
|
187
|
+
*
|
|
188
|
+
* @param name The name of the gate to apply
|
|
189
|
+
* @param params Optional parameter list for parametric gates
|
|
190
|
+
* @param wires Wires to apply gate to
|
|
191
|
+
* @param inverse Indicates whether to use inverse of gate
|
|
192
|
+
* @param controlled_wires Optional controlled wires applied to the operation
|
|
193
|
+
* @param controlled_values Optional controlled values applied to the operation
|
|
194
|
+
*/
|
|
195
|
+
virtual void
|
|
196
|
+
NamedOperation(const std::string &name, const std::vector<double> ¶ms,
|
|
197
|
+
const std::vector<QubitIdType> &wires, [[maybe_unused]] bool inverse = false,
|
|
198
|
+
[[maybe_unused]] const std::vector<QubitIdType> &controlled_wires = {},
|
|
199
|
+
[[maybe_unused]] const std::vector<bool> &controlled_values = {}) = 0;
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* @brief Apply a given matrix directly to the state vector of a device.
|
|
203
|
+
*
|
|
204
|
+
* @param matrix The matrix of data in row-major format
|
|
205
|
+
* @param wires Wires to apply gate to
|
|
206
|
+
* @param inverse Indicates whether to use inverse of gate
|
|
207
|
+
* @param controlled_wires Controlled wires applied to the operation
|
|
208
|
+
* @param controlled_values Controlled values applied to the operation
|
|
209
|
+
*/
|
|
210
|
+
virtual void
|
|
211
|
+
MatrixOperation(const std::vector<std::complex<double>> &matrix,
|
|
212
|
+
const std::vector<QubitIdType> &wires, [[maybe_unused]] bool inverse = false,
|
|
213
|
+
[[maybe_unused]] const std::vector<QubitIdType> &controlled_wires = {},
|
|
214
|
+
[[maybe_unused]] const std::vector<bool> &controlled_values = {}) = 0;
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* @brief Construct a named (Identity, PauliX, PauliY, PauliZ, and Hadamard)
|
|
218
|
+
* or Hermitian observable.
|
|
219
|
+
*
|
|
220
|
+
* @param id The type of the observable
|
|
221
|
+
* @param matrix The matrix of data to construct a hermitian observable
|
|
222
|
+
* @param wires Wires to apply observable to
|
|
223
|
+
*
|
|
224
|
+
* @return `ObsIdType` Index of the constructed observable
|
|
225
|
+
*/
|
|
226
|
+
virtual auto Observable(ObsId id, const std::vector<std::complex<double>> &matrix,
|
|
227
|
+
const std::vector<QubitIdType> &wires) -> ObsIdType = 0;
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* @brief Construct a tensor product of observables.
|
|
231
|
+
*
|
|
232
|
+
* @param obs The vector of observables indices of type ObsIdType
|
|
233
|
+
*
|
|
234
|
+
* @return `ObsIdType` Index of the constructed observable
|
|
235
|
+
*/
|
|
236
|
+
virtual auto TensorObservable(const std::vector<ObsIdType> &obs) -> ObsIdType = 0;
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* @brief Construct a Hamiltonian observable.
|
|
240
|
+
*
|
|
241
|
+
* @param coeffs The vector of coefficients
|
|
242
|
+
* @param obs The vector of observables indices of size `coeffs`
|
|
243
|
+
*
|
|
244
|
+
* @return `ObsIdType` Index of the constructed observable
|
|
245
|
+
*/
|
|
246
|
+
virtual auto HamiltonianObservable(const std::vector<double> &coeffs,
|
|
247
|
+
const std::vector<ObsIdType> &obs) -> ObsIdType = 0;
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* @brief Compute the expected value of an observable.
|
|
251
|
+
*
|
|
252
|
+
* @param obsKey The index of the constructed observable
|
|
253
|
+
*
|
|
254
|
+
* @return `double` The expected value
|
|
255
|
+
*/
|
|
256
|
+
virtual auto Expval(ObsIdType obsKey) -> double = 0;
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* @brief Compute the variance of an observable.
|
|
260
|
+
*
|
|
261
|
+
* @param obsKey The index of the constructed observable
|
|
262
|
+
*
|
|
263
|
+
* @return `double` The variance
|
|
264
|
+
*/
|
|
265
|
+
virtual auto Var(ObsIdType obsKey) -> double = 0;
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* @brief Get the state-vector of a device.
|
|
269
|
+
*
|
|
270
|
+
* @param state The pre-allocated `DataView<complex<double>, 1>`
|
|
271
|
+
*/
|
|
272
|
+
virtual void State(DataView<std::complex<double>, 1> &state) = 0;
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* @brief Compute the probabilities of each computational basis state.
|
|
276
|
+
|
|
277
|
+
* @param probs The pre-allocated `DataView<double, 1>`
|
|
278
|
+
*/
|
|
279
|
+
virtual void Probs(DataView<double, 1> &probs) = 0;
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* @brief Compute the probabilities for a subset of the full system.
|
|
283
|
+
*
|
|
284
|
+
* @param probs The pre-allocated `DataView<double, 1>`
|
|
285
|
+
* @param wires Wires will restrict probabilities to a subset of the full system
|
|
286
|
+
*/
|
|
287
|
+
virtual void PartialProbs(DataView<double, 1> &probs,
|
|
288
|
+
const std::vector<QubitIdType> &wires) = 0;
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* @brief Compute samples with the number of shots on the entire wires,
|
|
292
|
+
* returing raw samples.
|
|
293
|
+
*
|
|
294
|
+
* @param samples The pre-allocated `DataView<double, 2>`representing a matrix of
|
|
295
|
+
* shape `shots * numQubits`. The built-in iterator in `DataView<double, 2>`
|
|
296
|
+
* iterates over all elements of `samples` row-wise.
|
|
297
|
+
* @param shots The number of shots
|
|
298
|
+
*/
|
|
299
|
+
virtual void Sample(DataView<double, 2> &samples, size_t shots) = 0;
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* @brief Compute partial samples with the number of shots on `wires`,
|
|
303
|
+
* returing raw samples.
|
|
304
|
+
*
|
|
305
|
+
* @param samples The pre-allocated `DataView<double, 2>`representing a matrix of
|
|
306
|
+
* shape `shots * numWires`. The built-in iterator in `DataView<double, 2>`
|
|
307
|
+
* iterates over all elements of `samples` row-wise.
|
|
308
|
+
* @param wires Wires to compute samples on
|
|
309
|
+
* @param shots The number of shots
|
|
310
|
+
*/
|
|
311
|
+
virtual void PartialSample(DataView<double, 2> &samples, const std::vector<QubitIdType> &wires,
|
|
312
|
+
size_t shots) = 0;
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* @brief Sample with the number of shots on the entire wires, returning the
|
|
316
|
+
* number of counts for each sample.
|
|
317
|
+
*
|
|
318
|
+
* @param eigvals The pre-allocated `DataView<double, 1>`
|
|
319
|
+
* @param counts The pre-allocated `DataView<int64_t, 1>`
|
|
320
|
+
* @param shots The number of shots
|
|
321
|
+
*/
|
|
322
|
+
virtual void Counts(DataView<double, 1> &eigvals, DataView<int64_t, 1> &counts,
|
|
323
|
+
size_t shots) = 0;
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* @brief Partial sample with the number of shots on `wires`, returning the
|
|
327
|
+
* number of counts for each sample.
|
|
328
|
+
*
|
|
329
|
+
* @param eigvals The pre-allocated `DataView<double, 1>`
|
|
330
|
+
* @param counts The pre-allocated `DataView<int64_t, 1>`
|
|
331
|
+
* @param wires Wires to compute samples on
|
|
332
|
+
* @param shots The number of shots
|
|
333
|
+
*/
|
|
334
|
+
virtual void PartialCounts(DataView<double, 1> &eigvals, DataView<int64_t, 1> &counts,
|
|
335
|
+
const std::vector<QubitIdType> &wires, size_t shots) = 0;
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* @brief A general measurement method that acts on a single wire.
|
|
339
|
+
*
|
|
340
|
+
* @param wire The wire to compute Measure on
|
|
341
|
+
* @param postselect Which basis state to postselect after a mid-circuit measurement (-1 denotes
|
|
342
|
+
no post-selection)
|
|
343
|
+
|
|
344
|
+
* @return `Result` The measurement result
|
|
345
|
+
*/
|
|
346
|
+
virtual auto Measure(QubitIdType wire, std::optional<int32_t> postselect) -> Result = 0;
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* @brief Compute the gradient of a quantum tape, that is cached using
|
|
350
|
+
* `Catalyst::Runtime::Simulator::CacheManager`, for a specific set of trainable
|
|
351
|
+
* parameters.
|
|
352
|
+
*
|
|
353
|
+
* @param gradients The vector of pre-allocated `DataView<double, 1>*`
|
|
354
|
+
* to store gradients resutls for the list of cached observables.
|
|
355
|
+
* @param trainParams The vector of trainable parameters; if none, all parameters
|
|
356
|
+
* would be assumed trainable
|
|
357
|
+
*
|
|
358
|
+
*/
|
|
359
|
+
virtual void Gradient(std::vector<DataView<double, 1>> &gradients,
|
|
360
|
+
const std::vector<size_t> &trainParams) = 0;
|
|
361
|
+
};
|
|
362
|
+
} // namespace Catalyst::Runtime
|