%run init.ipynb

CX_12 = tp(proj(cb(2,0)),id(2)) + tp(proj(cb(2,1)),pauli(1)); CX_12

CX_21 = tp(id(2),proj(cb(2,0))) + tp(pauli(1),proj(cb(2,1))); CX_21

SWAP = CX_12*CX_21*CX_12; SWAP

Cz = tp(proj(cb(2,0)), id(2)) + tp(proj(cb(2,1)), pauli(3)); Cz_12 = Cz
Cz_21 = tp(id(2),proj(cb(2,0))) + tp(pauli(3),proj(cb(2,1)))
Cz_12, Cz_21

H = (1/sqrt(2))*Matrix([[1,1],[1,-1]]); H

Cx12 = tp(id(2),H)*Cz*tp(id(2),H); Cx12

Cx21 = tp(H,id(2))*Cz*tp(H,id(2)); Cx21

CX_21 = tp(id(2),proj(cb(2,0))) + tp(pauli(1),proj(cb(2,1))); CX_21

Cz12 = tp(id(2),H)*Cx12*tp(id(2),H); Cz12

Cz21 = tp(id(2),H)*Cx21*tp(id(2),H); Cz12

CCX = tp(proj(cb(2,0)),id(4)) + tp(proj(cb(2,1)),Cx12); CCX

def qc_ry(th):
    qr = QuantumRegister(1)
    qc = QuantumCircuit(qr, name = 'RY')
    qc.ry(th, 0)
    return qc

from qiskit import *
qr = QuantumRegister(3) 
cr = ClassicalRegister(2)
qc = QuantumCircuit(qr, cr)
qc_ry_ = qc_ry(math.pi)
ccry = qc_ry_.to_gate().control(2)
qc.append(ccry, [2, 0, 1])
qc.draw(output = 'mpl')

from qiskit import *
qc = QuantumCircuit(1, 1); qc.h([0]); qc.h([0]); qc.measure([0], [0]); qc.draw(output = 'mpl')

qc = QuantumCircuit(1, 1); qc.barrier(); qc.h([0]); qc.barrier(); qc.h([0]); qc.barrier(); qc.measure([0], [0])
qc.draw(output = 'mpl')

qc.decompose().draw(output = 'mpl')

qc = QuantumCircuit(1, 1)
N = 5
for j in range(0, N):
    qc.id([0])
qc.measure([0], [0])
qc.draw(output = 'mpl')

nshots = 8192
qiskit.IBMQ.load_account()
#provider = IBMQ.get_provider(hub='ibm-q-community', group='ibmquantumawards', project='open-science-22')
provider = qiskit.IBMQ.get_provider(hub = 'ibm-q-research-2', group = 'federal-uni-sant-1', project = 'main')
device = provider.get_backend('ibmq_bogota')
simulator = Aer.get_backend('qasm_simulator')
from qiskit.tools.monitor import job_monitor
from qiskit.ignis.verification.tomography import state_tomography_circuits, StateTomographyFitter
from qiskit.tools.monitor import backend_overview, backend_monitor
from qiskit.tools.visualization import plot_histogram

qc = QuantumCircuit(1, 1)
N = 0
for j in range(0, N):
    qc.barrier(); qc.id([0])
qc.barrier(); qc.measure([0], [0])
qc.draw(output = 'mpl')

job_exp = execute(qc, backend = device, shots = nshots)
print(job_exp.job_id()); job_monitor(job_exp)
plot_histogram(job_exp.result().get_counts(qc))

qc = QuantumCircuit(1, 1)
N = 10000
for j in range(0, N):
    qc.barrier(); qc.id([0])
qc.barrier(); qc.measure([0], [0]);
#qc.draw(output = 'mpl')

job_exp = execute(qc, backend = device, shots = nshots)
print(job_exp.job_id()); job_monitor(job_exp)
plot_histogram(job_exp.result().get_counts(qc))

qc = QuantumCircuit(4); qc.h([0]); qc.cx([0], [2]); qc.h([1]); qc.cx([1], [3]); qc.draw(output = 'mpl')

qc = QuantumCircuit(3); qc.h([0]); qc.cx([0], [1]); qc.reset([0]); qc.h([0]); qc.cx([0], [2])
qc.draw(output = 'mpl')

