q

package module
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2026 License: MIT Imports: 5 Imported by: 13 modules, 14 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

q

PkgGoDev tests

A quantum computing simulator in Go using only the standard library.

Installation

go get github.com/itsubaki/q@latest

Examples

Bell State
qsim := q.New()

// generate qubits in the state |0>|0>
q0 := qsim.Zero()
q1 := qsim.Zero()

// apply the quantum circuit
qsim.H(q0)
qsim.CNOT(q0, q1)

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

// [00] ( 0.7071 0.0000i): 0.5000
// [11] ( 0.7071 0.0000i): 0.5000

m0 := qsim.Measure(q0)
m1 := qsim.Measure(q1)
fmt.Println(m0.Equal(m1)) // true

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

// [00] ( 1.0000 0.0000i): 1.0000
// or
// [11] ( 1.0000 0.0000i): 1.0000
Quantum Teleportation
qsim := q.New()

// generate qubits in the state |psi>|0>|0>
psi := qsim.New(1, 2)
q0 := qsim.Zero()
q1 := qsim.Zero()

// |psi> is normalized. |psi> = a|0> + b|1>, where |a|^2 = 0.2 and |b|^2 = 0.8
for _, s := range qsim.State(psi) {
	fmt.Println(s)
}

// [0] ( 0.4472 0.0000i): 0.2000
// [1] ( 0.8944 0.0000i): 0.8000

qsim.H(q0)
qsim.CNOT(q0, q1)
qsim.CNOT(psi, q0)
qsim.H(psi)

// Alice sends mz and mx to Bob
mz := qsim.Measure(psi)
mx := qsim.Measure(q0)

// Bob applies X and Z
qsim.CondX(mx.IsOne(), q1)
qsim.CondZ(mz.IsOne(), q1)

// Bob obtains the |psi> state in q1
for _, s := range qsim.State(q1) {
	fmt.Println(s)
}

// [0] ( 0.4472 0.0000i): 0.2000
// [1] ( 0.8944 0.0000i): 0.8000
Grover's Search Algorithm
qsim := q.New()

// initial state
q0 := qsim.Zero()
q1 := qsim.Zero()
q2 := qsim.Zero()
q3 := qsim.One()

// superposition
qsim.H(q0, q1, q2)

// prepare minus state for phase kickback
qsim.H(q3)

// iterations
N := number.Pow(2, qsim.NumQubits()-1)
R := int(math.Pi / 4 * math.Sqrt(float64(N)))
for range R {
	// oracle for |110>|x>
	qsim.X(q2)
	qsim.CCCNOT(q0, q1, q2, q3)
	qsim.X(q2)

	// diffuser
	qsim.H(q0, q1, q2)
	qsim.X(q0, q1, q2)
	qsim.CCZ(q0, q1, q2)
	qsim.X(q0, q1, q2)
	qsim.H(q0, q1, q2)
}

for _, s := range qsim.State([]q.Qubit{q0, q1, q2}, q3) {
	fmt.Println(s)
}

// [000 0] (-0.0625 0.0000i): 0.0039
// [000 1] ( 0.0625 0.0000i): 0.0039
// [001 0] (-0.0625 0.0000i): 0.0039
// [001 1] ( 0.0625 0.0000i): 0.0039
// [010 0] (-0.0625 0.0000i): 0.0039
// [010 1] ( 0.0625 0.0000i): 0.0039
// [011 0] (-0.0625 0.0000i): 0.0039
// [011 1] ( 0.0625 0.0000i): 0.0039
// [100 0] (-0.0625 0.0000i): 0.0039
// [100 1] ( 0.0625 0.0000i): 0.0039
// [101 0] (-0.0625 0.0000i): 0.0039
// [101 1] ( 0.0625 0.0000i): 0.0039
// [110 0] ( 0.6875 0.0000i): 0.4727 (answer)
// [110 1] (-0.6875 0.0000i): 0.4727 (answer)
// [111 0] (-0.0625 0.0000i): 0.0039
// [111 1] ( 0.0625 0.0000i): 0.0039
Shor's Factoring Algorithm
N := 15
a := 7 // co-prime with N

