Skip to content

Commit bc1d1b8

Browse files
committed
Update density matrix
1 parent 9765b0b commit bc1d1b8

File tree

3 files changed

+30
-43
lines changed

3 files changed

+30
-43
lines changed

quantum/density/matrix.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,6 @@ func NewPureState(qb *qubit.Qubit) *Matrix {
4242
})
4343
}
4444

45-
// NewZeroState returns a new zero state density matrix for the given number of qubits.
46-
func NewZeroState(n ...int) *Matrix {
47-
return NewPureState(qubit.Zero(n...))
48-
}
49-
5045
// At returns a value of matrix at (i,j).
5146
func (m *Matrix) At(i, j int) complex128 {
5247
return m.rho.At(i, j)

quantum/density/matrix_test.go

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ func ExampleMatrix_Project() {
3838

3939
computationalBasis := []*qubit.Qubit{
4040
qubit.From("00"),
41-
qubit.From("01"),
42-
qubit.From("10"),
4341
qubit.From("11"),
4442
}
4543

@@ -58,16 +56,6 @@ func ExampleMatrix_Project() {
5856
// [(0+0i) (0+0i) (0+0i) (0+0i)]
5957
// [(0+0i) (0+0i) (0+0i) (0+0i)]
6058
// [(0+0i) (0+0i) (0+0i) (0+0i)]
61-
// [[01][ 1]( 1.0000 0.0000i): 1.0000]: 0.00
62-
// [(0+0i) (0+0i) (0+0i) (0+0i)]
63-
// [(0+0i) (0+0i) (0+0i) (0+0i)]
64-
// [(0+0i) (0+0i) (0+0i) (0+0i)]
65-
// [(0+0i) (0+0i) (0+0i) (0+0i)]
66-
// [[10][ 2]( 1.0000 0.0000i): 1.0000]: 0.00
67-
// [(0+0i) (0+0i) (0+0i) (0+0i)]
68-
// [(0+0i) (0+0i) (0+0i) (0+0i)]
69-
// [(0+0i) (0+0i) (0+0i) (0+0i)]
70-
// [(0+0i) (0+0i) (0+0i) (0+0i)]
7159
// [[11][ 3]( 1.0000 0.0000i): 1.0000]: 0.50
7260
// [(0+0i) (0+0i) (0+0i) (0+0i)]
7361
// [(0+0i) (0+0i) (0+0i) (0+0i)]
@@ -121,7 +109,7 @@ func ExampleMatrix_Probability() {
121109
}
122110

123111
func ExampleMatrix_IsHermite() {
124-
s0 := density.NewZeroState()
112+
s0 := density.NewPureState(qubit.Zero())
125113
s1 := density.New([]density.State{
126114
{0.1, qubit.Zero()},
127115
{0.9, qubit.One()},
@@ -136,7 +124,7 @@ func ExampleMatrix_IsHermite() {
136124
}
137125

138126
func ExampleMatrix_Trace() {
139-
s0 := density.NewZeroState()
127+
s0 := density.NewPureState(qubit.Zero())
140128
s1 := density.New([]density.State{
141129
{0.1, qubit.Zero()},
142130
{0.9, qubit.One()},
@@ -151,7 +139,7 @@ func ExampleMatrix_Trace() {
151139
}
152140

153141
func ExampleMatrix_Purity() {
154-
s0 := density.NewZeroState()
142+
s0 := density.NewPureState(qubit.Zero())
155143
s1 := density.New([]density.State{
156144
{0.1, qubit.Zero()},
157145
{0.9, qubit.One()},
@@ -233,16 +221,16 @@ func ExampleMatrix_PartialTrace_x8() {
233221
}
234222

235223
func ExampleMatrix_Depolarizing() {
236-
rho := density.NewZeroState()
224+
rho := density.NewPureState(qubit.Zero())
237225
fmt.Printf("0: %.2f\n", rho.Probability(qubit.Zero()))
238226
fmt.Printf("1: %.2f\n", rho.Probability(qubit.One()))
239227
fmt.Println()
240228

241229
// XrhoX = |1><1|, YrhoY = |1><1|, ZrhoZ = |0><0|
242230
// E(rho) = 0.7|0><0| + 0.1|1><1| + 0.1|1><1| + 0.1|0><0| = 0.8|0><0| + 0.2|1><1|
243-
dep := rho.Depolarizing(0.3, 0)
244-
fmt.Printf("0: %.2f\n", dep.Probability(qubit.Zero()))
245-
fmt.Printf("1: %.2f\n", dep.Probability(qubit.One()))
231+
erho := rho.Depolarizing(0.3, 0)
232+
fmt.Printf("0: %.2f\n", erho.Probability(qubit.Zero()))
233+
fmt.Printf("1: %.2f\n", erho.Probability(qubit.One()))
246234

247235
// Output:
248236
// 0: 1.00
@@ -253,7 +241,7 @@ func ExampleMatrix_Depolarizing() {
253241
}
254242

255243
func ExampleMatrix_ApplyChannel() {
256-
rho := density.NewZeroState(2)
244+
rho := density.NewPureState(qubit.Zero(2))
257245
s1 := rho.ApplyChannel(0.3, gate.X(), 0)
258246

259247
fmt.Printf("%.2f\n", s1.Probability(qubit.From("00")))
@@ -265,7 +253,7 @@ func ExampleMatrix_ApplyChannel() {
265253
}
266254

267255
func ExampleMatrix_ApplyChannel_qb1() {
268-
rho := density.NewZeroState(2)
256+
rho := density.NewPureState(qubit.Zero(2))
269257
s1 := rho.ApplyChannel(0.3, gate.X(), 1)
270258

271259
fmt.Printf("%.2f\n", s1.Probability(qubit.From("00")))
@@ -277,7 +265,7 @@ func ExampleMatrix_ApplyChannel_qb1() {
277265
}
278266

279267
func ExampleMatrix_BitFlip() {
280-
rho := density.NewZeroState()
268+
rho := density.NewPureState(qubit.Zero())
281269
x := rho.BitFlip(0.3, 0)
282270

283271
fmt.Printf("%.2f\n", x.Probability(qubit.Zero()))
@@ -313,7 +301,7 @@ func ExampleMatrix_PhaseFlip() {
313301
}
314302

315303
func ExampleMatrix_phaseAndBitPhaseFlip() {
316-
rho := density.NewZeroState()
304+
rho := density.NewPureState(qubit.Zero())
317305
y := rho.BitPhaseFlip(0.3, 0)
318306
z := rho.PhaseFlip(0.3, 0)
319307

@@ -430,11 +418,11 @@ func TestPartialTrace(t *testing.T) {
430418
}
431419

432420
cases := []struct {
433-
s []density.State
434-
cs []Case
421+
state []density.State
422+
cs []Case
435423
}{
436424
{
437-
s: []density.State{
425+
state: []density.State{
438426
{
439427
Probability: 1.0,
440428
Qubit: qubit.Zero(2),
@@ -458,7 +446,7 @@ func TestPartialTrace(t *testing.T) {
458446
},
459447
},
460448
{
461-
s: []density.State{
449+
state: []density.State{
462450
{
463451
Probability: 1.0,
464452
Qubit: qubit.One(2),
@@ -482,7 +470,7 @@ func TestPartialTrace(t *testing.T) {
482470
},
483471
},
484472
{
485-
s: []density.State{
473+
state: []density.State{
486474
{
487475
Probability: 1.0,
488476
Qubit: qubit.Zero(2).Apply(gate.H(2)),
@@ -506,7 +494,7 @@ func TestPartialTrace(t *testing.T) {
506494
},
507495
},
508496
{
509-
s: []density.State{
497+
state: []density.State{
510498
{
511499
Probability: 0.5,
512500
Qubit: qubit.Zero(2),
@@ -534,7 +522,7 @@ func TestPartialTrace(t *testing.T) {
534522
},
535523
},
536524
{
537-
s: []density.State{
525+
state: []density.State{
538526
{
539527
Probability: 0.5,
540528
Qubit: qubit.Zero(2).Apply(gate.H(2)),
@@ -562,7 +550,7 @@ func TestPartialTrace(t *testing.T) {
562550
},
563551
},
564552
{
565-
s: []density.State{
553+
state: []density.State{
566554
{
567555
Probability: 0.75,
568556
Qubit: qubit.Zero(2).Apply(gate.H(2)),
@@ -590,7 +578,7 @@ func TestPartialTrace(t *testing.T) {
590578
},
591579
},
592580
{
593-
s: []density.State{
581+
state: []density.State{
594582
{
595583
Probability: 0.25,
596584
Qubit: qubit.Zero(2).Apply(gate.H(2)),
@@ -618,7 +606,7 @@ func TestPartialTrace(t *testing.T) {
618606
},
619607
},
620608
{
621-
s: []density.State{
609+
state: []density.State{
622610
{
623611
Probability: 1.0,
624612
Qubit: qubit.Zero(3),
@@ -642,7 +630,7 @@ func TestPartialTrace(t *testing.T) {
642630
},
643631
},
644632
{
645-
s: []density.State{
633+
state: []density.State{
646634
{
647635
Probability: 1.0,
648636
Qubit: qubit.One(3),
@@ -666,7 +654,7 @@ func TestPartialTrace(t *testing.T) {
666654
},
667655
},
668656
{
669-
s: []density.State{
657+
state: []density.State{
670658
{
671659
Probability: 1.0,
672660
Qubit: qubit.Zero(3).Apply(gate.H(3)),
@@ -693,7 +681,7 @@ func TestPartialTrace(t *testing.T) {
693681

694682
for _, c := range cases {
695683
for _, s := range c.cs {
696-
got := density.New(c.s).PartialTrace(s.qb...)
684+
got := density.New(c.state).PartialTrace(s.qb...)
697685

698686
p, q := got.Dim()
699687
if p != len(s.want) || q != len(s.want) {

quantum/density/state.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ func Normalize(ensemble []State) []State {
1515
sum += s.Probability
1616
}
1717

18+
out := make([]State, len(ensemble))
1819
for i := range ensemble {
19-
ensemble[i].Probability = ensemble[i].Probability / sum
20+
out[i] = State{
21+
Probability: ensemble[i].Probability / sum,
22+
Qubit: ensemble[i].Qubit,
23+
}
2024
}
2125

22-
return ensemble
26+
return out
2327
}

0 commit comments

Comments
 (0)