Skip to content

Commit 016ea56

Browse files
committed
Rename FlipChannel
1 parent 6add342 commit 016ea56

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

quantum/density/matrix.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -210,29 +210,26 @@ func (m *Matrix) PartialTrace(qb ...int) *Matrix {
210210
}
211211

212212
// Depolarizing returns the depolarizing channel.
213-
// It applies the identity with probability (1 - p),
214-
// and applies each of the Pauli gates X, Y, and Z with probability p/3.
213+
// It applies the identity with probability (1 - p), and applies each of the Pauli gates X, Y, and Z with probability p/3.
215214
func (m *Matrix) Depolarizing(p float64, qb int) *Matrix {
216215
n := m.NumQubits()
217216

218-
idx := []int{qb}
219-
id := m.rho.Mul(complex(1-p, 0))
220-
xg := gate.TensorProduct(gate.X(), n, idx)
221-
yg := gate.TensorProduct(gate.Y(), n, idx)
222-
zg := gate.TensorProduct(gate.Z(), n, idx)
217+
xg := gate.TensorProduct(gate.X(), n, []int{qb})
218+
yg := gate.TensorProduct(gate.Y(), n, []int{qb})
219+
zg := gate.TensorProduct(gate.Z(), n, []int{qb})
223220

224221
x := matrix.MatMul(xg, m.rho, xg.Dagger()).Mul(complex(p/3, 0))
225222
y := matrix.MatMul(yg, m.rho, yg.Dagger()).Mul(complex(p/3, 0))
226223
z := matrix.MatMul(zg, m.rho, zg.Dagger()).Mul(complex(p/3, 0))
227224

228225
return &Matrix{
229-
rho: id.Add(x).Add(y).Add(z),
226+
rho: m.rho.Mul(complex(1-p, 0)).Add(x).Add(y).Add(z),
230227
}
231228
}
232229

233-
// ApplyChannel applies a channel to the density matrix.
230+
// FlipChannel applies a channel to the density matrix.
234231
// It applies the identity with probability 1-p, and applies the gate g with probability p.
235-
func (m *Matrix) ApplyChannel(p float64, u *matrix.Matrix, qb ...int) *Matrix {
232+
func (m *Matrix) FlipChannel(p float64, u *matrix.Matrix, qb ...int) *Matrix {
236233
n := m.NumQubits()
237234

238235
e0 := gate.I().Mul(complex(math.Sqrt(1-p), 0))
@@ -252,17 +249,17 @@ func (m *Matrix) ApplyChannel(p float64, u *matrix.Matrix, qb ...int) *Matrix {
252249

253250
// BitFlip applies a bit flip channel to the density matrix.
254251
func (m *Matrix) BitFlip(p float64, qb int) *Matrix {
255-
return m.ApplyChannel(p, gate.X(), qb)
252+
return m.FlipChannel(p, gate.X(), qb)
256253
}
257254

258255
// BitPhaseFlip applies a bit-phase flip channel to the density matrix.
259256
func (m *Matrix) BitPhaseFlip(p float64, qb int) *Matrix {
260-
return m.ApplyChannel(p, gate.Y(), qb)
257+
return m.FlipChannel(p, gate.Y(), qb)
261258
}
262259

263260
// PhaseFlip applies a phase flip channel to the density matrix.
264261
func (m *Matrix) PhaseFlip(p float64, qb int) *Matrix {
265-
return m.ApplyChannel(p, gate.Z(), qb)
262+
return m.FlipChannel(p, gate.Z(), qb)
266263
}
267264

268265
// split separates the bits of x into two integers according to mask.

quantum/density/matrix_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,13 @@ func ExampleMatrix_Depolarizing() {
295295
// 1: 0.20
296296
}
297297

298-
func ExampleMatrix_ApplyChannel() {
298+
func ExampleMatrix_FlipChannel() {
299299
rho, err := density.NewPureState(qubit.Zero(2))
300300
if err != nil {
301301
panic(err)
302302
}
303303

304-
s1 := rho.ApplyChannel(0.3, gate.X(), 0)
304+
s1 := rho.FlipChannel(0.3, gate.X(), 0)
305305
fmt.Printf("%.2f\n", s1.Probability(qubit.From("00")))
306306
fmt.Printf("%.2f\n", s1.Probability(qubit.From("10")))
307307

@@ -310,13 +310,13 @@ func ExampleMatrix_ApplyChannel() {
310310
// 0.30
311311
}
312312

313-
func ExampleMatrix_ApplyChannel_qb1() {
313+
func ExampleMatrix_FlipChannel_qb1() {
314314
rho, err := density.NewPureState(qubit.Zero(2))
315315
if err != nil {
316316
panic(err)
317317
}
318318

319-
s1 := rho.ApplyChannel(0.3, gate.X(), 1)
319+
s1 := rho.FlipChannel(0.3, gate.X(), 1)
320320
fmt.Printf("%.2f\n", s1.Probability(qubit.From("00")))
321321
fmt.Printf("%.2f\n", s1.Probability(qubit.From("01")))
322322

0 commit comments

Comments
 (0)