for i := range 10 {
	qsim := q.New()

	// initial state
	q0 := qsim.Zero()
	q1 := qsim.Zero()
	q2 := qsim.Zero()

	q3 := qsim.Zero()
	q4 := qsim.Zero()
	q5 := qsim.Zero()
	q6 := qsim.One()

	// superposition
	qsim.H(q0, q1, q2)

	// Controlled-U
	qsim.CNOT(q2, q4)
	qsim.CNOT(q2, q5)
	F.Fredkin(qsim, q1, q3, q5)
	F.Fredkin(qsim, q1, q4, q6)

	// inverse QFT
	F.Swap(qsim, q0, q2)
	F.InvQFT(qsim, q0, q1, q2)

	// measure q0, q1, q2
	m := qsim.Measure(q0, q1, q2)
	k := number.MustParseInt(m.BinaryString())
	phi := number.Ldexp(k, -m.NumQubits())

	// find s/r. 0.010 -> 0.25 -> 1/4, 0.110 -> 0.75 -> 3/4, ...
	s, r, d, ok := number.FindOrder(a, N, phi)
	if !ok || number.IsOdd(r) {
		continue
	}

	// gcd(a^(r/2)-1, N), gcd(a^(r/2)+1, N)
	p0 := number.GCD(number.Pow(a, r/2)-1, N)
	p1 := number.GCD(number.Pow(a, r/2)+1, N)
	if number.IsTrivial(N, p0, p1) {
		continue
  	}

	// output result
	fmt.Printf("i=%d: N=%d, a=%d. p=%v, q=%v. s/r=%d/%d ([0.%v]~%.3f)\n", i, N, a, p0, p1, s, r, m, d)
}

// i=2: N=15, a=7. p=3, q=5. s/r=1/4 ([0.010]~0.250)
Building Arbitrary Single-Qubit and Controlled Gates
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.G(h, q0)
qsim.C(x, q0, q1)

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

// [00] ( 0.7071 0.0000i): 0.5000
// [11] ( 0.7071 0.0000i): 0.5000

Reference

  • Nielsen, M. A., & Chuang, I. L. Quantum Computation and Quantum Information. 10th Anniversary ed., Cambridge University Press, 2010.

Documentation

Overview

Example (Any)
package main

import (
	"fmt"
	"math"

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

func main() {
	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.G(h, q0)
	qsim.C(x, q0, q1)

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

}
Output:
[00] ( 0.7071 0.0000i): 0.5000
[11] ( 0.7071 0.0000i): 0.5000
Example (Bell)
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	q0 := qsim.Zero()
	q1 := qsim.Zero()

	qsim.H(q0)
	qsim.CNOT(q0, q1)

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

	m0 := qsim.Measure(q0)
	m1 := qsim.Measure(q1)
	fmt.Println(m0.Equal(m1))

}
Output:
[00] ( 0.7071 0.0000i): 0.5000
[11] ( 0.7071 0.0000i): 0.5000
true
Example (DeutschJozsa)
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	constant := func(qsim *q.Q, q0, q1 q.Qubit) string {
		return "Constant"
	}

	balanced := func(qsim *q.Q, q0, q1 q.Qubit) string {
		qsim.CNOT(q0, q1)
		return "Balanced"
	}

	deutschJozsa := func(oracle func(qsim *q.Q, q0, q1 q.Qubit) string) (string, int) {
		qsim := q.New()
		q0 := qsim.Zero()
		q1 := qsim.One()

		qsim.H(q0, q1)
		ans := oracle(qsim, q0, q1)
		qsim.H(q0)

		if qsim.M(q0).IsZero() {
			return ans, 0
		}

		return ans, 1
	}

	fmt.Println(deutschJozsa(constant))
	fmt.Println(deutschJozsa(balanced))

}
Output:
Constant 0
Balanced 1
Example (ECC)
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	q0 := qsim.New(1, 2)
	q1 := qsim.Zero()
	q2 := qsim.Zero()

	qsim.CNOT(q0, q1)
	qsim.CNOT(q0, q2)

	fmt.Println("encoded:")
	for _, s := range qsim.State(q0, []q.Qubit{q1, q2}) {
		fmt.Println(s)
	}

	// error: the first qubit is flipped
	qsim.X(q0)

	fmt.Println("flipped:")
	for _, s := range qsim.State(q0, []q.Qubit{q1, q2}) {
		fmt.Println(s)
	}

	q3 := qsim.Zero()
	q4 := qsim.Zero()

	// error correction
	qsim.CNOT(q0, q3)
	qsim.CNOT(q1, q3)
	qsim.CNOT(q1, q4)
	qsim.CNOT(q2, q4)

	m3 := qsim.Measure(q3)
	m4 := qsim.Measure(q4)

	qsim.CondX(m3.IsOne() && m4.IsZero(), q0)
	qsim.CondX(m3.IsOne() && m4.IsOne(), q1)
	qsim.CondX(m3.IsZero() && m4.IsOne(), q2)

	fmt.Println("corrected:")
	for _, s := range qsim.State(q0, []q.Qubit{q1, q2}) {
		fmt.Println(s)
	}

}
Output:
encoded:
[0 00] ( 0.4472 0.0000i): 0.2000
[1 11] ( 0.8944 0.0000i): 0.8000
flipped:
[0 11] ( 0.8944 0.0000i): 0.8000
[1 00] ( 0.4472 0.0000i): 0.2000
corrected:
[0 00] ( 0.4472 0.0000i): 0.2000
[1 11] ( 0.8944 0.0000i): 0.8000
Example (GateTeleportation)
package main

