Esempio n. 1
0
 def __init__(self,circuit):
     self.defaultBlochSphere=self.figToResponse(plot_bloch_vector([0,0,1]))
     self.circuit=circuit
     self.num_qubits=self.circuit.num_qubits
     #self.circutDrawing=self.draw()
     self.statevector=self.stateVector()
     self.reversedStatevector=self.reversedStateVector()
Esempio n. 2
0
def rotate_y(i, v):
    fig = plot_bloch_vector([
        np.sin((i / 180) * np.pi) * np.cos((v / 180) * np.pi),
        np.sin((i / 180) * np.pi) * np.sin((v / 180) * np.pi),
        np.cos((i / 180) * np.pi)
    ])
    fig.savefig('png/to_gif/out_%4.d.png' % i)
    plt.close()
Esempio n. 3
0
def plot_bloch_vector_spherical(coords):
    clear_output()
    theta, phi, r = coords[0], coords[1], coords[2]
    x = r*sin(theta)*cos(phi)
    y = r*sin(theta)*sin(phi)
    z = r*cos(theta)
    output = widgets.Output()
    return plot_bloch_vector([x,y,z])
def qiskit():
    # Plotting Single Bloch Sphere
    plot_bloch_vector([0, 1, 0], title='Bloch Sphere')

    # Building Quantum Circuit to use for multiqubit systems
    qc = QuantumCircuit(2)
    qc.h(0)
    qc.cx(0, 1)

    # Plotting Multi Bloch System
    state = Statevector.from_instruction(qc)
    plot_bloch_multivector(state, title="New Bloch Multivector")

    # Plotting Bloch City Scape
    plot_state_city(state,
                    color=['midnightblue', 'midnightblue'],
                    title="New State City")

    # Plotting Bloch Pauli Vectors
    plot_state_paulivec(state, color='midnightblue', title="New PauliVec plot")
Esempio n. 5
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$:")
    data = BytesIO()
    image = _img(value=plot_bloch_vector([0, 0, 1]))

    def on_button_click(b):
        from math import pi, sqrt
        try:
            theta = eval(theta_input.value)
            phi = eval(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)
Esempio n. 6
0
 def separatedBlochSpheres(self):
     """
     returns list of response object for the bloch sphere of every wire
     """
     pos=list(range(self.num_qubits))
     res={}
     for i in range(self.num_qubits):
         #density matrix of the wire
         [[a, b], [c, d]] = partial_trace(self.statevector, pos[:i]+pos[i+1:]).data 
         #polar coordinate
         x = 2*b.real
         y = 2*c.imag
         z = a.real-d.real
         #blochSphere figure
         fig=plot_bloch_vector([x,y,z])#,title="qubit "+str(i)
         #appending response object of the figure
         res[i]=self.figToResponse(fig)
     return res
Esempio n. 7
0
 def on_button_click(b):
     from math import pi, sqrt
     try:
         theta = eval(theta_input.value)
         phi = eval(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])
Esempio n. 8
0
from qiskit.visualization import plot_bloch_vector
import matplotlib.pyplot as plt
import numpy as np
up=np.array([0,0,1])
down=np.array([0,0,-1])
#Compuerta de Hadamard
H=1/np.sqrt(0.5)*np.array([[0,0,0],[0,1,1],[0,1,-1]])
n=np.dot(H,down)/2
plot_bloch_vector(n)
plt.show()
Esempio n. 9
0
def vector():
    fig = plot_bloch_vector([0, -1, 0])
    fig.plot([1, 0], [0, 0])
    fig.savefig('png/out.png')
    plt.close()
unit = Aer.get_backend('unitary_simulator')
noise_model = noise.bit_flip_noise(error_prob)
qecc = ThreeQubitCode()

# Visualize parameters
print(noise_model)
qecc.visualize()

