Skip to content

Commit

Permalink
Allow setting noise on measurement operation and apply it accordingly (
Browse files Browse the repository at this point in the history
…NVIDIA#2447)

Signed-off-by: Thien Nguyen <[email protected]>
  • Loading branch information
1tnguyen authored Dec 4, 2024
1 parent f433d74 commit f7a0b12
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion runtime/common/NoiseModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ class noise_model {
std::unordered_map<std::string, PredicateFuncTy> gatePredicates;

static constexpr const char *availableOps[] = {
"x", "y", "z", "h", "s", "t", "rx", "ry", "rz", "r1", "u3"};
"x", "y", "z", "h", "s", "t", "rx", "ry", "rz", "r1", "u3", "mz"};

public:
/// @brief default constructor
Expand Down
6 changes: 6 additions & 0 deletions runtime/nvqir/CircuitSimulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,12 @@ class CircuitSimulatorBase : public CircuitSimulator {
// Flush the Gate Queue
flushGateQueue();

// Apply measurement noise (if any)
// Note: gate noises are applied during flushGateQueue
if (executionContext && executionContext->noiseModel)
applyNoiseChannel(/*gateName=*/"mz", /*controls=*/{},
/*targets=*/{qubitIdx}, /*params=*/{});

// If sampling, just store the bit, do nothing else.
if (handleBasicSampling(qubitIdx, registerName))
return true;
Expand Down
41 changes: 41 additions & 0 deletions unittests/integration/noise_tester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -641,3 +641,44 @@ CUDAQ_TEST(NoiseTest, checkCustomOperation) {
}
}
#endif

#if defined(CUDAQ_BACKEND_DM) || defined(CUDAQ_BACKEND_STIM)

CUDAQ_TEST(NoiseTest, checkMeasurementNoise) {
cudaq::set_random_seed(13);
constexpr double bitFlipRate = 0.1;
cudaq::bit_flip_channel bf(bitFlipRate);
cudaq::noise_model noise;
// 10% bit flipping during measurement
noise.add_channel("mz", {0}, bf);
cudaq::set_noise(noise);
{
auto kernel = []() {
cudaq::qubit q;
x(q);
mz(q);
};
auto counts = cudaq::sample(10000, kernel);
counts.dump();
// Due to measurement errors, we have both 0/1 results.
EXPECT_EQ(2, counts.size());
EXPECT_NEAR(counts.probability("0"), bitFlipRate, 0.01);
EXPECT_NEAR(counts.probability("1"), 1.0 - bitFlipRate, 0.01);
}
{
auto kernel = []() {
cudaq::qvector q(2);
x(q);
mz(q);
};
auto counts = cudaq::sample(10000, kernel);
counts.dump();
// We only have measurement noise on the first qubit.
EXPECT_EQ(2, counts.size());
EXPECT_NEAR(counts.probability("01"), bitFlipRate, 0.01);
EXPECT_NEAR(counts.probability("11"), 1.0 - bitFlipRate, 0.01);
}
cudaq::unset_noise(); // clear for subsequent tests
}

#endif

0 comments on commit f7a0b12

Please sign in to comment.