import (
	"fmt"

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

func main() {
	qsim := q.New()
	psi := qsim.New(1, 2)
	a := qsim.Zero()

	qsim.H(a)
	qsim.T(a) // magic state

	qsim.CNOT(a, psi)
	m0 := qsim.Measure(psi)
	qsim.Cond(m0.IsOne(), gate.X(), a)
	qsim.Cond(m0.IsOne(), gate.S(), a)
	s0 := qsim.State(a)

	{
		qs := q.New()
		qb := qs.New(1, 2)
		qs.T(qb)
		s1 := qs.State(qb)

		fmt.Println(qubit.EqualUpToGlobalPhase(s0, s1))
	}

}
Output:
true
Example (Grover)
package main

import (
	"fmt"
	"math"

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

func main() {
	qsim := q.New()
	q0 := qsim.Zero()
	q1 := qsim.Zero()
	q2 := qsim.Zero()
	q3 := qsim.One()

	// superposition
	qsim.H(q0, q1, q2)

	// prepare minus state for phase kickback
	qsim.H(q3)

	N := number.Pow(2, qsim.NumQubits()-1)
	R := int(math.Pi / 4 * math.Sqrt(float64(N)))
	for range R {
		// oracle for |110>|x>
		qsim.X(q2)
		qsim.CCCNOT(q0, q1, q2, q3)
		qsim.X(q2)

		// diffuser
		qsim.H(q0, q1, q2)
		qsim.X(q0, q1, q2)
		qsim.CCZ(q0, q1, q2)
		qsim.X(q0, q1, q2)
		qsim.H(q0, q1, q2)
	}

	for _, s := range qsim.State([]q.Qubit{q0, q1, q2}, q3) {
		fmt.Println(s)
	}

}
Output:
[000 0] (-0.0625 0.0000i): 0.0039
[000 1] ( 0.0625 0.0000i): 0.0039
[001 0] (-0.0625 0.0000i): 0.0039
[001 1] ( 0.0625 0.0000i): 0.0039
[010 0] (-0.0625 0.0000i): 0.0039
[010 1] ( 0.0625 0.0000i): 0.0039
[011 0] (-0.0625 0.0000i): 0.0039
[011 1] ( 0.0625 0.0000i): 0.0039
[100 0] (-0.0625 0.0000i): 0.0039
[100 1] ( 0.0625 0.0000i): 0.0039
[101 0] (-0.0625 0.0000i): 0.0039
[101 1] ( 0.0625 0.0000i): 0.0039
[110 0] ( 0.6875 0.0000i): 0.4727
[110 1] (-0.6875 0.0000i): 0.4727
[111 0] (-0.0625 0.0000i): 0.0039
[111 1] ( 0.0625 0.0000i): 0.0039
Example (Qft)
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	q0 := qsim.Zero()
	q1 := qsim.Zero()
	q2 := qsim.One()

	qsim.H(q0)
	qsim.CR(q.Theta(2), q0, q1)
	qsim.CR(q.Theta(3), q0, q2)

	qsim.H(q1)
	qsim.CR(q.Theta(2), q1, q2)

	qsim.H(q2)

	// swap
	qsim.CNOT(q0, q2)
	qsim.CNOT(q2, q0)
	qsim.CNOT(q0, q2)

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

}
Output:
[000] ( 0.3536 0.0000i): 0.1250
[001] ( 0.2500 0.2500i): 0.1250
[010] ( 0.0000 0.3536i): 0.1250
[011] (-0.2500 0.2500i): 0.1250
[100] (-0.3536 0.0000i): 0.1250
[101] (-0.2500-0.2500i): 0.1250
[110] ( 0.0000-0.3536i): 0.1250
[111] ( 0.2500-0.2500i): 0.1250
Example (QuantumTeleportation)
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	psi := qsim.New(1, 2)
	q0 := qsim.Zero()
	q1 := qsim.Zero()

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

	qsim.H(q0)
	qsim.CNOT(q0, q1)
	qsim.CNOT(psi, q0)
	qsim.H(psi)

	mz := qsim.Measure(psi)
	mx := qsim.Measure(q0)

	qsim.CondX(mx.IsOne(), q1)
	qsim.CondZ(mz.IsOne(), q1)

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

}
Output:
[0] ( 0.4472 0.0000i): 0.2000
[1] ( 0.8944 0.0000i): 0.8000
[0] ( 0.4472 0.0000i): 0.2000
[1] ( 0.8944 0.0000i): 0.8000
Example (SuperDenseCoding)
package main

