# How to optimize controls robust to strong noise sources

**Design controls that are robust against strong time-dependent noise sources with stochastic optimization**

In this notebook we demonstrate how to apply stochastic optimization to design robust pulses using our highly-flexible optimization engine.

The stochastic optimizer is well suited to treat strong and non-static noises, allowing us to go beyond the weak noise regime required for using filter functions in robust optimization as illustrated in this tutorial. For stronger noise fields, typically neither the first-order Magnus approximation of the toggling frame Hamiltonian nor the second-order approximation of the time evolution operator hold.

## Summary workflow

#### 1. Define the system, variables to be optimized and cost function in the computational graph

The flexible Boulder Opal optimization engine expresses all optimization problems as data flow graphs, which describe how optimization variables (variables that can be tuned by the optimizer) are transformed into the cost function (the objective that the optimizer attempts to minimize).

You can define physical constraints, decide on which variables to optimize and provide a value range, include noise terms, and specify the cost function, which can be based on the target operator and the infidelity.

#### 2. Execute graph-based optimization

With the graph object created, an optimization can be run by providing the name of the graph node representing the cost function to be minimized, the names of the desired output nodes and the graph itself to the `qctrl.functions.calculate_stochastic_optimization`

function.

Optionally, you can also set the number of iterations to perform, after which the results with the lowest cost are returned (defaults to 1000), a target cost which early-stops the optimization when/if reached (else the optimizer runs until all iterations are completed), and an optimization algorithm (with Adam as the default).

The function returns the results of the optimization, which include the lowest cost achieved across all iterations, as well as the specified nodes evaluated at the optimized variables.

You can then use the qctrlvisualizer package to plot the optimized pulses, which are available in the result object.

## Worked example: Robust control of a qubit under strong noise

In this example we show how to find robust controls with the stochastic optimizer. The single-qubit system is represented by the following Hamiltonian:

\begin{align} H(t) &= (1 + \beta(t))(\gamma_x(t) \sigma_x + \gamma_y(t) \sigma_y) \end{align}

where $\gamma_x(t)$ and $\gamma_y(t)$ are the real time pulses and $\beta(t)$ is a stochastic noise process.

To perform a stochastic optimization, we assume that the stochastic process can be decomposed into 10 Fourier components

\begin{align} \beta(t) &= \sum_{i=1}^{10} a_i \cos(\omega_i t) + b_i \sin(\omega_i t) \end{align}

where $a_i$ and $b_i$ are amplitude samples of a normal distribution and $\omega_i$ are frequency samples of a uniform distribution. At each iteration step a new set of samples is taken.

This example uses the nodes `graph.random_uniform`

and `graph.random_normal`

to generate the stochastic process. These nodes provide random values sampled from uniform and normal probability distributions, respectively. Other ways of generating a stochastic process include using the `graph.random_choices`

node to shuffle your own set of numerical values (which you can sample from any distribution you want) or `graph.random_colored_noise_stf_signal`

to create a stochastic signal from a power spectral density.

The cost is calculated by averaging the infidelities corresponding to a batch of samples of the stochastic process. Averaging over a batch can lead to more reliable cost function estimates and gradients, which in turn can result in better overall optimization performance.

```
import matplotlib.pyplot as plt
import numpy as np
from qctrlvisualizer import get_qctrl_style, plot_controls
plt.style.use(get_qctrl_style())
from qctrl import Qctrl
# Starting a session with the API
qctrl = Qctrl()
```

```
# Define physical constraints
gamma_min = -np.pi
gamma_max = np.pi
segment_count = 20
duration = 2
sample_times = (0.5 + np.arange(segment_count)) * duration / segment_count
batch_size = 200
# Create graph object
graph = qctrl.create_graph()
gamma_x = graph.utils.real_optimizable_pwc_signal(
segment_count=segment_count,
minimum=gamma_min,
maximum=gamma_max,
duration=duration,
name="gamma_x",
)
gamma_y = graph.utils.real_optimizable_pwc_signal(
segment_count=segment_count,
minimum=gamma_min,
maximum=gamma_max,
duration=duration,
name="gamma_y",
)
noise_x_term = []
noise_y_term = []
# a cos(wt) + b sin(wt)
for _ in range(10):
a = graph.random_normal((batch_size, 1), mean=0.0, standard_deviation=0.05)
b = graph.random_normal((batch_size, 1), mean=0.0, standard_deviation=0.05)
omega = graph.random_uniform(
(batch_size, 1), lower_bound=np.pi, upper_bound=2 * np.pi
)
sampled_fourier = graph.pwc_signal(
values=a * graph.cos(omega * sample_times[None])
+ b * graph.sin(omega * sample_times[None]),
duration=duration,
)
noise_x_term.append(gamma_x * sampled_fourier * graph.pauli_matrix("X"))
noise_y_term.append(gamma_y * sampled_fourier * graph.pauli_matrix("Y"))
# Define Hamiltonian
total_noise_terms = graph.pwc_sum(noise_x_term) + graph.pwc_sum(noise_y_term)
hamiltonian = (
gamma_x * graph.pauli_matrix("X")
+ gamma_y * graph.pauli_matrix("Y")
+ total_noise_terms
)
# Create target
target_operator = graph.target(operator=graph.pauli_matrix("X"))
# Create infidelity
infidelities = graph.infidelity_pwc(hamiltonian, target_operator, name="infidelities")
cost = graph.sum(infidelities / batch_size, name="cost")
```

```
# Run the optimization
optimization_result = qctrl.functions.calculate_stochastic_optimization(
graph=graph,
cost_node_name="cost",
output_node_names=["gamma_x", "gamma_y", "infidelities"],
iteration_count=10000,
target_cost=1e-6,
)
print(f"\nOptimized cost:\t {optimization_result.best_cost:.3e}\n")
```

```
Your task calculate_stochastic_optimization (action_id="1164486") has started. You can use the `qctrl.get_result` method to retrieve previous results.
Your task calculate_stochastic_optimization (action_id="1164486") has completed.
Optimized cost: 2.497e-05
```

```
# Plot histogram of infidelities evaluated at the optimized variables
print(
f'Batch mean: {optimization_result.best_output["infidelities"]["value"].mean():.2e}, '
f'standard deviation: {optimization_result.best_output["infidelities"]["value"].std():.2e}'
)
plt.title("Batch of optimized infidelities")
plt.xlabel("Infidelities")
plt.ylabel("Count")
plt.hist(
optimization_result.best_output["infidelities"]["value"], bins="auto", ec="black"
)
plt.xlim([0.0, 1e-4])
plt.show()
```

```
Batch mean: 4.13e-05, standard deviation: 6.49e-05
```

```
# Plot the optimized controls
plot_controls(
plt.figure(),
controls={
"$\gamma_x$": optimization_result.best_output["gamma_x"],
"$\gamma_y$": optimization_result.best_output["gamma_y"],
},
)
plt.show()
```

This notebook was run using the following package versions. It should also be compatible with newer versions of the Q-CTRL Python package.

Package | version |
---|---|

Python | 3.9.12 |

matplotlib | 3.5.1 |

numpy | 1.21.5 |

scipy | 1.7.3 |

qctrl | 19.1.0 |

qctrlcommons | 17.1.1 |

qctrltoolkit | 1.5.0 |

qctrlvisualizer | 3.2.1 |