-
Notifications
You must be signed in to change notification settings - Fork 1
/
register.go
63 lines (51 loc) · 1.01 KB
/
register.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package machine
import (
"fmt"
"github.com/Quant-Team/qvm/pkg/circuit"
"github.com/Quant-Team/qvm/pkg/config"
)
var _ Register = ®ister{}
type Register interface {
Probability() [][]float64
Measure() []circuit.Qubiter
}
type register struct {
q []circuit.Qubiter
}
type Bit int
var (
One Bit = 1
Zero Bit = 0
)
var (
ErrInvalidCountQubit = fmt.Errorf("Invalid count qubit in config and init state")
)
func (r *register) Probability() [][]float64 {
t := [][]float64{}
for _, q := range r.q {
t = append(t, q.Probability())
}
return t
}
func (r *register) Measure() []circuit.Qubiter {
for i := range r.q {
r.q[i] = r.q[i].Measure()
}
return r.q
}
func NewRegister(cfg *config.Register, state []Bit) (r *register, err error) {
if len(state) != cfg.QubitCount {
err = ErrInvalidCountQubit
return
}
r = ®ister{q: []circuit.Qubiter{}}
for _, b := range state {
switch b {
case One:
r.q = append(r.q, circuit.One())
case Zero:
r.q = append(r.q, circuit.Zero())
}
}
return
}