import (
	"fmt"

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

func main() {
	sdc := func(g *matrix.Matrix) string {
		qsim := q.New()
		q0 := qsim.Zero()
		q1 := qsim.Zero()

		qsim.H(q0)
		qsim.CNOT(q0, q1)

		// encode
		qsim.G(g, q0)

		// decode
		qsim.CNOT(q0, q1)
		qsim.H(q0)

		// measure
		return qsim.M(q0, q1).BinaryString()
	}

	fmt.Printf("I : %v\n", sdc(gate.I()))
	fmt.Printf("X : %v\n", sdc(gate.X()))
	fmt.Printf("Z : %v\n", sdc(gate.Z()))
	fmt.Printf("ZX: %v\n", sdc(gate.Z().Apply(gate.X())))

}
Output:
I : 00
X : 01
Z : 10
ZX: 11

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Index

func Index(qb ...Qubit) []int

Index returns the indices of the given qubits.

func Theta added in v0.0.3

func Theta(k int) float64

Theta returns 2*pi/2^k.

func Top added in v0.0.10

func Top(s []qubit.State, n int) []qubit.State

Top returns the top n states by probability. If n < 0, it returns s.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	qb := qsim.Zeros(3)

	qsim.H(qb...)
	for _, s := range q.Top(qsim.State(), 2) {
		fmt.Println(s)
	}

}
Output:
[000] ( 0.3536 0.0000i): 0.1250
[001] ( 0.3536 0.0000i): 0.1250
Example (All)
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	qb := qsim.Zeros(3)

	qsim.H(qb...)
	for _, s := range q.Top(qsim.State(), -1) {
		fmt.Println(s)
	}

}
Output:
[000] ( 0.3536 0.0000i): 0.1250
[001] ( 0.3536 0.0000i): 0.1250
[010] ( 0.3536 0.0000i): 0.1250
[011] ( 0.3536 0.0000i): 0.1250
[100] ( 0.3536 0.0000i): 0.1250
[101] ( 0.3536 0.0000i): 0.1250
[110] ( 0.3536 0.0000i): 0.1250
[111] ( 0.3536 0.0000i): 0.1250

Types

type Q

type Q struct {
	// contains filtered or unexported fields
}

Q is a quantum computing simulator.

func New

func New() *Q

New returns a new quantum computing simulator.

func (*Q) Amplitude

func (q *Q) Amplitude() []complex128

Amplitude returns the amplitudes of the qubits.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	q0 := qsim.Zero()
	q1 := qsim.Zero()

	qsim.H(q0)
	qsim.CNOT(q0, q1)

	for _, a := range qsim.Amplitude() {
		fmt.Printf("%.4f\n", a)
	}

}
Output:
(0.7071+0.0000i)
(0.0000+0.0000i)
(0.0000+0.0000i)
(0.7071+0.0000i)

func (*Q) Apply

func (q *Q) Apply(g ...*matrix.Matrix) *Q

Apply applies a list of gates to the qubits.

Example
package main