# Define test circuit and input state
output = ClassicalRegister(3)
circ = QuantumCircuit(qecc.code, qecc.syndrm, output)
circ.ry(theta, qecc.code[0])
circ.rz(phi, qecc.code[0])
plot_bloch_vector(
    [np.sin(theta) * np.cos(phi),
     np.sin(theta) * np.sin(phi),
     np.cos(theta)],
    title="Input State")
plt.show()

# Define final measurement circuit
meas = QuantumCircuit(qecc.code, qecc.syndrm, output)
meas.measure(qecc.code, output)

# QASM simulation w/o error correction
job = execute(circ + qecc.encoder_ckt + qecc.noise_ckt + meas,
              backend=qasm,
              noise_model=noise_model,
              basis_gates=noise_model.basis_gates)
counts_noisy = job.result().get_counts()
    print(
        "Your qubit parameters are not normalized.\nResetting to basic superposition"
    )
    a = 1 / sqrt(2)
    b = 1 / sqrt(2)

bits = {
    "bit = 0": np.matrix([1, 0]),
    "bit = 1": np.matrix([0, 1]),
    "|0>": np.matrix([1, 0]),
    "|1>": np.matrix([0, 1]),
    "(|0>+|1>)/\u221a2": np.matrix([a, b])
}

#print("|0>",bits["|0>"])
#print("<0|", bits["|0>"].getH())
#print("Pauli X",x)
#print("Rho =|0><0|", bits["|0>"].getH()*bits["|0>"])
#print("X*Rho", x*(bits["|0>"].getH()*bits["|0>"]))
#print((np.trace(y*(bits["|0>"].getH()*bits["|0>"])).real))

# Print the bits and qubits on the Bloch sphere
for b in bits:
    bloch = [(np.trace(x * (bits[b].getH() * bits[b]))).real,
             (np.trace(y * (bits[b].getH() * bits[b]))).real,
             (np.trace(z * (bits[b].getH() * bits[b]))).real]
    display(plot_bloch_vector(bloch, title=b))
    print("Y real:", (np.trace(y * (bits[b].getH() * bits[b])).real))
    print("Y imag:", (np.trace(y * (bits[b].getH() * bits[b])).imag))
    print("State vector:", bits.get(b))
Esempio n. 12
0
circ.h(2)
circ.h(3)
# circ.draw()

backend = Aer.get_backend('statevector_simulator')
job = execute(circ, backend)
result = job.result()
outputstate = result.get_statevector(circ, decimals=3)

#print(outputstate)
plt.show(plot_state_city(outputstate))

meas = QuantumCircuit(4, 4)
meas.barrier(range(4))
meas.measure(range(4), range(4))
qc = circ + meas
plt.show(qc.draw(output='mpl'))
# print(qc)

backend_sim = Aer.get_backend('qasm_simulator')
job_sim = execute(qc, backend_sim, shots=1024)

result_sim = job_sim.result()
counts = result_sim.get_counts(qc)

#print(counts)
plt.show(plot_histogram(counts))

bloch = [1, 0, 0]
plt.show(plot_bloch_vector(bloch))
Esempio n. 13
0
from qiskit.visualization import plot_bloch_vector

plot_bloch_vector([0, 1, 0], title="New Bloch Sphere")
#circ.draw(output='mpl')
#plt.show()
#plot_bloch_vector()
Esempio n. 14
0
from matplotlib import pyplot as plt
import numpy as np
from qiskit import *
from qiskit.visualization import plot_bloch_vector

plt.figure()
ax = plt.gca()
ax.quiver([3], [5], angles='xy', scale_units='xy', scale=1)
ax.set_xlim([-1, 10])
ax.set_ylim([-1, 10])
#plt.draw()
#plt.show()

plot_bloch_vector([1, 0, 0])

"""
Matrices:
    A unitary matrix is very similar. Specifically, it is a matrix such that the inverse matrix is equal to the conjugate transpose of the original matrix.
    A Hermitian matrix is simply a matrix that is equal to its conjugate transpose (denoted with a †  symbol).
"""