Exemplo n.º 1
0
def binary_widget(nbits=5):
    nbits = max(min(10, nbits), 2) # Keep nbits between 2 and 10

    output = _pre()
    bits = ['0' for i in range(nbits)]
    button_list = [widgets.ToggleButton(description=str(2**i),
                                        layout=widgets.Layout(width='3em',height='3em'))
                   for i in reversed(range(nbits))]

    hbox = widgets.HBox(button_list)
    label1 = widgets.Label(value="Toggle the bits below to change the binary number.")
    label2 = widgets.Label(value="Think of a number between 0 and %i and try to write it down in binary." % ((2**nbits)-1))
    vbox = widgets.VBox([label1,label2,hbox])
    def on_btn_click(b):
        for b in range(nbits):
            if button_list[b].value:
                bits[b] = '1'
            else:
                bits[b] = '0'
        string = "".join(bits)
        output.value = "Binary" + " "*(nbits//2) + " Decimal\n " + string + " = " + str(int(string,2))

    for button in button_list: button.observe(on_btn_click)

    # Add output before button press
    string = "".join(bits)
    output.value = "Binary" + " "*(nbits//2) + " Decimal\n " + string + " = " + str(int(string,2))

    display(vbox)
    display(output.widget)
Exemplo n.º 2
0
def state_vector_exercise(target):
    output = _pre()
    button = widgets.Button(description="Check", layout=widgets.Layout(width='5em'))
    text_input = widgets.Text(value='[1, 0]',
                              placeholder='Type something',
                              width='50px',
                              disabled=False)

    label = widgets.Label(value="State Vector:")

    def on_button_click(b):
        try:
            state_vector = eval(text_input.value)
            c1, c2 = state_vector[0], state_vector[1]
        except Exception as e:
            output.value = str(e).split("(")[0]
            return

        squared_magnitude = abs(c1)**2 + abs(c2)**2
        p = abs(c1)**2
        if not (squared_magnitude < 1.01 and squared_magnitude > .99): # Close Enough
            output.value = "Magnitude is not equal to 1"
            return
        elif p > target*0.99 and p < target*1.01:
            output.value = "Correct!"
        else:
            output.value = "The absolute value of " + str(c1) + ", squared is not equal to " + str(target)

    hbox = widgets.HBox([text_input, button])
    vbox = widgets.VBox([label, hbox])
    button.on_click(on_button_click)

    display(vbox)
    display(output.widget)
Exemplo n.º 3
0
def bloch_calc():
    output = _pre()
    button = widgets.Button(description="Plot",
                            layout=widgets.Layout(width='4em'))
    theta_input = widgets.Text(label='$\\theta$',
                               placeholder='Theta',
                               disabled=False)
    phi_input = widgets.Text(label='$\phi$', placeholder='Phi', disabled=False)

    label = widgets.Label(
        value="Define a qubit state using $\\theta$ and $\phi$:")
    image = _img(value=plot_bloch_vector([0, 0, 1]))

    def on_button_click(b):
        from math import pi, sqrt
        try:
            theta = numexpr.evaluate(theta_input.value)
            phi = numexpr.evaluate(phi_input.value)
        except Exception as e:
            output.value = "Error: " + str(e)
            return
        x = sin(theta) * cos(phi)
        y = sin(theta) * sin(phi)
        z = cos(theta)
        # Remove horrible almost-zero results
        if abs(x) < 0.0001:
            x = 0
        if abs(y) < 0.0001:
            y = 0
        if abs(z) < 0.0001:
            z = 0
        output.value = "x = r * sin(" + theta_input.value + ") * cos(" + phi_input.value + ")\n"
        output.value += "y = r * sin(" + theta_input.value + ") * sin(" + phi_input.value + ")\n"
        output.value += "z = r * cos(" + theta_input.value + ")\n\n"
        output.value += "Cartesian Bloch Vector = [" + str(x) + ", " + str(
            y) + ", " + str(z) + "]"
        image.value = plot_bloch_vector([x, y, z])

    hbox = widgets.HBox([phi_input, button])
    vbox = widgets.VBox([label, theta_input, hbox])
    button.on_click(on_button_click)
    display(vbox)
    display(output.widget)
    display(image.widget)