import (
	"fmt"

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

func main() {
	qsim := q.New()
	q0 := qsim.Zero()
	q1 := qsim.Zero()

	n := qsim.NumQubits()
	h := gate.H()
	cnot := gate.CNOT(n, q0.Index(), q1.Index())

	qsim.G(h, q0)
	qsim.Apply(cnot)

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

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

func (*Q) C

func (q *Q) C(g *matrix.Matrix, control, target Qubit) *Q

C applies a controlled operation with g.

Example
package main

import (
	"fmt"

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

func main() {
	qsim := q.New()
	q0 := qsim.Zero()
	q1 := qsim.Zero()

	qsim.H(q0)
	qsim.C(gate.X(), q0, q1) // qsim.CNOT(q0, q1)

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

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

func (*Q) CCCNOT

func (q *Q) CCCNOT(control0, control1, control2, target Qubit) *Q

CCCNOT applies the CCCNOT gate.

func (*Q) CCNOT

func (q *Q) CCNOT(control0, control1, target Qubit) *Q

CCNOT applies the CCNOT gate.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	q0 := qsim.Zero()
	q1 := qsim.Zero()
	q2 := qsim.Zero()

	qsim.H(q0)
	qsim.H(q1)
	qsim.CCNOT(q0, q1, q2)

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

}
Output:
[000] ( 0.5000 0.0000i): 0.2500
[010] ( 0.5000 0.0000i): 0.2500
[100] ( 0.5000 0.0000i): 0.2500
[111] ( 0.5000 0.0000i): 0.2500

func (*Q) CCZ

func (q *Q) CCZ(control0, control1, target Qubit) *Q

CCZ applies the CCZ gate with two controls.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	q0 := qsim.Zero()
	q1 := qsim.Zero()
	q2 := qsim.One()

	qsim.H(q0)
	qsim.H(q1)
	qsim.CCZ(q0, q1, q2)

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

}
Output:
[001] ( 0.5000 0.0000i): 0.2500
[011] ( 0.5000 0.0000i): 0.2500
[101] ( 0.5000 0.0000i): 0.2500
[111] (-0.5000 0.0000i): 0.2500

func (*Q) CNOT

func (q *Q) CNOT(control, target Qubit) *Q

CNOT applies the CNOT gate.

func (*Q) CR

func (q *Q) CR(theta float64, control, target Qubit) *Q

CR applies the controlled-R gate.

func (*Q) CU added in v0.0.8

func (q *Q) CU(theta, phi, lambda float64, control, target Qubit) *Q

CU applies a controlled unitary operation.

Example
package main

import (
	"fmt"
	"math"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	q0 := qsim.Zero()
	q1 := qsim.Zero()

	qsim.H(q0)
	qsim.CU(math.Pi, 0, math.Pi, q0, q1)

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

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

func (*Q) CX added in v0.0.8

func (q *Q) CX(control, target Qubit) *Q

CX applies the CNOT gate.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	q0 := qsim.Zero()
	q1 := qsim.Zero()

	qsim.H(q0)
	qsim.CX(q0, q1)

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

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

func (*Q) CZ

func (q *Q) CZ(control, target Qubit) *Q

CZ applies the controlled-Z gate.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	q0 := qsim.Zero()
	q1 := qsim.Zero()

	qsim.H(q0)
	qsim.H(q1)
	qsim.CZ(q0, q1)

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

}
Output:
[00] ( 0.5000 0.0000i): 0.2500
[01] ( 0.5000 0.0000i): 0.2500
[10] ( 0.5000 0.0000i): 0.2500
[11] (-0.5000 0.0000i): 0.2500

func (*Q) Clone

func (q *Q) Clone() *Q

Clone returns a copy of q.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()

	clone := qsim.Clone()
	clone.Zero()
	clone.Zero()

	q0 := qsim.Zero()
	q1 := qsim.Zero()
	qsim.X(q0, q1)

	fmt.Println(clone)
	fmt.Println(qsim)
	fmt.Println(qsim.Clone())

}
Output:
[(1+0i) (0+0i) (0+0i) (0+0i)]
[(0+0i) (0+0i) (0+0i) (1+0i)]
[(0+0i) (0+0i) (0+0i) (1+0i)]

func (*Q) Cond added in v0.0.3

func (q *Q) Cond(condition bool, g *matrix.Matrix, qb ...Qubit) *Q

Cond applies g if condition is true.

Example
package main

import (
	"fmt"

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

func main() {
	qsim := q.New()
	qb := qsim.Zero()

	for _, b := range []bool{false, true} {
		qsim.Cond(b, gate.X(), qb)

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

}
Output:
[0] ( 1.0000 0.0000i): 1.0000
[1] ( 1.0000 0.0000i): 1.0000

func (*Q) CondX added in v0.0.3

func (q *Q) CondX(condition bool, qb ...Qubit) *Q

CondX applies the X gate if condition is true.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	qb := qsim.Zero()

	for _, b := range []bool{false, true} {
		qsim.CondX(b, qb)

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

}
Output:
[0] ( 1.0000 0.0000i): 1.0000
[1] ( 1.0000 0.0000i): 1.0000

func (*Q) CondZ added in v0.0.3

func (q *Q) CondZ(condition bool, qb ...Qubit) *Q

CondZ applies the Z gate if condition is true.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	qb := qsim.One()

	for _, b := range []bool{false, true} {
		qsim.CondZ(b, qb)

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

}
Output:
[1] ( 1.0000 0.0000i): 1.0000
[1] (-1.0000 0.0000i): 1.0000

func (*Q) Controlled

func (q *Q) Controlled(g *matrix.Matrix, control, target []Qubit) *Q

Controlled applies a controlled operation with g.

func (*Q) ControlledH added in v0.0.8

func (q *Q) ControlledH(control, target []Qubit) *Q

ControlledH applies the controlled-Hadamard gate.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	q0 := qsim.Zero()
	q1 := qsim.Zero()

	qsim.H(q0)
	qsim.ControlledH([]q.Qubit{q0}, []q.Qubit{q1})

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

}
Output:
[00] ( 0.7071 0.0000i): 0.5000
[10] ( 0.5000 0.0000i): 0.2500
[11] ( 0.5000 0.0000i): 0.2500

func (*Q) ControlledNot

func (q *Q) ControlledNot(control, target []Qubit) *Q

ControlledNot applies the CNOT gate.

func (*Q) ControlledR

func (q *Q) ControlledR(theta float64, control, target []Qubit) *Q

ControlledR applies the controlled-R gate.

func (*Q) ControlledU added in v0.0.8

func (q *Q) ControlledU(theta, phi, lambda float64, control, target []Qubit) *Q

ControlledU applies a controlled unitary operation.

func (*Q) ControlledX added in v0.0.8

func (q *Q) ControlledX(control, target []Qubit) *Q

ControlledX applies the CNOT gate.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	q0 := qsim.Zero()
	q1 := qsim.Zero()

	qsim.H(q0)
	qsim.ControlledX([]q.Qubit{q0}, []q.Qubit{q1})

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

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

func (*Q) ControlledZ

func (q *Q) ControlledZ(control, target []Qubit) *Q

ControlledZ applies the controlled-Z gate.

func (*Q) From added in v0.0.6

func (q *Q) From(binary string) []Qubit

From returns a new qubit from a binary string.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	qb := qsim.From("01+-")

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

}
Output:
[0100] ( 0.5000 0.0000i): 0.2500
[0101] (-0.5000 0.0000i): 0.2500
[0110] ( 0.5000 0.0000i): 0.2500
[0111] (-0.5000 0.0000i): 0.2500

func (*Q) G added in v0.0.8

func (q *Q) G(g *matrix.Matrix, qb ...Qubit) *Q

G applies a gate.

func (*Q) H

func (q *Q) H(qb ...Qubit) *Q

H applies the H gate.

func (*Q) I

func (q *Q) I(qb ...Qubit) *Q

I applies the I gate.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	qb := qsim.Zero()

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

}
Output:
[0] ( 1.0000 0.0000i): 1.0000

