Skip to content

Commit

Permalink
Fix NoiseModel for single-precision backends (#2406)
Browse files Browse the repository at this point in the history
Signed-off-by: Thien Nguyen <[email protected]>
  • Loading branch information
1tnguyen authored Nov 27, 2024
1 parent d133531 commit 5435af0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
4 changes: 0 additions & 4 deletions runtime/common/NoiseModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ void validateCompletenessRelation_fp64(const std::vector<kraus_op> &ops) {
"Provided kraus_ops are not completely positive and trace preserving.");
}

kraus_channel::kraus_channel(std::vector<kraus_op> &_ops) : ops(_ops) {
validateCompleteness();
}

kraus_channel::kraus_channel(const kraus_channel &other)
: ops(other.ops), noise_type(other.noise_type),
parameters(other.parameters) {}
Expand Down
17 changes: 15 additions & 2 deletions runtime/common/NoiseModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ class kraus_channel {
std::vector<kraus_op> ops;

/// @brief Validate that Sum K_i^† K_i = I
// Important: as this function dispatches different implementations based on
// `cudaq::complex`, which is a pre-processor define, do not call this in
// `NoiseModel.cpp`. `NoiseModel.cpp` is compiled as a `cudaq-common` library,
// which is not aware of the backend complex type.
void validateCompleteness() {
if constexpr (std::is_same_v<cudaq::complex::value_type, float>) {
validateCompletenessRelation_fp32(ops);
Expand All @@ -122,7 +126,14 @@ class kraus_channel {
noise_model_type noise_type = noise_model_type::unknown;

/// @brief Noise parameter values
std::vector<real> parameters;
// Use `double` as the uniform type to store channel parameters (for both
// single- and double-precision channel definitions). Some
// `kraus_channel` methods, e.g., copy constructor/assignment, are implemented
// in `NoiseModel.cpp`, which is compiled to `cudaq-common` with
// double-precision configuration regardless of the backends.
// Hence, having a templated `parameters` member variable may cause data
// corruption.
std::vector<double> parameters;

~kraus_channel() = default;

Expand All @@ -143,7 +154,9 @@ class kraus_channel {

/// @brief The constructor, take qubits and channel kraus_ops as lvalue
/// reference
kraus_channel(std::vector<kraus_op> &ops);
kraus_channel(const std::vector<kraus_op> &inOps) : ops(inOps) {
validateCompleteness();
}

/// @brief The constructor, take qubits and channel kraus_ops as rvalue
/// reference
Expand Down

0 comments on commit 5435af0

Please sign in to comment.