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

Fix reallocation after free #19

Merged
merged 2 commits into from
Jul 12, 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
2 changes: 1 addition & 1 deletion pennylane_qrack/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
Version number (major.minor.patch[-label])
"""

__version__ = "0.7.0"
__version__ = "0.7.1"
7 changes: 4 additions & 3 deletions pennylane_qrack/qrack_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ struct QrackDevice final : public Catalyst::Runtime::QuantumDevice {
}
}

qsim = QSIM_CONFIG(mapped_qubits);
qsim = QSIM_CONFIG(0U);
}

QrackDevice &operator=(const QuantumDevice &) = delete;
Expand All @@ -516,6 +516,7 @@ struct QrackDevice final : public Catalyst::Runtime::QuantumDevice {
+ std::to_string(allocated_qubits) + " allocated qubits. "
+ "(Set your wires count high enough, for the device.)");
}
qsim->Allocate(1U);
auto it = qubit_map.begin();
std::advance(it, allocated_qubits);
const QubitIdType label = it->first;
Expand Down Expand Up @@ -589,13 +590,13 @@ struct QrackDevice final : public Catalyst::Runtime::QuantumDevice {
qsim->M(id);
// Deallocate
qsim->Dispose(id, 1U);
qubit_map.erase(label);
--allocated_qubits;
}
void ReleaseAllQubits() override
{
// State vector is left empty
qsim = QSIM_CONFIG(0U);
qubit_map.clear();
allocated_qubits = 0;
}
[[nodiscard]] auto GetNumQubits() const -> size_t override
{
Expand Down
20 changes: 20 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ def test_load_device(self):
assert dev.short_name == "qrack.simulator"
assert "model" in dev.__class__.capabilities()

def test_realloc(self):
"""Test that the Qrack device can reallocate after free."""
dev = QrackDevice(2, shots=int(1e6), isOpenCL=False)

@qjit
@qml.qnode(dev)
def circuit():
qml.Hadamard(wires=0)
qml.CNOT(wires=[0, 1])
qml.Hadamard(wires=0)
qml.CNOT(wires=[0, 1])
qml.Hadamard(wires=0)
return qml.expval(qml.PauliY(0))

circuit()
try:
circuit()
except:
pytest.fail("Reallocation after free failed")

def test_expectation(self):
"""Test that expectation of a non-trivial circuit is correct."""
dev = QrackDevice(2, shots=int(1e6), isOpenCL=False)
Expand Down
Loading