# create a pulse and send it to the Q-CTRL cloud
import numpy as np
from qctrl import Qctrl
import os

import matplotlib.pyplot as plt
import numpy as np

from qctrlvisualizer import get_qctrl_style, plot_controls

qctrl = Qctrl(email=os.getenv('EMAIL'), password=os.getenv('PASSWORD'))

control_count = 5
segment_count = 16
duration = 30.0
shot_count = 32

controls = []
for k in range(control_count):
    # Create a random string of complex numbers for each controls.
    real_part = np.random.random(size=[segment_count])
    imag_part = np.random.random(size=[segment_count])
    print(real_part, imag_part)
    values = 0.15 * k * (real_part + 1j * imag_part)
    print(values)

    controls.append({"duration": duration, "values": values})

# Plot the last control as an example.
plot_controls(
    figure=plt.figure(),
    controls={
        "$\Omega$": [
            {"duration": duration / segment_count, "value": value} for value in values
        ]
    },
)

# Obtain the results of the experiment.
experiment_results = qctrl.functions.calculate_qchack_measurements(
    controls=controls,
    shot_count=shot_count,
)

measurements = experiment_results.measurements
for k, measurement_counts in enumerate(measurements):
    print(f"control #{k}: {measurement_counts}")