Skip to content

geope.grape¤

geope.grape.Grape ¤

Gradient/Hessian-based GRAPE optimiser for quantum gate synthesis.

Mirrors the Geope usage pattern in every respect: it is constructed from a Parameters object (the single source of truth for all configuration and the live optimisation state), while the update rule and max_steps are arguments of :meth:optimize; and it builds one GeometricContext per step inside a single jitted update_step.

Where Geope walks the geodesic toward the target, Grape walks the infidelity's own gradient. The step is therefore assembled from the context's cost tier — value_and_grad, and cost_hessian for the Newton rules — and never from point, jacobian or A. Because every context quantity is lazy, that is the whole reason a GRAPE run traces no matrix logarithm and never builds the Jacobian; see geope.optimizers for the rule an update rule has to obey to keep it that way.

Attributes:

Name Type Description
params

The bound Parameters object (also the source of the lazily-built/cached optimisation functions).

precision

Target fidelity threshold.

optimizer

The active geope.optimizers.Optimizer from the most recent :meth:optimize call; None until :meth:optimize is first called.

optimizer_state

The current optimiser state pytree (re-init()d per run); None until :meth:optimize is first called.

method

The active rule's name (e.g. 'newton_trm'), or None.

step_size

Transient last accepted step (the optimiser result's step_size).

history

Optional History logger (None unless supplied).

__dict__ class-attribute ¤
__doc__ class-attribute ¤

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

__module__ class-attribute ¤

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

__weakref__ property ¤

list of weak references to the object

method property ¤

The active update rule's name, or None before the first run.

__init__(params, precision=0.9999999, verbose=False, history=None) ¤

Initialise the Grape optimiser.

Grape requires a Parameters object — the engine, initial parameters, drift, seed, initialisation spread, projective flag and param_transform are all read from it.

Parameters:

Name Type Description Default
params Parameters

A Parameters instance bundling every input the optimiser needs.

required
precision float

Target fidelity. Defaults to 0.9999999.

0.9999999
verbose bool

Whether to print progress. Defaults to False.

False
history History | None

Optional History logger. When supplied, the full run trajectory is recorded into it; when None (default), no history is kept.

None

Raises:

Type Description
TypeError

If params is not a Parameters instance.

init(init_parameters=None, drift_parameters=None, seed=None) ¤

(Re-)initialise optimiser state.

Sets up initial parameters, drift parameters and the live state (params.parameters / params.fidelity), and records step 0 into history when one is attached.

Parameters:

Name Type Description Default
init_parameters ndarray | None

Initial parameter array. Defaults to random.

None
drift_parameters ndarray | None

Fixed drift parameter values. Defaults to ones.

None
seed int | Array | None

Random seed (int) or JAX key for reproducibility.

None
get_update_step() ¤

Build the JIT-compiled GRAPE update step.

One jitted function per update rule, and one geope.geometry.GeometricContext per call of it. The step is: open the context, let the rule read the cost tier off it and choose a descent direction and a (positive) step, then move.

The context is built inside this function and never leaves it — it is a trace-time object, not a pytree. Everything the rule needs it reads off that one context, and everything it does not read costs nothing, which is why no matrix logarithm and no Jacobian is traced here.

Returns:

Type Description
Callable[..., tuple]

A JIT-compiled callable update_step(free_params, delta_t, opt_state)

Callable[..., tuple]

returning (new_parameters, infidelity, step_size, new_opt_state),

Callable[..., tuple]

where infidelity is measured at new_parameters and

Callable[..., tuple]

step_size is the accepted line-search step.

optimize(max_steps=100, optimizer=None, callbacks=None) ¤

Run the GRAPE optimisation loop.

Iterates gradient/Hessian update steps until the fidelity exceeds self.precision or max_steps is reached.

Parameters:

Name Type Description Default
max_steps int

Maximum number of optimisation steps. Defaults to 100.

100
optimizer Optimizer | None

The geope.optimizers.Optimizer to run — GradientDescent, Adam, LBFGS, NewtonTRM, NewtonRFO or NewtonSaddleFree, each a frozen dataclass carrying its own hyperparameters. Defaults to geope.optimizers.NewtonTRM. Prefer geope.optimizers.NewtonSaddleFree on a rank-deficient landscape, which for gate synthesis is the usual case: the solutions form a manifold, so the cost Hessian is singular near one and the shift-based rules degenerate toward gradient descent. geope.optimizers.LBFGS is the cheap alternative when \(P\) is large enough that the \((P, P)\) Hessian and its eigh dominate: it never forms one. The three Newton rules also take wolfe=True for a strong-Wolfe line search in place of backtracking Armijo, which geope.optimizers.LBFGS uses by default.

None
callbacks Callable | list[Callable] | tuple[Callable, ...] | None

Optional callback, or list/tuple of callbacks, invoked at the end of every step with the signature callback(step, history, grape) -> bool. All callbacks run each step; the loop stops early if any returns a falsy value (so a pure logging callback must return True). step is the 1-based index of the step just completed, history is grape.history (may be None), and grape is this optimiser.

None

Returns:

Type Description
Parameters

The bound Parameters instance, carrying the final

Parameters

parameters (current array) and fidelity (scalar) — which

Parameters

describe the same pulse, since the reported infidelity is measured

Parameters

at the step just taken. The full trajectory and best_* live on

Parameters

grape.history when a History was supplied.

Raises:

Type Description
TypeError

If optimizer is not an geope.optimizers.Optimizer.