Circuit Optimizer

class perceval.utils.algorithms.circuit_optimizer.CircuitOptimizer(threshold=1e-06, ntrials=4, max_eval_per_trial=200000)

CircuitOptimizer is a tool designed to set up a circuit with enough variable parameters (i.e. sufficient degrees of freedom) so that it reproduces to the behavior of any other unitary circuit/matrix.

Be aware that some circuits are not “universal interferometers” and thus cannot reach every arbitrary unitary matrix. However, the optimizer will get as close as possible. The metric to measure the distance between the target unitary and the optimized interferometer is the matrix fidelity available in perceval.utils.algorithms.norm.

CircuitOptimizer can be configured with the following parameters: :type threshold: float :param threshold: Error threshold = 1-fidelity - i.e. the lower the threshold, the better the output fidelity

(default 1e-6)

  • ntrials (int) – Number of optimization trials (default 4)

  • max_eval_per_trial (int) – maximum number of evaluations per optimization trial (default 200000)

optimize(target, template)

Optimize a template circuit unitary’s fidelity with a target matrix or circuit.

  • target (Union[ACircuit, Matrix]) – The target unitary circuit or matrix

  • template (ACircuit) – A circuit with variable parameters (supports only beam splitters and phase shifters)

Return type:

Tuple[ACircuit, float]


A tuple of the best optimized circuit and its fidelity to the target

>>> def mzi(i):
>>>    return Circuit(2) // PS(P(f"phi_1_{i}")) // BS() // PS(P(f"phi_2_{i}")) // BS()
>>> def ps(i):
>>>    return PS(P(f"phi_3_{i}"))
>>> template = GenericInterferometer(12, mzi, phase_shifter_fun_gen=ps, phase_at_output=True)
>>> random_unitary = Matrix.random_unitary(12)
>>> result_circuit, fidelity = CircuitOptimizer().optimize(random_unitary, template)
optimize_rectangle(target, template_component_generator_func=None, phase_at_output=True, allow_error=False)

Optimize a rectangular circuit to reach a target unitary matrix fidelity.

  • target (Matrix) – Target unitary matrix

  • template_component_generator_func (Optional[Callable[[int], ACircuit]]) – Function which generates a base component of the output rectangular circuit (signature(int) -> ACircuit) (default generates a MZI with two variable phases)

  • phase_at_output (bool) – If True, a layer of phase shifters is added at the output of the circuit. Otherwise, the layer is at the input (default True)

  • allow_error (bool) – If True, this call will not raise an error when the best fidelity is below threshold Otherwise, raises an error (default False)

Return type: