Skip to content

Example: Dynamic simulation

Vu Tuan Hai edited this page Feb 26, 2024 · 1 revision

In this post, we consider to prepare such unitary matrix:

$$ \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos(\theta) & 0 & \sin(\theta) \\ 0 & 0 & 1 & 0 \\ 0 & \sin(\theta) & 0 & -\cos(\theta) \\ \end{bmatrix} $$

import numpy as np
# Vector form
U = np.array(np.random.uniform(size=2**2))
# Matrix form
theta = np.pi/2
U = np.array([[1, 0, 0, 0], 
              [0, np.cos(theta), 0, np.sin(theta)], 
              [0, 0, 1, 0],
              [0, np.sin(theta), 0, -np.cos(theta)]]
)

We prepare U by the default ansatz $g2gn$, the fit() method and its default hyperparameter also called.

compiler = QuantumStatePreparation.prepare(U)

Then, the compiler stores $g2gn$ parameters, we can reconstruct $U$ by: $U = g2gn(\theta^*)$