Skip to content

Commit 3727ace

Browse files
committed
fix dynamics backend to accept density-matrix initial state in createFromData
Signed-off-by: Sachin Pisal <spisal@nvidia.com>
1 parent 6531dc3 commit 3727ace

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

docs/sphinx/applications/python/quantum_pagerank.ipynb

Lines changed: 15 additions & 5 deletions
Large diffs are not rendered by default.

runtime/nvqir/cudensitymat/CuDensityMatState.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,29 @@ void CuDensityMatState::dump(std::ostream &os) const {
8989
os << state << std::endl;
9090
}
9191

92+
std::unique_ptr<SimulationState>
93+
CuDensityMatState::createFromData(const state_data &data) {
94+
if (std::holds_alternative<cudaq::complex_matrix>(data)) {
95+
auto &cMat = std::get<cudaq::complex_matrix>(data);
96+
if (cMat.rows() != cMat.cols())
97+
throw std::runtime_error(
98+
"[CuDensityMatState] Density matrix input must be square.");
99+
const std::size_t size = cMat.rows() * cMat.cols();
100+
void *dataPtr = const_cast<cudaq::complex_matrix &>(cMat).get_data(
101+
cudaq::complex_matrix::order::row_major);
102+
std::complex<double> *devicePtr = static_cast<std::complex<double> *>(
103+
cudaq::dynamics::DeviceAllocator::allocate(
104+
size * sizeof(std::complex<double>)));
105+
HANDLE_CUDA_ERROR(cudaMemcpy(devicePtr, dataPtr,
106+
size * sizeof(std::complex<double>),
107+
cudaMemcpyDefault));
108+
auto result = std::make_unique<CuDensityMatState>(size, devicePtr);
109+
result->isDensityMatrix = true;
110+
return result;
111+
}
112+
return SimulationState::createFromData(data);
113+
}
114+
92115
std::unique_ptr<SimulationState>
93116
CuDensityMatState::createFromSizeAndPtr(std::size_t size, void *dataPtr,
94117
std::size_t type) {

runtime/nvqir/cudensitymat/CuDensityMatState.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ class CuDensityMatState : public cudaq::SimulationState {
8787
}
8888

8989
// Create the state from external data
90+
std::unique_ptr<SimulationState>
91+
createFromData(const state_data &data) override;
92+
9093
std::unique_ptr<SimulationState>
9194
createFromSizeAndPtr(std::size_t size, void *dataPtr,
9295
std::size_t type) override;

0 commit comments

Comments
 (0)