Exemple #1
0
    def bloch_field(self, time):
        """
        Plot magnetic field vector normalised to the bloch sphere
        """
        # seperate coordinates into axis set
        xb = self.fields[0](time)
        yb = self.fields[1](time)
        zb = self.fields[2](time)
        # add to list and normalise
        points = [list(xb), list(yb), list(zb)]
        sqsum = 0.0
        for i, point in enumerate(points[0]):
            # compute magnitude and store largest value
            sqsum_test = np.sqrt(points[0][i]**2 + points[1][i]**2 +
                                 points[2][i]**2)
            if sqsum_test > sqsum:
                sqsum = sqsum_test

        points = points / sqsum

        # create Bloch sphere instance
        bloch = Bloch()
        # add points and show
        bloch.add_points(points)
        bloch.show()
Exemple #2
0
def func(f1, b, set):
    """
	This function Perform the gate application on the initial state of qubit and then the state tomography,
	at the same time compute the analytical bloch state.
	INCOME:
	f1="string", b="string"
	fig_data="svg",fig_data2="svg"
	"""
    #### Create the bloch-sphere on qutip
    b1 = Bloch()
    b2 = Bloch()
    ## create the analyical quantum state and perform tranformation
    ## Add states to the bloch sphere and plot it
    b1.add_states(analytic(f1, b))
    if not set:
        states = qtomography(f1, b)
        b2.add_vectors(states)
    else:
        ### implements for
        states = qtomography_s(f1, b)
        for i in states:
            b2.add_points(i)
        b2.add_vectors(np.average(states, axis=0))
        ###
    b2.render()
    fig_data = print_figure(b2.fig, 'svg')
    b1.render()
    fig_data2 = print_figure(b1.fig, 'svg')

    #b1.show()
    #b2.show()
    b1.clear()
    b2.clear()
    return fig_data, fig_data2
Exemple #3
0
    def bloch_plot(self, filename, points=None, save=False):
        """
        Plot the current state on the Bloch sphere using
        qutip. 
        """
        if points is None:
            # convert current state into density operator
            rho = np.outer(self.state.H, self.state)
            # get Bloch vector representation
            points = self.get_bloch_vec(rho)
            # Can only plot systems of dimension 2 at this time
            assert len(
                points
            ) == 3, "System dimension must be spin 1/2 for Bloch sphere plot"

        # create instance of 3d plot
        bloch = Bloch(fig=1, figsize=[9, 9], view=[190, 10])
        # add state
        bloch.add_points(points)
        bloch.render()

        if save is True:
            print('Bloch plot saved to Sim Results folder')
            path = 'C:/Users/Boundsy/Desktop/Uni Work/PHS2360/Sim Results/' + str(
                filename) + '.png'
            bloch.fig.savefig(path, dpi=800, transparent=True)
def draw_sphere_zenith(zenith_points, hv_points, root):
    b = Bloch()
    b.point_color = ['m', 'k', 'g', 'b', 'w', 'c', 'y', 'r']
    b.zlabel = ['$z$', '']
    b.point_marker = ['o']
    b.point_size = [30]
    b.frame_width = 1.2
    fig = plt.figure(figsize=(20, 20))
    b.fig = fig

    x = (basis(2, 0) + (1 + 0j) * basis(2, 1)).unit()
    y = (basis(2, 0) + (0 + 1j) * basis(2, 1)).unit()
    z = (basis(2, 0) + (0 + 0j) * basis(2, 1)).unit()
    b.add_states([x, y, z])

    for i in range(len(zenith_points)):
        # Transform xyz to zxy coordinates
        tmp1 = np.array(
            [zenith_points[i][2], zenith_points[i][0], zenith_points[i][1]])
        tmp2 = np.vstack(
            [hv_points[i][:, 2], hv_points[i][:, 0], hv_points[i][:, 1]]).T
        tmp = np.vstack([tmp1, -tmp1, tmp2]).T
        b.add_points(tmp)

    # tmp1 = np.array([zenith_points[-1][2], zenith_points[-1][0], zenith_points[-1][1]])
    # tmp = np.array([tmp1, -tmp1]).T
    # b.add_points(tmp)
    name = os.path.join(root, 'zenith_on_sphere.jpg')
    b.save(name=name)
