def test_spectrum_invalid_energies():
    """Test if function `plot.spectrum` raises a ``ValueError`` when the number of sampled energies
    is less than two"""
    with pytest.raises(
            ValueError,
            match="Number of sampled energies must be at least two"):
        plot.spectrum([1000.0])
def run_and_create(n):
    inputfile = open('./Task2Code/input_for_exercise3.txt', "r")

    N = int(inputfile.readline())  #Number of Atoms
    nmodes = 3 * N - 6  #Number of Modes

    w = np.zeros(nmodes, float)  #vib. frequencies of ground electronic state
    wp = np.zeros(nmodes, float)  #vib. frequencies of excited electronic state
    Ud = np.zeros((nmodes, nmodes), float)  #Duschinsky Matrix
    delta = np.zeros(nmodes, float)  #Displacement Vector

    ###############################
    ##### READ IN PARAMETERS ######
    ###############################

    for i in range(nmodes):
        w[i] = float(inputfile.readline())
    for i in range(nmodes):
        wp[i] = float(inputfile.readline())
    for i in range(nmodes):
        for j in range(nmodes):
            Ud[i, j] = float(inputfile.readline())

    for i in range(nmodes):
        delta[i] = float(inputfile.readline())

    T = 500  # temperature

    #####################################
    ##### CALCULATE GBS PARAMETERS ######
    #####################################

    t, U1, r, U2, alpha = vibronic.gbs_params(w, wp, Ud, delta, T)

    ###############################
    ###### GENERATE SAMPLES #######
    ###############################

    nr_samples = n
    s = sample.vibronic(t, U1, r, U2, alpha, nr_samples)
    e = vibronic.energies(s, w, wp)

    ###############################
    ######## PLOT SPECTRUM ########
    ###############################

    spectrum = plot.spectrum(e, xmin=-300, xmax=2000)
    offline.plot(spectrum, filename="spectrum_{}_samples.html".format(n))
print(np.around(e[:5], 4))  # 4 decimal precision

##############################################################################
# Once the GBS parameters have been obtained, it is straightforward to run the GBS algorithm: we
# generate many samples, compute their energies, and make a histogram of the observed energies.
# The :mod:`~.apps.sample` module contains the function :func:`~.vibronic`, which is tailored for
# use in vibronic spectra applications. Similarly, the :mod:`~.apps.plot` module includes a
# :func:`~.spectrum` function that generates the vibronic spectrum from the GBS samples. Let's see
# how this is done for just a few samples:

from strawberryfields.apps import sample, plot
import plotly
nr_samples = 10
s = sample.vibronic(t, U1, r, U2, alpha, nr_samples)
e = vibronic.energies(s, w, wp)
spectrum = plot.spectrum(e, xmin=-1000, xmax=8000)
plotly.offline.plot(spectrum, filename="spectrum.html")

##############################################################################
# .. raw:: html
#     :file: ../../examples_apps/spectrum.html
#
# .. note::
#     The command ``plotly.offline.plot()`` is used to display plots in the documentation. In
#     practice, you can simply use ``spectrum.show()`` to generate the figure.

##############################################################################
# The bars in the plot are the histogram of energies. The curve surrounding them is a Lorentzian
# broadening of the spectrum, which better represents the observations from an actual experiment.
# Of course, 10 samples are not enough to accurately reconstruct the vibronic spectrum. Let's
# instead use the 20,000 pre-generated samples from the :mod:`~.apps.data` module.
for i in range(nmodes):
    for j in range(nmodes):
        Ud[i,j]=float(inputfile.readline())

for i in range(nmodes):
    delta[i]=float(inputfile.readline())

T = 500  # temperature

#####################################
##### CALCULATE GBS PARAMETERS ######
#####################################

t, U1, r, U2, alpha = vibronic.gbs_params(w, wp, Ud, delta, T)


###############################
###### GENERATE SAMPLES #######
###############################

nr_samples = 20000
s = sample.vibronic(t, U1, r, U2, alpha, nr_samples)
e = vibronic.energies(s, w, wp)

###############################
######## PLOT SPECTRUM ########
###############################

spectrum = plot.spectrum(e, xmin=-300, xmax=2000)
offline.plot(spectrum, filename="spectrum.html")