!pip install qiskit
!pip install pylatexenc

import numpy as np
from qiskit.visualization import array_to_latex
from qiskit.quantum_info import random_statevector
psi = random_statevector(2)
display(array_to_latex(psi, prefix="|\\psi\\rangle ="))

from qiskit import QuantumCircuit, assemble, Aer
import qiskit.quantum_info as qi
import numpy as np
import qiskit.quantum_info as qi
qc = QuantumCircuit(3,2)
qc.initialize(psi, 0)
stv0 = qi.Statevector.from_instruction(qc)
qc.barrier()
qc.h(1) # Hadamard
qc.cx(1,2) # CNOT
qc.barrier()
qc.cx(0,1)
qc.h(0)
qc.barrier()
stv = qi.Statevector.from_instruction(qc)
qc.measure(0,0)
qc.measure(1,1)
qc.draw("mpl")


from qiskit.visualization import plot_bloch_multivector
plot_bloch_multivector(stv0)

from qiskit import QuantumCircuit, Aer,execute
from qiskit.visualization import plot_histogram
backend = Aer.get_backend("qasm_simulator")
shots = 1000
#backend = Aer.get_backend('qasm_simulator')
job = backend.run(qc, shots=1)
print(job.result().get_counts(qc))
plot_histogram(job.result().get_counts())

import qiskit.quantum_info as qi
import numpy as np
qc1 = QuantumCircuit(3,2)
print(job.result().get_counts())
for key in job.result().data()["counts"]:
  if int(key,0) == 0 :
    print("Result: 00")
    q2_state = [np.round(stv[0],3),np.round(stv[4],3)]
    qc1.initialize(0, 0)
    qc1.initialize(0, 1)
  elif int(key,0) == 1 :
    print("Result: 01")
    q2_state = [np.round(stv[1],3),np.round(stv[5],3)]
    qc1.initialize(1, 0)
    qc1.initialize(0, 1)
  elif int(key,0) == 2 :
    print("Result: 10")
    q2_state = [np.round(stv[2],3),np.round(stv[6],3)]
    qc1.initialize(0, 0)
    qc1.initialize(1, 1)
  elif int(key,0) == 3 :
    print("Result: 11")
    q2_state = [np.round(stv[3],3),np.round(stv[7],3)]
    qc1.initialize(1, 0)
    qc1.initialize(1, 1)
q2_normalizado = q2_state/np.linalg.norm(q2_state)
qc1.barrier()
qc1.initialize(q2_normalizado, 2)
for key in job.result().data()["counts"]:
  if int(key,0) == 1 :
    qc1.z(2)
  if int(key,0) == 2 :
    qc1.x(2)
  if int(key,0) == 3 :
    qc1.x(2)
    qc1.z(2)

stv1 = qi.Statevector.from_instruction(qc1)
stv1.draw('latex', prefix='Estado \quad del \quad qubit \quad 2 = ')

import qiskit.quantum_info as qi
import numpy as np
qc2 = QuantumCircuit(3,2)
for key in job.result().data(qc)["counts"]:
  if int(key,0) != 0 :
    qc2.initialize(psi, 2)
qcc = qc.compose(qc1)
qc3 = qcc.compose(qc2)

qc3.draw("mpl")

from qiskit.visualization import plot_bloch_multivector
plot_bloch_multivector(stv1)

from qiskit import IBMQ
from qiskit import QuantumCircuit
from qiskit_aer import AerSimulator
from qiskit.tools.visualization import plot_histogram
from qiskit.providers.ibmq import least_busy
IBMQ.save_account('CLAVE',overwrite=True) # See https://quantum-computing.ibm.com/
IBMQ.load_account() # Load account from disk
print(IBMQ.providers())    # List all available providers
provider = IBMQ.get_provider(hub='ibm-q')
provider.backends()


from qiskit_aer.noise import NoiseModel
provider = IBMQ.load_account()
backend = provider.get_backend('ibmq_manila')
noise_model = NoiseModel.from_backend(backend)
print("----------Noise Model for",backend,"--------------\n",noise_model)

provider = IBMQ.get_provider(hub='ibm-q')
backend = least_busy(provider.backends(filters=lambda x: x.configuration().n_qubits >= 2
                                       and not x.configuration().simulator
                                       and x.status().operational==True))
print("least busy backend: ", backend)
print("----------Noise Model for",backend,"--------------\n",noise_model)

from qiskit import QuantumCircuit, assemble, Aer
from qiskit import IBMQ, transpile
from qiskit.providers.ibmq import least_busy
from qiskit.tools.visualization import plot_histogram

