Documentation
¶
Overview ¶
Package builder provides a fluent API for constructing quantum circuits.
Package builder provides a fluent API for constructing quantum circuits.
A Builder accumulates gate operations and measurements, then produces an immutable ir.Circuit via Builder.Build. Qubit indices are validated eagerly; the first error short-circuits all subsequent calls and is returned by Build.
c, err := builder.New("bell", 2).
H(0).
CNOT(0, 1).
MeasureAll().
Build()
Index ¶
- type Builder
- func (b *Builder) Apply(g gate.Gate, qubits ...int) *Builder
- func (b *Builder) Barrier(qubits ...int) *Builder
- func (b *Builder) Build() (*ir.Circuit, error)
- func (b *Builder) CCX(c0, c1, target int) *Builder
- func (b *Builder) CCZ(c0, c1, target int) *Builder
- func (b *Builder) CH(control, target int) *Builder
- func (b *Builder) CNOT(control, target int) *Builder
- func (b *Builder) CSX(control, target int) *Builder
- func (b *Builder) CZ(control, target int) *Builder
- func (b *Builder) Compose(c *ir.Circuit, qubitMap map[int]int) *Builder
- func (b *Builder) ComposeInverse(c *ir.Circuit, qubitMap map[int]int) *Builder
- func (b *Builder) Ctrl(g gate.Gate, controls []int, targets ...int) *Builder
- func (b *Builder) DCX(q0, q1 int) *Builder
- func (b *Builder) Delay(qubit int, duration float64, unit string) *Builder
- func (b *Builder) ECR(q0, q1 int) *Builder
- func (b *Builder) FSim(theta, phi float64, q0, q1 int) *Builder
- func (b *Builder) For(clbit int, start, end, step int, body func(*Builder)) *Builder
- func (b *Builder) GlobalPhase(phi float64, q int) *Builder
- func (b *Builder) H(q int) *Builder
- func (b *Builder) ISWAP(q0, q1 int) *Builder
- func (b *Builder) If(clbit, value int, g gate.Gate, qubits ...int) *Builder
- func (b *Builder) IfBlock(clbit, value int, fn func(*Builder)) *Builder
- func (b *Builder) IfElseBlock(clbit, value int, ifBody, elseBody func(*Builder)) *Builder
- func (b *Builder) MCP(phi float64, controls []int, target int) *Builder
- func (b *Builder) MCX(controls []int, target int) *Builder
- func (b *Builder) MCZ(controls []int, target int) *Builder
- func (b *Builder) Measure(qubit, clbit int) *Builder
- func (b *Builder) MeasureAll() *Builder
- func (b *Builder) PSwap(phi float64, q0, q1 int) *Builder
- func (b *Builder) Phase(phi float64, q int) *Builder
- func (b *Builder) PhasedXZ(x, z, a float64, q int) *Builder
- func (b *Builder) RX(theta float64, q int) *Builder
- func (b *Builder) RXX(theta float64, q0, q1 int) *Builder
- func (b *Builder) RY(theta float64, q int) *Builder
- func (b *Builder) RYY(theta float64, q0, q1 int) *Builder
- func (b *Builder) RZ(theta float64, q int) *Builder
- func (b *Builder) RZZ(theta float64, q0, q1 int) *Builder
- func (b *Builder) Reset(qubit int) *Builder
- func (b *Builder) Rot(phi, theta, omega float64, q int) *Builder
- func (b *Builder) S(q int) *Builder
- func (b *Builder) SWAP(q0, q1 int) *Builder
- func (b *Builder) SetMetadata(key, value string) *Builder
- func (b *Builder) StatePrep(amplitudes []complex128, qubits ...int) *Builder
- func (b *Builder) Switch(clbits []int, cases map[int]func(*Builder), defaultBody func(*Builder)) *Builder
- func (b *Builder) Sycamore(q0, q1 int) *Builder
- func (b *Builder) SymCP(phi param.Expr, q0, q1 int) *Builder
- func (b *Builder) SymFSim(theta, phi param.Expr, q0, q1 int) *Builder
- func (b *Builder) SymGlobalPhase(phi param.Expr, q int) *Builder
- func (b *Builder) SymPSwap(phi param.Expr, q0, q1 int) *Builder
- func (b *Builder) SymPhase(phi param.Expr, q int) *Builder
- func (b *Builder) SymRX(theta param.Expr, q int) *Builder
- func (b *Builder) SymRXX(theta param.Expr, q0, q1 int) *Builder
- func (b *Builder) SymRY(theta param.Expr, q int) *Builder
- func (b *Builder) SymRYY(theta param.Expr, q0, q1 int) *Builder
- func (b *Builder) SymRZ(theta param.Expr, q int) *Builder
- func (b *Builder) SymRZZ(theta param.Expr, q0, q1 int) *Builder
- func (b *Builder) SymRot(phi, theta, omega param.Expr, q int) *Builder
- func (b *Builder) SymU1(lambda param.Expr, q int) *Builder
- func (b *Builder) SymU2(phi, lambda param.Expr, q int) *Builder
- func (b *Builder) SymU3(theta, phi, lambda param.Expr, q int) *Builder
- func (b *Builder) T(q int) *Builder
- func (b *Builder) U1(lambda float64, q int) *Builder
- func (b *Builder) U2(phi, lambda float64, q int) *Builder
- func (b *Builder) U3(theta, phi, lambda float64, q int) *Builder
- func (b *Builder) Unitary(name string, matrix []complex128, qubits ...int) *Builder
- func (b *Builder) While(clbit, value int, body func(*Builder)) *Builder
- func (b *Builder) WithClbits(n int) *Builder
- func (b *Builder) X(q int) *Builder
- func (b *Builder) Y(q int) *Builder
- func (b *Builder) Z(q int) *Builder
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder accumulates operations and produces an immutable Circuit.
Example (Bell) ¶
package main
import (
"fmt"
"github.com/splch/goqu/circuit/builder"
)
func main() {
c, err := builder.New("bell", 2).
H(0).
CNOT(0, 1).
Build()
if err != nil {
panic(err)
}
stats := c.Stats()
fmt.Printf("name=%s qubits=%d depth=%d gates=%d two_qubit=%d\n",
c.Name(), c.NumQubits(), stats.Depth, stats.GateCount, stats.TwoQubitGates)
}
Output: name=bell qubits=2 depth=2 gates=2 two_qubit=1
Example (Ghz) ¶
package main
import (
"fmt"
"github.com/splch/goqu/circuit/builder"
)
func main() {
b := builder.New("ghz-4", 4).H(0)
for i := 0; i < 3; i++ {
b.CNOT(i, i+1)
}
c, err := b.Build()
if err != nil {
panic(err)
}
stats := c.Stats()
fmt.Printf("qubits=%d depth=%d two_qubit=%d\n",
c.NumQubits(), stats.Depth, stats.TwoQubitGates)
}
Output: qubits=4 depth=4 two_qubit=3
func (*Builder) Compose ¶
Compose appends all operations from c into the builder, remapping qubit indices. nil qubitMap uses identity mapping (c's qubit N → builder's qubit N). Classical bits use identity mapping (c's clbit N → builder's clbit N).
func (*Builder) ComposeInverse ¶
ComposeInverse appends the inverse of c's operations (reversed order, each gate adjointed). Measurements, resets, and barriers are skipped. nil qubitMap uses identity mapping.
func (*Builder) Delay ¶ added in v1.2.0
Delay idles a qubit for the given duration. The unit should be one of the gate.Unit* constants (e.g., gate.UnitNs).
func (*Builder) For ¶ added in v1.2.0
For executes body for each iteration in [start, end) with the given step. The iteration count is stored in the specified clbit (least-significant bit).
func (*Builder) GlobalPhase ¶ added in v1.2.0
GlobalPhase applies a global phase gate.
func (*Builder) If ¶
If adds a classically-conditioned gate. The gate is applied only when clbit == value.
func (*Builder) IfBlock ¶
IfBlock conditions multiple operations on clbit == value. The function fn is called with a sub-builder; all ops added inside are conditioned.
func (*Builder) IfElseBlock ¶ added in v1.2.0
IfElseBlock conditions operations on clbit == value, with an else branch.
func (*Builder) MeasureAll ¶
MeasureAll adds measurements for all qubits to corresponding classical bits. Automatically sets numClbits to numQubits if not already set.
func (*Builder) SetMetadata ¶
SetMetadata sets a metadata key-value pair.
func (*Builder) StatePrep ¶
func (b *Builder) StatePrep(amplitudes []complex128, qubits ...int) *Builder
StatePrep adds a state preparation gate.
func (*Builder) Switch ¶ added in v1.2.0
func (b *Builder) Switch(clbits []int, cases map[int]func(*Builder), defaultBody func(*Builder)) *Builder
Switch branches on the integer value of the given classical bits. cases maps integer values to body functions. defaultBody (may be nil) executes if no case matches.
func (*Builder) SymGlobalPhase ¶ added in v1.2.0
SymGlobalPhase applies a symbolic GlobalPhase gate.
func (*Builder) Unitary ¶
func (b *Builder) Unitary(name string, matrix []complex128, qubits ...int) *Builder
Unitary applies a custom unitary gate created from the given matrix. The name is used for display; the matrix must be a valid unitary (2x2, 4x4, or 8x8).
func (*Builder) While ¶ added in v1.2.0
While executes body repeatedly while clbit == value. At simulation time, iterations are capped at ir.MaxControlFlowIterations.
func (*Builder) WithClbits ¶
WithClbits sets the number of classical bits.