Exemple #1
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)
Exemple #2
0
def scalable_circuit(func):
    """Makes a scalable circuit interactive. Function must take 
    qc (QuantumCircuit) and number of qubits (int) as positional inputs"""
    from qiskit import QuantumCircuit
    def interactive_function(n):
        qc = QuantumCircuit(n)
        func(qc, n)
        return qc.draw('mpl')
    
    from ipywidgets import IntSlider
    # Ideally this would use `interact` from ipywidgets but this is
    # incompatible with thebe lab
    image = _img()
    n_slider = IntSlider(min=1,max=8,step=1,value=4)
    image.value = interactive_function(n_slider.value)
    def update_output(b):
        image.value = interactive_function(n_slider.value)
    n_slider.observe(update_output)
    display(n_slider)
    display(image.widget)
    def __init__(self,initialize, success_condition, allowed_gates, vi, qubit_names, eps=0.1, backend=None, shots=1024,mode='circle',verbose=False):
        """
        initialize
            List of gates applied to the initial 00 state to get the starting state of the puzzle.
            Supported single qubit gates (applied to qubit '0' or '1') are 'x', 'y', 'z', 'h', 'ry(pi/4)'.
            Supported two qubit gates are 'cz' and 'cx'. Specify only the target qubit.
        success_condition
            Values for pauli observables that must be obtained for the puzzle to declare success.
        allowed_gates
            For each qubit, specify which operations are allowed in this puzzle. 'both' should be used only for operations that don't need a qubit to be specified ('cz' and 'unbloch').
            Gates are expressed as a dict with an int as value. If this is non-zero, it specifies the number of times the gate is must be used (no more or less) for the puzzle to be successfully solved. If the value is zero, the player can use the gate any number of times.
        vi
            Some visualization information as a three element list. These specify:
            * which qubits are hidden (empty list if both shown).
            * whether both circles shown for each qubit (use True for qubit puzzles and False for bit puzzles).
            * whether the correlation circles (the four in the middle) are shown.
        qubit_names
            The two qubits are always called '0' and '1' from the programming side. But for the player, we can display different names.
        eps=0.1
            How close the expectation values need to be to the targets for success to be declared.
        backend=Aer.get_backend('aer_simulator')
            Backend to be used by Qiskit to calculate expectation values (defaults to local simulator).
        shots=1024
            Number of shots used to to calculate expectation values.
        mode='circle'
            Either the standard 'Hello Quantum' visualization can be used (with mode='circle'), or the extended one (mode='y') or the alternative line based one (mode='line').
        y_boxes = False
            Whether to show expectation values involving y.
        verbose=False
        """
        def get_total_gate_list():
            # Get a text block describing allowed gates.

            total_gate_list = ""
            for qubit in allowed_gates:
                gate_list = ""
                for gate in allowed_gates[qubit]:
                    if required_gates[qubit][gate] > 0 :
                        gate_list += '  ' + gate+" (use "+str(required_gates[qubit][gate])+" time"+"s"*(required_gates[qubit][gate]>1)+")"
                    elif allowed_gates[qubit][gate]==0:
                        gate_list += '  '+gate + ' '
                if gate_list!="":
                    if qubit=="both" :
                        gate_list = "\nAllowed symmetric operations:" + gate_list
                    else :
                        gate_list = "\nAllowed operations for " + qubit_names[qubit] + ":\n" + " "*10 + gate_list
                    total_gate_list += gate_list +"\n"
            return total_gate_list

        def get_success(required_gates):
            # Determine whether the success conditions are satisfied, both for expectation values, and the number of gates to be used.

            success = True
            grid.get_rho()
            if verbose:
                print(grid.rho)
            for pauli in success_condition:
                success = success and (abs(success_condition[pauli] - grid.rho[pauli])<eps)
            for qubit in required_gates:
                for gate in required_gates[qubit]:
                    success = success and (required_gates[qubit][gate]==0)
            return success

        def show_circuit():
            gates = get_total_gate_list

        def get_command(gate,qubit):
            # For a given gate and qubit, return the string describing the corresoinding Qiskit string.

            if qubit=='both':
                qubit = '1'
            qubit_name = qubit_names[qubit]
            for name in qubit_names.values():
                if name!=qubit_name:
                    other_name = name
            # then make the command (both for the grid, and for printing to screen)
            if gate in ['x','y','z','h']:
                real_command  = 'grid.qc.'+gate+'(grid.qr['+qubit+'])'
                clean_command = 'qc.'+gate+'('+qubit_name+')'
            elif gate in ['ry(pi/4)','ry(-pi/4)']:
                real_command  = 'grid.qc.ry('+'-'*(gate=='ry(-pi/4)')+'np.pi/4,grid.qr['+qubit+'])'
                clean_command = 'qc.ry('+'-'*(gate=='ry(-pi/4)')+'np.pi/4,'+qubit_name+')'
            elif gate in ['rx(pi/4)','rx(-pi/4)']:
                real_command  = 'grid.qc.rx('+'-'*(gate=='rx(-pi/4)')+'np.pi/4,grid.qr['+qubit+'])'
                clean_command = 'qc.rx('+'-'*(gate=='rx(-pi/4)')+'np.pi/4,'+qubit_name+')'
            elif gate in ['cz','cx','swap']:
                real_command  = 'grid.qc.'+gate+'(grid.qr['+'0'*(qubit=='1')+'1'*(qubit=='0')+'],grid.qr['+qubit+'])'
                clean_command = 'qc.'+gate+'('+other_name+','+qubit_name+')'
            return [real_command,clean_command]

        bloch = [None]

        # set up initial state and figure
        if mode=='y':
            grid = pauli_grid(backend=backend,shots=shots,mode='circle',y_boxes=True)
        else:
            grid = pauli_grid(backend=backend,shots=shots,mode=mode)
        for gate in initialize:
            eval( get_command(gate[0],gate[1])[0] )

        required_gates = copy.deepcopy(allowed_gates)

        # determine which qubits to show in figure
        if allowed_gates['0']=={} : # if no gates are allowed for qubit 0, we know to only show qubit 1
                shown_qubit = 1
        elif allowed_gates['1']=={} : # and vice versa
                shown_qubit = 0
        else :
                shown_qubit = 2

        # show figure
        grid_view = _img()
        grid.update_grid(bloch=bloch[0],hidden=vi[0],qubit=vi[1],corr=vi[2],message=get_total_gate_list(),output=grid_view)
        display(grid_view.widget)



        description = {'gate':['Choose gate'],'qubit':['Choose '+'qu'*vi[1]+'bit'],'action':['Make it happen!']}

        all_allowed_gates_raw = []
        for q in ['0','1','both']:
            all_allowed_gates_raw += list(allowed_gates[q])
        all_allowed_gates_raw = list(set(all_allowed_gates_raw))

        all_allowed_gates = []
        for g in ['bloch','unbloch']:
            if g in all_allowed_gates_raw:
                all_allowed_gates.append( g )
        for g in ['x','y','z','h','cz','cx']:
            if g in all_allowed_gates_raw:
                all_allowed_gates.append( g )
        for g in all_allowed_gates_raw:
            if g not in all_allowed_gates:
                all_allowed_gates.append( g )

        gate = widgets.ToggleButtons(options=description['gate']+all_allowed_gates)
        qubit = widgets.ToggleButtons(options=[''])
        action = widgets.ToggleButtons(options=[''])

        boxes = widgets.VBox([gate,qubit,action])
        display(boxes)
        self.program = []

        def given_gate(a):
            # Action to be taken when gate is chosen. This sets up the system to choose a qubit.

            if gate.value:
                if gate.value in allowed_gates['both']:
                    qubit.options = description['qubit'] + ["not required"]
                    qubit.value = "not required"
                else:
                    allowed_qubits = []
                    for q in ['0','1']:
                        if (gate.value in allowed_gates[q]) or (gate.value in allowed_gates['both']):
                            allowed_qubits.append(q)
                    allowed_qubit_names = []
                    for q in allowed_qubits:
                        allowed_qubit_names += [qubit_names[q]]
                    qubit.options = description['qubit'] + allowed_qubit_names

        def given_qubit(b):
            # Action to be taken when qubit is chosen. This sets up the system to choose an action.

            if qubit.value not in ['',description['qubit'][0],'Success!']:
                action.options = description['action']+['Apply operation']

        def given_action(c):
            # Action to be taken when user confirms their choice of gate and qubit.
            # This applied the command, updates the visualization and checks whether the puzzle is solved.

            if action.value not in ['',description['action'][0]]:
                # apply operation
                if action.value=='Apply operation':
                    if qubit.value not in ['',description['qubit'][0],'Success!']:
                        # translate bit gates to qubit gates
                        if gate.value=='NOT':
                            q_gate = 'x'
                        elif gate.value=='CNOT':
                            q_gate = 'cx'
                        else:
                            q_gate = gate.value
                        if qubit.value=="not required":
                            q = qubit_names['1']
                        else:
                            q = qubit.value
                        q01 = '0'*(qubit.value==qubit_names['0']) + '1'*(qubit.value==qubit_names['1']) + 'both'*(qubit.value=="not required")
                        if q_gate in ['bloch','unbloch']:
                            if q_gate=='bloch':
                                bloch[0] = q01
                            else:
                                bloch[0] = None
                        else:
                            command = get_command(q_gate,q01)
                            eval(command[0])
                            self.program.append( command[1] )
                        if required_gates[q01][gate.value]>0:
                            required_gates[q01][gate.value] -= 1

                        grid.update_grid(bloch=bloch[0],hidden=vi[0],qubit=vi[1],corr=vi[2],message=get_total_gate_list(),output=grid_view)

                success = get_success(required_gates)
                if success:
                    gate.options = ['Success!']
                    qubit.options = ['Success!']
                    action.options = ['Success!']
                    plt.close(grid.fig)
                else:
                    gate.value = description['gate'][0]
                    qubit.options = ['']
                    action.options = ['']

        gate.observe(given_gate)
        qubit.observe(given_qubit)
        action.observe(given_action)
