# optimize

Source: https://docs.q-ctrl.com/references/boulder-opal/boulderopal/superconducting/optimize

```python
boulderopal.superconducting.optimize(transmons, cavities, interactions, gate_duration, initial_state=None, target_state=None, target_operation=None, sample_count=128, cutoff_frequency=None, **optimization_kwargs)
```

Find optimal pulses or parameters for a system composed of transmons and cavities,
in order to achieve a target state or implement a target operation.

At least one of the terms in the transmons, cavities, or interactions arguments
must be optimizable.

To optimize a state transfer, you need to provide an initial and a target state.
To optimize a target gate/unitary, you need to provide a target operation.

This function builds a graph describing the Hamiltonian (see the note part for details) of a
superconducting system, and calls [`run_optimization`](https://docs.q-ctrl.com/references/boulder-opal/boulderopal/graph/run_optimization.md#boulderopal.run_optimization) to
to perform the optimization task.

### Parameters
* **transmons** (*list* *[*[*Transmon*](https://docs.q-ctrl.com/references/boulder-opal/boulderopal/superconducting/Transmon.md#boulderopal.superconducting.Transmon) *]*) – List of objects containing the physical information about the transmons.
  It can be an empty list, but at least one transmon or cavity must be provided.
* **cavities** (*list* *[*[*Cavity*](https://docs.q-ctrl.com/references/boulder-opal/boulderopal/superconducting/Cavity.md#boulderopal.superconducting.Cavity) *]*) – List of objects containing the physical information about the cavities.
  It can be an empty list, but at least one transmon or cavity must be provided.
* **interactions** (*list* *[*[*TransmonTransmonInteraction*](https://docs.q-ctrl.com/references/boulder-opal/boulderopal/superconducting/TransmonTransmonInteraction.md#boulderopal.superconducting.TransmonTransmonInteraction) *or* [*TransmonCavityInteraction*](https://docs.q-ctrl.com/references/boulder-opal/boulderopal/superconducting/TransmonCavityInteraction.md#boulderopal.superconducting.TransmonCavityInteraction) *or*             [*CavityCavityInteraction*](https://docs.q-ctrl.com/references/boulder-opal/boulderopal/superconducting/CavityCavityInteraction.md#boulderopal.superconducting.CavityCavityInteraction) *]*) – List of objects containing the physical information about the interactions in the system.
  It can be an empty list.
* **gate_duration** (*float*) – The duration of the gate to be optimized, $t_\mathrm{gate}$.
  It must be greater than zero.
* **initial_state** (*np.ndarray* *or* *None* *,* *optional*) – The initial state of the system, $|\Psi_\mathrm{initial}\rangle$, as a 1D array of
  length `D = np.prod([system.dimension for system in transmon + cavities])`.
  If provided, the function also returns its time evolution.
  This is a required parameter if you pass a target_state.
* **target_state** (*np.ndarray* *or* *None* *,* *optional*) – The target state of the optimization, $|\Psi_\mathrm{target}\rangle$,
  as a 1D array of length D.
  You must provide exactly one of target_state or target_operation.
* **target_operation** (*np.ndarray* *or* *None* *,* *optional*) – The target operation of the optimization, $U_\mathrm{target}$,
  as a 2D array of shape `(D, D)`.
  You must provide exactly one of target_state or target_operation.
* **sample_count** (*int* *,* *optional*) – The number of times between 0 and gate_duration (included)
  at which the evolution is sampled.
  Defaults to 128.
* **cutoff_frequency** (*float* *or* *None* *,* *optional*) – The cutoff frequency of a linear sinc filter to be applied to the piecewise-constant
  signals you provide for the coefficients. If not provided, the signals are not filtered.
  If the signals are filtered, a larger sample count leads to a more accurate numerical
  integration. If the signals are not filtered, the sample count has no effect on the
  numerical precision of the integration.
* **\*\*optimization_kwargs** (*dict*) – Additional parameters to pass to boulderopal.run_optimization.
### Returns
The result of the run_optimization call.
Its `output` item is a dictionary containing the optimized coefficients and information
about the time evolution of the system, with the following keys:
> optimized coefficients
> : These are the names of the requested optimized Hamiltonian coefficients, under
>   keys such as `[transmon_1_name].drive`, `[cavity_2_name].frequency`,
>   `[transmon_2_name]_[cavity_1_name]_interaction.dispersive_shift`, and
>   `[cavity_1_name]_[cavity_2_name]_interaction.cross_kerr_coefficient` (where
>   `[transmon_n_name]` and  `[cavity_n_name]` are the names assigned to the
>   respective transmons or cavities).
>   If you pass a cutoff_frequency, the filtered versions of the
>   piecewise-constant coefficients are also included with keys such as
>   `[transmon_2_name].drive_filtered`.

  > `infidelity`
  > : The state/operational infidelity of the optimized evolution.

  > `sample_times`
  > : The times at which the system’s evolution is sampled,
  >   as an array of shape `(T,)`.

  > `unitaries`
  > : The system’s unitary time-evolution operators at each sample time,
  >   as an array of shape `(T, D, D)`.

  > `state_evolution`
  > : The time evolution of the initial state at each sample time,
  >   as an array of shape `(T, D)`.
  >   This is only returned if you provide an initial state.
### Return type
dict

### SEE ALSO
[`boulderopal.superconducting.simulate`](https://docs.q-ctrl.com/references/boulder-opal/boulderopal/superconducting/simulate.md#boulderopal.superconducting.simulate)
: Simulate a system composed of transmons and cavities.

## Notes

The Hamiltonian of the system is of the form

$$
H = \sum_i H_{\mathrm{transmon}_i}
    + \sum_i H_{\mathrm{cavity}_i}
    + \sum_{i,j} H_{\mathrm{transmon}_i-\mathrm{transmon}_j}
    + \sum_{i,j} H_{\mathrm{transmon}_i-\mathrm{cavity}_j}
    + \sum_{i,j} H_{\mathrm{cavity}_i-\mathrm{cavity}_j}
$$

where i and j mark the i-th and j-th transmon or cavity.
For their definition of each Hamiltonian term, see its respective class.

The Hilbert space of the system is defined as the outer product of all the
transmon Hilbert spaces (in the order they’re provided in transmons) with
the cavity Hilbert spaces (in the order they’re provided in cavities), that is:

$$
\mathcal{H} =
    \mathcal{H}_{\mathrm{transmon}_1} \otimes \mathcal{H}_{\mathrm{transmon}_2}
    \otimes \ldots
    \otimes \mathcal{H}_{\mathrm{cavity}_1} \otimes \mathcal{H}_{\mathrm{cavity}_2}
    \otimes \ldots
$$

The system dimension D is then the product of all transmon and cavity dimensions.

If you provide an initial_state and a target_state, the optimization cost is defined as the
infidelity of the state transfer process,

$$
\mathcal{I}
    = 1 - \left|
        \langle
            \Psi_\mathrm{target} | U(t_\mathrm{gate}) | \Psi_\mathrm{initial}
        \rangle
    \right|^2 ,
$$

where $U(t)$ is the unitary time-evolution operator generated by the Hamiltonian.

If you provide a target_operation, the optimization cost is defined as the operational
infidelity,

$$
\mathcal{I}
    = 1 - \left| \frac
        {\mathrm{Tr} (U_\mathrm{target}^\dagger U(t_\mathrm{gate}))}
        {\mathrm{Tr} (U_\mathrm{target}^\dagger U_\mathrm{target})}
    \right|^2 .
$$

See the [superconducting systems namespace classes](https://docs.q-ctrl.com/boulder-opal/references/boulder-opal/boulderopal/superconducting.html)
for a list of the relevant objects to describe subsystems and optimizable coefficients.