def draw_consensus_rectified_sphere(hv_points, root):
    b = Bloch()
    b.point_color = ['m', 'k', 'g', 'b', 'w', 'c', 'y', 'r']
    b.zlabel = ['$z$', '']
    b.point_marker = ['o']
    b.point_size = [80]
    b.frame_width = 1.2
    fig = plt.figure(figsize=(20, 20))
    b.fig = fig

    x = (basis(2, 0) + (1 + 0j) * basis(2, 1)).unit()
    y = (basis(2, 0) + (0 + 1j) * basis(2, 1)).unit()
    z = (basis(2, 0) + (0 + 0j) * basis(2, 1)).unit()
    b.add_states([x, y, z])
    for i in range(len(hv_points)):
        # Transform xyz to zxy coordinates
        tmp2 = np.vstack(
            [hv_points[i][:, 2], hv_points[i][:, 0], hv_points[i][:, 1]]).T
        tmp = tmp2.T
        b.add_points(tmp)

    # b.add_points([ 0.99619469809174555, 0.087155742747658166, 0])
    # b.add_points([0.99619469809174555, -0.087155742747658166, 0])
    # b.add_points(tmp)
    name = os.path.join(root, 'consensus_zenith_on_rectified_sphere.jpg')
    b.save(name=name)
def PlotStateAnalyzer(InputState, hwpAngle, qwpAngle):
    QWPstate = QWP(qwpAngle) @ InputState
    OutputState = HWP(hwpAngle) @ QWP(qwpAngle) @ InputState
    pntsX = [
        StateToBloch(QWP(th) @ InputState)[0] for th in arange(0, pi, pi / 64)
    ]
    pntsY = [
        StateToBloch(QWP(th) @ InputState)[1] for th in arange(0, pi, pi / 64)
    ]
    pntsZ = [
        StateToBloch(QWP(th) @ InputState)[2] for th in arange(0, pi, pi / 64)
    ]
    pnts = [pntsX, pntsY, pntsZ]
    pntsX2 = [
        StateToBloch(HWP(th) @ QWP(qwpAngle) @ InputState)[0]
        for th in arange(0, pi, pi / 64)
    ]
    pntsY2 = [
        StateToBloch(HWP(th) @ QWP(qwpAngle) @ InputState)[1]
        for th in arange(0, pi, pi / 64)
    ]
    pntsZ2 = [
        StateToBloch(HWP(th) @ QWP(qwpAngle) @ InputState)[2]
        for th in arange(0, pi, pi / 64)
    ]
    pnts2 = [pntsX2, pntsY2, pntsZ2]
    bsph = Bloch()
    bsph.add_points(pnts)
    bsph.add_points(pnts2)
    bsph.add_vectors(StateToBloch(OutputState))
    bsph.add_vectors(StateToBloch(InputState))
    bsph.add_vectors(StateToBloch(QWPstate))
    return bsph.show()
    def bloch_plot(self, points=None, F=None):
        """
        Plot the current state on the Bloch sphere using
        qutip. 
        """
        # create instance of 3d plot
        bloch = Bloch(figsize=[9, 9])
        bloch.add_vectors([0, 0, 1])
        bloch.xlabel = ['$<F_x>$', '']
        bloch.ylabel = ['$<F_y>$', '']
        bloch.zlabel = ['$<F_z>$', '']
        if self.spin == 'half':
            if points is None:
                # convert current state into density operator
                rho = np.outer(self.state.H, self.state)
                # get Bloch vector representation
                points = self.get_bloch_vec(rho)
                # Can only plot systems of dimension 2 at this time
                assert len(
                    points
                ) == 3, "System dimension must be spin 1/2 for Bloch sphere plot"
                # create instance of 3d plot
            bloch = Bloch(figsize=[9, 9])
        elif self.spin == 'one':
            #points is list of items in format [[x1,x2],[y1,y2],[z1,z2]]
            if points is None:
                points = [getStars(self.state)]
            bloch.point_color = ['g', 'r',
                                 'b']  #ensures point and line are same colour
            bloch.point_marker = ['o', 'd', 'o']
            #bloch.point_color  = ['g','r'] #ensures point and line are same colour
            #bloch.point_marker = ['o','d']
            for p in points:
                bloch.add_points([p[0][0], p[1][0], p[2][0]])
                bloch.add_points([p[0][1], p[1][1], p[2][1]])
                bloch.add_points(p, meth='l')
            '''

            bloch.point_color = ['b','b'] #ensures point and line are same colour
            bloch.point_marker = ['o','o']
            for p in points:
                bloch.add_points(p)
                bloch.add_points(p, meth='l')
            '''
        # add state
        #bloch.render(bloch.fig, bloch.axes)
        #bloch.fig.savefig("bloch.png",dpi=600, transparent=True)
        bloch.show()
