Giter Club home page Giter Club logo

bloch-sphere's Introduction

Bloch-Sphere

256px-bloch_sphere svg

Definition

Bloch sphere is a geometric repersentation of a quantum bit. The initial state is ,which is the positive phase on the z-axis. Then, when X gate is put, the arrow will rotate 180˚about the x-axis and go to , which is the negative phase along z-axis.

Implementation

Here, I implemented a toy model of bloch sphere. You can see how the coordinates changes from any points of the sphere, if a quantum gate(,,) is applied. I'm going to introduce X-gate as an example.

You can specify two angles and as its initial condition.

# Remove the # below if you are using Jupyter Notebook.
#%matplotlib nbagg

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import animation
import numpy as np

print("Put angle theta and phi, 0≤ theta ≤180, 0≤ phi ≤360")
theta = input("theta:")
phi = input("phi:")
theta = np.radians(float(theta))
phi = np.radians(float(phi))
X = np.sin(theta) * np.cos(phi)
Y = np.sin(theta) * np.sin(phi)
Z = np.cos(theta)

This is the figure of the sphere and its background.

# If the length of vector is more than 1, I divides each coordinate by its original length.
length = np.sqrt(X**2 + Y**2 + Z**2)
if length > 1:
    X = X/length
    Y = Y/length
    Z = Z/length

# Figure of the animation   
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.set_aspect("equal")
u, v = np.mgrid[0:2*np.pi:20j, 0:np.pi:10j]
x = np.cos(u)*np.sin(v)
y = np.sin(u)*np.sin(v)
z = np.cos(v)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.plot_wireframe(x,y,z, color="black")

Then you can input how many times the arrow moves (those coordinates changes).

# Number of moments on the animation    
number = 10
# The value of theta, phi in each moment
xgate_theta = np.linspace(theta,theta+np.pi,number)
xgate_phi = np.linspace(phi,phi,number)
# The array of all the coordinates in each moment
xgate= []
#The array of each coordinate in each moment
xgate_x = []
xgate_y = []
xgate_z = []
for i in range(number):
    xgate_x.append(X)
    xgate_z.append(np.cos(xgate_theta[i]))
    xgate_y.append(np.sqrt(1-np.sqrt(xgate_x[i]**2+xgate_z[i]**2))*(-1))

Then, those coordinates in each moment would be an arrow and you can see them as animation.

for j in range(number):        
    arrow = plt.quiver(0,0,0,xgate_x[j],xgate_y[j],xgate_z[j],color="red")
    xgate.append([arrow])
    
ani = animation.ArtistAnimation(fig,xgate,interval=1000)
plt.show()

bloch-sphere's People

Contributors

makotonakai avatar

Stargazers

Fay Alradhi avatar  avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.