Skip to content

Commit

Permalink
Update quantum state
Browse files Browse the repository at this point in the history
  • Loading branch information
itsubaki committed Sep 16, 2024
1 parent 721bc46 commit 26f94a0
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 68 deletions.
10 changes: 5 additions & 5 deletions cmd/shor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func main() {
qsim.Measure(r1...)
print("measure reg1", qsim, r0, r1)

for _, state := range qsim.State(r0) {
i, m := state.Value()
for _, st := range qsim.State(r0) {
i, m := st.Int(), st.BinaryString()
s, r, d, ok := number.FindOrder(a, N, fmt.Sprintf("0.%s", m))
if !ok || number.IsOdd(r) {
fmt.Printf(" i=%3d: N=%d, a=%d, t=%d; s/r=%2d/%2d ([0.%v]~%.4f);\n", i, N, a, t, s, r, m, d)
Expand All @@ -107,9 +107,9 @@ func print(desc string, qsim *q.Q, reg ...any) {
fmt.Println(desc)

max := slices.Max(qsim.Probability())
for _, s := range qsim.State(reg...) {
p := strings.Repeat("*", int(s.Probability/max*32))
fmt.Printf("%s: %s\n", s, p)
for _, st := range qsim.State(reg...) {
p := strings.Repeat("*", int(st.Probability()/max*32))
fmt.Printf("%s: %s\n", st, p)
}

fmt.Println()
Expand Down
8 changes: 4 additions & 4 deletions q_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1249,7 +1249,7 @@ func Example_top() {
qsim.M(r1...)

top := func(s []qubit.State, n int) []qubit.State {
sort.Slice(s, func(i, j int) bool { return s[i].Probability > s[j].Probability })
sort.Slice(s, func(i, j int) bool { return s[i].Probability() > s[j].Probability() })
if len(s) < n {
return s
}
Expand Down Expand Up @@ -1318,13 +1318,13 @@ func TestEigenVector(t *testing.T) {

us := make(map[string]complex128)
for _, s := range qsim.State(r1) {
_, m := s.Value()
m := s.BinaryString()
if v, ok := us[m]; ok {
us[m] = v + s.Amplitude
us[m] = v + s.Amplitude()
continue
}

us[m] = s.Amplitude
us[m] = s.Amplitude()
}

if len(us) != len(c.bin) {
Expand Down
10 changes: 3 additions & 7 deletions quantum/qubit/qubit.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,17 +237,13 @@ func (q *Qubit) State(index ...[]int) []State {
continue
}

s := State{
Amplitude: amp,
Probability: math.Pow(cmplx.Abs(amp), 2),
}

b := fmt.Sprintf(f, strconv.FormatInt(int64(i), 2))
var bin []string
for _, idx := range index {
s.Add(take(b, idx))
bin = append(bin, take(b, idx))
}

state = append(state, s)
state = append(state, NewState(amp, bin...))
}

return state
Expand Down
72 changes: 48 additions & 24 deletions quantum/qubit/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package qubit

import (
"fmt"
"math"
"math/cmplx"
"strconv"

Expand All @@ -11,54 +12,77 @@ import (

// State is a quantum state.
type State struct {
Amplitude complex128
Probability float64
Int []int64
BinaryString []string
amp complex128
prob float64
binaryString []string
intValue []int64
}

func (s *State) Add(binary string) {
s.Int = append(s.Int, number.Must(strconv.ParseInt(binary, 2, 0)))
s.BinaryString = append(s.BinaryString, binary)
// NewState returns a new State.
func NewState(amp complex128, binary ...string) State {
var intv []int64
for _, bin := range binary {
intv = append(intv, number.Must(strconv.ParseInt(bin, 2, 0)))
}

return State{
amp: amp,
prob: math.Pow(cmplx.Abs(amp), 2),
binaryString: binary,
intValue: intv,
}
}

func (s State) Value(index ...int) (int64, string) {
func (s State) Probability() float64 {
return s.prob
}

func (s State) Amplitude() complex128 {
return s.amp
}

func (s State) BinaryString(index ...int) string {
var i int
if len(index) > 0 {
i = index[0]
}

return s.Int[i], s.BinaryString[i]
return s.binaryString[i]
}

func (s State) Int(index ...int) int64 {
var i int
if len(index) > 0 {
i = index[0]
}

return s.intValue[i]
}

func (s State) String() string {
return fmt.Sprintf("%v%3v(% .4f% .4fi): %.4f", s.BinaryString, s.Int, real(s.Amplitude), imag(s.Amplitude), s.Probability)
return fmt.Sprintf("%v%3v(% .4f% .4fi): %.4f",
s.binaryString,
s.intValue,
real(s.amp),
imag(s.amp),
s.prob,
)
}

// Equals returns true if s equals v.
// If eps is not given, epsilon.E13 is used.
func (s State) Equals(v State, eps ...float64) bool {
if len(s.Int) != len(v.Int) {
return false
}

if len(s.BinaryString) != len(v.BinaryString) {
if len(s.binaryString) != len(v.binaryString) {
return false
}

for i := range s.Int {
if s.Int[i] != v.Int[i] {
return false
}
}

for i := range s.BinaryString {
if s.BinaryString[i] != v.BinaryString[i] {
for i := range s.binaryString {
if s.binaryString[i] != v.binaryString[i] {
return false
}
}

return cmplx.Abs(s.Amplitude-v.Amplitude) < epsilon.E13(eps...)
return cmplx.Abs(s.amp-v.amp) < epsilon.E13(eps...)
}

// Equals returns true if s equals v.
Expand Down
45 changes: 17 additions & 28 deletions quantum/qubit/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,12 @@ import (
)

func ExampleState() {
s := qubit.State{
Amplitude: 1,
Probability: 1,
Int: []int64{4, 10, 8},
BinaryString: []string{"0100", "1010", "1000"},
}
s := qubit.NewState(1, []string{"0100", "1010", "1000"}...)

fmt.Println(s.Value())
fmt.Println(s.Value(0))
fmt.Println(s.Value(1))
fmt.Println(s.Value(2))
fmt.Println(s.Int(), s.BinaryString())
fmt.Println(s.Int(0), s.BinaryString(0))
fmt.Println(s.Int(1), s.BinaryString(1))
fmt.Println(s.Int(2), s.BinaryString(2))
fmt.Println(s.String())

// Output:
Expand All @@ -36,35 +31,29 @@ func TestState_Equals(t *testing.T) {
want bool
}{
{
qubit.State{Int: []int64{1}},
qubit.NewState(complex(1, 0)),
qubit.State{},
false,
},
{
qubit.State{Int: []int64{1}},
qubit.State{Int: []int64{1}, BinaryString: []string{"001"}},
qubit.NewState(complex(1, 0), "001"),
qubit.NewState(complex(1, 0), "100"),
false,
},
{
qubit.State{Int: []int64{1}, BinaryString: []string{"001"}, Amplitude: complex(1, 1)},
qubit.State{Int: []int64{1}, BinaryString: []string{"001"}, Amplitude: complex(1, 2)},
qubit.NewState(complex(0, 1), "001"),
qubit.NewState(complex(1, 0), "001"),
false,
},
{
qubit.State{Int: []int64{1}, BinaryString: []string{"001"}, Amplitude: complex(1, 1)},
qubit.State{Int: []int64{1}, BinaryString: []string{"001"}, Amplitude: complex(1, 1)},
true,
},
{
qubit.State{Int: []int64{1}, BinaryString: []string{"001"}, Amplitude: complex(1, 1)},
qubit.State{Int: []int64{1}, BinaryString: []string{"002"}, Amplitude: complex(1, 1)},
qubit.NewState(complex(1, 0), "001", "010"),
qubit.NewState(complex(1, 0), "001"),
false,
},

{
qubit.State{Int: []int64{1}, BinaryString: []string{"001"}, Amplitude: complex(1, 1)},
qubit.State{Int: []int64{2}, BinaryString: []string{"001"}, Amplitude: complex(1, 1)},
false,
qubit.NewState(complex(1, 0), "001", "010"),
qubit.NewState(complex(1, 0), "001", "010"),
true,
},
}

Expand Down Expand Up @@ -93,8 +82,8 @@ func TestEquals(t *testing.T) {
false,
},
{
[]qubit.State{{Amplitude: complex(1, 0)}},
[]qubit.State{{Amplitude: complex(0, 1)}},
[]qubit.State{qubit.NewState(complex(1, 0))},
[]qubit.State{qubit.NewState(complex(0, 1))},
false,
},
}
Expand Down

0 comments on commit 26f94a0

Please sign in to comment.