gate

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 9, 2023 License: MIT Imports: 6 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func C

func C(u matrix.Matrix, n int, c int, t int) matrix.Matrix

C returns a controlled-u gate.

func CCNOT

func CCNOT(n, c0, c1, t int) matrix.Matrix

CCNOT returns a controlled-controlled-not gate.

func CNOT

func CNOT(n, c, t int) matrix.Matrix

CNOT returns a controlled-not gate.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q/quantum/gate"
)

func main() {
	g := gate.CNOT(2, 0, 1)
	for _, r := range g {
		fmt.Println(r)
	}

}
Output:

[(1+0i) (0+0i) (0+0i) (0+0i)]
[(0+0i) (1+0i) (0+0i) (0+0i)]
[(0+0i) (0+0i) (0+0i) (1+0i)]
[(0+0i) (0+0i) (1+0i) (0+0i)]

func CR

func CR(theta float64, n, c, t int) matrix.Matrix

CR returns a controlled-r gate.

func CS

func CS(n, c, t int) matrix.Matrix

CS returns a controlled-s gate.

func CZ

func CZ(n, c, t int) matrix.Matrix

CZ returns a controlled-z gate.

func Controlled

func Controlled(u matrix.Matrix, n int, c []int, t int) matrix.Matrix

Controlled returns a controlled-u gate.

func ControlledModExp2

func ControlledModExp2(n, a, j, N, c int, t []int) matrix.Matrix

ControlledModExp2 returns gate of controlled modular exponentiation operation. |j>|k> -> |j>|a**(2**j) * k mod N>. len(t) must be larger than log2(N).

Example
package main

import (
	"fmt"
	"strconv"

	"github.com/itsubaki/q/math/number"
	"github.com/itsubaki/q/quantum/gate"
)

func main() {
	n, a, j, N := 5, 7, 0, 15
	g := gate.ControlledModExp2(n, a, j, N, 0, []int{1, 2, 3, 4})

	f := fmt.Sprintf("%s%s%s", "%0", strconv.Itoa(n), "s")
	for i, r := range g.Transpose() {
		bin := fmt.Sprintf(f, strconv.FormatInt(int64(i), 2))
		if bin[:1] == "0" { // control qubit is |0>
			continue
		}

		// decimal number representation of target qubits
		k := number.Must(strconv.ParseInt(bin[1:], 2, 64))
		if k >= int64(N) {
			continue
		}

		for ii, e := range r {
			if e == complex(0, 0) {
				continue
			}

			// decimal number representation of a^2^j * k mod N
			a2jkmodNs := fmt.Sprintf(f, strconv.FormatInt(int64(ii), 2)[1:])
			a2jkmodN := number.Must(strconv.ParseInt(a2jkmodNs, 2, 64))
			got := (int64(number.ModExp2(a, j, N)) * k) % int64(N)

			fmt.Printf("%s:%s=%2d %s:%s=%2d %2d\n", bin[:1], bin[1:], k, bin[:1], a2jkmodNs[1:], a2jkmodN, got)
		}
	}

}
Output:

1:0000= 0 1:0000= 0  0
1:0001= 1 1:0111= 7  7
1:0010= 2 1:1110=14 14
1:0011= 3 1:0110= 6  6
1:0100= 4 1:1101=13 13
1:0101= 5 1:0101= 5  5
1:0110= 6 1:1100=12 12
1:0111= 7 1:0100= 4  4
1:1000= 8 1:1011=11 11
1:1001= 9 1:0011= 3  3
1:1010=10 1:1010=10 10
1:1011=11 1:0010= 2  2
1:1100=12 1:1001= 9  9
1:1101=13 1:0001= 1  1
1:1110=14 1:1000= 8  8

func ControlledNot

func ControlledNot(n int, c []int, t int) matrix.Matrix

ControlledNot returns a controlled-not gate.

func ControlledR

func ControlledR(theta float64, n int, c []int, t int) matrix.Matrix

ControlledR returns a controlled-r gate.

func ControlledS

func ControlledS(n int, c []int, t int) matrix.Matrix

ControlledS returns a controlled-s gate.

func ControlledZ

func ControlledZ(n int, c []int, t int) matrix.Matrix