import qiskit.quantum_info as qi
import numpy as np
import qiskit.quantum_info as qi
theta=np.pi*np.random.rand()
phi = 2*np.pi*np.random.rand()
print("theta =",np.round(theta,2),"rad,   theta =",np.round(180*theta/np.pi,2),"degrees")
print("phi=",np.round(phi,2),"rad,   phi =",np.round(180*phi/np.pi,2),"degrees")
qc = QuantumCircuit(3,3)
qc.ry(theta,0)
qc.rz(phi,0)
stv0 = qi.Statevector.from_instruction(qc)
qc.barrier()
qc.h(1) # Hadamard
qc.cx(1,2) # CNOT
qc.barrier()
qc.cx(0,1)
qc.h(0)
qc.barrier()

qc.rz(-phi,2)
qc.ry(-theta,2)
qc.measure(0,0)
qc.measure(1,1)
qc.measure(2,2)
qc.draw("mpl")

from qiskit.visualization import plot_bloch_multivector
plot_bloch_multivector(stv0)

from qiskit import IBMQ, transpile
sim_backend = AerSimulator.from_backend(backend)
print("least busy backend: ", sim_backend)

# Transpile the circuit for the noisy basis gates
tqc = transpile(qc, sim_backend,optimization_level=3)

# Execute noisy simulation and get counts
job = sim_backend.run(tqc,shots=1000)
print("Counts for teleportation with simulated noise model for", backend,"\n\n")
try :
  n_error = job.result().get_counts(qc)['100']
except:
  n_error = 0
import matplotlib.pyplot as plt
try :
  n_error = job.result().get_counts(qc)['100']
except:
  n_error = 0
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(10,6))

states = ['000', '100']
counts = [job.result().get_counts(qc)['000'],n_error]
plt.rcParams.update({'font.size': 20})
plt.bar(states,counts,width=0.4, bottom=None,align='center', data=None)
for index, value in enumerate(counts):
    plt.text(index, value, str(value),fontsize=20)
plt.xlabel('States',fontsize=20)
plt.ylabel('Counts',fontsize=20)
plt.show()

tqc.draw("mpl")

from qiskit import IBMQ
from qiskit import QuantumCircuit
from qiskit_aer import AerSimulator
from qiskit.tools.visualization import plot_histogram
from qiskit.providers.ibmq import least_busy
IBMQ.load_account() # Load account from disk
provider = IBMQ.get_provider(hub='ibm-q')
backend = least_busy(provider.backends(filters=lambda x: x.configuration().n_qubits >= 2
                                       and not x.configuration().simulator
                                       and x.status().operational==True))
print("least busy backend: ", backend)
print("----------Noise Model for",backend,"--------------\n",noise_model)

from qiskit import QuantumCircuit, assemble, Aer
from qiskit.providers.ibmq import least_busy
from qiskit.tools.visualization import plot_histogram

import qiskit.quantum_info as qi
import numpy as np
import qiskit.quantum_info as qi
theta=np.pi*np.random.rand()
phi = 2*np.pi*np.random.rand()
print("theta =",np.round(theta,2),"rad,   theta =",np.round(180*theta/np.pi,2),"degrees")
print("phi=",np.round(phi,2),"rad,   phi =",np.round(180*phi/np.pi,2),"degrees")
qc = QuantumCircuit(3,3)
qc.ry(theta,0)
qc.rz(phi,0)
stv0 = qi.Statevector.from_instruction(qc)
qc.barrier()
qc.h(1) # Hadamard
qc.cx(1,2) # CNOT
qc.barrier()
qc.cx(0,1)
qc.h(0)
qc.barrier()

qc.rz(-phi,2)
qc.ry(-theta,2)
qc.measure(0,0)
qc.measure(1,1)
qc.measure(2,2)
qc.draw("mpl")

from qiskit.visualization import plot_bloch_multivector
plot_bloch_multivector(stv0)

from qiskit import IBMQ, transpile
tqc = transpile(qc, backend, optimization_level=3)
job_teleportation = backend.run(tqc,shots = 1000)

print(job_teleportation.status(),job_teleportation.queue_position())

print("Counts for teleportation on", backend,"\n\n")
try :
  n_error = job.result().get_counts(qc)['100']
except:
  n_error = 0
import matplotlib.pyplot as plt
try :
  n_error = job.result().get_counts(qc)['100']
except:
  n_error = 0
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(10,6))

states = ['000', '100']
counts = [job_teleportation.result().get_counts(qc)['000'],n_error]
plt.rcParams.update({'font.size': 20})
plt.bar(states,counts,width=0.4, bottom=None,align='center', data=None)
for index, value in enumerate(counts):
    plt.text(index, value, str(value),fontsize=20)
plt.xlabel('States',fontsize=20)
plt.ylabel('Counts',fontsize=20)
plt.show()