2
$\begingroup$

I have tried to plot a histogram for the multiple circuits for the code given below. I think I have done it correctly but don't know why it's not working. Please help me out. If possible please solve it for the Bloch sphere also

# quantum_phase_bloch.py
import numpy as np
from qiskit.visualization import plot_histogram
from qiskit.tools.monitor import job_monitor
from matplotlib import style
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, execute, Aer
from qiskit.tools.visualization import plot_bloch_vector

# Define the Quantum and Classical Registers
q = QuantumRegister(1)
c = ClassicalRegister(1)

# Build the circuits
pre = QuantumCircuit(q, c)
pre.h(q)
pre.barrier()

meas_x = QuantumCircuit(q, c)
meas_x.barrier()
meas_x.h(q)
meas_x.measure(q, c)

meas_y = QuantumCircuit(q, c)
meas_y.barrier()
meas_y.s(q).inverse()
meas_y.h(q)
meas_y.measure(q, c)

meas_z = QuantumCircuit(q, c)
meas_z.barrier()
meas_z.measure(q, c)

bloch_vector = ['x', 'y', 'z']
exp_vector = range(0, 2)
circuits = []
print(len(exp_vector))
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)
print(len(circuits))

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

# trying to plot histogram 
counts = []
# count = np.zeros([0])

for x in range(len(circuits)):
#     count[x] = result.get_counts(circuits[x])
    count = result.get_counts(circuits[x])
    counts.append(count)

plot_histogram(counts , legend=['1','2','3','4','5','6'])
print(counts)    
$\endgroup$
3
$\begingroup$

No output shows for your code as you have a line underneath the call to plot_histogram(). This should be the last line of the section in the Jupyter notebook if you would like the image to be displayed.

I was able to run your code by removing the final line (print(counts)) and it displayed the histogram below. enter image description here

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.