func (*Q) M

func (q *Q) M(qb ...Qubit) *qubit.Qubit

M returns the measured state of the given qubits.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
	"github.com/itsubaki/q/math/rand"
)

func main() {
	qsim := q.New()
	qsim.SetRand(rand.Const())

	q0 := qsim.Zero()
	q1 := qsim.Zero()
	q2 := qsim.Zero()

	qsim.X(q0)

	fmt.Println(qsim.M(q0))
	fmt.Println(qsim.M(q0, q1, q2))
	fmt.Println(qsim.M())

}
Output:
[(0+0i) (1+0i)]
[(0+0i) (0+0i) (0+0i) (0+0i) (1+0i) (0+0i) (0+0i) (0+0i)]
[(0+0i) (0+0i) (0+0i) (0+0i) (1+0i) (0+0i) (0+0i) (0+0i)]

func (*Q) Measure

func (q *Q) Measure(qb ...Qubit) *qubit.Qubit

Measure returns the measured state of the given qubits.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
	"github.com/itsubaki/q/math/rand"
)

func main() {
	qsim := q.New()
	qsim.SetRand(rand.Const())

	q0 := qsim.Zero()
	q1 := qsim.Zero()
	q2 := qsim.Zero()

	qsim.X(q0)

	fmt.Println(qsim.Measure(q0))
	fmt.Println(qsim.Measure(q0, q1, q2))
	fmt.Println(qsim.Measure())

}
Output:
[(0+0i) (1+0i)]
[(0+0i) (0+0i) (0+0i) (0+0i) (1+0i) (0+0i) (0+0i) (0+0i)]
[(0+0i) (0+0i) (0+0i) (0+0i) (1+0i) (0+0i) (0+0i) (0+0i)]

