# ms_optimize

Source: https://docs.q-ctrl.com/references/boulder-opal/boulderopal/ions/ms_optimize

```python
boulderopal.ions.ms_optimize(drives, duration, lamb_dicke_parameters, relative_detunings, target_phases, sample_count=128, robust=False, **optimization_kwargs)
```

Find optimal pulses to perform a target Mølmer–Sørensen-type operation
on a system composed of ions.

This function builds a graph describing the Mølmer–Sørensen operation
and calls boulderopal.run_optimization to minimize the target cost.

### Parameters
* **drives** (*list* *[*[*OptimizableDrive*](https://docs.q-ctrl.com/references/boulder-opal/boulderopal/ions/OptimizableDrive.md#boulderopal.ions.OptimizableDrive) *]*) – A list of optimizable drives addressing the ions.
  Each ion can only be addressed by a single drive,
  but there may be ions not addressed by any drive.
* **duration** (*float*) – The duration, in seconds, of the dynamics to be optimized, $T$.
  It must be greater than zero.
* **lamb_dicke_parameters** (*np.ndarray*) – The laser-ion coupling strength, $\{\eta_{jkl}\}$.
  Its shape must be `(3, N, N)`, where the dimensions indicate,
  respectively, axis, collective mode, and ion.
* **relative_detunings** (*np.ndarray*) – The difference $\{\delta_{jk} = \nu_{jk} - \delta\}$ (in Hz) between each motional
  mode frequency and the laser detuning from the qubit transition frequency $\omega_0$.
  Its shape must be `(3, N)`, where the dimensions indicate, respectively,
  axis and collective mode.
* **target_phases** (*np.ndarray* *or* *None* *,* *optional*) – The target total relative phases between ion pairs, $\{\Psi_{ln}\}$,
  as a strictly lower triangular matrix of shape `(N, N)`.
  $\Psi_{ln}$ with $l > n$ indicates the relative phase between ions
  $l$ and $n$, while $\Psi_{ln} = 0$ for $l \leq n$.
* **sample_count** (*int* *,* *optional*) – The number of times $T$ between 0 and duration (both included)
  at which the evolution is sampled.
  Defaults to 128.
* **robust** (*bool* *,* *optional*) – If set to False, the cost corresponds to the infidelity at the end of the gate.
  If set to True, the cost is the final infidelity plus a dephasing-robust cost term.
  Defaults to False.
* `\*\*optimization_kwargs` – Additional parameters to pass to boulderopal.run_optimization.
### Returns
The result of the run_optimization call.
Its `output` item is a dictionary containing information about
the optimized drive and the evolution of the system, with the following keys:
> optimized drives
> : The piecewise-constant optimized drives implementing the gate.
>   The keys are the names of the drives provided to the function.

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

  > `phases`
  > : Acquired phases $\{\Phi_{ln}(t_i) = \phi_{ln}(t_i) + \phi_{nl}(t_i)\}$
  >   for each sample time and for all ion pairs, as a strictly lower triangular
  >   matrix of shape `(T, N, N)`.
  >   $\Phi_{ln}(t_i)$ with $l > n$ indicates the relative phase between ions
  >   $l$ and $n$, while $\Phi_{ln}(t_i) = 0$ for $l \leq n$.

  > `displacements`
  > : Displacements $\{\eta_{jkl}\alpha_{jkl}(t_i)\}$ for all mode-ion combinations,
  >   as an array of shape `(T, 3, N, N)`, where the dimensions indicate, respectively,
  >   time, axis, collective mode, and ion.

  > `infidelities`
  > : A 1D array of length `T` with the operational infidelities of
  >   the Mølmer–Sørensen gate at each sample time, $\mathcal{I}(t_i)$.
### Return type
dict

### SEE ALSO
[`boulderopal.ions.ComplexOptimizableDrive`](https://docs.q-ctrl.com/references/boulder-opal/boulderopal/ions/ComplexOptimizableDrive.md#boulderopal.ions.ComplexOptimizableDrive)
: Class describing a piecewise-constant complex-valued optimizable drive.

[`boulderopal.ions.RealOptimizableDrive`](https://docs.q-ctrl.com/references/boulder-opal/boulderopal/ions/RealOptimizableDrive.md#boulderopal.ions.RealOptimizableDrive)
: Class describing a piecewise-constant real-valued optimizable drive.

[`boulderopal.ions.ms_simulate`](https://docs.q-ctrl.com/references/boulder-opal/boulderopal/ions/ms_simulate.md#boulderopal.ions.ms_simulate)
: Simulate a Mølmer–Sørensen-type operation on a trapped ions system.

[`boulderopal.ions.obtain_ion_chain_properties`](https://docs.q-ctrl.com/references/boulder-opal/boulderopal/ions/obtain_ion_chain_properties.md#boulderopal.ions.obtain_ion_chain_properties)
: Calculate the properties of an ion chain.

## Notes

See the notes of [`boulderopal.ions.ms_simulate`](https://docs.q-ctrl.com/references/boulder-opal/boulderopal/ions/ms_simulate.md#boulderopal.ions.ms_simulate) for the main equations and definitions.

You can use the robust flag to construct a Mølmer–Sørensen gate that is
robust against dephasing noise. This imposes a symmetry <sup>[1](#id2)</sup> in the optimizable
ion drives and aims to minimize the time-averaged positions of the phase-space
trajectories,

$$
\langle \alpha_{jkl} \rangle
    = \frac{1}{t_\text{gate}} \int_0^{t_\text{gate}}
        \alpha_{jkl}(t) \mathrm{d} t .
$$

This is achieved by adding an additional term to the cost function,
consisting of the sum of the square moduli of the time-averaged positions
multiplied by the corresponding Lamb–Dicke parameters. That is to say,

$$
C_\text{robust} = \mathcal{I} + \sum_{j=1}^{3} \sum_{k=1}^{N} \sum_{l=1}^{N}
        \left| \eta_{jkl} \langle \alpha_{jkl} \rangle \right|^2 .
$$

## References

* <a id='id2'>**[1]**</a> [C. D. B. Bentley, H. Ball, M. J. Biercuk, A. R. R. Carvalho, M. R. Hush, and H. J. Slatyer, Advanced Quantum Technologies 3, 2000044 (2020).](https://doi.org/10.1002/qute.202000044)
