From f6d9357450b5dbe58665ac24c32e4873c3539a85 Mon Sep 17 00:00:00 2001 From: Thien Nguyen Date: Tue, 11 Jun 2024 00:31:30 +0000 Subject: [PATCH] Add a 'strong' validation for qubit allocation batching to match the assumption of the underlying simulator backends --- .../default/DefaultExecutionManager.cpp | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/runtime/cudaq/qis/managers/default/DefaultExecutionManager.cpp b/runtime/cudaq/qis/managers/default/DefaultExecutionManager.cpp index be1af009c4..6fdfb3378f 100644 --- a/runtime/cudaq/qis/managers/default/DefaultExecutionManager.cpp +++ b/runtime/cudaq/qis/managers/default/DefaultExecutionManager.cpp @@ -56,16 +56,32 @@ class DefaultExecutionManager : public cudaq::BasicExecutionManager { const void *state, cudaq::simulation_precision precision) override { // Here we have qubits in requestedAllocations - // want to allocate and set state + // want to allocate and set state. // There could be previous 'default' allocations whereby we just cached them // in requestedAllocations. // These default allocations need to be dispatched separately. - // FIXME: this assumes no qubit reuse, aka the qubits in targets are the - // last ones to be allocated. This is consistent with the Kronecker product - // assumption in CircuitSimulator. if (!requestedAllocations.empty() && targets.size() != requestedAllocations.size()) { assert(targets.size() < requestedAllocations.size()); + // This assumes no qubit reuse, aka the qubits are allocated in order. + // This is consistent with the Kronecker product assumption in + // CircuitSimulator. + for (std::size_t i = 0; i < requestedAllocations.size() - 1; ++i) { + // Verify this assumption to make sure the simulator set + // the state of appropriate qubits. + const auto &thisAlloc = requestedAllocations[i]; + const auto &nextAlloc = requestedAllocations[i + 1]; + if (nextAlloc.id != (thisAlloc.id + 1)) { + std::stringstream errorMsg; + errorMsg << "Out of order allocation detected. This is not supported " + "by simulator backends. Qubit allocations: [ "; + for (const auto &alloc : requestedAllocations) { + errorMsg << alloc.id << " "; + } + errorMsg << "]"; + throw std::logic_error(errorMsg.str()); + } + } const auto numDefaultAllocs = requestedAllocations.size() - targets.size(); simulator()->allocateQubits(numDefaultAllocs);