Ejemplo n.º 1
0
func main() {
	bits := 3
	h := quantum.NewHadamardGate(bits)
	u_f := quantum.NewClassicalGate(func(x int) int {
		secret := 5 // 101
		value := -1
		y := x >> 3
		smaller := y ^ secret
		if y < smaller {
			smaller = y
		}
		for i := 0; i <= smaller; i++ {
			if i < i^secret {
				value++
			}
		}
		return x ^ value
	},
		2*bits) // This function maps {0, 1}^6 -> {0, 1}^6
	system := make([]int, bits) // This holds the values in our linear
	// system
	system_map := make(map[int]bool)
	// Fill our linear system
	for len(system_map) < bits {
		qreg := quantum.NewQReg(2*bits, 0)
		h.ApplyRange(qreg, bits)
		u_f.ApplyReg(qreg)
		h.ApplyRange(qreg, bits)
		value := qreg.Measure() >> uint(bits)
		if value != 0 && system_map[value] == false {
			system[len(system_map)] = value
			system_map[value] = true
		}
	}
	// Solve the linear system
	secret := solveLinearSystem(system)
	fmt.Printf("Secret is %d\n", secret)
	os.Exit(0)
}
Ejemplo n.º 2
0
func main() {
	n := 3
	qreg := quantum.NewQReg(n+1, 0)
	quantum.HadamardRange(qreg, 1, n+1)
	h := quantum.NewHadamardGate(1)
	u_f := quantum.NewClassicalGate(func(x int) int {
		if x>>1 == 5 {
			return x ^ 1
		}
		return x
	},
		n+1)
	states := 1 << uint(n)
	d := quantum.NewDiffusionGate(n)
	iterations := int((math.Pi * math.Sqrt(float64(states))) / 4.0)
	for i := 0; i < iterations; i++ {
		qreg.BSet(0, 1)
		h.ApplyRange(qreg, 0)
		u_f.ApplyReg(qreg)
		d.ApplyRange(qreg, 1)
	}
	fmt.Printf("Found %d\n", qreg.Measure()>>1)
	os.Exit(0)
}