Exemple #4
0
def bv_widget(nqubits, hidden_string, display_ancilla=False, hide_oracle=True):
    if nqubits < 1:
        print("nqubits must be 1 or greater, setting to 1.")
        nqubits = 1
    if nqubits < len(hidden_string):
        difference = len(hidden_string) - nqubits
        hidden_string = hidden_string[difference:]
        print("Error: s is too long, trimming the first %i bits and using '%s' instead." % (difference, hidden_string))
    import numpy as np
    from qiskit_textbook.tools import num_to_latex, array_to_latex
    from qiskit import QuantumCircuit, Aer, execute
    backend = Aer.get_backend('statevector_simulator')
    nqubits += 1
    if hide_oracle:
        oracle_qc = QuantumCircuit(nqubits)
        q = 0
        for char in hidden_string:
            if char == "1":
                oracle_qc.cx(q,nqubits-1)
            q += 1
        oracle_gate = oracle_qc.to_gate()
    qc = QuantumCircuit(nqubits)
    qc.h(nqubits-1)
    qc.z(nqubits-1)
    class Message():
        def __init__(self):
            if display_ancilla:
                self.ops = "|{-}\\rangle\\otimes|" + "0"*(nqubits-1) + "\\rangle"
                self.vec = "|{-}\\rangle\\otimes|" + "0"*(nqubits-1) + "\\rangle"
            else:
                self.ops = "|" + "0"*(nqubits-1) + "\\rangle"
                self.vec = "|" + "0"*(nqubits-1) + "\\rangle"
    
    msg = Message()
    def vec_in_braket(vec, nqubits):
        scalfac = ""
        tensorfac = ""
        state = ""
        # Factor out separable 'output' qubit if possible
        if nqubits > 1:
            vfirst = vec[:2**nqubits//2]
            vlast = vec[2**nqubits//2:]
            if np.allclose(vfirst, 0):
                vec = vlast
                tensorfac += "|1\\rangle"
                nqubits -= 1
            elif np.allclose(vlast, 0):
                vec = vfirst
                tensorfac += "|0\\rangle"
                nqubits -= 1
            elif np.allclose(vfirst, vlast):
                vec = vfirst*np.sqrt(2)
                tensorfac += "|{+}\\rangle"
                nqubits -= 1
            elif np.allclose(vfirst, -vlast):
                vec = vfirst*np.sqrt(2)
                tensorfac += "|{-}\\rangle"
                nqubits -= 1

        if np.allclose(np.abs(vec), np.abs(vec[0])):
            scalfac = num_to_latex(vec[0])
            vec = vec/vec[0]

        for i in range(len(vec)):
            if not np.isclose(vec[i], 0):
                basis = format(i, 'b').zfill(nqubits)
                if not np.isclose(vec[i], 1):
                    if np.isclose(vec[i], -1):
                        if state.endswith("+ "):
                            state = state[:-2]
                        state += "-"
                    else:
                        state += num_to_latex(vec[i])
                state += "|" + basis +"\\rangle + "
        state = state.replace("j", "i")
        state = state[:-2]
        if len(state) > 5000:
            return "\\text{(Too large to display)}"
        if scalfac != "" or (tensorfac != "" and len(state)>(9+nqubits) and display_ancilla):
            state = ("(%s)" % state)
        if scalfac != "":
            state = scalfac + state
        if tensorfac != "" and display_ancilla:
            state =  tensorfac + "\otimes" + state
        return state


    def hadamards(qc, nqubits):
        for q in range(nqubits-1):
            qc.h(q)

    def oracle(qc, nqubits):
        if hide_oracle:
            qc.append(oracle_gate, range(nqubits))
        else:
            qc.barrier()
            q = 0
            for char in hidden_string:
                if char == "1":
                    qc.cx(q,nqubits-1)
                q += 1
            qc.barrier()
    
    def update_output():
        statevec = execute(qc, backend).result().get_statevector()
        msg.vec = vec_in_braket(statevec, nqubits)
        html_math.value = "$$ %s = %s $$" % (msg.ops, msg.vec)
        image.value = qc.draw('mpl')
    
    def on_hads_click(b):
        hadamards(qc, nqubits)
        if display_ancilla:
            msg.ops = "|{-}\\rangle\\otimes H^{\\otimes n}" + msg.ops[18:]
        else:
            msg.ops = "H^{\\otimes n}" + msg.ops            
        update_output()
    def on_oracle_click(b):
        oracle(qc, nqubits)
        if display_ancilla:
            msg.ops = "|{-}\\rangle\\otimes U_f" + msg.ops[18:]
        else:
            msg.ops = "U_f" + msg.ops
        update_output()
    
    def on_clear_click(b):
        for i in range(len(qc.data)-2):
            qc.data.pop()
        msg.__init__()
        update_output()
    
    hads_btn = widgets.Button(description="H⊗ⁿ")
    hads_btn.on_click(on_hads_click)
    oracle_btn = widgets.Button(description="Oracle")
    oracle_btn.on_click(on_oracle_click)
    clear_btn = widgets.Button(description="Clear")
    clear_btn.on_click(on_clear_click)
        
    hbox = widgets.HBox([hads_btn, oracle_btn, clear_btn])
    html_math = widgets.HTMLMath()
    html_math.value = "$$ %s = %s $$" % (msg.ops, msg.vec)
    image = _img()
    image.value = qc.draw('mpl')
    display(hbox, html_math, image.widget)
Exemple #5
0
def gate_demo(gates='full',qsphere=True):
    from qiskit import QuantumCircuit, execute, Aer
    from qiskit.visualization import plot_bloch_multivector, plot_state_qsphere
    gate_list = []
    showing_rz = False
    if 'pauli' in gates:
        gate_list += ['X','Y','Z']
    if '+h' in gates:
        gate_list.append('H')
    if '+rz' in gates:
        showing_rz = True
    if gate_list == [] or gates == 'full':
        gate_list = ['I','X','Y','Z','H','S','Sdg','T','Tdg']
        showing_rz = True

    backend = Aer.get_backend('statevector_simulator')
    qc = QuantumCircuit(1)
    button_list = [widgets.Button(description=gate, layout=widgets.Layout(width='3em', height='3em')) for gate in gate_list]
    button_list.append(widgets.Button(description='Reset', layout=widgets.Layout(width='6em', height='3em')))
    image = _img()
    def update_output():
        out_state = execute(qc,backend).result().get_statevector()
        if qsphere: 
            image.value = plot_state_qsphere(out_state)
        else:
            image.value = plot_bloch_multivector(out_state)

    def apply_gates(b,qc):
        functionmap = {
            'X':qc.x,
            'Y':qc.y,
            'Z':qc.z,
            'H':qc.h,
            'S':qc.s,
            'T':qc.t,
            'Sdg':qc.sdg,
            'Tdg':qc.tdg,
        }
        if b.description == 'I':
            pass
        elif b.description == 'Reset':
            qc.data = []
        elif b.description == 'Rz':
                qc.rz(zrot_slider.value,0)
        else:
            functionmap[b.description](0)

    def on_button_click(b):
        apply_gates(b,qc)
        update_output()

    for button in button_list:
        button.on_click(on_button_click)
    if showing_rz:
        rz_button = widgets.Button(description='Rz', layout=widgets.Layout(width='3em', height='3em'))
        rz_button.on_click(on_button_click)
        zrot_slider = widgets.FloatSlider(value=pi,
                                         min= -pi,
                                         max= pi,
                                         disabled=False,
                                         readout_format='.2f')
    qc = QuantumCircuit(1)
    update_output()

    if showing_rz:
        top_box = widgets.HBox(button_list)
        bottom_box = widgets.HBox([rz_button, zrot_slider])
        main_box = widgets.VBox([top_box, bottom_box])
    else:
        main_box = widgets.HBox(button_list)

    display(main_box)
    display(image.widget)
Exemple #6
0
        update_output()
    def on_oracle_click(b):
        apply_oracle(qc, nqubits)
        if display_ancilla:
            msg.ops = "|{-}\\rangle\\otimes U_f" + msg.ops[18:]
        else:
            msg.ops = "U_f" + msg.ops
        update_output()
    
    def on_clear_click(b):
        for i in range(len(qc.data)-2):
            qc.data.pop()
        msg.__init__()
        update_output()
    
    hads_btn = widgets.Button(description="H⊗ⁿ")
    hads_btn.on_click(on_hads_click)
    oracle_btn = widgets.Button(description="Oracle")
    oracle_btn.on_click(on_oracle_click)
    clear_btn = widgets.Button(description="Clear")
    clear_btn.on_click(on_clear_click)
        
    hbox = widgets.HBox([hads_btn, oracle_btn, clear_btn])
    html_math = widgets.HTMLMath()
    html_math.value = "$$ %s = %s $$" % (msg.ops, msg.vec)
    image = _img()
    image.value = qc.draw('mpl')
    display(hbox, html_math, image.widget)