Skip to content

Commit 30071f8

Browse files
authored
Support more error types (#556)
1 parent c6368c9 commit 30071f8

File tree

4 files changed

+479
-17
lines changed

4 files changed

+479
-17
lines changed

docs/src/examples/9.noisy-simulation/main.jl

Lines changed: 130 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
# # Noisy Simulation
2+
13
using Yao
24
using YaoBlocks.Optimise: replace_block
35
using CairoMakie
46

5-
# # Noisy Simulation
67
# To start, we create a simple circuit that we want to simulate, the one generating the 4-qubit GHZ state $|\psi\rangle = \frac{1}{\sqrt{2}}(|0000\rangle + |1111\rangle)$.
78
# The code is as follows:
89
n_qubits = 4
@@ -39,4 +40,131 @@ rho = apply(density_matrix(reg), circ_noisy)
3940
samples = measure(rho, nshots=1000);
4041

4142
# Visualize the results
42-
hist(map(x -> x.buf, samples))
43+
hist(map(x -> x.buf, samples))
44+
45+
# # Error Types and Quantum Channels
46+
#
47+
# Yao provides various types of quantum errors and their corresponding quantum channel representations.
48+
# Let's explore the different error types available:
49+
50+
# ## 1. Bit Flip Error
51+
# A bit flip error with probability p applies X gate with probability p
52+
bit_flip = BitFlipError(0.1)
53+
bit_flip_channel = quantum_channel(bit_flip)
54+
println("Bit Flip Error Channel: ", bit_flip_channel)
55+
56+
# ## 2. Phase Flip Error
57+
# A phase flip error with probability p applies Z gate with probability p
58+
phase_flip = PhaseFlipError(0.1)
59+
phase_flip_channel = quantum_channel(phase_flip)
60+
println("Phase Flip Error Channel: ", phase_flip_channel)
61+
62+
# ## 3. Depolarizing Error
63+
# A depolarizing error with probability p applies X, Y, or Z gate with equal probability p/3
64+
depolarizing = DepolarizingError(1, 0.1)
65+
depolarizing_channel = quantum_channel(depolarizing)
66+
println("Depolarizing Error Channel: ", depolarizing_channel)
67+
68+
# ## 4. Pauli Error
69+
# A Pauli error with probabilities px, py, pz for X, Y, Z gates respectively
70+
pauli_error = PauliError(0.05, 0.03, 0.02)
71+
pauli_channel = quantum_channel(pauli_error)
72+
println("Pauli Error Channel: ", pauli_channel)
73+
74+
# ## 5. Reset Error
75+
# A reset error that resets qubits to |0⟩ or |1⟩ with given probabilities
76+
reset_error = ResetError(0.1, 0.05) # p0, p1
77+
reset_channel = quantum_channel(reset_error)
78+
println("Reset Error Channel: ", reset_channel)
79+
80+
# ## 6. Thermal Relaxation Error
81+
# Models decoherence with T1 and T2 times
82+
thermal_relaxation = ThermalRelaxationError(100.0, 200.0, 1.0, 0.0)
83+
thermal_channel = quantum_channel(thermal_relaxation)
84+
println("Thermal Relaxation Error Channel: ", thermal_relaxation)
85+
86+
# ## 7. Amplitude Damping Error
87+
# Models energy loss to environment
88+
amplitude_damping = AmplitudeDampingError(0.1)
89+
amplitude_channel = quantum_channel(amplitude_damping)
90+
println("Amplitude Damping Error Channel: ", amplitude_damping)
91+
92+
# ## 8. Phase Damping Error
93+
# Models pure dephasing
94+
phase_damping = PhaseDampingError(0.1)
95+
phase_channel = quantum_channel(phase_damping)
96+
println("Phase Damping Error Channel: ", phase_damping)
97+
98+
# ## 9. Phase-Amplitude Damping Error
99+
# Combines both amplitude and phase damping
100+
phase_amplitude_damping = PhaseAmplitudeDampingError(0.1, 0.05, 0.0)
101+
phase_amplitude_channel = quantum_channel(phase_amplitude_damping)
102+
println("Phase-Amplitude Damping Error Channel: ", phase_amplitude_damping)
103+
104+
# ## 10. Coherent Error
105+
# A deterministic error represented by a quantum gate
106+
coherent_error = CoherentError(X)
107+
coherent_channel = quantum_channel(coherent_error)
108+
println("Coherent Error Channel: ", coherent_error)
109+
110+
# # Channel Representations
111+
#
112+
# Each error type can be converted to different channel representations:
113+
114+
# ## Kraus Channel Representation
115+
# Represents the channel as a set of Kraus operators
116+
bit_flip_kraus = KrausChannel(bit_flip)
117+
println("Bit Flip Kraus Operators:")
118+
for (i, op) in enumerate(bit_flip_kraus.operators)
119+
println("K$i = ", mat(op))
120+
end
121+
122+
# ## Mixed Unitary Channel Representation
123+
# Represents the channel as a convex combination of unitary operators
124+
bit_flip_mixed = MixedUnitaryChannel(bit_flip)
125+
println("Bit Flip Mixed Unitary Channel:")
126+
for (i, (prob, gate)) in enumerate(zip(bit_flip_mixed.probs, bit_flip_mixed.operators))
127+
println("p$i = $prob, U$i = ", mat(gate))
128+
end
129+
130+
# ## Superoperator Representation
131+
# Represents the channel as a superoperator matrix
132+
bit_flip_superop = SuperOp(bit_flip)
133+
println("Bit Flip Superoperator Matrix:")
134+
println(bit_flip_superop.superop)
135+
136+
# # Example: Comparing Different Error Types
137+
#
138+
# Let's compare how different error types affect a simple circuit:
139+
140+
# Create a simple test circuit
141+
test_circ = chain(2, put(1=>H), control(2, 1=>X))
142+
143+
# Test different error types
144+
error_types = [
145+
("Bit Flip", BitFlipError(0.1)),
146+
("Phase Flip", PhaseFlipError(0.1)),
147+
("Depolarizing", DepolarizingError(1, 0.1)),
148+
("Amplitude Damping", AmplitudeDampingError(0.1)),
149+
("Phase Damping", PhaseDampingError(0.1))
150+
]
151+
152+
println("\nComparing different error types on a 2-qubit circuit:")
153+
for (name, error) in error_types
154+
# Add error after each gate
155+
noisy_circ = replace_block(test_circ) do block
156+
if block isa PutBlock && length(block.locs) == 1
157+
chain(block, put(nqubits(block), block.locs => quantum_channel(error)))
158+
elseif block isa ControlBlock && length(block.ctrl_locs) == 1 && length(block.locs) == 1
159+
chain(block, put(nqubits(block), (block.ctrl_locs..., block.locs...) =>
160+
kron(quantum_channel(error), quantum_channel(error))))
161+
else
162+
block
163+
end
164+
end
165+
166+
# Simulate
167+
rho = noisy_simulation(zero_state(2), noisy_circ)
168+
fid = fidelity(rho, apply(density_matrix(zero_state(2)), test_circ))
169+
println("$name Error: Fidelity = $(round(fid, digits=3))")
170+
end

lib/YaoBlocks/src/channel/channel.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
# error types
12
export AbstractErrorType, BitFlipError, PhaseFlipError, DepolarizingError, PauliError, ResetError,
2-
KrausChannel, MixedUnitaryChannel, DepolarizingChannel, quantum_channel, SuperOp,
3+
ThermalRelaxationError, PhaseAmplitudeDampingError, PhaseDampingError, AmplitudeDampingError,
4+
CoherentError
5+
6+
# channels
7+
export KrausChannel, MixedUnitaryChannel, DepolarizingChannel, quantum_channel, SuperOp,
38
add_noise, noisy_simulation
49

510
"""

0 commit comments

Comments
 (0)