ControlledZ returns a controlled-z gate.

func Empty

func Empty(n ...int) []matrix.Matrix

Empty returns a list of empty gate.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q/quantum/gate"
)

func main() {
	g0 := gate.Empty()
	fmt.Println(g0)

	g1 := gate.Empty(3)
	fmt.Println(g1)

}
Output:

[]
[[] [] []]

func Fredkin

func Fredkin(n, c, t0, t1 int) matrix.Matrix

Fredkin returns a fredkin gate.

func H

func H(n ...int) matrix.Matrix

H returns a Hadamard gate.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q/quantum/gate"
)

func main() {
	g := gate.H()
	for _, r := range g {
		fmt.Printf("%.4v\n", r)
	}

}
Output:

[(0.7071+0i) (0.7071+0i)]
[(0.7071+0i) (-0.7071+0i)]
Example (HH)
package main

import (
	"fmt"

	"github.com/itsubaki/q/quantum/gate"
)

func main() {
	g := gate.H(2)
	for _, r := range g {
		fmt.Printf("%.4v\n", r)
	}

}
Output:

[(0.5+0i) (0.5+0i) (0.5+0i) (0.5+0i)]
[(0.5+0i) (-0.5+0i) (0.5+0i) (-0.5+0i)]
[(0.5+0i) (0.5+0i) (-0.5+0i) (-0.5+0i)]
[(0.5+0i) (-0.5+0i) (-0.5+0i) (0.5-0i)]

func I

func I(n ...int) matrix.Matrix

I returns a identity gate.

func New

func New(v ...[]complex128) matrix.Matrix

New returns a new gate.

func QFT

func QFT(n int) matrix.Matrix

QFT returns a gate of Quantum Fourier Transform operation.

func R

func R(theta float64) matrix.Matrix

R returns a rotation gate. R(Theta(k)) = [[1, 0], [0, exp(2 * pi * i / 2**k)]].

func RX

func RX(theta float64) matrix.Matrix

RX returns a rotation gate around the X axis.

func RY

func RY(theta float64) matrix.Matrix

RY returns a rotation gate around the Y axis.

func RZ

func RZ(theta float64) matrix.Matrix

RZ returns a rotation gate around the Z axis.

func S

func S(n ...int) matrix.Matrix

S returns a S gate.

func Swap

func Swap(n, c, t int) matrix.Matrix

Swap returns a swap gate.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q/quantum/gate"
)

func main() {
	g := gate.Swap(2, 0, 1)
	for _, r := range g {
		fmt.Println(r)
	}

}
Output:

[(1+0i) (0+0i) (0+0i) (0+0i)]
[(0+0i) (0+0i) (1+0i) (0+0i)]
[(0+0i) (1+0i) (0+0i) (0+0i)]
[(0+0i) (0+0i) (0+0i) (1+0i)]

func T

func T(n ...int) matrix.Matrix

T returns a T gate.

func Theta

func Theta(k int) float64

Theta returns 2 * pi / 2**k

func Toffoli

func Toffoli(n, c0, c1, t int) matrix.Matrix

Toffoli returns a toffoli gate.

func U

func U(theta, phi, lambda float64) matrix.Matrix

U returns a unitary gate.

func X

func X(n ...int) matrix.Matrix

X returns a Pauli-X gate.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q/quantum/gate"
)

func main() {
	g := gate.X()
	for _, r := range g {
		fmt.Println(r)
	}

}
Output:

[(0+0i) (1+0i)]
[(1+0i) (0+0i)]
Example (XX)
package main

import (
	"fmt"

	"github.com/itsubaki/q/quantum/gate"
)

func main() {
	g := gate.X(2)
	for _, r := range g {
		fmt.Println(r)
	}

}
Output:

[(0+0i) (0+0i) (0+0i) (1+0i)]
[(0+0i) (0+0i) (1+0i) (0+0i)]
[(0+0i) (1+0i) (0+0i) (0+0i)]
[(1+0i) (0+0i) (0+0i) (0+0i)]

func Y

func Y(n ...int) matrix.Matrix

Y returns a Pauli-Y gate.

func Z

func Z(n ...int) matrix.Matrix

Z returns a Pauli-Z gate.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL