Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debug Qrack tutorial #8

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions pennylane_qrack/qrack_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#define CL_HPP_TARGET_OPENCL_VERSION 300
#include "qrack/qfactory.hpp"

#define QSIM_CONFIG(numQubits) CreateQuantumInterface(simulatorType, numQubits, Qrack::ZERO_BCI, nullptr, Qrack::CMPLX_DEFAULT_ARG, false, true, is_host_pointer)

std::string trim(std::string s)
{
// Cut leading, trailing, and extra spaces
Expand All @@ -30,11 +32,14 @@ struct QrackObservable {

struct QrackDevice final : public Catalyst::Runtime::QuantumDevice {
bool tapeRecording;
bool is_host_pointer;
bitLenInt allocated_qubits;
bitLenInt mapped_qubits;
size_t shots;
Qrack::QInterfacePtr qsim;
std::map<QubitIdType, bitLenInt> qubit_map;
std::vector<QrackObservable> obs_cache;
std::vector<Qrack::QInterfaceEngine> simulatorType;

// static constants for RESULT values
static constexpr bool QRACK_RESULT_TRUE_CONST = true;
Expand Down Expand Up @@ -369,7 +374,9 @@ struct QrackDevice final : public Catalyst::Runtime::QuantumDevice {

QrackDevice([[maybe_unused]] std::string kwargs = "{}")
: tapeRecording(false)
, is_host_pointer(false)
, allocated_qubits(0U)
, mapped_qubits(0U)
, shots(1U)
, qsim(nullptr)
{
Expand All @@ -390,14 +397,12 @@ struct QrackDevice final : public Catalyst::Runtime::QuantumDevice {
keyMap["'is_gpu'"] = 8;
keyMap["'is_host_pointer'"] = 9;

bitLenInt wires = 0U;
bool is_hybrid_stabilizer = true;
bool is_tensor_network = false;
bool is_tensor_network = true;
bool is_schmidt_decomposed = true;
bool is_schmidt_decomposition_parallel = true;
bool is_qbdd = false;
bool is_gpu = true;
bool is_host_pointer = false;

size_t pos;
while ((pos = kwargs.find(":")) != std::string::npos) {
Expand All @@ -421,8 +426,8 @@ struct QrackDevice final : public Catalyst::Runtime::QuantumDevice {
}
}
if (isInt) {
wires = stoi(trim(kwargs.substr(0, pos)));
for (size_t i = 0U; i < wires; ++i) {
mapped_qubits = stoi(trim(kwargs.substr(0, pos)));
for (size_t i = 0U; i < mapped_qubits; ++i) {
qubit_map[i] = i;
}
kwargs.erase(0, pos + 1U);
Expand All @@ -436,15 +441,14 @@ struct QrackDevice final : public Catalyst::Runtime::QuantumDevice {
kwargs.erase(0, pos + 3U);
size_t p = value.find("[");
value.erase(0, p + 1U);
wires = 0U;
size_t q;
while ((q = value.find(",")) != std::string::npos) {
qubit_map[(QubitIdType)stoi(trim(value.substr(0, q)))] = wires;
++wires;
qubit_map[(QubitIdType)stoi(trim(value.substr(0, q)))] = mapped_qubits;
++mapped_qubits;
value.erase(0, q + 1U);
}
qubit_map[stoi(trim(value))] = wires;
++wires;
qubit_map[stoi(trim(value))] = mapped_qubits;
++mapped_qubits;

continue;
}
Expand Down Expand Up @@ -486,8 +490,6 @@ struct QrackDevice final : public Catalyst::Runtime::QuantumDevice {
}

// Construct backwards, then reverse:
std::vector<Qrack::QInterfaceEngine> simulatorType;

if (!is_gpu) {
simulatorType.push_back(Qrack::QINTERFACE_CPU);
}
Expand Down Expand Up @@ -515,7 +517,7 @@ struct QrackDevice final : public Catalyst::Runtime::QuantumDevice {
simulatorType.push_back(Qrack::QINTERFACE_CPU);
}

qsim = CreateQuantumInterface(simulatorType, wires, Qrack::ZERO_BCI, nullptr, Qrack::CMPLX_DEFAULT_ARG, false, true, is_host_pointer);
qsim = QSIM_CONFIG(mapped_qubits);
}

QrackDevice &operator=(const QuantumDevice &) = delete;
Expand Down Expand Up @@ -607,7 +609,7 @@ struct QrackDevice final : public Catalyst::Runtime::QuantumDevice {
void ReleaseAllQubits() override
{
// State vector is left empty
qsim->Dispose(0U, qsim->GetQubitCount());
qsim = QSIM_CONFIG(0U);
qubit_map.clear();
}
[[nodiscard]] auto GetNumQubits() const -> size_t override
Expand Down
6 changes: 1 addition & 5 deletions pennylane_qrack/qrack_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,7 @@ def get_c_interface():

def __init__(self, wires=0, shots=None, **kwargs):
super().__init__(wires=wires, shots=shots)

if "isTensorNetwork" in kwargs:
self._state = QrackSimulator(self.num_wires, **kwargs)
else:
self._state = QrackSimulator(self.num_wires, isTensorNetwork=False, **kwargs)
self._state = QrackSimulator(self.num_wires, **kwargs)

def _reverse_state(self):
end = self.num_wires - 1
Expand Down
Loading