Exemple #1
0
circuits = []
for exp_index in exp_vector:
    middle = QuantumCircuit(q, c)
    phase = 2 * np.pi * exp_index / (len(exp_vector) - 1)
    middle.u1(phase, q)
    circuits.append(pre + middle + meas_x)
    circuits.append(pre + middle + meas_y)
    circuits.append(pre + middle + meas_z)

# Execute the circuit
job = execute(circuits,
              backend=BasicAer.get_backend('qasm_simulator'),
              shots=1024)
result = job.result()

# Plot the result
for exp_index in exp_vector:
    bloch = [0, 0, 0]
    for bloch_index in range(len(bloch_vector)):
        data = result.get_counts(circuits[3 * exp_index + bloch_index])
        try:
            p0 = data['0'] / 1024.0
        except KeyError:
            p0 = 0
        try:
            p1 = data['1'] / 1024.0
        except KeyError:
            p1 = 0
        bloch[bloch_index] = p0 - p1
    plot_bloch_vector(bloch)
Exemple #2
0
    new_mat[1] = mat[1][0] + mat[1][1] * 1j
    new_mat = [new_mat]
    new_mat = np.transpose(new_mat)
    print('new mat : ', new_mat)
    print('transp  : ', np.transpose(np.conj(new_mat)))
    state_matrix = np.matmul(new_mat, np.transpose(np.conj(new_mat)))
    print('STATE MATRIX \n', state_matrix)
    x1 = np.matmul(x, state_matrix)
    y1 = np.matmul(y, state_matrix)
    z1 = np.matmul(z, state_matrix)
    # matrices are correct

    print('x1 : ', x1)
    print('y1 : ', y1)
    print('z1 : ', z1)

    trcx = np.trace(x1)
    trcy = np.trace(y1)
    trcz = np.trace(z1)

    vals = [trcx.real, trcy.real, trcz.real]

    print('VALS : ', vals)

    print()

    blch = plot_bloch_vector(vals)
    blch.savefig(
        '/Users/madeleinetod/Documents/NoughtsAndCrosses/GUI/imgs/testing/bloch'
        + key + '.png')
Exemple #3
0
q = QuantumRegister(1)
c = ClassicalRegister(1)
qc = QuantumCircuit(q, c)

qc.h(q[0])
qc.ry(pi / 8, q)
qc.measure(q, c)

print(qc)

backend = Aer.get_backend('qasm_simulator')
job = execute(qc, backend, shots=1)
result = job.result()
counts = result.get_counts(qc)
print(counts)

from qiskit.tools.visualization import plot_histogram
plot_histogram(counts)

from qiskit.tools.visualization import plot_bloch_vector
#Display the Bloch vector for|0>.
plot_bloch_vector([0, 0, 1], title='Qubit in ground state |0>')

from qiskit.tools.visualization import plot_bloch_vector
#Display the Bloch vector for|0>.
plot_bloch_vector([1, 0, 0], title='Qubit in super position')

from qiskit.tools.visualization import plot_bloch_vector
#Display the Bloch vector for|0>.
plot_bloch_vector([0.924, 0, -0.383], title='Qubit pi/8 radians closer to |1>')
Exemple #4
0
from qiskit import QuantumRegister, ClassicalRegister
from qiskit import QuantumCircuit, Aer, execute

q = QuantumRegister(1)
c = ClassicalRegister(1)
qc = QuantumCircuit(q, c)

qc.h(q[0])
qc.measure(q, c)

print(qc)

backend = Aer.get_backend('qasm_simulator')
job = execute(qc, backend, shots=1)
result = job.result()
counts = result.get_counts(qc)
print(counts)

from qiskit.tools.visualization import plot_histogram

plot_histogram(counts)

from qiskit.tools.visualization import plot_bloch_vector
#Create an empty dictionary for the two possible states.
counts_dict = {'0': 0, '1': 0}
#Merge in the actual result.
counts_dict.update(counts)
#Display the Bloch vector with the result as 0 or 1 on the Z-axis.
plot_bloch_vector([0, 0, (counts_dict['0'] - counts_dict['1'])])
Exemple #5
0
all_blochs = []
for exp_index in exp_vector:
    bloch = [0, 0, 0]
    for bloch_index in range(len(bloch_vector)):
        data = result.get_counts(circuits[3 * exp_index + bloch_index])
        try:
            p0 = data['0'] / 1024.0
        except KeyError:
            p0 = 0
        try:
            p1 = data['1'] / 1024.0
        except KeyError:
            p1 = 0
        bloch[bloch_index] = p0 - p1
    all_blochs.append(bloch)
    plot_bloch_vector(bloch)

# In[21]:

# We need this function to plot inside a loop in a notebook
import matplotlib.pyplot as plt


def show_figure(fig):
    new_fig = plt.figure()
    new_mngr = new_fig.canvas.manager
    new_mngr.canvas.figure = fig
    fig.set_canvas(new_mngr.canvas)
    plt.show(fig)