sweep

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package sweep provides parameter sweep types for evaluating variational quantum circuits across ranges of parameter values.

A Sweep resolves to a list of parameter binding maps. Primitive sweeps (Linspace, Points, Single) cover a single parameter or fixed bindings. Combinators (Product, Zip) compose sweeps into higher-dimensional spaces.

RunSim and RunDensitySim execute a parameterized circuit across all sweep points in parallel using statevector or density matrix simulation.

theta := sweep.Linspace{Key: "theta", Start: 0, Stop: math.Pi, Count: 10}
results, err := sweep.RunSim(ctx, circuit, 1024, theta)

Package sweep provides parameter sweep types for variational quantum circuits.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Linspace

type Linspace struct {
	Key   string
	Start float64
	Stop  float64
	Count int
}

Linspace sweeps a single parameter over evenly-spaced values from Start to Stop (inclusive).

Example
package main

import (
	"fmt"

	"github.com/splch/goqu/sweep"
)

func main() {
	l := sweep.Linspace{Key: "theta", Start: 0, Stop: 1, Count: 3}
	fmt.Println("Len:", l.Len())
	for _, m := range l.Resolve() {
		fmt.Printf("theta=%.1f\n", m["theta"])
	}
}
Output:

Len: 3
theta=0.0
theta=0.5
theta=1.0

func (Linspace) Len

func (l Linspace) Len() int

func (Linspace) Params

func (l Linspace) Params() []string

func (Linspace) Resolve

func (l Linspace) Resolve() []map[string]float64

type PointResult

type PointResult struct {
	Index    int
	Bindings map[string]float64
	Counts   map[string]int
	Err      error
}

PointResult holds the outcome for a single sweep point.

func RunDensitySim

func RunDensitySim(ctx context.Context, c *ir.Circuit, shots int, sw Sweep, nm *noise.NoiseModel) ([]PointResult, error)

RunDensitySim executes a parameterized circuit across all sweep points using density matrix simulation with an optional noise model. Results are returned in sweep order.

func RunSim

func RunSim(ctx context.Context, c *ir.Circuit, shots int, sw Sweep) ([]PointResult, error)

RunSim executes a parameterized circuit across all sweep points using statevector simulation. Each point gets a fresh simulator instance. Results are returned in sweep order.

Example
package main

import (
	"context"
	"fmt"
	"math"

	"github.com/splch/goqu/circuit/builder"
	"github.com/splch/goqu/circuit/param"
	"github.com/splch/goqu/sweep"
)

func main() {
	theta := param.New("theta")
	c, err := builder.New("ry", 1).
		SymRY(theta.Expr(), 0).
		MeasureAll().
		Build()
	if err != nil {
		panic(err)
	}

	sw := sweep.Linspace{Key: "theta", Start: 0, Stop: math.Pi, Count: 3}
	results, err := sweep.RunSim(context.Background(), c, 1000, sw)
	if err != nil {
		panic(err)
	}

	for _, r := range results {
		ones := r.Counts["1"]
		fmt.Printf("theta=%.2f: P(1)≈%.1f\n", r.Bindings["theta"], float64(ones)/1000)
	}
}
Output:

theta=0.00: P(1)≈0.0
theta=1.57: P(1)≈0.5
theta=3.14: P(1)≈1.0

type Points

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

Points holds explicit values for a single parameter.

func NewPoints

func NewPoints(key string, values []float64) Points

NewPoints creates a Points sweep with a defensive copy of the values slice.

func (Points) Len

func (p Points) Len() int

func (Points) Params

func (p Points) Params() []string

func (Points) Resolve

func (p Points) Resolve() []map[string]float64

type Sweep

type Sweep interface {
	// Len returns the number of parameter points in the sweep.
	Len() int

	// Params returns the parameter names covered by this sweep.
	Params() []string

	// Resolve expands the sweep into concrete binding maps.
	Resolve() []map[string]float64
}

Sweep defines a set of parameter bindings to evaluate a circuit over.

func MustZip

func MustZip(sweeps ...Sweep) Sweep

MustZip is like Zip but panics on length mismatch.

func Product

func Product(sweeps ...Sweep) Sweep

Product returns a sweep that is the Cartesian product of the given sweeps. If any inner sweep has length 0, the result is empty.

Example
package main

import (
	"fmt"

	"github.com/splch/goqu/sweep"
)

func main() {
	x := sweep.NewPoints("x", []float64{1, 2})
	y := sweep.NewPoints("y", []float64{10, 20})
	p := sweep.Product(x, y)
	fmt.Println("Len:", p.Len())
	for _, m := range p.Resolve() {
		fmt.Printf("x=%.0f y=%.0f\n", m["x"], m["y"])
	}
}
Output:

Len: 4
x=1 y=10
x=1 y=20
x=2 y=10
x=2 y=20

func Zip

func Zip(sweeps ...Sweep) (Sweep, error)

Zip returns a sweep that merges the given sweeps element-wise. All sweeps must have the same length; returns an error otherwise.

type UnitSweep

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

UnitSweep is a single-point sweep with fixed bindings.

func Single

func Single(bindings map[string]float64) UnitSweep

Single creates a UnitSweep with a defensive copy of the bindings map.

func (UnitSweep) Len

func (u UnitSweep) Len() int

func (UnitSweep) Params

func (u UnitSweep) Params() []string

func (UnitSweep) Resolve

func (u UnitSweep) Resolve() []map[string]float64

Jump to

Keyboard shortcuts

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