|
7 | 7 | ******************************************************************************/ |
8 | 8 | // [Begin Documentation] |
9 | 9 |
|
| 10 | +// Simple 3-qubit repetition code with real-time decoding |
| 11 | +// This is the most basic QEC example possible |
| 12 | + |
| 13 | +#include "cudaq.h" |
| 14 | +#include "cudaq/qec/code.h" |
| 15 | +#include "cudaq/qec/experiments.h" |
| 16 | +#include "cudaq/qec/pcm_utils.h" |
10 | 17 | #include "cudaq/qec/realtime/decoding.h" |
11 | 18 | #include "cudaq/qec/realtime/decoding_config.h" |
| 19 | +#include <common/NoiseModel.h> |
| 20 | +#include <fstream> |
12 | 21 |
|
13 | | -// Configure decoder before circuit execution |
14 | | -cudaq::qec::decoding::config::configure_decoders_from_file( |
15 | | - "decoder_config.yaml"); |
| 22 | +// Save decoder configuration to YAML file |
| 23 | +void save_dem(const cudaq::qec::detector_error_model &dem, |
| 24 | + const std::string &filename) { |
| 25 | + // Create decoder config |
| 26 | + cudaq::qec::decoding::config::decoder_config config; |
| 27 | + config.id = 0; |
| 28 | + config.type = "multi_error_lut"; |
| 29 | + config.block_size = dem.num_error_mechanisms(); |
| 30 | + config.syndrome_size = dem.num_detectors(); |
| 31 | + config.H_sparse = cudaq::qec::pcm_to_sparse_vec(dem.detector_error_matrix); |
| 32 | + config.O_sparse = cudaq::qec::pcm_to_sparse_vec(dem.observables_flips_matrix); |
| 33 | + |
| 34 | + // Calculate numRounds from DEM (we send 1 additional round, so add 1) |
| 35 | + uint64_t numSyndromesPerRound = 2; // Z0Z1 and Z1Z2 |
| 36 | + auto numRounds = dem.num_detectors() / numSyndromesPerRound + 1; |
| 37 | + config.D_sparse = cudaq::qec::generate_timelike_sparse_detector_matrix( |
| 38 | + numSyndromesPerRound, numRounds, false); |
| 39 | + |
| 40 | + cudaq::qec::decoding::config::multi_error_lut_config lut_config; |
| 41 | + lut_config.lut_error_depth = 2; |
| 42 | + config.decoder_custom_args = lut_config; |
| 43 | + |
| 44 | + cudaq::qec::decoding::config::multi_decoder_config multi_config; |
| 45 | + multi_config.decoders.push_back(config); |
| 46 | + |
| 47 | + std::ofstream file(filename); |
| 48 | + file << multi_config.to_yaml_str(200); |
| 49 | + file.close(); |
| 50 | + printf("Saved config to %s\n", filename.c_str()); |
| 51 | +} |
16 | 52 |
|
17 | | -__qpu__ void prep0(cudaq::qec::patch logical) { |
18 | | - // Your state preparation logic |
19 | | - continue; |
| 53 | +// Load decoder configuration from YAML file |
| 54 | +void load_dem(const std::string &filename) { |
| 55 | + std::ifstream file(filename); |
| 56 | + std::string yaml((std::istreambuf_iterator<char>(file)), |
| 57 | + std::istreambuf_iterator<char>()); |
| 58 | + auto config = cudaq::qec::decoding::config::multi_decoder_config::from_yaml_str(yaml); |
| 59 | + cudaq::qec::decoding::config::configure_decoders(config); |
| 60 | + printf("Loaded config from %s\n", filename.c_str()); |
20 | 61 | } |
21 | | -__qpu__ std::vector<bool> measure_stabilizers(cudaq::qec::patch logical) { |
22 | | - // Your stabilizer measurement logic |
23 | | - return std::vector<bool>(12, false); |
| 62 | + |
| 63 | +// Prepare logical |0⟩ |
| 64 | +__qpu__ void prep0(cudaq::qec::patch logical) { |
| 65 | + for (std::size_t i = 0; i < logical.data.size(); ++i) { |
| 66 | + cudaq::reset(logical.data[i]); |
| 67 | + } |
24 | 68 | } |
25 | 69 |
|
26 | | -// Quantum kernel with real-time decoding |
27 | | -__qpu__ void qec_circuit(int decoder_id, int num_rounds) { |
28 | | - // Reset decoder state |
29 | | - cudaq::qec::decoding::reset_decoder(decoder_id); |
| 70 | +// Measure ZZ stabilizers for 3-qubit repetition code |
| 71 | +__qpu__ std::vector<cudaq::measure_result> measure_stabilizers( |
| 72 | + cudaq::qec::patch logical) { |
| 73 | + for (std::size_t i = 0; i < logical.ancz.size(); ++i) { |
| 74 | + cudaq::reset(logical.ancz[i]); |
| 75 | + } |
| 76 | + |
| 77 | + // Z0Z1 stabilizer |
| 78 | + cudaq::x<cudaq::ctrl>(logical.data[0], logical.ancz[0]); |
| 79 | + cudaq::x<cudaq::ctrl>(logical.data[1], logical.ancz[0]); |
| 80 | + |
| 81 | + // Z1Z2 stabilizer |
| 82 | + cudaq::x<cudaq::ctrl>(logical.data[1], logical.ancz[1]); |
| 83 | + cudaq::x<cudaq::ctrl>(logical.data[2], logical.ancz[1]); |
| 84 | + |
| 85 | + return {mz(logical.ancz[0]), mz(logical.ancz[1])}; |
| 86 | +} |
30 | 87 |
|
31 | | - // Allocate qubits |
32 | | - cudaq::qvector data(25), ancx(12), ancz(12); |
| 88 | +// QEC circuit with real-time decoding |
| 89 | +__qpu__ int64_t qec_circuit() { |
| 90 | + cudaq::qec::decoding::reset_decoder(0); |
| 91 | + |
| 92 | + cudaq::qvector data(3); |
| 93 | + cudaq::qvector ancz(2); |
| 94 | + cudaq::qvector ancx; // Empty for repetition code |
33 | 95 | cudaq::qec::patch logical(data, ancx, ancz); |
34 | | - |
35 | | - // Prepare logical state |
| 96 | + |
36 | 97 | prep0(logical); |
37 | | - |
38 | | - // Syndrome extraction with real-time decoding |
39 | | - for (int round = 0; round < num_rounds; ++round) { |
| 98 | + |
| 99 | + // 3 rounds of syndrome measurement |
| 100 | + for (int round = 0; round < 3; ++round) { |
40 | 101 | auto syndromes = measure_stabilizers(logical); |
41 | | - cudaq::qec::decoding::enqueue_syndromes(decoder_id, syndromes); |
| 102 | + cudaq::qec::decoding::enqueue_syndromes(0, syndromes); |
42 | 103 | } |
43 | | - |
44 | | - // Get and apply corrections |
45 | | - auto corrections = |
46 | | - cudaq::qec::decoding::get_corrections(decoder_id, 1, false); |
47 | | - if (corrections[0]) { |
48 | | - cudaq::x(data); // Apply correction |
| 104 | + |
| 105 | + // Get corrections and apply them |
| 106 | + auto corrections = cudaq::qec::decoding::get_corrections(0, 3); |
| 107 | + for (std::size_t i = 0; i < 3; ++i) { |
| 108 | + if (corrections[i]) |
| 109 | + cudaq::x(data[i]); |
49 | 110 | } |
50 | | - |
51 | | - // Measure logical observable |
52 | | - auto result = mz(data); |
| 111 | + |
| 112 | + return cudaq::to_integer(mz(data)); |
53 | 113 | } |
54 | 114 |
|
55 | | -// Clean up |
56 | | -cudaq::qec::decoding::config::finalize_decoders(); |
| 115 | +int main() { |
| 116 | + auto code = cudaq::qec::get_code("repetition", |
| 117 | + cudaqx::heterogeneous_map{{"distance", 3}}); |
| 118 | + |
| 119 | + // Step 1: Generate detector error model |
| 120 | + printf("Step 1: Generating DEM...\n"); |
| 121 | + cudaq::noise_model noise; |
| 122 | + noise.add_all_qubit_channel("x", cudaq::depolarization2(0.01), 1); |
| 123 | + |
| 124 | + auto dem = cudaq::qec::z_dem_from_memory_circuit( |
| 125 | + *code, cudaq::qec::operation::prep0, 3, noise); |
| 126 | + |
| 127 | + save_dem(dem, "config.yaml"); |
| 128 | + |
| 129 | + // Step 2: Load config and run circuit |
| 130 | + printf("\nStep 2: Running circuit with decoding...\n"); |
| 131 | + load_dem("config.yaml"); |
| 132 | + |
| 133 | + cudaq::run(10, qec_circuit); |
| 134 | + printf("Ran 10 shots\n"); |
| 135 | + |
| 136 | + cudaq::qec::decoding::config::finalize_decoders(); |
| 137 | + printf("\nDone!\n"); |
| 138 | + return 0; |
| 139 | +} |
0 commit comments