Skip to content

Example: Preparing abitrary state

Vu Tuan Hai edited this page Apr 10, 2024 · 2 revisions

We aim to prepare the below state:

$$ |\psi\rangle = \dfrac{1}{\sqrt{2}}|000\rangle + \dfrac{i}{\sqrt{6}}|001\rangle - \dfrac{i}{\sqrt{3}}|010\rangle + \dfrac{1+i}{2\sqrt{2}}|011\rangle. $$

import numpy as np
from qoop.compilation.qsp import QuantumStatePreparation

This state can be declared by creating a dictionary

# Define the target state 
target_state = {
    "000": 1 / np.sqrt(2),
    "001": 1j / np.sqrt(6),
    "010": -1j / np.sqrt(3),
    "011": (1 + 1j) / (2 * np.sqrt(2)),
}

Then, use the default prepare() method.

# Run the compiler
compiler = QuantumStatePreparation.prepare(target_state)

# Call and draw circuit U
compiler.u.draw('mpl')

# Plot figure of metric
compiler.plot()