def PlotStateQWP(InputState, qwpAngle):
    OutputState = QWP(qwpAngle) @ InputState
    pntsX = [
        StateToBloch(QWP(th) @ InputState)[0] for th in arange(0, pi, pi / 64)
    ]
    pntsY = [
        StateToBloch(QWP(th) @ InputState)[1] for th in arange(0, pi, pi / 64)
    ]
    pntsZ = [
        StateToBloch(QWP(th) @ InputState)[2] for th in arange(0, pi, pi / 64)
    ]
    pnts = [pntsX, pntsY, pntsZ]
    bsph = Bloch()
    bsph.add_points(pnts)
    bsph.add_vectors(StateToBloch(OutputState))
    bsph.add_vectors(StateToBloch(InputState))
    return bsph.show()
Exemple #9
0
    def field_plot(self, params, bloch=[False, 1]):
        """
        Plots the signal components given simulation parameters over the specified time doman
        """

        # get time varying fields and simulation data
        time, cparams, Bfields = field_get(params=params)

        # plot magnetic field vector on Bloch sphere
        if bloch[0]:
            Bfields = Bfields[::bloch[1], :]
            # normalise each magnetic field vector
            for i in range(len(Bfields)):
                norm = np.sqrt(Bfields[i, 0]**2 + Bfields[i, 1]**2 +
                               Bfields[i, 2]**2)
                Bfields[i, 0] /= norm
                Bfields[i, 1] /= norm
                Bfields[i, 2] /= norm

            # extract x,y,z fields
            Bx = Bfields[:, 0]
            By = Bfields[:, 1]
            Bz = Bfields[:, 2]

            # define bloch object
            b = Bloch()
            b.add_points([Bx, By, Bz])
            b.show()

        else:
            # plot fields
            fig, ax = plt.subplots(nrows=3, ncols=1, sharex=True, sharey=False)
            for i, row in enumerate(ax):
                # plot each component, skipping zero time value
                row.plot(time, Bfields[:, i])
                row.set_title("Field vector along {} axis".format(
                    ['x', 'y', 'z'][i]))

            plt.ylabel('Frequency (Hz)')
            plt.xlabel("Time (s)")
            #plt.ylabel("Ampltiude ($Hz/Gauss$)")
            plt.show()
Exemple #10
0
    def bloch_plot(self, points=None):
        """
        Plot the current state on the Bloch sphere using
        qutip. 
        """
        if points is None:
            # convert current state into density operator
            rho = np.outer(self.state.H, self.state)
            # get Bloch vector representation
            points = self.get_bloch_vec(rho)
            # Can only plot systems of dimension 2 at this time
            assert len(
                points
            ) == 3, "System dimension must be spin 1/2 for Bloch sphere plot"

        # create instance of 3d plot
        bloch = Bloch(figsize=[9, 9])
        # add state
        bloch.add_points(points)
        bloch.add_vectors([0, 0, 1])
        bloch.render(bloch.fig, bloch.axes)
        bloch.fig.savefig("bloch.png", dpi=600, transparent=True)
        bloch.show()
