grover

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package grover implements Grover's quantum search algorithm.

The algorithm amplifies the probability of marked states using an oracle and a diffusion operator. For N states with M solutions, the optimal number of iterations is approximately π/4 * √(N/M).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// NumQubits is the number of search qubits.
	NumQubits int
	// Oracle marks the target states.
	Oracle Oracle
	// NumIters is the number of Grover iterations.
	// 0 means optimal: floor(π/4 * √(2^n / M)).
	NumIters int
	// NumSolutions is the number of marked states (M). Default: 1.
	NumSolutions int
	// Shots is the number of measurement shots. Default: 1024.
	Shots int
}

Config specifies the Grover search parameters.

type Oracle

type Oracle func(b *builder.Builder, qubits []int)

Oracle marks target states by flipping their phase. It receives a builder and the working qubit indices.

func BooleanOracle

func BooleanOracle(f func(int) bool, numQubits int) Oracle

BooleanOracle creates an oracle from a classical boolean function. The function f takes a bit pattern (as int) and returns true for marked states.

This implementation enumerates all 2^numQubits inputs to find the satisfying assignments, then delegates to PhaseOracle which applies a phase flip to each marked state. This is suitable for simulation but not for hardware, where the oracle would need to be compiled into a reversible circuit.

func PhaseOracle

func PhaseOracle(targets []int, numQubits int) Oracle

PhaseOracle creates an oracle that marks specific target states by index. Each target is an integer representing a computational basis state.

type Result

type Result struct {
	Circuit   *ir.Circuit
	Counts    map[string]int
	TopResult string
	NumIters  int
}

Result holds Grover search output.

func Run

func Run(ctx context.Context, cfg Config) (*Result, error)

Run executes Grover's search algorithm.

Jump to

Keyboard shortcuts

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