Optimization#
- qnetti.optimize(cost, settings, step_size=0.1, num_steps=20, verbose=False, init_opt_dict=None, filepath='./', filename='', step_only=False, **meta_opt_kwargs)[source]#
Minimizes the
cost
function and find the optimal settings.The objective of the optimization is \(\min_{\vec{\theta}} Cost(\vec{\theta})\). In the optimization a gradient descent algorithm is applied. In each iteration of gradient, descent, the settings \(\vec{\theta}`\) are updated as
\[\vec{\theta}' = \vec{\theta} - \eta \nabla_{\vec{\theta}} Cost(\vec{\theta})\]where \(\eta\) is the step size and \(\nabla_{\vec{\theta}}Cost(\vec{\theta})\) is the gradient of the cost function evaluated as \(\vec{\theta}\).
- Parameters:
cost (function) – The function to be minimized. This function will be called as cost(settings).
settings (qml.numpy.ndarray) – A PennyLane Numpy tensor that has the
requires_grad=True
attribute.step_size (float) – The step to take in the direction of steepest descent. Default
step_size=0.1
.num_steps (int) – The number of iterations of gradient descent to perform. Default
num_steps=20
.verbose (bool) – If
True
, the iteration step and cost will be printed every 5 iterations.init_opt_dict (dictionary) – An optimization dictionary to serve as a warm start for the optimization. The optimization will continue from the provided dictionary.
step_only (bool) – If
True
, the cost is not evaluated. DefaultFalse
.meta_opt_kwargs (keyword arguments) – Generic keyword arguments to write the optimization dictionary.
- Returns:
A dictionary of optimization data.
- Return type:
dictionary
The return dictionary has the following keys: *
"settings_list"
-list[list[float]]
, The settings considered in each optimization step. *"cost_vals"
-list[float]
, The cost evaluated in each optimization step. *"min_cost"
-float
, The smallest cost evaluated during optimization. *"opt_settings"
-list[float]
, The optimal settings to achieve the minimum cost. *"step_size"
-float
, The step size used in the optimization. *"datetime"
-string
, The date/time at which the data is optimization begins. *"opt_step_time"
-float
, The time needed to make each optimization step. *"error"
-boolean
, Indicates whether or not an error occurred during optimization.
- qnetti.qubit_probs_qnode_fn(prep_node, meas_wires=None, dev_kwargs={}, qnode_kwargs={})[source]#
Constructs a qnode function that returns the probabilities for local qubit measurements. The resulting quantum circuit prepares the state described by the
prep_node
and then applies arbitrary qubit unitaries to each wire before measurement.- Parameters:
prep_node (qnetvo.PrepareNode) – An ansatz for the network state preparation.
meas_wires (list[int]) – The wires to apply arbitrary qubit measurements to. If
None
, all qubits are measured.dev_kwargs – Keyword arguments to pass to the PennyLane device constructor. Useful keys are include
"name"
, which specifies the device (default:"default.qubit"
), and"shots"
, which specifies the integert number of shots to run during circuit execution (default:None
).qnode_kwargs (dict) – Keyword arguments passed through to the PennyLane qnode decorator.
- Returns:
A qnode function that returns the qubit probabilities for the circuit and the device that evaluates the qnode. The function is called as
qnode(settings)
wherelen(settings) == 3 * num_wires
.- Return type:
tuple(qml.QNode, qml.Device)