Skip to content

Commit 858dcfd

Browse files
authored
polish (#228)
1 parent f0dccf3 commit 858dcfd

File tree

2 files changed

+89
-45
lines changed

2 files changed

+89
-45
lines changed

README.md

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,51 @@ For Julia or optional features (LLVM, CUDA), see the [Getting Started Guide](doc
2828

2929
## Quick Example
3030

31-
Create and simulate a Bell state—an entangled pair of qubits using [Guppy](https://github.com/CQCL/guppylang), a pythonic quantum programming language:
31+
Simulate a distance-3 repetition code with syndrome extraction using [Guppy](https://github.com/CQCL/guppylang), a pythonic quantum programming language:
3232

3333
```python
34-
from pecos import sim, state_vector
34+
from pecos import Guppy, sim, state_vector, depolarizing_noise
3535
from guppylang import guppy
36-
from guppylang.std.quantum import qubit, h, cx, measure
36+
from guppylang.std.quantum import qubit, cx, measure
37+
from guppylang.std.builtins import array, result
3738

3839

3940
@guppy
40-
def bell_state() -> tuple[bool, bool]:
41-
q0 = qubit()
42-
q1 = qubit()
43-
h(q0)
44-
cx(q0, q1)
45-
return measure(q0), measure(q1)
46-
47-
48-
# Run 10 shots
49-
results = sim(bell_state).qubits(2).quantum(state_vector()).seed(42).run(10)
50-
print(results.to_dict())
51-
# {'measurement_0': [1, 1, 0, 0, ...], 'measurement_1': [1, 1, 0, 0, ...]}
41+
def repetition_code() -> None:
42+
# 3 data qubits encode logical |0⟩ = |000⟩
43+
d0, d1, d2 = qubit(), qubit(), qubit()
44+
45+
# 2 ancillas for syndrome extraction
46+
s0, s1 = qubit(), qubit()
47+
48+
# Measure parity between adjacent data qubits
49+
cx(d0, s0)
50+
cx(d1, s0)
51+
cx(d1, s1)
52+
cx(d2, s1)
53+
54+
# Extract syndromes as an array
55+
result("syndrome", array(measure(s0), measure(s1)))
56+
57+
# Measure data qubits (required by Guppy)
58+
_ = measure(d0), measure(d1), measure(d2)
59+
60+
61+
# Run 10 shots with 10% depolarizing noise
62+
noise = depolarizing_noise().with_uniform_probability(0.1)
63+
results = (
64+
sim(Guppy(repetition_code))
65+
.qubits(5)
66+
.quantum(state_vector())
67+
.noise(noise)
68+
.seed(42)
69+
.run(10)
70+
)
71+
print(results["syndrome"])
72+
# [[1, 1], [0, 1], [0, 0], [1, 1], [0, 0], [0, 1], [1, 1], [0, 0], [0, 1], [0, 1]]
5273
```
5374

54-
The measurements always match (`measurement_0` equals `measurement_1`)—that's quantum entanglement in action.
75+
Non-trivial syndromes like `[1, 0]`, `[0, 1]`, `[1, 1]` indicate detected errors that a decoder would use to identify and correct faults.
5576

5677
For OpenQASM, PHIR, or other formats, see the [documentation](#documentation). For a Rust example, see [For Rust Users](#for-rust-users) below.
5778

docs/user-guide/getting-started.md

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,15 @@ This guide will help you get up and running with PECOS quickly.
5454

5555
## Your First Simulation
5656

57-
Now that PECOS is installed, let's create a simple quantum circuit. We'll create a **Bell state**—a fundamental entangled state used throughout quantum computing and quantum error correction.
57+
Now that PECOS is installed, let's simulate a quantum error correction circuit. We'll create a **distance-3 repetition code**—a fundamental building block for protecting quantum information from errors.
5858

5959
### What We're Building
6060

61-
A Bell state is created by:
61+
A repetition code encodes a single logical qubit across multiple physical qubits:
6262

63-
1. Applying a Hadamard gate (H) to put a qubit in superposition
64-
2. Applying a CNOT gate to entangle two qubits
65-
66-
The result is the state $\frac{1}{\sqrt{2}}(|00\rangle + |11\rangle)$, where measuring either qubit always gives the same result as the other.
63+
1. **3 data qubits** store the logical state: $|0\rangle_L = |000\rangle$, $|1\rangle_L = |111\rangle$
64+
2. **2 ancilla qubits** measure parity between adjacent data qubits (syndrome extraction)
65+
3. **Noise** introduces random errors that the syndromes detect
6766

6867
### Running the Simulation
6968

@@ -72,27 +71,45 @@ The result is the state $\frac{1}{\sqrt{2}}(|00\rangle + |11\rangle)$, where mea
7271
We'll use **Guppy**, a Python-embedded quantum programming language that offers type-safe qubit tracking and native control flow:
7372

7473
```python
74+
from pecos import Guppy, sim, state_vector, depolarizing_noise
7575
from guppylang import guppy
76-
from guppylang.std.quantum import h, cx, measure, qubit
77-
from pecos import sim, Guppy
78-
from pecos_rslib import state_vector
76+
from guppylang.std.quantum import qubit, cx, measure
77+
from guppylang.std.builtins import array, result
7978

8079

8180
@guppy
82-
def bell_state() -> tuple[bool, bool]:
83-
"""Create and measure a Bell state."""
84-
q0 = qubit()
85-
q1 = qubit()
86-
h(q0)
87-
cx(q0, q1)
88-
return measure(q0), measure(q1)
89-
90-
91-
# Run 10 shots of the simulation
92-
results = sim(Guppy(bell_state)).qubits(2).quantum(state_vector()).seed(42).run(10)
93-
94-
# View results
95-
print(f"Results: {results.to_dict()}")
81+
def repetition_code() -> None:
82+
# 3 data qubits encode logical |0⟩ = |000⟩
83+
d0, d1, d2 = qubit(), qubit(), qubit()
84+
85+
# 2 ancillas for syndrome extraction
86+
s0, s1 = qubit(), qubit()
87+
88+
# Measure parity between adjacent data qubits
89+
cx(d0, s0)
90+
cx(d1, s0)
91+
cx(d1, s1)
92+
cx(d2, s1)
93+
94+
# Extract syndromes as an array
95+
result("syndrome", array(measure(s0), measure(s1)))
96+
97+
# Measure data qubits (required by Guppy)
98+
_ = measure(d0), measure(d1), measure(d2)
99+
100+
101+
# Run 10 shots with 10% depolarizing noise
102+
noise = depolarizing_noise().with_uniform_probability(0.1)
103+
results = (
104+
sim(Guppy(repetition_code))
105+
.qubits(5)
106+
.quantum(state_vector())
107+
.noise(noise)
108+
.seed(42)
109+
.run(10)
110+
)
111+
print(results["syndrome"])
112+
# [[1, 1], [0, 1], [0, 0], [1, 1], [0, 0], [0, 1], [1, 1], [0, 0], [0, 1], [0, 1]]
96113
```
97114

98115
=== ":fontawesome-brands-rust: Rust"
@@ -103,14 +120,14 @@ The result is the state $\frac{1}{\sqrt{2}}(|00\rangle + |11\rangle)$, where mea
103120
use pecos_hugr::hugr_sim;
104121

105122
fn main() -> Result<(), Box<dyn std::error::Error>> {
106-
// Load and run a pre-compiled Bell state circuit
107-
let results = hugr_sim("bell_state.hugr")
123+
// Load and run a pre-compiled repetition code circuit
124+
let results = hugr_sim("repetition_code.hugr")
108125
.seed(42)
109126
.run(10)?;
110127

111128
// View results
112129
for shot in &results.shots {
113-
println!("Measurement: {:?}", shot.data);
130+
println!("Syndrome: {:?}", shot.data);
114131
}
115132
Ok(())
116133
}
@@ -122,10 +139,16 @@ The result is the state $\frac{1}{\sqrt{2}}(|00\rangle + |11\rangle)$, where mea
122139

123140
### Understanding the Output
124141

125-
Run the code multiple times (with different seeds). You'll notice:
142+
The syndromes tell you which errors occurred:
143+
144+
| Syndrome | Meaning |
145+
|----------|---------|
146+
| `[0, 0]` | No detected errors |
147+
| `[1, 0]` | Error on qubit d0 (left edge) |
148+
| `[0, 1]` | Error on qubit d2 (right edge) |
149+
| `[1, 1]` | Error on qubit d1 (middle) |
126150

127-
- Results contain values like `0` (binary `00`) and `3` (binary `11`)
128-
- Both qubits **always** have the same value—this is quantum entanglement!
151+
A decoder uses these syndromes to identify and correct errors—see the [Decoders](decoders.md) guide.
129152

130153
The `sim()` function is PECOS's unified simulation API. It accepts circuits in various formats (Guppy, HUGR, QASM) and provides a builder pattern for configuration.
131154

0 commit comments

Comments
 (0)