func (*Q) Minus added in v0.0.12

func (q *Q) Minus() Qubit

Minus returns a qubit in the minus state. The minus state is defined as (|0> - |1>) / sqrt(2).

func (*Q) Minuses added in v0.0.12

func (q *Q) Minuses(n int) []Qubit

Minuses returns n qubits in the minus state.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	qb := qsim.Minuses(2)

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

}
Output:
[00] ( 0.5000 0.0000i): 0.2500
[01] (-0.5000 0.0000i): 0.2500
[10] (-0.5000 0.0000i): 0.2500
[11] ( 0.5000 0.0000i): 0.2500

func (*Q) New

func (q *Q) New(v ...complex128) Qubit

New appends a new qubit and returns its index.

func (*Q) NumQubits added in v0.0.6

func (q *Q) NumQubits() int

NumQubits returns the number of qubits.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	fmt.Println(qsim.NumQubits())

	qsim.Zeros(3)
	fmt.Println(qsim.NumQubits())

	qsim.Zeros(10)
	fmt.Println(qsim.NumQubits())

}
Output:
0
3
13

func (*Q) One

func (q *Q) One() Qubit

One returns a qubit in the one state.

func (*Q) Ones added in v0.0.6

func (q *Q) Ones(n int) []Qubit

Ones returns n qubits in the one state.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	qb := qsim.Ones(2)

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

}
Output:
[00] ( 0.5000 0.0000i): 0.2500
[01] (-0.5000 0.0000i): 0.2500
[10] (-0.5000 0.0000i): 0.2500
[11] ( 0.5000 0.0000i): 0.2500

func (*Q) Plus added in v0.0.12

func (q *Q) Plus() Qubit

Plus returns a qubit in the plus state. The plus state is defined as (|0> + |1>) / sqrt(2).

func (*Q) Pluses added in v0.0.12

func (q *Q) Pluses(n int) []Qubit

Pluses returns n qubits in the plus state.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	qb := qsim.Pluses(2)

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

}
Output:
[00] ( 0.5000 0.0000i): 0.2500
[01] ( 0.5000 0.0000i): 0.2500
[10] ( 0.5000 0.0000i): 0.2500
[11] ( 0.5000 0.0000i): 0.2500

func (*Q) Probability

func (q *Q) Probability() []float64

Probability returns the probabilities of the qubits.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	q0 := qsim.Zero()
	q1 := qsim.Zero()

	qsim.H(q0)
	qsim.CNOT(q0, q1)

	for _, p := range qsim.Probability() {
		fmt.Printf("%.4f\n", p)
	}

}
Output:
0.5000
0.0000
0.0000
0.5000

func (*Q) Qubit added in v0.0.11

func (q *Q) Qubit() *qubit.Qubit

Qubit returns the internal qubit.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	qsim.Zero()

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

}
Output:
[0] ( 1.0000 0.0000i): 1.0000

func (*Q) R

func (q *Q) R(theta float64, qb ...Qubit) *Q

R applies the R gate with theta.

Example
package main

import (
	"fmt"
	"math"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	qb := qsim.Zero()

	qsim.H(qb)
	qsim.R(2*math.Pi/4, qb)

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

}
Output:
[0] ( 0.7071 0.0000i): 0.5000
[1] ( 0.0000 0.7071i): 0.5000

func (*Q) RX

func (q *Q) RX(theta float64, qb ...Qubit) *Q

RX applies the RX gate with theta.

Example
package main

import (
	"fmt"
	"math"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	qb := qsim.Zero()

	qsim.H(qb)
	qsim.RX(math.Pi, qb)

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

}
Output:
[0] ( 0.0000-0.7071i): 0.5000
[1] ( 0.0000-0.7071i): 0.5000

func (*Q) RY

func (q *Q) RY(theta float64, qb ...Qubit) *Q

RY applies the RY gate with theta.

Example
package main

import (
	"fmt"
	"math"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	qb := qsim.Zero()

	qsim.H(qb)
	qsim.RY(math.Pi, qb)

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

}
Output:
[0] (-0.7071 0.0000i): 0.5000
[1] ( 0.7071 0.0000i): 0.5000

func (*Q) RZ

func (q *Q) RZ(theta float64, qb ...Qubit) *Q

RZ applies the RZ gate with theta.

Example
package main

