mimiqcircuits.optimization

Functions

check_optimization_args(circuit, initparams, ...)

Classes

OptimizationExperiment(circuit[, ...])

A variational optimization problem: a symbolic circuit, initial values for its symbols, and the optimizer settings.

OptimizationResults([best, history])

The result of an optimization: the lowest-cost run and, when asked for, every evaluated run.

OptimizationRun([cost, parameters, results])

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: object

A 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 zregister over the samples of the circuit evaluated with them. Push the quantity to minimize into that register, for example with push_expval(). Run the experiment with LocalBackend.optimize, MimiqRemoteBackend.optimize or MimiqConnection.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 None for 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, initparams does not give a value for exactly the symbols of the circuit, or zregister is 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.

is_valid()[source]

Return whether the experiment passes the checks of the constructor.

num_params()[source]

Return the number of symbols with an initial value.

num_qubits()[source]

Return the number of qubits of the circuit.

num_bits()[source]

Return the number of classical bits of the circuit.

num_zvars()[source]

Return the number of z-variables of the circuit.

get_param(n)[source]

Return the initial value of n, a key of initparams.

get_params()[source]

Return the initparams dictionary itself, not a copy.

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 in initparams are 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 theta does 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 OptimizationExperiment saved with saveproto().

class OptimizationResults(best=<factory>, history=<factory>)[source]

Bases: object

The result of an optimization: the lowest-cost run and, when asked for, every evaluated run.

Returned by the optimize method of a backend, and by get_result or get_results for a job started with a connection’s optimize. history is filled only with return_history=True (local) or history=True (connection).

Parameters:

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.

get_resultofbest()[source]

Return the QCSResults of the best run.

get_resultsofhistory()[source]

Return the QCSResults of every run in the history, in order.

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 OptimizationResults saved with saveproto().

class OptimizationRun(cost=0.0, parameters=<factory>, results=<factory>)[source]

Bases: object

One 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 initparams of 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(...)
get_cost()[source]

Return the cost.

get_params()[source]

Return the parameters dictionary itself, not a copy.

get_param(n)[source]

Return the value of n, a key of parameters.

get_resultofbest()[source]

Return the QCSResults of this run.

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 OptimizationRun saved with saveproto().