mimiqcircuits.optimization¶
Functions
|
Classes
|
A variational optimization problem: a symbolic circuit, initial values for its symbols, and the optimizer settings. |
|
The result of an optimization: the lowest-cost run and, when asked for, every evaluated run. |
|
One evaluation of the cost during an optimization: the parameter values, the cost they gave, and the results of the execution. |
- class OptimizationExperiment(circuit, initparams=<factory>, optimizer='COBYLA', label='optexp_python', maxiters=None, zregister=0)[source]¶
Bases:
objectA variational optimization problem: a symbolic circuit, initial values for its symbols, and the optimizer settings.
The cost of a set of values is the real part of the mean of z-register
zregisterover the samples of the circuit evaluated with them. Push the quantity to minimize into that register, for example withpush_expval(). Run the experiment withLocalBackend.optimize,MimiqRemoteBackend.optimizeorMimiqConnection.optimize.- Parameters:
circuit (Circuit) – The symbolic circuit.
initparams (dict) – Initial value of every symbol of the circuit, keyed by symbol or by symbol name.
optimizer (str) –
"BFGS","LBFGS","CG","NELDERMEAD","NEWTON","COBYLA"or"CMAES". Checked when the experiment runs, not here. Defaults to"COBYLA".label (str) – Name of the experiment. Defaults to
"optexp_python".maxiters (int or None) – Iteration limit, or
Nonefor the optimizer’s own.zregister (int) – Index of the z-register that holds the cost. Defaults to 0.
- Raises:
ValueError – If the circuit is empty or has no symbols,
initparamsdoes not give a value for exactly the symbols of the circuit, orzregisteris negative.
Examples
>>> from mimiqcircuits import * >>> from symengine import symbols
The cost is
⟨Z⟩of qubit 0 after two rotations, written to z-register 0.>>> x, y = symbols("x y") >>> c = Circuit().push(GateRX(x), 0).push(GateRY(y), 0) >>> c.push(ExpectationValue(GateZ()), 0, 0) 1-qubit, 1-zvar circuit with 3 instructions: ├── RX(x) @ q[0] ├── RY(y) @ q[0] └── ⟨Z⟩ @ q[0], z[0]
Every symbol needs an initial value.
>>> ex = OptimizationExperiment(c, {x: 0.1, y: 2.0}, maxiters=100) >>> ex OptimizationExperiment: ├── optimizer: COBYLA ├── label: optexp_python ├── maxiters: 100 ├── zregister: 0 └── initparams: ├── x => 0.1 └── y => 2.0
Changing the initial values gives a new experiment and leaves this one unchanged.
>>> ex.change_parameters({x: 0.5}).get_params() {x: 0.5, y: 2.0}
See also
LocalBackend.optimize, whose example runs an experiment.- change_parameters(newvals)[source]¶
Return a copy of the experiment with some initial values replaced.
- Parameters:
newvals (Dict[Symbol, float]) – New values, keyed like
initparams. Keys that are not ininitparamsare ignored.- Returns:
A new
OptimizationExperiment; this one is unchanged.
- change_list_of_parameters(theta)[source]¶
Return a copy of the experiment with all initial values replaced.
- Parameters:
theta (List[float]) – One value per symbol of the circuit, in the order of the symbol names sorted alphabetically.
- Returns:
A new
OptimizationExperiment.- Raises:
ValueError – If
thetadoes not have one value per symbol.
- saveproto(file)[source]¶
Save the experiment to a Protobuf file or file-like object.
Returns the number of bytes written. See
saveproto().
- static loadproto(file)[source]¶
Load an
OptimizationExperimentsaved withsaveproto().
- class OptimizationResults(best=<factory>, history=<factory>)[source]¶
Bases:
objectThe result of an optimization: the lowest-cost run and, when asked for, every evaluated run.
Returned by the
optimizemethod of a backend, and byget_resultorget_resultsfor a job started with a connection’soptimize.historyis filled only withreturn_history=True(local) orhistory=True(connection).- Parameters:
best (OptimizationRun) – The run with the lowest cost.
history (list[OptimizationRun]) – Every evaluated run, in order.
Examples
>>> from mimiqcircuits import * >>> run1 = OptimizationRun(cost=0.5, parameters={"x": 0.1}, results=QCSResults("", "")) >>> run1 OptimizationRun: ├── cost: 0.5 ├── parameters: │ x => 0.1 └── results: QCSResults(...) >>> run2 = OptimizationRun(cost=0.3, parameters={"x": 0.2}, results=QCSResults()) >>> run2 OptimizationRun: ├── cost: 0.3 ├── parameters: │ x => 0.2 └── results: QCSResults(...) >>> results = OptimizationResults(best=run2, history=[run1, run2]) >>> results OptimizationResults: ├── Best Run: │ OptimizationRun: │ ├── cost: 0.3 │ ├── parameters: │ │ x => 0.2 │ └── results: QCSResults(...) └── History (2 runs): ├── Run 0: │ OptimizationRun: │ ├── cost: 0.5 │ ├── parameters: │ │ x => 0.1 │ └── results: QCSResults(...) └── Run 1: OptimizationRun: ├── cost: 0.3 ├── parameters: │ x => 0.2 └── results: QCSResults(...)
- get_best()[source]¶
Return the lowest-cost
OptimizationRun.
- saveproto(file)[source]¶
Save the results to a Protobuf file or file-like object.
Returns the number of bytes written. See
saveproto().
- static loadproto(file)[source]¶
Load an
OptimizationResultssaved withsaveproto().
- class OptimizationRun(cost=0.0, parameters=<factory>, results=<factory>)[source]¶
Bases:
objectOne evaluation of the cost during an optimization: the parameter values, the cost they gave, and the results of the execution.
- Parameters:
cost (float) – The cost at these values.
parameters (dict) – The value of each symbol, keyed like the
initparamsof the experiment.results (QCSResults) – The results of the execution that gave the cost.
Examples
>>> from mimiqcircuits import * >>> OptimizationRun(cost=0.153, parameters={"x": 0.2, "y": 1.5}, results=QCSResults("","")) OptimizationRun: ├── cost: 0.153 ├── parameters: │ x => 0.2 │ y => 1.5 └── results: QCSResults(...)
- saveproto(file)[source]¶
Save the run to a Protobuf file or file-like object.
Returns the number of bytes written. See
saveproto().
- static loadproto(file)[source]¶
Load an
OptimizationRunsaved withsaveproto().