Skip to content

Commit 8c9f1b6

Browse files
committed
Add IsValid
1 parent 0d7d4cd commit 8c9f1b6

File tree

2 files changed

+102
-13
lines changed

2 files changed

+102
-13
lines changed

quantum/density/matrix.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,27 @@ func NewPureState(qb *qubit.Qubit) *Matrix {
4141
})
4242
}
4343

44+
func IsValid(ensemble []State) bool {
45+
if len(ensemble) == 0 {
46+
return false
47+
}
48+
49+
n := ensemble[0].Qubit.Dim()
50+
for _, s := range ensemble {
51+
if s.Qubit.Dim() != n {
52+
return false
53+
}
54+
}
55+
56+
for _, s := range ensemble {
57+
if s.Probability < 0 {
58+
return false
59+
}
60+
}
61+
62+
return true
63+
}
64+
4465
// At returns a value of matrix at (i,j).
4566
func (m *Matrix) At(i, j int) complex128 {
4667
return m.rho.At(i, j)

quantum/density/matrix_test.go

Lines changed: 81 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -430,11 +430,11 @@ func TestPartialTrace(t *testing.T) {
430430
}
431431

432432
cases := []struct {
433-
state []density.State
434-
cs []Case
433+
s []density.State
434+
cs []Case
435435
}{
436436
{
437-
state: []density.State{
437+
s: []density.State{
438438
{
439439
Probability: 1.0,
440440
Qubit: qubit.Zero(2),
@@ -458,7 +458,7 @@ func TestPartialTrace(t *testing.T) {
458458
},
459459
},
460460
{
461-
state: []density.State{
461+
s: []density.State{
462462
{
463463
Probability: 1.0,
464464
Qubit: qubit.One(2),
@@ -482,7 +482,7 @@ func TestPartialTrace(t *testing.T) {
482482
},
483483
},
484484
{
485-
state: []density.State{
485+
s: []density.State{
486486
{
487487
Probability: 1.0,
488488
Qubit: qubit.Zero(2).Apply(gate.H(2)),
@@ -506,7 +506,7 @@ func TestPartialTrace(t *testing.T) {
506506
},
507507
},
508508
{
509-
state: []density.State{
509+
s: []density.State{
510510
{
511511
Probability: 0.5,
512512
Qubit: qubit.Zero(2),
@@ -534,7 +534,7 @@ func TestPartialTrace(t *testing.T) {
534534
},
535535
},
536536
{
537-
state: []density.State{
537+
s: []density.State{
538538
{
539539
Probability: 0.5,
540540
Qubit: qubit.Zero(2).Apply(gate.H(2)),
@@ -562,7 +562,7 @@ func TestPartialTrace(t *testing.T) {
562562
},
563563
},
564564
{
565-
state: []density.State{
565+
s: []density.State{
566566
{
567567
Probability: 0.75,
568568
Qubit: qubit.Zero(2).Apply(gate.H(2)),
@@ -590,7 +590,7 @@ func TestPartialTrace(t *testing.T) {
590590
},
591591
},
592592
{
593-
state: []density.State{
593+
s: []density.State{
594594
{
595595
Probability: 0.25,
596596
Qubit: qubit.Zero(2).Apply(gate.H(2)),
@@ -618,7 +618,7 @@ func TestPartialTrace(t *testing.T) {
618618
},
619619
},
620620
{
621-
state: []density.State{
621+
s: []density.State{
622622
{
623623
Probability: 1.0,
624624
Qubit: qubit.Zero(3),
@@ -642,7 +642,7 @@ func TestPartialTrace(t *testing.T) {
642642
},
643643
},
644644
{
645-
state: []density.State{
645+
s: []density.State{
646646
{
647647
Probability: 1.0,
648648
Qubit: qubit.One(3),
@@ -666,7 +666,7 @@ func TestPartialTrace(t *testing.T) {
666666
},
667667
},
668668
{
669-
state: []density.State{
669+
s: []density.State{
670670
{
671671
Probability: 1.0,
672672
Qubit: qubit.Zero(3).Apply(gate.H(3)),
@@ -693,7 +693,7 @@ func TestPartialTrace(t *testing.T) {
693693

694694
for _, c := range cases {
695695
for _, s := range c.cs {
696-
got := density.New(c.state).PartialTrace(s.qb...)
696+
got := density.New(c.s).PartialTrace(s.qb...)
697697

698698
p, q := got.Dim()
699699
if p != len(s.want) || q != len(s.want) {
@@ -757,3 +757,71 @@ func TestApply(t *testing.T) {
757757
}
758758
}
759759
}
760+
761+
func TestIsValid(t *testing.T) {
762+
type Case struct {
763+
s []density.State
764+
want bool
765+
}
766+
767+
cases := []Case{
768+
{
769+
s: []density.State{},
770+
},
771+
{
772+
s: []density.State{
773+
{
774+
Probability: 1,
775+
Qubit: qubit.Zero(),
776+
},
777+
},
778+
want: true,
779+
},
780+
{
781+
s: []density.State{
782+
{
783+
Probability: 0.5,
784+
Qubit: qubit.Zero(),
785+
},
786+
{
787+
Probability: 0.5,
788+
Qubit: qubit.One(),
789+
},
790+
},
791+
want: true,
792+
},
793+
{
794+
s: []density.State{
795+
{
796+
Probability: -0.1,
797+
Qubit: qubit.Zero(),
798+
},
799+
{
800+
Probability: 1.1,
801+
Qubit: qubit.One(),
802+
},
803+
},
804+
want: false,
805+
},
806+
{
807+
s: []density.State{
808+
{
809+
Probability: 0.5,
810+
Qubit: qubit.Zero(),
811+
},
812+
{
813+
Probability: 0.5,
814+
Qubit: qubit.Zero(2),
815+
},
816+
},
817+
want: false,
818+
},
819+
}
820+
821+
for _, c := range cases {
822+
got := density.IsValid(c.s)
823+
if got != c.want {
824+
t.Errorf("got=%v, want=%v", got, c.want)
825+
}
826+
}
827+
}

0 commit comments

Comments
 (0)