Skip to content

Commit

Permalink
Add example
Browse files Browse the repository at this point in the history
  • Loading branch information
itsubaki committed Apr 25, 2024
1 parent a458194 commit 63da020
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
30 changes: 25 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

### Bell state

```golang
```go
qsim := q.New()

// generate qubits of |0>|0>
Expand Down Expand Up @@ -43,7 +43,7 @@ for _, s := range qsim.State() {

### Quantum teleportation

```golang
```go
qsim := q.New()

// generate qubits of |phi>|0>|0>
Expand Down Expand Up @@ -79,7 +79,7 @@ for _, s := range qsim.State(q1) {

### Error correction

```golang
```go
qsim := q.New()

q0 := qsim.New(1, 2) // (0.2, 0.8)
Expand Down Expand Up @@ -119,7 +119,7 @@ for _, s := range qsim.State(q0) {

### Grover's search algorithm

```golang
```go
qsim := q.New()

// initial state
Expand Down Expand Up @@ -171,7 +171,7 @@ for _, s := range qsim.State() {

### Shor's factoring algorithm

```golang
```go
N := 15
a := 7 // co-prime

Expand Down Expand Up @@ -228,6 +228,26 @@ for i := 0; i < 10; i++{

- In general, See [`cmd/shor`](./cmd/shor/main.go)

### Any quantum gate and its controlled gate

```go
h := gate.U(math.Pi/2, 0, math.Pi)
x := gate.U(math.Pi, 0, math.Pi)

qsim := q.New()
q0 := qsim.Zero()
q1 := qsim.Zero()

qsim.Apply(h, q0)
qsim.C(x, q0, q1)

for _, s := range qsim.State() {
fmt.Println(s)
}
// [00][ 0]( 0.7071 0.0000i): 0.5000
// [11][ 3]( 0.7071 0.0000i): 0.5000
```

## References

- Michael A. Nielsen, Issac L. Chuang. Quantum Computation and Quantum Information.
20 changes: 20 additions & 0 deletions q_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,26 @@ func Example_shorFactoring85() {
// N=85, a=14. p=5, q=17. s/r=7/16 ([0.0111]~0.438)
}

func Example_any() {
h := gate.U(math.Pi/2, 0, math.Pi)
x := gate.U(math.Pi, 0, math.Pi)

qsim := q.New()
q0 := qsim.Zero()
q1 := qsim.Zero()

qsim.Apply(h, q0)
qsim.C(x, q0, q1)

for _, s := range qsim.State() {
fmt.Println(s)
}

// Output:
// [00][ 0]( 0.7071 0.0000i): 0.5000
// [11][ 3]( 0.7071 0.0000i): 0.5000
}

func Example_top() {
N := 21
a := 11
Expand Down

0 comments on commit 63da020

Please sign in to comment.