%run init.ipynb

simplify(cos(pi/8)), simplify(sin(pi/8))

from qiskit.visualization import plot_bloch_vector
th, ph = pi/4, 0
#plot_bloch_vector([[math.sin(th)*math.cos(ph), math.sin(th)*math.sin(ph), math.cos(th)]])

float(1+1/sqrt(2))/2

X = Matrix([[0,1],[1,0]]); Z = Matrix([[1,0],[0,-1]]); O = (X+Z)/sqrt(2); O.eigenvects()

from qiskit import *
nshots = 8192
qiskit.IBMQ.load_account()
provider = qiskit.IBMQ.get_provider(hub='ibm-q', group='open', project='main')
simulator = Aer.get_backend('qasm_simulator')
device = provider.get_backend('ibmq_belem')
from qiskit.tools.monitor import job_monitor
from qiskit.tools.visualization import plot_histogram

qc = QuantumCircuit(1, 1)
qc.h(0) # preparação do estado
qc.barrier() 
# medida de O=H
th = math.pi/4; ph = 0; lb = math.pi - ph; qc.u(th, ph, lb, 0)
qc.measure(0, 0)
qc.draw()

result = execute(qc, backend = simulator, shots = nshots).result()
plot_histogram(result.get_counts(qc))

job = execute(qc, backend = device, shots = nshots)
job_monitor(job)

plot_histogram(job.result().get_counts(qc))

def qc_bb_cb():
    qc = QuantumCircuit(2, name = 'BB->CB')
    qc.cx(0,1); qc.h(0)#; qc.measure([0,1],[0,1])
    return qc
qc_bb_cb_ = qc_bb_cb(); qc_bb_cb_.draw()

qc = QuantumCircuit(2, 2)
qc.x(1); qc.h([0,1]) # preparação do estado |+->
qc.barrier()
qc_bb_cb_ = qc_bb_cb(); qc.append(qc_bb_cb_, [0,1]); qc.measure([0,1],[0,1])
qc.draw()

result = execute(qc, backend = simulator, shots = nshots).result()
plot_histogram(result.get_counts(qc))

job = execute(qc, backend = device, shots = nshots)
job_monitor(job)

plot_histogram(job.result().get_counts(qc))