import (
	"fmt"
	"math"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	qb := qsim.Zero()

	qsim.H(qb)
	qsim.RZ(math.Pi, qb)

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

}
Output:
[0] ( 0.0000-0.7071i): 0.5000
[1] ( 0.0000 0.7071i): 0.5000

func (*Q) Reset

func (q *Q) Reset(qb ...Qubit)

Reset sets the given qubits to the zero state.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	qb := qsim.Zeros(2)

	qsim.X(qb[0])
	qsim.Reset(qb...)

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

}
Output:
[00] ( 1.0000 0.0000i): 1.0000

func (*Q) S

func (q *Q) S(qb ...Qubit) *Q

S applies the S gate.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	qb := qsim.Zero()

	qsim.H(qb)
	qsim.S(qb)

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

}
Output:
[0] ( 0.7071 0.0000i): 0.5000
[1] ( 0.0000 0.7071i): 0.5000

func (*Q) SetRand added in v0.0.12

func (q *Q) SetRand(rand func() float64)

SetRand sets the random number generator.

func (*Q) State

func (q *Q) State(reg ...any) []qubit.State

State returns the states of the given qubits.

func (*Q) String

func (q *Q) String() string

String returns the string representation of q.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	q0 := qsim.Zero()
	q1 := qsim.One()

	qsim.X(q0, q1)
	fmt.Println(qsim)

}
Output:
[(0+0i) (0+0i) (1+0i) (0+0i)]

func (*Q) Swap

func (q *Q) Swap(qb0, qb1 Qubit) *Q

Swap applies the swap gate.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	q0 := qsim.Zero()
	q1 := qsim.One()

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

}
Output:
[10] ( 1.0000 0.0000i): 1.0000

func (*Q) T

func (q *Q) T(qb ...Qubit) *Q

T applies the T gate.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	qb := qsim.Zero()

	qsim.H(qb)
	qsim.T(qb)

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

}
Output:
[0] ( 0.7071 0.0000i): 0.5000
[1] ( 0.5000 0.5000i): 0.5000

func (*Q) U

func (q *Q) U(theta, phi, lambda float64, qb ...Qubit) *Q

U applies the U gate.

Example
package main

import (
	"fmt"
	"math"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	qb := qsim.Zero()

	qsim.U(math.Pi, 0, math.Pi, qb)
	for _, s := range qsim.State() {
		fmt.Println(s)
	}

}
Output:
[1] ( 1.0000 0.0000i): 1.0000

func (*Q) X

func (q *Q) X(qb ...Qubit) *Q

X applies the X gate.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	qb := qsim.Zero()

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

}
Output:
[1] ( 1.0000 0.0000i): 1.0000

func (*Q) Y

func (q *Q) Y(qb ...Qubit) *Q

Y applies the Y gate.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	qb := qsim.Zero()

	qsim.H(qb)
	qsim.Y(qb)

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

}
Output:
[0] ( 0.0000-0.7071i): 0.5000
[1] ( 0.0000 0.7071i): 0.5000

func (*Q) Z

func (q *Q) Z(qb ...Qubit) *Q

Z applies the Z gate.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	qb := qsim.Zero()

	qsim.H(qb)
	qsim.Z(qb)

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

}
Output:
[0] ( 0.7071 0.0000i): 0.5000
[1] (-0.7071 0.0000i): 0.5000

func (*Q) Zero

func (q *Q) Zero() Qubit

Zero returns a qubit in the zero state.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	q0 := qsim.Zero()
	q1 := qsim.Zero()

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

}
Output:
[00] ( 0.5000 0.0000i): 0.2500
[01] ( 0.5000 0.0000i): 0.2500
[10] ( 0.5000 0.0000i): 0.2500
[11] ( 0.5000 0.0000i): 0.2500

func (*Q) Zeros added in v0.0.6

func (q *Q) Zeros(n int) []Qubit

Zeros returns n qubits in the zero state.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/q"
)

func main() {
	qsim := q.New()
	qb := qsim.Zeros(2)

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

}
Output:
[00] ( 0.5000 0.0000i): 0.2500
[01] ( 0.5000 0.0000i): 0.2500
[10] ( 0.5000 0.0000i): 0.2500
[11] ( 0.5000 0.0000i): 0.2500

type Qubit

type Qubit int

Qubit is a quantum bit.

func (Qubit) Index

func (q Qubit) Index() int

Index returns the index of q.

Directories

Path Synopsis
cmd
chsh command
counting command
grover command
shor command
math
quantum

Jump to

Keyboard shortcuts

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