First QEC Simulation - Noro-Official/OQS GitHub Wiki

3. First QEC Simulation

This guide will walk you through your first quantum error correction (QEC) experiment using OpenQStack.

You’ll run a full encode β†’ error β†’ syndrome β†’ recovery β†’ decode cycle using the 3-qubit bit-flip code.


Step 1: Run the Demo

Navigate to the examples/ directory and run the demo script:

cd examples
python openq_blink.py

Step 2: Understand What’s Happening

The script performs a full QEC cycle:

  1. Encoding
    The logical qubit (e.g., |ψ⟩ = |0⟩) is encoded into a 3-qubit state.

  2. Error Injection
    A random bit-flip error (X gate) is applied to one of the physical qubits.

  3. Syndrome Measurement
    A parity check is performed to detect the error.

  4. Recovery
    Based on the syndrome, the most likely correction is applied.

  5. Decoding
    The recovered 3-qubit state is decoded back to a single-qubit logical state.

You will see:

  • Intermediate states printed to the terminal
  • Syndrome bits displayed (e.g., "010")
  • The final recovered logical qubit state
  • A probability plot (if matplotlib is installed)

Step 3: Project Structure

openqstack/
β”œβ”€β”€ openqstack/          # Core logic: codes, noise, visualization
β”‚   β”œβ”€β”€ qec.py
β”‚   β”œβ”€β”€ visualize.py
β”‚   └── __init__.py
β”‚
β”œβ”€β”€ examples/            # Demo scripts and walkthroughs
β”‚   └── openq_blink.py
β”‚
β”œβ”€β”€ tests/               # Unit tests
β”‚   └── test_bitflip.py
β”‚
β”œβ”€β”€ requirements.txt     # Python dependencies
β”œβ”€β”€ setup.py             # Installable module
└── README.md            # Project overview

Step 4: Try More

Once the basic demo is working, try:

  • Changing the logical input state:

    psi = [1/np.sqrt(2), 1/np.sqrt(2)]
    
  • Using a depolarizing noise model:

    from openqstack.qec import DepolarizingChannel
    channel = DepolarizingChannel(p=0.1, n_qubits=3)
    noisy_state = channel(encoded)
    
  • Visualizing results:

    from openqstack.visualize import plot_bloch
    plot_bloch(decoded)
    
  • Writing your own recovery logic or adding a custom error


Step 5: Troubleshooting

If something goes wrong:

  • Make sure you're using Python 3.8+

  • Install missing packages:

    pip install -r requirements.txt
    
  • If plots don’t show:

    pip install matplotlib
    
  • Run tests to confirm functionality:

    pytest tests/
    
  • Verify package visibility:

    python -m openqstack
    

Questions?

OpenQStack is an open-source initiative supporting quantum education.

  • Found a bug? Open an issue.
  • Teaching with OpenQStack? Let us know.
  • Want to contribute? Fork the repo or start a discussion.

We welcome collaboration from students, educators, and researchers.