def draw_center_hvps_rectified_sphere(hv_points, root):
    b = Bloch()
    b.point_color = ['m', 'k', 'g', 'b', 'w', 'c', 'y', 'r']
    b.zlabel = ['$z$', '']
    b.point_marker = ['o']
    b.point_size = [80]
    b.frame_width = 1.2
    fig = plt.figure(figsize=(20, 20))
    b.fig = fig

    x = (basis(2, 0) + (1 + 0j) * basis(2, 1)).unit()
    y = (basis(2, 0) + (0 + 1j) * basis(2, 1)).unit()
    z = (basis(2, 0) + (0 + 0j) * basis(2, 1)).unit()
    b.add_states([x, y, z])
    for i in range(len(hv_points)):
        # Transform xyz to zxy coordinates
        tmp1 = np.array([hv_points[i][2], hv_points[i][0], hv_points[i][1]])
        tmp2 = np.vstack([tmp1, -tmp1]).T
        tmp = tmp2
        b.add_points(tmp)

    name = os.path.join(root, 'consensus_hvps_center_on_rectified_sphere.jpg')
    b.save(name=name)
 def maj_vid(self, points):
     #takes screenshots of majorana stars over time to produce vid
     if points is None:
         points = [getStars(self.state)]
     i = 0
     for p in points:
         bloch = Bloch(figsize=[9, 9])
         bloch.xlabel = ['$<F_x>$', '']
         bloch.ylabel = ['$<F_y>$', '']
         bloch.zlabel = ['$<F_z>$', '']
         bloch.point_color = ['g', 'r',
                              'b']  #ensures point and line are same colour
         bloch.point_marker = ['o', 'd', 'o']
         bloch.add_points([p[0][0], p[1][0], p[2][0]])
         bloch.add_points([p[0][1], p[1][1], p[2][1]])
         bloch.add_points(p, meth='l')
         bloch.render(bloch.fig, bloch.axes)
         bloch.fig.savefig(
             "bloch" + str(i).zfill(int(np.ceil(np.log10(len(points))))) +
             ".png",
             dpi=600,
             transparent=False)
         i += 1
Exemple #13
0
    def bloch_plot2(self,
                    filename,
                    save=False,
                    vecList=[],
                    vecColour=[],
                    view=[190, 10],
                    points=None,
                    folder=False,
                    fig=False,
                    ax=False):
        """
        Plot the current state on the Bloch sphere using
        qutip. 
        """
        if points is None:
            # convert current state into density operator
            rho = np.outer(self.state.H, self.state)
            # get Bloch vector representation
            points = self.get_bloch_vec(rho)
            # Can only plot systems of dimension 2 at this time
            assert len(
                points
            ) == 3, "System dimension must be spin 1/2 for Bloch sphere plot"

        # create instance of 3d plot
        if not fig or not ax:
            bloch = Bloch(figsize=[9, 9], view=view)
        else:
            bloch = Bloch(fig=fig, axes=ax, view=view)
        # add state
        bloch.add_points(points)
        # bloch.zlabel = [r'$\left|+z\right>$',r'$\left|-z\right>$']
        bloch.zlabel = [r'$\ket{+_z}$', r'$\ket{-_z}$']
        # bloch.ylabel = [r'$\ket{+_y}$',r'$\ket{-_y}$']
        # bloch.ylabel = [r'$\ket{+_y}$',r'$\ket{-_y}$']
        # print(vecList.shape)

        # add vectors
        if vecList.shape[1] == 3:
            if len(vecColour) >= vecList.shape[0] and len(vecColour) > 0:
                bloch.vector_color = vecColour
            else:
                bloch.vector_color = ['#CC6600', 'royalblue', 'r', 'm', 'g']
            bloch.add_vectors(vecList)
        # add field vector
        # bloch.add_vectors([1,0,0.15])
        # bloch.add_vectors([0,0,1])
        # bloch.add_vectors([1,0,0])

        # render bloch sphere
        if not fig or not ax:
            bloch.render()
        else:
            # bloch.render(fig = fig, axes = ax)
            bloch.render(fig=fig)

        # save output
        if save is True:
            if not folder:
                folder = 'C:/Users/Boundsy/Desktop/Uni Work/PHS2360/Sim Results/'
            print('Bloch plot saved to ' + str(folder))
            path1 = folder + str(filename) + '.png'
            path2 = folder + str(filename) + '.pdf'
            bloch.fig.savefig(path1, dpi=800, transparent=True)
            bloch.fig.savefig(path2, dpi=800, transparent=True)

        # return axes for annotations
        return bloch.fig, bloch.axes