-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/itsubaki/q
- Loading branch information
Showing
17 changed files
with
338 additions
and
313 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,8 +20,6 @@ secret | |
.DS_Store | ||
|
||
# test | ||
cover.html | ||
cover.out | ||
coverage.txt | ||
coverage.html | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package number | ||
|
||
import "fmt" | ||
|
||
// Log2 returns the base 2 logarithm of N. | ||
// N must be a power of 2. | ||
func Log2(N int) (int, error) { | ||
if N == 1 { | ||
return 0, nil | ||
} | ||
|
||
var n int = 1 | ||
for { | ||
if N%2 != 0 { | ||
return -1, fmt.Errorf("N must be a power of 2") | ||
} | ||
|
||
if N/2 == 1 { | ||
break | ||
} | ||
|
||
N = N / 2 | ||
n++ | ||
} | ||
|
||
return n, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package number_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/itsubaki/q/math/number" | ||
) | ||
|
||
func ExampleLog2() { | ||
fmt.Println(number.Log2(8)) | ||
fmt.Println(number.Log2(32)) | ||
fmt.Println(number.Log2(15)) | ||
fmt.Println(number.Log2(20)) | ||
|
||
// Output: | ||
// 3 <nil> | ||
// 5 <nil> | ||
// -1 N must be a power of 2 | ||
// -1 N must be a power of 2 | ||
} | ||
|
||
func TestLog2(t *testing.T) { | ||
cases := []struct { | ||
n int | ||
want int | ||
}{ | ||
{1, 0}, | ||
{2, 1}, | ||
{4, 2}, | ||
{8, 3}, | ||
{16, 4}, | ||
{32, 5}, | ||
{64, 6}, | ||
{128, 7}, | ||
{256, 8}, | ||
{512, 9}, | ||
{1024, 10}, | ||
{2048, 11}, | ||
{4096, 12}, | ||
{8192, 13}, | ||
} | ||
|
||
for _, c := range cases { | ||
got := number.Must(number.Log2(c.n)) | ||
if got != c.want { | ||
t.Errorf("got=%v want=%d", got, c.want) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package number_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/itsubaki/q/math/number" | ||
) | ||
|
||
func TestMustPanic(t *testing.T) { | ||
defer func() { | ||
if rec := recover(); rec != nil { | ||
err, ok := rec.(error) | ||
if !ok { | ||
t.Fail() | ||
} | ||
|
||
if err.Error() != "something went wrong" { | ||
t.Fail() | ||
} | ||
} | ||
}() | ||
|
||
number.Must(-1, fmt.Errorf("something went wrong")) | ||
t.Fail() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package density | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
|
||
"github.com/itsubaki/q/math/matrix" | ||
"github.com/itsubaki/q/math/number" | ||
"github.com/itsubaki/q/quantum/gate" | ||
) | ||
|
||
// Flip returns the flip channel. | ||
func Flip(p float64, m matrix.Matrix) (matrix.Matrix, matrix.Matrix, error) { | ||
if p < 0 || p > 1 { | ||
return nil, nil, fmt.Errorf("p must be 0 <= p =< 1. p=%v", p) | ||
} | ||
|
||
d, _ := m.Dimension() | ||
n := number.Must(number.Log2(d)) | ||
|
||
e0 := gate.I(n).Mul(complex(math.Sqrt(p), 0)) | ||
e1 := m.Mul(complex(math.Sqrt(1-p), 0)) | ||
return e0, e1, nil | ||
} | ||
|
||
// BitFlip returns the bit flip channel. | ||
func BitFlip(p float64) (matrix.Matrix, matrix.Matrix, error) { | ||
return Flip(p, gate.X()) | ||
} | ||
|
||
// PhaseFlip returns the phase flip channel. | ||
func PhaseFlip(p float64) (matrix.Matrix, matrix.Matrix, error) { | ||
return Flip(p, gate.Z()) | ||
} | ||
|
||
// BitPhaseFlip returns the bit-phase flip channel. | ||
func BitPhaseFlip(p float64) (matrix.Matrix, matrix.Matrix, error) { | ||
return Flip(p, gate.Y()) | ||
} |
Oops, something went wrong.